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

hal-browser-express

v0.0.1

Published

A HAL browser middleware for Express.js

Downloads

5

Readme

HAL browser

This package provides a middleware for HAL APIs.

The middleware looks for HAL and JSON responses, and automatically converts them into a HTML interface if a browser access them.

It does so via the Accept: text/html header. If this header is not provided, the middleware does nothing.

It automatically decorates the following formats:

  • application/json
  • application/problem+json
  • application/hal+json
  • text/markdown

This package contains the Express.js middleware. The main package is hal-browser.

Example

An example. If a API normally returns the following HAL format:

{
  "_links": {
    "self": { "href":"/testing" },
    "previous": { "href":"/testing/?page=1", "title":"Previous page" },
    "next": { "href":"/testing/?page=2", "title":"Next page" },
    "author": { "href":"https://evertpot.com", "title":"Evert Pot" },
    "help": { "href":"https://google.com/", "title":"Google it" },
    "search": { "href":"https://google.com/{ ?q }", "templated":true },
    "edit": { "href":"/testing" }, "create-form":{ "href":"/testing" },
    "my-link": { "href":"/foo-bar", "title":"Custom link" }
  },
  "msg":"Hello world!",
  "version":"0.3.0",
  "name":"test resource!"
}

The browser will automatically convert it to this HTML format:

Screenshot from 0.3.0

Installation

npm install hal-browser-express

Getting started

To get started, just hook up the middleware

const express = require('express');
const halBowser = require('hal-browser-express').default;

const app = express();
app.use(halBrowser());

Or if you're using typescirpt

import express from 'express';
import halBrowser from 'hal-browser-express';

const app = express();
app.use(halBrowser());

Options

The halBrowser function takes an options object, which can take the following settings:

  • title - Change the main title.
  • stylesheets - Provide your own stylesheets. This is an array of strings. these are relative urls, and they are automatically expanded based on the assetBaseUrl setting.
  • navigationLinks - Specify (or remove) links that show up in the top navigation.
  • serveAssets - by default the browser plugin will also take responsibility for serving icons and stylesheet. If you're hosting these assets elsewhere, set this to false.
  • defaultLinks - A list of links that will show up by default, whether or not they were specified by the API. By default a home link is added here.
  • hiddenRels - List of relationship types that will be hidden from the user by default. This can be used for links that are simply not interesting for a human to see. (default: ['self', 'curies'].

Example:

app.use(halBrowser({
  title: 'My API',
  stylesheets: [
    '/my-stylesheet.css',
  ],

  // This should end with a / generally.
  assetBaseUrl: 'http://some-cdn.example.org/',

  navigationLinks: {
    // Create new 'author' button
    'author' : {
      // optional css class, by default this will be `rel-author`
      cssClass: 'rel-blabla',

      // Optional title to show when hovering over button
      defaultTitle: 'Click me',

      // Override icon. Also optional
      icon: 'icons/foobar.svg',

      // Either 'header' (default) or 'pager'
      position: 'header'

      // Set the order. Lower is earlier. Default is 0.
      priority: -100,

    },
    // passing 'true' will use default setting for the button
    'help' : true,

    // passing 'null' will remove the icon, if it was a default icon
    'up': null,
  },

  defaultLinks: [
    // Every page will have a 'help' link
    {
      rel: 'help',
      href: 'https://example.org/help',
      title: 'Support',
    }
  ],
});