@enspirit/magicrest
v0.1.0
Published
Super easy, magical and universal REST api client on top of axios
Downloads
4
Readme
magicrest
The goal of this library is to make rest api clients easy and generic.
Usage example
import { createClient } from '@enspirit/magicrest';
const client = createClient('http://localhost');
// GET http://localhost/
await client.get();
// GET http://localhost/people
await client.people.get();
// GET http://localhost/people?filter=value
await client.people.get({ filter: 'value' });
// GET http://localhost/people/12
await client.people(12).get();
// GET http://localhost/people/12/hobbies
await client.people(12).hobbies.get();
// DELETE http://localhost/people/12/hobbies/24
await client.people(12).hobbies(24).delete();
// POST http://localhost/people/12/hobbies
// with body: { name: 'Bass playing' }
await client.people(12).hobbies.post({ name: 'Bass playing' });
// You can create clients with defaults
const authdApi = client.withDefaults({ headers: { Authorization: 'Bearer some-token' }});
await authdApi.my.profile.get();
// Subclients deep merge their settings
const authdApiWithMoreHeaders = authdApi.withDefaults({
headers: {
Accept: 'application/xml'
}
});
await authdApiWithMoreHeaders.finance.reports.year(2022).get();
Close relationship with Promises
It is important to know that magicrest only triggers HTTP request when the Promise API is being used.
This means that http request will only be triggered when one of the following method is being called on magicrest's returned objects:
then()
catch()
finally()