siren-builder
v1.1.0
Published
A fluent Siren hypermedia representation builder.
Downloads
1,288
Readme
node-siren-builder
A fluent Siren hypermedia representation builder.
const Siren = require('siren-builder');
const entity = Siren.entity()
.addClass('home')
.addProperty('version', '2.4.1')
.addProperty('health', 'green')
.addAction('find-widget', Siren.action()
.setTitle('Find Widget')
.setMethod('GET')
.setHref('https://api.example.org/widgets/search')
.setType('application/x-www-form-urlencoded')
.addField('q', Siren.field()
.setType('text')))
.addLink('self', Siren.link()
.setHref('https://api.example.org/'));
const siren = entity.toJSON();
// siren = {
// class: ['home'],
// properties: {
// version: '2.4.1',
// health: 'green'
// },
// actions: [
// {
// name: 'find-widget',
// method: 'GET',
// href: 'https://api.example.org/widgets/search',
// title: 'Find Widget',
// type: 'application/x-www-form-urlencoded',
// fields: [
// {
// name: 'q',
// type: 'text'
// }
// ]
// }
// ],
// links: [
// {
// rel: ['self'],
// href: 'https://api.example.org/'
// }
// ]
// }
Installation
$ npm install siren-builder
API
Alternatives
If you are not building Siren representations imperatively and are able to specify them in one expression, you may prefer to use node-siren-writer for syntactic brevity.