Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fetch-intercept

werk85176.2kMIT2.4.0TypeScript support: included

Interceptor library for the native fetch command inspired by angular http intercepts.

fetch, request, intercept, interceptor, hooks

readme

fetch-intercept

Build Status

Interceptor library for the native fetch command inspired by angular http interceptors.

fetch-intercept monkey patches the global fetch method and allows you the usage in Browser, Node and Webworker environments.

Installation

npm install fetch-intercept --save

Usage

Note: You need to require fetch-intercept before you use fetch the first time.

Make sure you have a fetch compatible environment or added a appropriate polyfill.

import fetchIntercept from 'fetch-intercept';

const unregister = fetchIntercept.register({
    request: function (url, config) {
        // Modify the url or config here
        return [url, config];
    },

    requestError: function (error) {
        // Called when an error occured during another 'request' interceptor call
        return Promise.reject(error);
    },

    response: function (response) {
        // Modify the reponse object
        return response;
    },

    responseError: function (error) {
        // Handle an fetch error
        return Promise.reject(error);
    }
});

// Call fetch to see your interceptors in action.
fetch('http://google.com');

// Unregister your interceptor
unregister();

React-Native Compatibility

Support react-native 0.17 or higher versions.

License

MIT

changelog

Version 2.4.0

  • Request object added to the response and error object #36 (@neorel)

Version 2.3.1

  • Typings improved #24

Version 2.3.0

  • TypeScript definitions added #21
  • Let bundler decide whether env is node or browser #7

Version 2.2.3

  • Reverted #8 cause it leads to incorrect environment detection when webpack is used #9

Version 2.2.2

  • Remove irrelevant code #8

Version 2.2.1

  • .babelrc removed from npm package #4

Version 2.2.0

  • Lodash dependencies removed.
  • Production and development build added.

Version 2.1.0

  • React-Native Support added #2.

Version 2.0.1

  • Webpack externals fixed.

Version 2.0.0

  • Node and Webworker support added #1.