A pure JavaScript library for creating, modifying and validating HAL conform objects based on draft-kelly-json-hal-08
- Node.js >= 6.1.0
npm install hal9k
const hal9k = require('hal9k');
hal9k.resource()
.link('self', '/order/123')
.state({
"currency": "USD",
"status": "shipped",
"total": 10.20
});
const hal9k = require('hal9k');
// As object...
hal9k.fromJSON({
_links: {
self: { href: '/orders' },
curies: [{
name: 'acme',
href: 'http://docs.acme.com/relations/{rel}',
templated: true
}],
'acme:widgets': { href: '/widgets' }
}
});
// or as string.
hal9k.fromJSON(`{
"_links": {
"self": {
"href": "/orders/523"
},
"warehouse": {
"href": "/warehouse/56"
},
"invoice": {
"href": "/invoices/873"
}
},
"currency": "USD",
"status": "shipped",
"total": 10.20
}`);
// As json string...
hal9k.isValidHAL(`{
"_links": {
"self": {
"href": "/orders/523"
},
"warehouse": {
"href": "/warehouse/56"
},
"invoice": {
"href": "/invoices/873"
}
},
"currency": "USD",
"status": "shipped",
"total": 10.20
}`);
// as json object...
hal9k.isValidHAL({
"_links": {
"self": {
"href": "/orders/523"
},
"warehouse": {
"href": "/warehouse/56"
},
"invoice": {
"href": "/invoices/873"
}
},
"currency": "USD",
"status": "shipped",
"total": 10.20
});
// or as resource object
hal9k.isValidHAL(
hal9k.resource({
"currency": "USD",
"status": "shipped",
"total": 10.20
})
.link('self', '/orders/523')
.link('warehouse', '/warehouse/56')
.link('invoice', '/invoices/873')
);
// valid Hal
{
warnings: [],
errors: []
}
// with warnings
{
warnings: [
{
resourceRel: 'root',
title: 'missing self link',
description: 'Each Resource Object SHOULD contain a self link.'
}
],
errors: []
}
// with errors
{
warnings: [],
errors: [
{
resourceRel: 'yourResource',
title: 'missing href in link',
description: 'The "href" property is REQUIRED.'
}
]
}
// creates an empty resource
hal9k.resource();
// resource with state
hal9k.resource({
"currency": "USD",
"status": "shipped",
"total": 10.20
});
optionalParams:
href,
templated,
type,
deprecation,
name,
profile,
title,
hreflang
hal9k.resource()
.link('self', '/example/href/2');
hal9k.resource()
.link('self', '/example/href', {
type: 'json',
deprecation: 'url/to/further/information/concerning/deprecation',
name: 'sample name'
});
hal9k.resource()
.link('self', {
href: '/example/href',
type: 'json',
deprecation: 'url/to/further/information/concerning/deprecation',
name: 'sample name'
});
hal9k.resource()
.link('items', [
'/first_item',
'/second_item'
]);
hal9k.resource()
.link('self', '/example/href/2')
.embed('order', hal9k.resource());
hal9k.resource()
.link('self', '/example/href/2')
.embed('orders', [
hal9k.resource(),
hal9k.resource()
]);
hal9k.resource()
.link('self', '/example/href/2')
.state({
key1: 'value1',
key2: 'value2',
key3: 'value3',
});
hal9k.resource({
"currency": "USD",
"status": "shipped",
"total": 10.20
})
.link('self', '/orders/523')
.link('warehouse', '/warehouse/56')
.link('invoice', '/invoices/873')
.toJSON();
{
"_links": {
"self": { "href": "/orders/523" },
"warehouse": { "href": "/warehouse/56" },
"invoice": { "href": "/invoices/873" }
},
"currency": "USD",
"status": "shipped",
"total": 10.20
}
hal9k.resource()
.link('self', '/orders')
.curie('acme', 'http://docs.acme.com/relations/{rel}')
.link('acme:widgets', '/widgets')
.toJSON();
{
"_links": {
"self": {
"href": "/orders"
},
"curies": [{
"name": "acme",
"href": "http://docs.acme.com/relations/{rel}",
"templated": true
}],
"acme:widgets": {
"href": "/widgets"
}
}
}
See LICENSE.
coliquio ist mit über 170.000 registrierten Ärzten
aller Fachrichtungen das größte Ärzte-Netzwerk im deutschsprachigen Raum.
coliquios Applikationslandschaft setzt auf eine moderne Microservice-basierte
Architektur. Standardisierte API sind daher ein großer und wichtiger Teil unserer
Arbeit. HAL (Hypertext Application Language) hilft uns unsere API von innen
heraus zu entdecken und erleichtert das schnelle Arbeiten mit den Schnittstellen.
Ein wichtiger Faktor um unsere Produktivität zu erhöhen. Diese Library ist nach
dem neuesten HAL Draft erstellt worden.
coliquio is today with over 170,000 physicians of all disciplines
the largest medical community in the German-speaking world.
coliquios application landscape is based on a modern microservice
architecture. Standardized APIs are therefore a large and important part of our
business.
HAL (Hypertext Application Language) helps us to discover our APIs
and facilitate fast working with our interfaces.
It's an important factor to increase our productivity.
This library has been created according to the latest HAL Draft.
This is a project for the community, from developers for developers. This is NOT an official coliquio product. I.e. Maintenance and support are provided by the individual developers but not officially by coliquio.