npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

global-request-interceptor

v1.0.7

Published

global-request-interceptor

Downloads

9

Readme

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

| 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({
  // 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

| 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.