hal-object
v4.0.0
Published
object for the Hal mediatype
Downloads
26
Readme
HAL Object
hal-object
is a simple implementation of JSON Hypertext Application Language, written in Typescript, for the purpose of consuming JSON from external APIs.
This library is utilized by its developer but has not been extensively tested in production. Please use with caution.
Examples
Take an example JSON
{
_links: {
self: {
href: "http://example.org/api/foo",
},
root: {
href: "http://example.org/api",
},
collection: [
{ href: "http://example.org/one" },
{ href: "http://example.org/two" },
],
},
id: "foo",
property: "bar",
_embedded: {
related: [
{
_links: {
self: {
href: "http://example.org/api/biz",
},
},
id: "biz",
property: "baz",
},
{
_links: {
self: {
href: "http://example.org/api/qux",
},
},
id: "qux",
name: "lok",
},
],
additional: {
_links: {
self: {
href: "http://example.org/api/foobar",
},
},
id: "foobar",
property: "barfoo",
},
},
}
Instantiate HAL object
let halo = new HALO(json)
Retrieve state
data of JSON
halo.getData();
returns
{ id: "foo", property: "bar" }
Retrieve links
self
halo.getSelfLink();
returns
{ href: "http://example.org/api/foo", }
other links
(halo.getLinks("collection") as LinkObject[])[0];
returns
{ href: "http://example.org/one", }
Retrieve a list of link relations
halo.getLinkRelations();
returns an array of link relations
[
"self",
"root",
"collection",
]
Retrieve embedded
(halo.getEmbedded("additional") as HALO[])[0];
returns the embedded HAL Object
{
_links: { self: { href: "http://example.org/api/foobar" } },
id: "foobar",
property: "barfoo",
}
Retrieve a list of embedded relations
halo.getEmbeddedRelations();
returns an array of embedded relations
[
"related",
"additional",
]
Extensions
cacheable-hal-object
A library that extends hal-object
to add caching information
tealeaf-object
An experimental library that extends cacheable-hal-object
to add a property for BEM related data.