restify-links

Middleware that adds the res.links function from Express to Restify responses objects.

Usage no npm install needed!

<script type="module">
  import restifyLinks from 'https://cdn.skypack.dev/restify-links';
</script>

README

node-restify-links

Middleware that adds the res.links function from Express to Restify responses objects.

Usage

Join the given links to populate the "Link" response header field.

res.links({
    next: 'http://api.example.com/users?page=2',
    last: 'http://api.example.com/users?page=5'
});

yields:

Link: <http://api.example.com/users?page=2>; rel="next", 
      <http://api.example.com/users?page=5>; rel="last"

This library also works for link definition objects and arrays, allowing one to construct significantly more complex and descriptive link relations. You can mix and match as needed, plus keep adding to the list of links.

res.links({
    next: "http://api.example.com/users?page=2",
    self: "http://api.example.com/users",
    search: "http://api.example.com/users{?username,firstname,lastname}"
});
res.links({
    last: "http://api.example.com/users?page=5",
    stylesheet: {
        href: "http://static.example.com/ss.css",
        type: "text/css"
    },
    up: [
        "http://api.example.com/"
    ],
    service: [
        {
            href: "https://api.example.com/auth",
            method: "post",
            profile: "/schemas/auth",
            title: "auth"
        }
    ]
});

results in:

Link: <http://api.example.com/users>; rel="self",
      <http://api.example.com/users?page=2>; rel="next",
      <http://api.example.com/users{?username,firstname,lastname}>; rel="search",
      <http://api.example.com/users?page=5>; rel="last",
      <http://static.example.com/ss.css>; rel="stylesheet"; type="text/css",
      <http://api.example.com/>; rel="up",
      <https://api.example.com/auth>; rel="service"; method="post"; profile="/schemas/auth"; title="auth"

This greatly assists with working with REST level 3 (Hypermedia) APIs and making the resources self-describing. In fact, the link definition objects are compatible with JSON Schema for Hypermedia and HAL Link Objects. It can use the link-extension part of the Web Linking specification.