jcassee / hally

JavaScript module for getting and putting HAL resources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hally

Build Status Coverage Status npm npm downloads License

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'}, embeds: {car: {}, friends: {car: {}}}};
fetch('https://example.com/user1', opts).then(halJson(opts)).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

About

JavaScript module for getting and putting HAL resources

License:MIT License


Languages

Language:JavaScript 100.0%