intereq
Apply interceptors to fetch
and create a custom request function.
- ⚡️ Dependency free and pretty small, 0.3KB (minified + gzipped);
- 🏷 Well type defined with TypeScript (with generics);
- 📦 There are ESM, CommonJS and UMD distributions;
Installation
This library is published in the NPM registry and can be installed using any compatible package manager.
npm install intereq --save # Use the command below if you're using Yarn. yarn add intereq
Usage
intereq
is curry function, which applies interceptors to fetch
and returns a new request function.
; ; ; request'POST', '/user', .thenresponse.success && alert'User was created!';
Interceptors
-
onError
onError?:Promise<never>;Handle request and response errors.
-
onRequest
onRequest?:RequestOptions;Handle request and define request arguments.
A
generic type defines returnedrequest
function arguments type. -
onRequestError
onRequestError?:Promise<never>;Handle request errors. Overwrites
onError
handling request errors. -
onResponse
onResponse?:R | PromiseLike<R>;Handle response and define the request return.
R
generic type defines returnedrequest
function result type. -
onResponseError
onResponseError?:Promise<never>;Handle response errors. Overwrites
onError
handling response errors.
Usage on unsupported browsers
Older browsers don't support Fetch API
, but you can use unfetch
or other polyfill to achieve it.
;; fetch ...options headers: ...optionsheaders 'Content-Type': 'application/json' response;
Usage on Node.js
Node environment does not provide global fetch
function, but you can use node-fetch
to achieve it.
const fetch = ;const intereq = ; moduleexports = ;
License
Released under MIT License.