New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

global-request-interceptor

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

global-request-interceptor

global-request-interceptor

  • 1.0.7
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
0
Weekly downloads
 
Created
Source

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.

FAQs

Package last updated on 12 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc