1.0.6-beta.1 • Published 5 months ago

global-request-interceptor v1.0.6-beta.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Global Request Interceptor

简体中文

npm version

global-request-interceptor is a simple yet flexible JavaScript library designed for intercepting network requests globally. This library supports Axios, Fetch, and XMLHttpRequest (XHR), allowing you to easily add global processing logic, log requests and responses, handle errors, and enhance the maintainability and flexibility of your application.

Features

  • Support for Axios, Fetch, and XHR: Provides interceptors for Axios, Fetch, and XHR, giving you the option to use one, two, or all three simultaneously.
  • Global Interceptors: Set up request, response, and error interceptors that will take effect across all requests in your entire application.
  • Flexible Configuration: Through callback functions, you can execute custom logic within interceptors, such as modifying request configurations, logging, error handling, and more.

Installation

Install using npm:

npm install global-request-interceptor

Install using yarn:

yarn add global-request-interceptor

Global Interception with Axios

Usage

  1. Set up interceptors for multiple instances:
import { setupAxiosInterceptor } from 'global-request-interceptor';

// Configuration example with multiple instances
setupAxiosInterceptor({
  instances: [axiosInstance1, axiosInstance2],
  onRequest: (config) => {
    // Your request interceptor logic
    // config.url += '?token=123456789'; 
    // config.headers['Sailing'] = 'abc';
    return config;
  },
  onResponse: (response) => {
    // Your response interceptor logic
    return response;
  },
  onError: (error) => {
    // Your error callback logic
    console.error('An error occurred:', error);
  },
});

Afterwards, you can continue using Fetch to send requests, and the interceptors will execute during the request process.

axiosInstance1.get('https://api.example.com/data')
  .then(response => {
    console.log('Axios response:', response.data);
  })
  .catch(error => {
    console.error('Axios error:', error);
  });
axiosInstance2.post('https://api.example.com/data')
  1. Set up interceptors for a single instance:
import { setupAxiosInterceptor } from 'global-request-interceptor';

// Configuration example for a single instance
setupAxiosInterceptor({
  instances: axiosInstance,
  onRequest: (config) => {
    // Your request interceptor logic
    // config.url += '?token=123456789'; 
    // config.headers['Sailing'] = 'abc';
    return config;
  },
  onResponse: (response) => {
    // Your response interceptor logic
    return response;
  },
  onError: (error) => {
    // Your error callback logic
    console.error('An error occurred:', error);
  },
});

Afterwards, you can continue using Fetch to send requests, and the interceptors will execute during the request process.

axiosInstance.get('https://api.example.com/data')
  1. Create a new instance, set default options, and set interceptors
import { setupAxiosInterceptor } from 'global-request-interceptor';

// Configuration example for creating a new instance with default options
const myAxios = setupAxiosInterceptor({
  defaultOptions: { baseURL: 'https://api.example.com' },
  onRequest: (config) => {
    // Your request interceptor logic
    // config.url += '?token=123456789'; 
    // config.headers['Sailing'] = 'abc';
    return config;
  },
  onResponse: (response) => {
    // Your response interceptor logic
    return response;
  },
  onError: (error) => {
    // Your error callback logic
    console.error('An error occurred:', error);
  },
});

Afterwards, you can continue using Fetch to send requests, and the interceptors will execute during the request process.

myAxios.post('https://api.example.com/data', data)
  .then(response => {
    console.log('Axios response:', response.data);
  })
  .catch(error => {
    console.error('Axios error:', error);
  });

Configuration Options

PropertyDescriptionTypeDefault Value
instancesAn array of Axios instances or a single Axios instanceaxiosInstance / axiosInstance[]-
defaultOptionsDefault options to be used when creating a new Axios instanceAxios Options Object{}
onRequestCallback function for request interceptor logicfunction(config)config
onResponseCallback function for response interceptor logicfunction(response)response
onErrorCallback function for error handlingfunction(error)-

Global Interception with Fetch

Usage

import { setupFetchInterceptor } from 'global-request-interceptor';

setupFetchInterceptor({
  // Request interceptor callback
  onRequest: async (config) => {
    console.log('Intercepted fetch request:', config);
    // Customize request configuration here
    // config.url += '?token=123456789'; 
    // config.headers['Sailing'] = 'abc';
    return config;
  },
  // Response interceptor callback
  onResponse: async (response) => {
    console.log('Intercepted fetch response:', response);
    // Customize response data here
    // const data = await response.json();
    // console.log('Parsed JSON data:', data);
    return response;
  },
  // Error interceptor callback
  onError: (error) => {
    console.error('Intercepted fetch error:', error);
    throw error;
  }
});

Afterwards, you can continue using Fetch to send requests, and the interceptors will execute during the request process.

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => {
    console.log('Fetch response:', data);
  })
  .catch(error => {
    console.error('Fetch error:', error);
  });

Configuration Options

PropertyDescriptionTypeDefault Value
onRequestCallback function for request interceptor logicfunction(config)config
onResponseCallback function for response interceptor logicfunction(response)response
onErrorCallback function for error handlingfunction(error)-

License

This project is licensed under the MIT License. For more information, please see the LICENSE file.

1.0.6-beta.1

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4-beta.0

5 months ago

1.0.4

5 months ago

1.0.3-beta.2

5 months ago

1.0.3-beta.1

5 months ago

1.0.3-beta.0

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago