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

semantic-link

v1.0.8

Published

A utility library for manipulating a list of links that form a semantic interface to a resource.

Downloads

127

Readme

Semantic Link library

A utility library for manipulating a list of links that form a semantic interface to a resource.

This library is able to work with atom-like representations (ATOM is an XML-based document format). We use this to describe collections which describes lists of related information collections (in atom known as "feeds"). Collections (feeds in atom) are composed of a number of items (in atom known as "entries") each with an extensible set of attached metadata or attributes.

The library has two aspects:

  1. Matching and filtering of link relations on a representation [filter,matches,getUri,getTitle]
  2. Sending and receiving across the wire via http (axios) based on above [get,put,post,delete, patch, tryGet]

Representation

Very simple examples

import { getUri, matches, filter } from 'semantic-link';
import * as semanticLink from 'semantic-link';

const order = {
  links: [
    { rel: "self", href: "http://example.com/order/1" }
   ]
   title: "A first order"
 }

getUri(order, /self/);                                // returns 'http://example.com/order/1'
matches(order, /self/);                               // returns true
filter(order, /self/);                                // returns order object

semanticLink.get(order, /self/);                      // HTTP GET on self link to return representation (via axios)
semanticLink.put(order, /self/, {title: 'Updated'});  // HTTP PUT on self link to send back data

Installation

Using yarn:

$ yarn add semantic-link

Note: this has a dependency on axios

Usage

//individually
import { getUri, getTitle, matches, filter, get, put, post, del, patch, tryGet } from 'semantic-link';

// as wildcard
import * as semanticLink from 'semantic-link';

Matching on link relations

The basic interface for matching is to look for link rels on the representation.

get(resource, 'self')
get(resource, /self/)
get(resource, /self|canonical/)
post(resource, ['submit', 'self'])

There are lots of ways to do this:

  • string: exact match on only that value
    • 'self'
  • regex: result of the regular expression
    • /self/ (anywhere)
    • /^self$/ (must match-same as string match)
    • /self|canonical/ (either)
    • /edit-form/
  • list of strings: exact match in order
    • ['self', 'submit']

Note: you can have a list of regex too.

Matching on media type (optional)

The basic interface for matching can also be specific to media type.

filter(resource, 'self')
matches(resource, 'edit-form', 'text/uri-list')
get(resource, 'self', 'application/json')
post(resource, 'submit', 'application/json-patch+json', data)

Note: When the media type is specified it must be found there is no fallback other types.

Http (get,put,post,delete, patch, tryGet, link)

This is just a light wrapper around axios and can have all the options of axios handed in.


// Promise based

get(resource, 'self')
    .then(response => {
        // type: AxiosResponse<LinkedRepresentation|CollectionRepresentation>
    })
    .then(err => {
        // type: AxiosError
    });

// or async/await (or with error handling try/catch)

const response = await get(resource, 'self');


Cancellable (CancelToken)

There is also a specific param for a Cancellable.

import { CancelToken } from 'axios';

const CancelToken = axios.CancelToken;
const source = CancelToken.source();

get(resource, 'self', 'application/json', source.token)

source.cancel('Operation canceled by the user.');

Note: access to axios is through the link interface (all the other methods call this) and the AxiosRequestConfig can overridden on the options. Also note, it is likely that you'd be better to use the axios interceptors. There are a couple of scenarios:

  1. 401 unauthorised should be implemented through interceptors, as well as setting of www-authenticate headers
  2. Semantic link uses an atom-like across-the-wire microformat (aka media type) and the same for in-memory It is recommended that to use this in-memory structure and translate between other microformats (eg HAL, SIREN, Collection+JSON)). This can be done through many ways (1) on the outside of the request/response (2) through interceptors and (3) on the AxiosRequestConfig. Any of these should work with the library.
  3. Throttling should be done outside of both of these mechanisms. It has been implemented with this utility using Bottleneck.

License

MIT