An automatic HATEOAS traversing client.
$ npm install hateoasis
It's recommended creating interfaces extending the Hateoas
interface for all api responses.
export default interface Client extends Hateoas {
public name: string;
public address: string;
etc...
}
The Hateoas interface includes a links
property of type Hateoaslinks
. When constructing an Hateoas
object it requires you to create the links.
Serverside example:
...
const client: Client = {
name: 'John Doe',
address: 'Whereville',
links: [
{
rel: 'self',
method: 'GET',
href: '/api/clients/1'
}
]
}
res.status(200).json(client);
Too use HATEOAS from start, the server needs to have an index showing all available requests:
export default interface ApiIndex extends Hateoas {
version: string;
}
const index: Hateoas = {
version: '1.0.0',
links: [
{
rel: 'clients',
method: 'GET',
href: '/api/clients'
}
]
}
res.status(200).json(index);
Clientside example:
axios.get('/api').then(async response => {
const apiIndex = hateoasis(response.data);
const client = await apiIndex.request<Client>('client', 'GET');
console.log(client.data);
// If using strict TypeScript
const client = isRequestable(apiIndex) && await apiIndex.request<Client>('client', 'GET');
console.log(client.data);
});