2.1.8 • Published 6 years ago
common-http v2.1.8
CommonHttp
CommonHttp is a simple wrapper around the Fetch API to reduce the boilerplate code and improve developer experience, this package does not include any third party dependencies.
Usage
The usage is simple and straight forward, if you're familiar with Axios, Angular Services or jQuery then this should be second nature. First of all go to your project and install the package via NPM:
npm i common-http
Then import the package and create an instance.
// Import the package
import CommonHttp from 'common-http';
// Create an instance of `CommonHttp`
const http = new CommonHttp({
// Set the base url
url: 'https://jsonplaceholder.typicode.com',
// Inject default options before request is being made
injectOptions() {
return {
headers: {
Accept: 'application/json',
Authorization: 'Bearer: token',
'Content-Type': 'application/json'
}
};
},
// Do something after the request is finished (check 401 for example)
afterRequest(response) {
console.log('🏁 Request finished');
console.log(response);
console.log(response.url);
console.log(response.status);
console.log(response.headers);
console.log(response.statusText);
}
});
// Use `get`, `post`, `put`, `patch`, `delete` or `request`
http.get('/posts');