traverson / halfred

A parser for application/hal+json
MIT License
50 stars 5 forks source link
hal hypermedia hypermedia-client javascript

Halfred

Build Status Dependency Status NPM Greenkeeper badge

A "parser" for the JSON-flavour of HAL, the Hypertext Application Language (that is application/hal+json). If you feed it an object that has _links and _embedded properties, as described in the HAL spec, it will make all links and embedded resource available via convenient methods. If requested, Halfred can also validate a HAL object.

For more information on HAL, see

This module works in Node.js and in the browser. It has no dependencies, the size of the browser build is 8 KB / 4 KB (non-minified/minified)

Installation

Node.js

npm:

npm install halfred --save

Browser

Usage

var halfred = require('halfred');
var resource = halfred.parse(object);

Resource API

halfred.parse(object) returns a Resource object. Here's what you can do with it:

In addition to the methods mentioned here, resource has all properties of the source object. This is also true for embedded Resource objects. The non-HAL properties (that is, any property except _links and _embedded) are copied over to the Resource object. This is always a shallow copy, so modifying the a non-HAL property in the Resource object might also alter the source object and vice versa.

The Resource object also has the properties _links and _embedded but they might differ from the _links/_embedded properties in the source object (Halfred applies some normalization to them). These are not intended to be accessed by clients directly, instead, use the provided methods to work with links and embedded resources.

Extending Resource objects

The Resource class is exported on the halfred object, and therefore can be extended by attaching new methods to the prototype:

    var halfred = require('halfred');
    halfred.Resource.prototype.followLink = function followLink(key, callback) {
      var link = this.link(key);
      if (link) {
        return $.get(link.href, callback);
      }
    }
    var resource = halfred.parse(object);
    resource.followLink('self', function(data) {
      console.log('response data', data);
    });

Links And Embedded Resources

The resource methods allLinkArrays() and linkArray(key) an array for each link, instead of a single object. This might seem counterintuitive. The HAL spec allows a link to be either a single link object or an array of link objects, so halfred normalizes all that are not arrays to be single element arrays. If you are sure that there is only one link for a given key, you can use link(key) to directly get the first element from that array.

The same is true for embedded resources.

Once you have the link object, you can access the properties href, templated and so on (refer to the spec for details) on it. The templated property defaults to false, if it wasn't set in the object given to parse, all other properties described in the spec default to null.

Enable/Disable Validation

In some situations, it might be desirable to validate the resource you want to parse and check, if it is valid according to the HAL spec. By default, Halfred does not do validation checks. If you want to have validation checks you can enable them by calling halfred.enableValidation(). Then after parsing a source object, call validationIssues() on the Resource object returned by parse to get an array of all validation issues.

You can disable validation checks again by calling halfred.disableValidation(). You can also call halfred.enableValidation(true) or halfred.enableValidation(false) to enable/disable validation.

Inject a Logger

You can use halfred.injectLogger(logger) to inject any object that exposes the same API as console does, that is, it needs to provide the methods log, warn, etc. Actually, currently only logger.warn is used to print a deprecation warning as mandated by the Hal spec, section 5.4. If no logger is injected, the global console object will be used. If this object does not exist, a no-op logger will be used and deprecation warnings will not be logged.

Contributing

See Contributing to Halfred.

Code of Conduct

See Code of Conduct.

Release Notes

See CHANGELOG.

License

MIT