JSPM

hateoasis

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q5603F
  • License ISC

An automatic HATEOAS traversing client.

Package Exports

  • hateoasis

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hateoasis) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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