A Siren Hypermedia Object Generator
A generator for siren hypermedia API responses.
var writer = require"siren-writer";var siren = writer'';var entity = sirenclass: 'order'properties:orderNumber: 42itemCount: 3status: 'pending'entities:class: 'items' 'collection'rel: ''href: 'orders/42/items'class: 'info' 'customer'rel: ''properties:customerId: 'pj123'name: 'Peter Joseph'links:rel: 'self'href: 'customers/pj123'actions:name: 'add-item'title: 'Add Item'method: 'POST'href: 'orders/42/items'type: 'application/x-www-form-urlencoded'fields:name: 'orderNumber' type: 'hidden' value: '42'name: 'productCode' type: 'text'name: 'quantity' type: 'number'links:rel: 'self'href: 'orders/42'rel: 'previous'href: 'orders/41'rel: 'next'href: 'orders/43';console.logentity;
Creates a new
siren writer with the given
base as the base URL for things
href throughout an entity.
The returned function is the entire API. It returns an object that can be serialized as a JSON response.
// expressresjsonsiren/* options */;// koathisbody = siren/* options */;
Generally-speaking, this API avoids performing magic. It wants you to be explicit, only accepting objects instead of positional arguments. However, there are a few ways that this API improves upon generating the response entirely from scratch:
entitieswhere you could be merging several different types for a single response
relvalues that aren't defined by IANA)
relfor a link
The keys in
options match the spec,
so use that as a reference when building your configuration options. Some
things to be aware of:
base, especially when using a pathname
/in your relative URLs, as it will behave unexpectedly if your
basehas a pathname as part of it. (ie:
throwexceptions in the cases mentioned previously