simplehttpservice
v0.3.14
Published
[![Build Status](https://travis-ci.org/klamtlne/SimpleHTTPService.svg)](https://travis-ci.org/klamtlne/SimpleHTTPService)
Downloads
56
Readme
Promisify backend API to frontend
Usage
import Service from 'simplehttpservice';
// define your API map
var APIS = [
{ name: 'blog', url: '/blogs/:id' },
{
name: 'comment',
url: '/comments'
}
];
var config = {
headers: {
'Content-type': 'application/json'
},
withCredentials: true
};
var service = new Service(config);
service.config(APIS);
export default service;
import API from './service';
API.blog.post({ name: 'hello world' })
.then(handleResolve, handleReject);
API
import Service from 'simplehttpservice';
/**
* config {Object} custom config properties for all ajax requests
*
*/
new Service(config);
Installation
$ npm install simplehttpservice --save
Difference with fetch
Rules
// rule is a function you can get response and decide resolve/reject before Application code
function rule(xhr, resolve, reject) {
if (!xhr.headers['X-header']) return reject('no specify header');
resolve();
}
var service = new Service({ rules: [ rule ] });
Formatter
// formatter can help you format response to Application code
// For instance, you backend API is: { data: { id, name } }
// let's format it to { id, name }
function formatter(raw) {
var data = JSON.parse(raw);
return JSON.stringify(raw.data);
}
var service = new Service({ formatter: formatter });
License
MIT