hally
v0.2.0
Published
Module for getting and putting HAL resources
Downloads
1
Readme
Hally
JavaScript module for performing HTTP GET en PUT requests for JSON HAL resources.
Its main use is to embed linked resources, even when the server returns only the links.
Example
var hally = require('hally');
var halJson = hally.halJson;
var stateBody = hally.stateBody
var opts = {headers: {'Accept': 'application/hal+json'}};
var embeds = {car: {}, friends: {car: {}}};
fetch('https://example.com/user1', opts).then(halJson(opts, embeds)).then(function (user) {
console.log("User name: " + user.name);
var car = user._embedded.car;
console.log("Car brand: " + car.brand);
user._embedded.friends.forEach(function (friend) {
console.log(friend.name + "'s car brand: " + friend._embedded.car.brand);
});
car.brand = 'Ford';
var putOpts = {method: 'PUT', headers: {'Content-Type': 'applications/json'}, body: stateBody(car)};
return fetch(car._links.self.href, putOpts).then(function (response) {
// Do something with PUT response
});
});
Installation
Install using NPM:
npm install hally --save
Hally uses the WHATWG Fetch API to make HTTP requests. It is available on modern browsers. For older browsers a polyfill is available. Alternatively, and on Node.js, use the isomorphic-fetch polyfill:
npm install isomorphic-fetch --save
You also need a Promise implementation. Promises are available on most modern platforms, but older environments may require a polyfill:
npm install promise-polyfill --save