[![Build Status](https://travis-ci.org/klamtlne/SimpleHTTPService.svg)](https://travis-ci.org/klamtlne/SimpleHTTPService)

> Promisify backend API to frontend

### Usage

```javascript
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;

```

```javascript
import API from './service';

API.blog.post({ name: 'hello world' })
  .then(handleResolve, handleReject);

```

### API

```javascript
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

```javascript
// 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

```javascript
// 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
