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

@contentgrid/hal

v0.3.0

Published

Hypertext Application Language resource models

Downloads

341

Readme

@contentgrid/hal

Typescript models for reading the HAL+json format.

HAL links and embedded objects are aware of CURIEs and can resolve them using extended link relations.

Usage

The typical entrypoints for this library is HalObject.

A HalObject is constructed from the HAL+json response body and can be used to links, embedded objects and the original data.

For convenience, there is also a HalSlice object that can be used to more easily access paginated data. This assumes that standard link relations are used for pagination and that items on a page are _embedded.

import { HalObject, HalSlice } from '@contentgrid/hal';
import { HalObjectShape, HalSliceShape } from '@contentgrid/hal/shape';
import { createRelation } from '@contentgrid/hal/rels';

namespace myLibrary {

    export interface Gift {
        id: number;
        name: string;
    }

    export const objectData: HalObjectShape<Gift> = {
        id: 1,
        name: "Parachute",
        _links: {
            self: {
                href: "http://localhost/gifts/1"
            }
        }
    };

    export const sliceData: HalSliceShape<Gift> = {
        "_embedded": {
            "gifts": [
                objectData
            ]
        },
        "_links": {
            self: {
                href: "http://localhost/gifts?page=2"
            },
            first: {
                href: "http://localhost/gifts"
            },
            previous: {
                href: "http://localhost/gifts?page=1"
            },
            next: {
                href: "http://localhost/gifts?page=3"
            }
        }
    };
}

const object = new HalObject(myLibrary.objectData);

const selfLink = object.links.requireSingleLink(createRelation("self"));

console.log(selfLink);

var page = new HalSlice(myLibrary.sliceData);

for(const item of page.items) {
    console.log(item.self.href);
}

console.log("Next page:", page.next?.href)
console.log("Previous page:", page.previous?.href)

Link relations and CURIEs

In HAL, the keys for both _links and _embedded are RFC8288 link relation types or CURIEs.

The internal representation of this library can work with both, but accessing links (or embedded objects) is only possible with a link relation type (LinkRelation), not with a CURIE. This is because a CURIE is not a stable representation, as its prefix freely be changed.

The @contentgrid/hal/rels sub-package provides methods to work with link relations.

All IANA-registered link relations are available in the ianaRelations object.

Custom objects with extended LinkRelations can be created with the createRelations() function.

Utilities to work directly with CURIEs are available in the @contentgrid/hal/curies sub-package, but these functions are mostly for internal usage.

Shapes

The @contentgrid/hal/shapes sub-package provides POJO (plain old javascript object) types that can be used to represent the raw HAL JSON data.