hateoasis
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

HateOasis

Build Status Coverage Status npm dependencies Status devDependencies Status Downloads/week Dependabot Status

An automatic HATEOAS traversing client.

Installation

Node.js

$ npm install hateoasis

Example usage:

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);
});

Package Sidebar

Install

npm i hateoasis

Weekly Downloads

1

Version

1.0.3

License

ISC

Unpacked Size

10.3 kB

Total Files

6

Last publish

Collaborators

  • gnarr