All Projects → traverson → halfred

traverson / halfred

Licence: MIT License
A parser for application/hal+json

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to halfred

jesi
Hypermedia API Accelerator
Stars: ✭ 19 (-56.82%)
Mutual labels:  hypermedia, hal
Core
The server component of API Platform: hypermedia and GraphQL APIs in minutes
Stars: ✭ 2,004 (+4454.55%)
Mutual labels:  hypermedia, hal
akka-http-hal
HAL (Hypermedia Application Language) specification support for akka-http
Stars: ✭ 18 (-59.09%)
Mutual labels:  hypermedia, hal
HX711
HX711 driver for STM32 HAL
Stars: ✭ 34 (-22.73%)
Mutual labels:  hal
specification
RDF vocabulary and specification
Stars: ✭ 21 (-52.27%)
Mutual labels:  hypermedia
console
HAL management console
Stars: ✭ 41 (-6.82%)
Mutual labels:  hal
STM32 HAL FREEMODBUS RTU
FreeMODBUS RTU port for STM32 HAL library
Stars: ✭ 111 (+152.27%)
Mutual labels:  hal
bxcan
bxCAN peripheral driver for STM32 chips
Stars: ✭ 22 (-50%)
Mutual labels:  hal
ng-hal
A navigator for HAL documents in Angular
Stars: ✭ 24 (-45.45%)
Mutual labels:  hal
laravel5-hal-json
Laravel 5 HAL+JSON API Transformer Package
Stars: ✭ 15 (-65.91%)
Mutual labels:  hal
advene
Official Advene repository
Stars: ✭ 32 (-27.27%)
Mutual labels:  hypermedia
lhs
⚛️ REST services accelerator: Rails gem providing an easy, active-record-like interface for http (hypermedia) json services
Stars: ✭ 130 (+195.45%)
Mutual labels:  hypermedia-client
json-api-serializer
Node.js/browser framework agnostic JSON API (http://jsonapi.org/) serializer.
Stars: ✭ 141 (+220.45%)
Mutual labels:  hypermedia
Processor
Ontology-driven Linked Data processor and server for SPARQL backends. Apache License.
Stars: ✭ 54 (+22.73%)
Mutual labels:  hypermedia
HAL-Webinar
Webinar – Creating a Hardware Abstraction Layer in LabVIEW
Stars: ✭ 22 (-50%)
Mutual labels:  hal
php-hal
HAL+JSON & HAL+XML API transformer outputting valid (PSR-7) API Responses.
Stars: ✭ 30 (-31.82%)
Mutual labels:  hal
zend-expressive-hal
Hypertext Application Language implementation for PHP and PSR-7
Stars: ✭ 37 (-15.91%)
Mutual labels:  hal
gsm
gsm module library for STM32 LL
Stars: ✭ 28 (-36.36%)
Mutual labels:  hal
react-ketting
Ketting bindings for React
Stars: ✭ 13 (-70.45%)
Mutual labels:  hypermedia
MPU6050
STM32 HAL library for GY-521 (MPU6050) with Kalman filter
Stars: ✭ 114 (+159.09%)
Mutual labels:  hal

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

  • If you are using npm and Browserify: Just npm install halfred --save and require('halfred'), then browserify your module as usual.
  • If you are using Bower: bower install halfred --save
  • Otherwise you can grab a download from the latest release:
    • halfred.min.js: Minified build with UMD. This build can be used with an AMD loader like RequireJS or with a script tag (in which case it will register halfred in the global scope). If in doubt, use this build.
    • halfred.js: Non-minified build with UMD. Same as above, just larger :-)
    • halfred.external.min.js: Minified require/external build. Created with browserify's --require parameter and intended to be used (required) from other browserified modules, which were created with --external halfred. This build could be used if you use browserify but do not want to bundle Halfred with your own browserify build but keep it as a separate file.
    • halfred.external.js: Non-minified require/external build. Same as before, just bigger.

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:

  • allLinkArrays(): Returns an object which has an array for each link that was present in the source object. See below why each link is represented as an array.
  • allLinks(): Alias for allLinkArrays
  • linkArray(key): Returns the array of links for the given key, or null if there are no links for this key.
  • link(key): Returns the first element of the array of links for the given key or null if there are no links for this key.
  • allEmbeddedResourceArrays(): Returns an object which has an array for each embedded resource that was present in the source object. See below why each embedded resource is represented as an array. Each element of any of this arrays is in turn a Resource object.
  • allEmbeddedArrays(): Alias for allEmbeddedResourceArrays()
  • allEmbeddedResources(): Alias for allEmbeddedResourceArrays()
  • embeddedResourceArray(key): Returns the array of embedded resources for the given key, or null if there are no embedded resources for this key. Each element of this arrays is in turn a Resource object.
  • embeddedArray(key): Alias for embeddedResourceArray().
  • embeddedResource(key): Returns the first element of the array of embedded resources for the given key or null if there are no embedded resources for this key. The returend object is a Resource object.
  • embedded(key): Alias for embeddedResource(key)
  • original(): Returns the unmodified, original object that was parsed to this resource. This is rather uninteresting for the source object you give to the parse method (because you probably still have a reference to the source object) but it is a convenient way to get the part of the source object that corresponds to an embedded resource.
  • hasCuries(): Returns true if the resource has any CURIEs (Compact URIs).
  • curieArray(): Returns the array of CURIEs. Each object in the array is a link object, which means it can be templated etc. See below for the link object API.
  • curie(name): Returns the curie with the given name, if any. The returned object is a link object, which means it can be templated etc. See below for link object API.
  • reverseResolveCurie(fullUrl): Returns the compact URI for the given full URL, if any.
  • validationIssues(): Returns all validation issues. Validation issues are only gathered if validation has been turned on by calling halfred.enableValidation() before calling halfred.parse.
  • validation(): Alias for validationIssues()

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].