0.1.1 • Published 5 years ago
apist v0.1.1
apist
Simple REST API client
THIS PACKAGE IS NO LONGER MAINTAINED
Install
$ npm install --save apist
Usage
import Apist from 'apist';
const users = new Apist('users');
users.fetch(10, { query: 'query' }).then(data => console.log(data.firstName));
//=> GET /users/10?query=query
Within the used fetch
. You can add polyfill to your project.
Configure to work with your API, by overriding methods.
class CustomApist extends Apist {
constructor(resource) {
super(resource);
// Used only when sending data.
// By default `application/json`.
this.contentType = 'application/json';
// Fetch options.
this.options = {};
this.headers = {};
this.host = 'http://example.com';
this.namespace = 'api/v2';
}
handleError(err) {}
// Serialization of sent data.
serialize(data) {}
// Deserializing the received data.
deserialize(req, res) {}
}
API
Apist(resource)
resource
Type: string
The name of the API resource.
Instance
All CRUD methods return a Promise
.
.fetchAll(query)
GET
/resource?query
query
Type: object
.fetch(id, query)
GET
/resource/:id?query
id
Type: number|string
query
Type: object
.create(data)
POST
/resource
data
Type: any
.update(id, data)
PUT
/resource/:id
id
Type: number|string
data
Type: any
.delete(id)
DELETE
/resource/:id
id
Type: any
Related
- redux-apist - Creator API actions for redux-thunk