https://github.com/jcassee/hally
JavaScript module for getting and putting HAL resources
https://github.com/jcassee/hally
hal-json hateoas hypermedia javascript rest
Last synced: 7 months ago
JSON representation
JavaScript module for getting and putting HAL resources
- Host: GitHub
- URL: https://github.com/jcassee/hally
- Owner: jcassee
- License: mit
- Created: 2017-05-29T21:00:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-11T21:35:42.000Z (over 8 years ago)
- Last Synced: 2025-03-01T00:18:25.210Z (7 months ago)
- Topics: hal-json, hateoas, hypermedia, javascript, rest
- Language: JavaScript
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Hally
[](https://travis-ci.org/jcassee/hally)
[](https://coveralls.io/github/jcassee/hally?branch=master)
[](https://www.npmjs.com/package/hally)
[](https://www.npmjs.com/package/hally)
[](https://github.com/jcassee/hally/blob/master/LICENSE.md)JavaScript module for performing HTTP GET en PUT requests for
[JSON HAL](http://tools.ietf.org/html/draft-kelly-json-hal) resources.Its main use is to embed linked resources, even when the server returns only the links.
## Example
```javascript
var hally = require('hally');
var halJson = hally.halJson;
var stateBody = hally.stateBodyvar 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](https://fetch.spec.whatwg.org) to make HTTP
requests. It is available on [modern browsers](http://caniuse.com/#feat=fetch).
For older browsers a [polyfill](https://github.com/github/fetch) is available.
Alternatively, and on Node.js, use the [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch)
polyfill:npm install isomorphic-fetch --save
You also need a [Promise](https://promisesaplus.com) implementation. Promises are
[available on most modern platforms](https://kangax.github.io/compat-table/es6/#test-Promise),
but older environments may require a [polyfill](https://github.com/taylorhakes/promise-polyfill):npm install promise-polyfill --save