HAL serialization middleware. Works best with expressify/model resources.


  • contentType: The Content-Type to serialize to. You can pick one from require('@ambassify/expressify/constant').ContentType. Only JSON at this time.

The rest of these options are passed directly to the internal serializer library.

Translation options

Required if you have translatable resources. Uses ambassify/language-object under the hood.

  • languagePreference: An array with preferred languages in order of preferrence or a function that returns such an array when called with options.
  • availableLanguages: An array with available languages.

Self-href options

  • selfHref: string with self href for this response or a function that returns such a string when called with (data, options);
  • selfDefinitions: a dictionary of [constructor]->[(res, resource) => selfHref] mappings (see example)
const Resource = require('@ambassify/expressify/model/Resource');
const createHALMiddleware = require('@ambassify/expressify/middleware/HAL');

class Post extends Resource {}

// define how Post instances should get their self link
const selfDefinitions = {
    [Post]: (req, resource) => `https://api.com/post/${resource.id}`

const middleware = createHALMiddleware({ selfDefinitions });

Formatting options

A method for specific datatypes that will determine how values of that type are formatted.

  • formatters: An object with the key set to a supported datatype (currently only date), with the value set to a function that will receive the value needing formatting as the first and only argument.
const createHALMiddleware = require('@ambassify/expressify/middleware/HAL');

const formatters = {
	date: (date) => date.toString(),

const middleware = createHALMiddleware({ formatters });