Subhojit1992 / voca-http

Very light HTTP client based on Fetch API ⚙️

Home Page:https://github.com/Subhojit1992/voca-http

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


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

About

Very light HTTP client based on Fetch API ⚙️

https://github.com/Subhojit1992/voca-http


Languages

Language:JavaScript 98.8%Language:HTML 1.2%