Global Request Interceptor
简体中文
![npm version](https://badge.fury.io/js/global-request-interceptor.svg)
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
- Set up interceptors for multiple instances:
import { setupAxiosInterceptor } from 'global-request-interceptor';
setupAxiosInterceptor({
instances: [axiosInstance1, axiosInstance2],
onRequest: (config) => {
return config;
},
onResponse: (response) => {
return response;
},
onError: (error) => {
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')
- Set up interceptors for a single instance:
import { setupAxiosInterceptor } from 'global-request-interceptor';
setupAxiosInterceptor({
instances: axiosInstance,
onRequest: (config) => {
return config;
},
onResponse: (response) => {
return response;
},
onError: (error) => {
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')
- Create a new instance, set default options, and set interceptors
import { setupAxiosInterceptor } from 'global-request-interceptor';
const myAxios = setupAxiosInterceptor({
defaultOptions: { baseURL: 'https://api.example.com' },
onRequest: (config) => {
return config;
},
onResponse: (response) => {
return response;
},
onError: (error) => {
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
Property | Description | Type | Default Value |
---|
instances | An array of Axios instances or a single Axios instance | axiosInstance / axiosInstance [] | - |
defaultOptions | Default options to be used when creating a new Axios instance | Axios Options Object | {} |
onRequest | Callback function for request interceptor logic | function(config) | config |
onResponse | Callback function for response interceptor logic | function(response) | response |
onError | Callback function for error handling | function(error) | - |
Global Interception with Fetch
Usage
import { setupFetchInterceptor } from 'global-request-interceptor';
setupFetchInterceptor({
onRequest: async (config) => {
console.log('Intercepted fetch request:', config);
return config;
},
onResponse: async (response) => {
console.log('Intercepted fetch response:', response);
return response;
},
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
Property | Description | Type | Default Value |
---|
onRequest | Callback function for request interceptor logic | function(config) | config |
onResponse | Callback function for response interceptor logic | function(response) | response |
onError | Callback function for error handling | function(error) | - |
License
This project is licensed under the MIT License. For more information, please see the LICENSE file.