1.0.8 • Published 6 years ago

smallfetch v1.0.8

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

smallfetch

Smallfetch is a pre configured fetch object ready to interact with a Rest API

Tiny implementation of 200B with no dependencies.

  • It sets headers by default to 'application/json'.
  • It stringify object payload and pass it as body content for PUT/POST/PATCH.
  • It parses server response to JSON.
  • It throws a cachable error when the response is not in the range 200-299.

Installation

npm install smallfetch
import sf from 'smallfetch';

Usage

Get - Search

let books = await sf('https://example.com/books');
console.log(books);

Post - Create

sf('https://example.com/books','POST',{name:'Book Name',desc:'Description'})
.then(response=>console.log(response))
.catch(error=>console.log('something is wrong',error));

Put - Update

sf('https://example.com/books/1','PUT',{name:'Book Name',desc:'Description'})
.then(response=>console.log(response))
.catch(error=>console.log('something went wrong',error));

To avoid throwing errors (for parallel request) pass false at the end of the request.

Promise.all([
sf('https://example.com/books/1','POST',false),
sf('https://example.com/books/2','POST',false),
sf('https://example.com/books/3','POST',false)
]).then(results=>{
	results.forEach(result=>console.log(result))
});

All request will execute without failing. If the request fail an object with the request data will be returned but no exception will be thrown.

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago