[go: up one dir, main page]

voca-http

0.1.0 • Public • Published

voca-http

Voca HTTP

The voca-http is a very tiny wrapper for the Fetch API. No other dependency added in this library.

How to use Voca HTTP

import { voca } from 'voca-http';

// get API call 
voca.get('https://jsonplaceholder.typicode.com/todos')
	.then(data => console.log(data));
	
// post API call 
voca.get(
	'https://jsonplaceholder.typicode.com/todos', 
	{"id": 1, "value": "test"}
).then(data => console.log(data));

How to create Voca HTTP instance

The custom instance will help you to send custom headers, base url etc. Here is the example code how you create custom Voca HTTP instance.

// create instance
const API = voca.create(window.fetch, {
    onRequest: (method, route, data = undefined) => ({
      url: `${route}`,
      body: JSON.stringify(data),
      method,
      headers: { 'Content-Type': 'application/json' }
    }),
    onResponse: (response) => {
      if (response.status === 403) throw new Error('Authorization error.');
      return response.json();
    },
    onError: () => {
      return Promise.reject();
    },
});

// get API call 
API(
	'GET',
	'https://jsonplaceholder.typicode.com/todos'
)
.then((json) => console.log(json));

Browser support

The latest version of Chrome, Firefox, Edge and Safari.

Maintainer

Package Sidebar

Install

npm i voca-http

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

48.7 kB

Total Files

13

Last publish

Collaborators

  • subhojit1992