zfetchz
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

zfetchz

fetch has timeout and interceptor; Doc: https://klren0312.github.io/zfetch/

NPM version npm

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)
})

Example

in /example/index.html

Package Sidebar

Install

npm i zfetchz

Weekly Downloads

2

Version

0.1.2

License

MIT

Unpacked Size

307 kB

Total Files

10

Last publish

Collaborators

  • zzes