Interceptor plugin for window fetch API, inspired by Axios interceptors.
- Intercept request and response
Using npm:
$ npm install axios-fetch-interceptors
Using bower:
$ bower install axios-fetch-interceptors
Using yarn:
$ yarn add axios-fetch-interceptors
Using pnpm:
$ pnpm add axios-fetch-interceptors
Once the package is already installed and imported in the index
file, you can see a new property called interceptors
is added into the fetch.
Just like the way you are using it in Axios.
// Add an request interceptor
fetch.interceptors.request.use(
function (config : RequestConfig) {
console.log(config)
return config
},
function (error : Error, config: RequestConfig) {
console.log(error)
}
)
// Add an response interceptor
fetch.interceptors.response.use(
function (response : RequestConfig) {
console.log(response)
return response
},
function (error : Error, config: RequestConfig) {
console.log(error)
return Promise.reject(error)
}
)
If you want to remove all request/response
interceptors
fetch.interceptors.request.clear()
axios-fetch-interceptors
supports Typescript out of the box.
MIT