axios-fetch-interceptors
TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

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

Package Sidebar

Install

npm i axios-fetch-interceptors

Weekly Downloads

1

Version

0.0.9

License

MIT

Unpacked Size

167 kB

Total Files

8

Last publish

Collaborators

  • thinhductrinh