![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
The HAL+JSON Resource Object.
npm install halson --save
var halson = require('halson');
var embed = halson({
title: "joyent / node",
description: "evented I/O for v8 javascript"
})
.addLink('self', '/joyent/node')
.addLink('author', {
href: '/joyent',
title: 'Joyent'
});
var resource = halson({
title: "Juraj Hájovský",
username: "hajovsky",
emails: [
"juraj.hajovsky@example.com",
"hajovsky@example.com"
]
})
.addLink('self', '/hajovsky')
.addEmbed('starred', embed);
console.log(resource.title);
console.log(resource.emails[0]);
console.log(resource.getLink('self'));
console.log(resource.getEmbed('starred'));
console.log(JSON.stringify(resource));
halson([data])
Create a new HAL+JSON Resource Object.
data
(optional): Initial data as serialized string or Object.// empty HAL+JSON Resource Object
var resource = halson();
// resource from a serialized data
var resource = halson('{title:"Lorem Ipsum",_links:{self:{href:"/ipsum"}}');
// resource from an Object
resource = halson({
_links: {
self: {
href: {"/ipsum"}
}
},
title: "Lorem Ipsum"
});
// resource from another resource (no-op)
var resourceX = halson(resorce);
console.log(resorce === resorceX); // true
HALSONResource#listLinkRels()
List all link relations.
var data = {
_links: {
self: {href: '/hajovsky'},
related: [
{href: 'http://hajovsky.sk'},
{href: 'https://twitter.com/hajovsky'}
]
}
}
var resource = halson(data);
console.log(resource.listLinkRels()); // ['self', 'related']
HALSONResource#listEmbedRels()
List all link relations.
var data = {
_embedded: {
starred: {
_links: {
self: {href: '/joyent/node'}
}
title: "joyent / node",
description: "evented I/O for v8 javascript"
}
}
}
var resource = halson(data);
console.log(resource.listEmbedRels()); // ['starred']
HALSONResource#getLinks(rel, [filterCallback, [begin, [end]]])
Get all links with relation rel
.
rel
(required): Relation name.filterCallback
(optional): Function used to filter array of links. docbegin
, end
(optional): slice filtered links. docvar twitterLinks = resource.getLinks('related', function(item) {
return item.name === "twitter";
});
HALSONResource#getLink(rel, [filterCallback])
Get first link with relation rel
.
rel
(required): Relation name.filterCallback
(optional): Function used to filter array of links. docvar firstRelatedLink = resource.getLink('related');
HALSONResource#getEmbeds(rel, [filterCallback, [begin, [end]]])
Get all embedded resources with relation rel
.
rel
(required): Relation name.filterCallback
(optional): Function used to filter array of embeds. docbegin
, end
(optional): slice filtered links. docvar embeds = resource.getEmbeds('starred');
HALSONResource#getEmbed(rel, [filterCallback])
Get first embedded resource with relation rel
.
rel
(required): Relation name.filterCallback
(optional): Function used to filter array of embeds. docvar nodeProject = resource.getEmbed('starred', function(embed) {
var selfLink = embed.getLink('self') || {};
return selfLink.href === '/joyent/node';
});
HALSONResource#addLink(rel, link)
Add a link with relation rel
.
rel
(required): Relation name.link
(required): Link to be added (string or Object).resource
.addLink('related', 'http://hajovsky.sk')
.addLink('related', {
href: 'https://twitter.com/hajovsky',
name: 'twitter'
});
HALSONResource#addEmbed(rel, embed)
Add a nested resource with relation rel
.
rel
(required): Relation name.embed
(required): Resource to be embedded (Object or HALSONResource).var embed = {
_links: {
self: {href: '/joyent/node'}
},
title: "joyent / node"
}
resource.addEmbed('starred', embed);
HALSONResource#removeLinks(rel, [filterCallback])
Remove links with relation rel
. If filterCallback
is not defined, all links with relation rel
will be removed.
rel
(required): Relation name.filterCallback
(optional): Function used to filter array of links. doc// remove links with relation 'related' and name 'twitter'
resource.removeLinks('related', function(link) {
return link.name === "twitter";
});
HALSONResource#removeEmbeds(rel, [filterCallback])
Remove embedded resources with relation rel
. If filterCallback
is not defined, all embeds with relation rel
will be removed.
rel
(required): Relation name.filterCallback
(optional): Function used to filter array of links. doc// remove embedded resources with relation 'starred' and self-link '/koajs/koa'
resource.removeLinks('starred', function(embed) {
var selfLink = embed.getLink('self') || {};
return selfLink.href === '/koajs/koa';
});
FAQs
The HAL+JSON Resource Object
The npm package halson receives a total of 5,214 weekly downloads. As such, halson popularity was classified as popular.
We found that halson demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.