HAL middleware
HAL serialization middleware. Works best with expressify/model resources.
Options
contentType: The Content-Type to serialize to. You can pick one fromrequire('@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 withoptions.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 onlydate), 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 });
Fields
fields: AQL-fields config object.
Fields named on a collection endpoint apply to its embedded resources. The collection’s own root fields are always kept.
Set fields: true to have Expressify parse the req.query.fields value. Setting fields to an object uses it as-is and ignores the query parameter. Defaults to false, which disables field selection.