Add interceptors in fetch api
Usage
-
install
npm i intercept-fetch --save
-
add interceptors
fetchClient.setInterceptorsinterceptor 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',
- 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
- 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
I.mergenew Interceptor // I// a: {// }, // b: {}
https://github.com/werk85/fetch-intercept
Differences with-
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