zfetchz
v0.1.2
Published
fetch has timeout and interceptor
Downloads
5
Readme
zfetchz
fetch has timeout and interceptor; Doc: https://klren0312.github.io/zfetch/
Install
$ npm install zfetchz --save
Build
$ npm run build
Test
$ npm run test
Usage
1.Init
If use it in Browser, you should use in browser' in first param; Else if you use it in NodeJS, you should use 'node' in first param. The second param is timeout, when you fetch data
const z = new zfetchz('browser', 2000);
2.config interceptor (Default no interceptor)
var zfetchzInterceptor = z.interceptor
zfetchzInterceptor.register({
request: function (...request) {
console.log('request', request)
return request
},
response: function (response) {
console.log('response', response)
return response
}
})
3.If you want to unregister the interceptor, you should assign it to a variable, then call it
const unregister = zfetchzInterceptor.register({
request: function (...args: any[]) {
return args;
}
});
unregister();
4.Fetch data
z.newFetch('https://cnodejs.org/api/v1/topics', {
headers: new Headers({
'content-type': 'application/json'
})
})
.then(res => res.json())
.then(res => {
console.log(res)
})