npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-hateoas-yml

v1.12.0

Published

This package will generate HATEOAS links automatically based on a configuration file.

Downloads

49

Readme

Express HATEOAS with YML

Building Coverage Status Dependencies Dev Dependencies Last commit Downloads NPM version Linked In

This package will generate HATEOAS links automatically based on a configuration file and is based on express-hateoas-links by John Doherty.

Install

Install the package with NPM:

npm install --save express-hateoas-yml

Or with Yarn:

yarn add express-hateoas-yml

Use

Declare and specity the path for the YML/YAML file with the related links:

const hateoas = require('express-hateoas-yml');
const hateoasOptions = {
    linksFile: path.join(__dirname, 'libs/links.yml')
};

In the Examples section you'll find examples of how to configure related links.

Add the middleware to Express:

const express = require('express');
const app = express();

app.use('*', (req, res, next) => hateoas(req, res, next, hateoasOptions));

Examples

Basics

In the following example we've a basic example where the a GET URL /api/v1/customers should return a reference to URL /api/v1/customers/birthdays and method GET.

/api/v1/customers:
  GET:
    - rel: customers
      method: GET
      href: /api/v1/customers/birthtoday

Send your response as usual:

result.data = await controller.getAll();

return res.status(200).json(result);

Then the response body should includes the original resultdata and the links:

{
    "data": [
        {
            "name": "Paulo",
        },
        {
            "name": "Cesar"
        },
        {
            "name": "Silva"
        }
    ],
    "_link": [
        {
            "rel": "customers",
            "method": "GET",
            "href": "http://localhost:3000/api/v1/customers/birthtoday"
        }
    ]
}

Placeholders

The reference can return a link with dynamic URL, like a record id. Note the :1 below:

/api/v1/customers:
  POST:
    - rel: self
      method: GET
      href: /api/v1/customers/:1

Then in the route code:

// save the record as usually
const data = await controller.save(req.body);

// and put the record id as an additional parameters
return res.status(201).json(data, data._id);

URL Parameters

This package will recognize the parameters and replace it before look for links in YML file:

  • Object Id will be replaced by :oid
  • UUID by :uuid
  • E-mail by :email
  • Number by :number

Examples:

  • /api/v1/customers/123 will be replaced for /api/v1/customers/:number.
  • /api/v1/customers/98949bc9-bf0a-4ceb-8f45-fa07ccd39d76 will be replaced for /api/v1/customers/:uuid.

Reuse

It's possible to reuse other definition within the original URL.

/api/v1/customers:
  POST:
    - rel: self
      method: GET
      href: /api/v1/customers/:1
  PUT:
    use: POST

You also can reuse an entire endpoint as shown below where a PUT request will return all related links of GET requests:

/api/v1/customers/:oid:
  PUT:
    use: GET
  GET:
    - rel: sales
      method: GET
      href: /api/v1/customers/:1/sales
    - rel: self
      method: DELETE
      href: /api/v1/customers/:1
    - rel: self
      method: PUT
      href: /api/v1/customers/:1
/api/v1/customers/:email:
  use: /api/v1/customers/:oid
/api/v1/customers/:uuid:
  use: /api/v1/customers/:oid

Property name

By default the related links in the response object will be inside the property named _links as indicative of a metadata. But you can change it's name by setting a propertyName in options object:

const hateoasOptions = {
    linksFile: path.join(__dirname, 'libs/links.yml'),
    propertyName: 'links'
};

Additional data

You can add any extra information within the related link item and it will be included in the response as you can see below:

/api/v1/customers/:oid:
  PUT:
    use: GET
  GET:
    - rel: sales
      method: GET
      description: Get all customer's orders
      href: /api/v1/customers/:1/sales
    - rel: self
      method: DELETE
      warning: Delete this customer permanently
      href: /api/v1/customers/:1
    - rel: self
      method: PUT
      href: /api/v1/customers/:1

Below the result with extra information description and warning

{
    "active": true,
    "_id": "5c1be67e55087519a29065e6",
    "name": "Paulo",
    "_links": [
        {
            "rel": "sales",
            "method": "GET",
            "description": "Get all customer's orders",
            "href": "http://localhost:3000/api/v1/customers/5c1be67e55087519a29065e6/sales"
        },
        {
            "rel": "self",
            "method": "DELETE",
            "warning": "Delete this customer permanently",
            "href": "http://localhost:3000/api/v1/customers/5c1be67e55087519a29065e6"
        },
        {
            "rel": "self",
            "method": "PUT",
            "href": "http://localhost:3000/api/v1/customers/5c1be67e55087519a29065e6"
        }
    ]
}

Bugs

Please use the Project Issues page to report bugs or send suggestions.

License

Licended under ISC License © Paulo César da Silva