0.2.1 • Published 6 years ago
@willsoto/json-api-client v0.2.1
JSON API Client
Installation
yarn add @willsoto/json-api-client
npm install @willsoto/json-api-client
30 second look
import { JSONApiClient, JSONApiModel } from '@willsoto/json-api-client';
const client = new JSONApiClient();
class Author extends JSONApiModel {
static __type = 'authors';
static __endpoint = '/authors';
constructor(...args) {
super(...args);
}
}
// this is optional
client.register(Author.__type, () => {
// callback to instantiate model, etc
});
client
.query(Author)
.all()
.then((response) => {
// response.data is now a usable document
})
.catch((err) => {
// normal error object
});
Usage
API Client
JSONApiClient
is backed by axios and any options that axios expects can be passed during creation as axiosOptions
:
const client = new JSONApiClient({
axiosConfig: {
/* See https://github.com/axios/axios#request-config */
}
});
Once a client is created, you can begin making API calls:
// will make a request to `/authors`
const response = client
.query({
__endpoint: '/authors'
})
.all();
// will make a request to `/authors/1`
const response = client
.query({
__endpoint: '/authors'
})
.get('1');
Inclusion of Related Resources
client
.query({
__endpoint: '/authors'
})
.include('articles')
.all();
client
.query({
__endpoint: '/authors'
})
.include(['articles.comments', 'tags'])
.all();
TODO
- Inclusion of Related Resources (https://github.com/willsoto/json-api-client/issues/1)
- Sparse Fieldsets (https://github.com/willsoto/json-api-client/issues/2)
- Sorting (https://github.com/willsoto/json-api-client/issues/3)
- Pagination (https://github.com/willsoto/json-api-client/issues/4)
- Filtering (https://github.com/willsoto/json-api-client/issues/5)