Shorthand methods
The API schema keys can be mapped to custom method names:
let api = service.getEntry({
getItems: 'GET /items',
getItem: 'GET /items/:id',
setItem: 'POST /items/:id',
});
With such a mapping in place, service.send('GET /items/:id', {...})
has another equivalent form:
let response = await api.getItem({
params: {
id: 10,
},
query: {
mode: 'full',
},
});
The getEntry()
method doesn't have to take all the API schema keys at once. The API methods can be split into logical scopes:
let api = {
users: service.getEntry({
getList: 'GET /users',
getInfo: 'GET /users/:id',
}),
items: service.getEntry({
getList: 'GET /items',
getInfo: 'GET /items/:id',
setInfo: 'POST /items/:id',
}),
};
let userList = await api.users.getList();
let firstUser = await api.users.getInfo({params: {id: userList[0].id}});
For API methods controlled only with query parameters, there is also a shorthand option: the getQueryEntry()
method, returning aliases accepting only query parameters, without the need to nest them into the query
key.