CollectionJsonExtension

CollectionJsonExtension

Extension for processing Collection+JSON. By default, the extension will only process links and embedded resources in responses if the HTTP response Content-Type header equals application/vnd.collection+json. If you have a custom media type that extends C+J, you can register it by passing it in the mediaTypes parameter.

C+J queries are exposed as forms, and can be accessed using Resource#$form or Resource#$forms. For adding items, a form is accessible using the create-form IANA standard link relation.

Collection items can be extracted using the item standard link relation using Resource#$sub or Resource#$subs.

A given embedded item can be edited by using the form with the edit-form standard link relation.

Constructor

new CollectionJsonExtension(mediaTypesopt)

Create the Collection+JSON extension

Parameters:
Name Type Attributes Description
mediaTypes Array <optional>

Media types in addition to application/vnd.collection+json that should be handled by this extensions. This allows for custom media types based on Collection+JSON to be handled properly.

Implements:
Source:
Example

Example editing an existing item

new Root('http://localhost/posts', axios, [new CollectionJsonExtension()]).follow().then(function(coll) {
  var firstItem = coll.$subs('item')[0];
  var editForm = firstItem.$form('edit-form');
  editForm.field('title').value = 'Edited Title';
  var newFirstItem = editForm.submit().$followOne('item');
});

Methods

applies(data, headers, context) → {Boolean}

Determine whether this extension should be used to process a particular response

Parameters:
Name Type Description
data Object

The body of the response, pre-parsed if some form of JSON.

headers Object.<string, string>

The HTTP headers of the response

context Context

The context of the current resource/operation.

Implements:
Source:
Returns:

True if the extension should be used to process the response.

Type
Boolean

dataParser(data, headers, context) → {Array.<{name: String, value: Object}>}

Parse any field data that is part of the resource.

Parameters:
Name Type Description
data Object

The body of the response, pre-parsed if some form of JSON.

headers Object.<string, string>

The HTTP headers of the response

context Context

The context of the current resource/operation.

Implements:
Source:
Returns:

The fields, as an array of name/value pairs.

Type
Array.<{name: String, value: Object}>

embeddedParser(data, headers, context, the) → {Object.<String, Array.<Resource>>}

Parse any embedded resources found in this resource.

Parameters:
Name Type Description
data Object

The body of the response, pre-parsed if some form of JSON.

headers Object.<string, string>

The HTTP headers of the response

context Context

The context of the current resource/operation.

the Resource

parent resource that contains the parsed resources.

Implements:
Source:
Returns:

The embedded resources, aggregated by the link relation.

Type
Object.<String, Array.<Resource>>

formParser(data, headers, context) → {Object.<String, Array.<Form>>}

Parse any hypermedia forms found in this resource.

Parameters:
Name Type Description
data Object

The body of the response, pre-parsed if some form of JSON.

headers Object.<string, string>

The HTTP headers of the response

context Context

The context of the current resource/operation.

Implements:
Source:
Returns:

The hypermedia forms, aggregated by the link relation.

Type
Object.<String, Array.<Form>>

linkParser(data, headers, context) → {Object.<String, LinkCollection>}

Parse any links found in the resources.

Parameters:
Name Type Description
data Object

The body of the response, pre-parsed if some form of JSON.

headers Object.<string, string>

The HTTP headers of the response

context Context

The context of the current resource/operation.

Implements:
Source:
Returns:

The links, aggregated by the link relation.

Type
Object.<String, LinkCollection>