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

@hateoas/resolver

v1.5.0

Published

Resolve HATEOAS links from objects

Downloads

93

Readme

@hateoas/resolver

npm version Continuous integration status Maintainability Test Coverage

Translate HATEOAS HAL links into URLs that can be used by your application trough one method. Handles missing links and translation of templated link options into query string parameters.

It supports following expansions of the URI template spec:

  • {}
  • {#}
  • {.}
  • {/}
  • {;}
  • {?}
  • {&}

Valid arguments for expansion are single string or arrays of strings. When providing an array in combination with an operator, this variable will be exploded by default with the separator associated with the operator i.e.:

const links = {
  _links: {
    example: {
      href: 'https://local{/api}{?param}'
    }
  }
}

resolve(links, 'example', {
  api: ['path', 'subpath'],
  param: ['p1', 'p2'],
}) >> 'https://local/path/subpath?param=p1&param=p2'

Install

npm install hateoas

Usage

Resolver is written for EcmaScript modules, so after installing you are one step from using it.

import { resolve } from 'hateoas';

This defined user object will be a base for all of our examples.

const user = {
  name: 'The Doctor',
  _links: {
    self: 'https://example.com/characters/1',
    companions: {
      href: 'https://example.com/characters/1/companions{?page,size}',
      templated: true,
    },
    enemies: [
      {
        href: 'https://example.com/characters/1/enemies{?race,page,size}',
        templated: true,
      },
      {
        href: 'https://example.com/characters/1/enemies{?nameSearch,page,size}',
        templated: true,
      },
      {
        href: 'https://example.com/characters/1/enemies?noParams',
        templated: false,
      },
    ],
  },
};

Getting simple string link

resolve(user._links, 'self');
// https://example.com/characters/1

Getting templated link

When you pass no parameters, the link template will be simply stripped of options. Based on top example.

resolve(user._links, 'companions');
// https://example.com/characters/1/companions

When you pass parameters, they will be translated into the link template options. Based on top example.

resolve(user._links, 'companions', {
  page: 3,
  size: 5,
});
// https://example.com/characters/1/companions?page=3&size=5

Missing some parameters?

Beware, all parameters that have no matching link option will get lost. Based on top example.

resolve(user._links, 'companions', {
  page: 3,
  filter: 'Bad Wolf',
});
// https://example.com/characters/1/companions?page=3

Getting link from array of links

The resolver will choose the best link for you based on the list of parameters you provide. If you provide none, then the non-templated links are preferred. Based on top example.

resolve(user._links, 'enemies');
// Returns the non-templated link
// https://example.com/characters/1/enemies?noParams
resolve(user._links, 'enemies', {
  race: 'timelord',
});
// https://example.com/characters/1/enemies?race=timelord

Conflicting values

It may happen that you provide parameters that are mutually exclusive by the links definition. In that case, resolver will give you the first link specified. In the top example, we cannot search by name and race at the same time.

resolve(user._links, 'enemies', {
  race: 'timelord',
  nameSearch: 'The Master',
  page: 10,
  size: 5,
});
// https://example.com/characters/1/enemies?race=timelord&page=10&size=5

Object shortcut

If your API uses _links or links to provide HAL links, you can simply pass the object to get branch shortcut.

resolve(user, 'self');
// https://example.com/characters/1