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

intercept-fetch

v1.4.3

Published

add interceptors in fetch

Downloads

69

Readme

Build Status Downloads Versions License

Add interceptors in fetch api

Usage

  • install npm i intercept-fetch --save

  • add interceptors

import {
  FetchClient,
  Interceptor
} from 'intercept-fetch'

const fetchClient = new FetchClient()
const interceptor = new Interceptor({
  cors: {
    id: 0,
    request(url, config) {
      url += '&a=1'
      config.mode = 'cors'
      return Promise.resolve([url, config])
    },
    success(data) {
      return new Promise((resolve) => {
        setTimeout(() => {
          console.log('res a', data)
          data.a = 'intercepta'
          resolve(data)
        }, 1000)
      })
    }
  },
  credentials: {
    id: 1,
    request(url, config) {
      url += '&b=2'
      config.credentials = 'include'
      return Promise.resolve([url, config])
    },
    requestError(fetchError) {
      return Promise.reject('requestError reject')
      // or return Promise.resolve('requestError resolve')
    },
    response(response) {
      return Promise.resolve(error)
    },
    success(data) {
      return new Promise((resolve) => {
        setTimeout(() => {
          console.log('res b', data)
          data.b = 'interceptb'
          resolve(data)
        }, 1000)
      })
    },
    error(res) {
      return Promise.resolve(res)
    },
    timeout(url) {
      return Promise.resolve('timeout')
      // default timeout is 10s
      // or return Promise.reject('timeout)
    }
  }
})

fetchClient.setInterceptors(interceptor)

fetchClient.get('http://google.com')

Warning: at end of each interceptor, you can reject(any) to catch, if you want to complete the fetch flow, please resolve as ref example!!!

class FetchClient

  • getInterceptors(): IInterceptors
  • setInterceptors(interceptors: Interceptor): void
  • clearInterceptors(): void
  • request(url: string | Request, config?: RequestInit): Promise
  • get(url: string, param?: { [key: string]: any }, config?: RequestInit): Promise
fetchClient.get('http://google.com', { date: Date.now() })
  • post(url: string, config?: RequestInit): Promise
  • put(url: string, config?: RequestInit): Promise
  • delete(url: string, config?: RequestInit): Promise
  • options(url: string, config?: RequestInit): Promise
  • head(url: string, config?: RequestInit): Promise
  • patch(url: string, config?: RequestInit): Promise

class Interceptor

  • interface
export interface IInterceptor {
  id?: number
  request?: (url: string | Request, config: RequestInit) => Promise<[string | Request, RequestInit]>
  requestError?: (error: any) => Promise<any>
  response?: (res: Response) => Promise<Response>
  success?: (data: any) => Promise<any>
  error?: (res: Response) => Promise<Response>
  timeout?: (url: string) => Promise<any>
  jsonpRequest?: (url: string, config?: fetchJsonp.Options) => Promise<[string, fetchJsonp.Options]>
  jsonpResponse?: (res: fetchJsonp.Response) => Promise<fetchJsonp.Response>
  jsonpSuccess?: (data: any) => Promise<any>
  jsonpError?: (error: any) => Promise<any>
}
export interface IInterceptors {
  [key: string]: IInterceptor
}
  • set(key: string, value: IInterceptor): void
  • get(key: string): IInterceptor
  • delete(key: string): void
  • has(key: string): boolean
  • forEach(callback: (value?: IInterceptor, key?: string, target?: Interceptor) => void, thisArg?): void
  • merge(interceptors: Interceptor): Interceptor // merge this Interceptor to param Interceptor
const I = new Interceptor({
  a: {

  }
})
I.merge(new Interceptor({
  a: {
    request(url, config){
      return Promise.resolve([url, config])
    }
  }, 
  b: {}
}))

// I
// a: {
// }, 
// b: {}

Differences with https://github.com/werk85/fetch-intercept

  • All interceptors(request, response, success, error) are Promise

  • Provide a interceptor class

  • Can add more than one interceptors ordered by id, and the smaller id is call first

  • Support typescript