0.0.9 • Published 3 years ago

axios-fetch-interceptors v0.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

fetch-interceptors

npm version

Interceptor plugin for window fetch API, inspired by Axios interceptors.

Table of Contents

Features

  • Intercept request and response

Installing

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.

Example

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()

Typescript

axios-fetch-interceptors supports Typescript out of the box.

License

MIT