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

1.0.2 • Public • Published

A lightweight wrapper to use fetch more efficiently and easily and with better error handling, and this is promisified!

Size Types Downloads


Basic usage:
import from CDN

<script type="text/javascript" src="https://unpkg.com/fetch-req@1.0.0"></script>

Or from NPM,

import fetch from 'fetch-req';
const someFunction = async ()=> {
  const respGET = await fetch.get('http://dummy.restapiexample.com/api/v1/employees');
  const respPOST = await fetch.post('http://dummy.restapiexample.com/api/v1/employees', { 'dummyData': 'Mukesh'});
}

Additional methods:
1. Poll - call an endpoint and handle response in interval.

const endpoint = () => fetch.get('http://dummy.restapiexample.com/api/v1/employees');

const poll = fetch.poll(endpoint, callback, 1000);
poll.pause();                // Pause polling
poll.resume();               // Resume polling
poll.updateInterval(1000);  // Update the interval, set it to 1000ms

Note: This doesn't use setInterval, and it prevents the race conditions because, it makes the next call exactly after given time(in ms) from the time last response received.

2. Retry - Retry no of times in case request fails.

const resp = await fetch.retry(3).get('http://dummy.restapiexample.com/api/v1/employees');

Package Sidebar

Install

npm i fetch-req

Weekly Downloads

4

Version

1.0.2

License

MIT

Unpacked Size

14.2 kB

Total Files

11

Last publish

Collaborators

  • muke1908