API Docs for: 0.2.1
Show:

ApplicationAdapter Class

Adapter for a JSON API endpoint, use as a service for your backend

Methods

_urlForRelationship

(
  • resource
  • relationship
)
String private

Parameters:

  • resource Resource

    instance, has URLs via it's relationships property

  • relationship String

    name

Returns:

String:

url

cacheRemove

(
  • resource
)

Noop as a hook to remove a resource from cached data

Parameters:

cacheResource

(
  • resp
)

Noop as a hook for defining how deserialized resource objects are cached, e.g. in memory

Parameters:

  • resp Object

    w/ props: {Object} meta, {Array|Object} data, & {Object} headers

cacheUpdate

(
  • resp
)

Noop as a hook for defining how to handle cache after updating a resource

Parameters:

  • resp Object

    w/ props: {Object} meta, {Array|Object} data, & {Object} headers

createRelationship

(
  • resource
  • relationship
  • id
)
Promise

Create (add) a relationship for to-many relation, sends a POST request.

See: http://jsonapi.org/format/#crud-updating-to-many-relationships

Adds a relation using a payload with a resource identifier object:

{
  "data": [
    { "type": "comments", "id": "12" }
  ]
}

Parameters:

  • resource Resource

    instance, has URLs via it's relationships property

  • relationship String

    name

  • id String

    of the related resource

Returns:

Promise:

createResource

(
  • resource
)
Promise

Create a new resource, sends a POST request, updates resource instance with persisted data, and updates cache with persisted resource

Parameters:

  • resource Resource

    the instance to serialize

Returns:

Promise:

deleteRelationship

(
  • resource
  • relationship
  • id
)
Promise

Deletes a relationship for to-many relation, sends a DELETE request.

See: http://jsonapi.org/format/#crud-updating-to-many-relationships

Remove using a payload with the resource identifier object:

For to-many:

{
  "data": [
    { "type": "comments", "id": "1" }
  ]
}

Parameters:

  • resource Resource

    instance, has URLs via it's relationships property

  • relationship String

    name

  • id String

    of the related resource

Returns:

Promise:

deleteResource

(
  • resource
)
Promise

Delete an existing resource, sends a DELETE request

Parameters:

  • resource String | Resource

    name (plural) or instance w/ self link

Returns:

Promise:

deserialize

(
  • json
  • headers
  • options
)

Parameters:

  • json Object

    payload from response

  • headers Object

    received from response

  • options Object

    passed into original request

fetch

(
  • url
  • options
)
Promise

Fetches data using Fetch API or XMLHttpRequest

Parameters:

  • url String
  • options Object

    may include a query object or an update flag

Returns:

Promise:

fetchAuthorizationHeader

(
  • options
)

Sets Authorization header if accessible in the authorizationCredential property

Parameters:

  • options Object

fetchOptions

(
  • options
)
Object

Adds params and headers or Fetch request.

  • The HTTP Header is set for Content-Type: application/vnd.api+json
  • Sets Authorization header if accessible in the authorizationCredential property

Parameters:

  • options Object

Returns:

Object:

fetchUrl

(
  • url
)
String

Hook to customize the URL, e.g. if your API is behind a proxy and you need to swap a portion of the domain to make a request on the same domain.

Parameters:

  • url String

Returns:

String:

find

(
  • options
)
Promise

Find resource(s) using an id or a using a query {id: '', query: {}}

Parameters:

  • options Object | String | Number

    use a string for a single id or an object.

Returns:

Promise:

findOne

(
  • id
  • query
)
Promise

Find a resource by id, optionally pass a query object, e.g. w/ filter param(s)

Uses a url like: /photos/1

Parameters:

  • id String
  • query Object

Returns:

Promise:

findQuery

(
  • options
)
Promise

Find resources using an optional query object, e.g. w/ pagination params

Parameters:

  • options Object

Returns:

Promise:

findRelated

(
  • relation
  • resource.type
  • url
)
Promise

Find resources by relationship or use a specificed (optional) service to find relation

A Url like: /photos/1/relationships/photographer is a required param

service.findRelated('photographer', '/api/v1/photos/1/relationships/photographer');

Or, with option to find related resource using a different service

service.findRelated({relation: 'photographer', type: 'people'}, url);

Parameters:

  • relation String | Object

    name to lookup the service object w/ serializer

    • relation String

      the name of the relationship

  • resource.type String

    the name of the resource

  • url String

Returns:

Promise:

patchRelationship

(
  • resource
  • relationship
)
Promise

Patch a relationship, either adds or removes everyting, sends a PATCH request.

See: http://jsonapi.org/format/#crud-updating-to-one-relationships

For to-one relation:

  • Remove (delete) with payload: { "data": null }
  • Create/Update with payload:
    {
      "data": { "type": "comments", "id": "1" }
    }
    

For to-many relation:

  • Remove (delete) all with payload: { "data": [] }
  • Replace all with payload:
    {
      "data": [
        { "type": "comments", "id": "1" },
        { "type": "comments", "id": "2" }
      ]
    }
    

Parameters:

  • resource Resource

    instance, has URLs via it's relationships property

  • relationship String

Returns:

Promise:

updateResource

(
  • resource
  • includeRelationships
)
Promise

Patch an existing resource, sends a PATCH request.

Parameters:

  • resource Resource

    instance to serialize the changed attributes

  • includeRelationships Array

    (optional) list of {String} relationships to opt-into an update

Returns:

Promise:

resolves with PATCH response or null if nothing to update

Properties

authorizationCredential

String

Authentication credentials/token used with HTTP authentication This property should be added by an Authorization Mixin

authorizationHeaderField

String

The name of the Authorization request-header field This property should be added by an Authorization Mixin

type

String

The name of the entity

url

String

The url for the entity, e.g. /posts or /api/v1/posts

defaults to config.APP.API_HOST/config.APP.API_PATH/pluralize(type) if not set explicitly.

useAjax

Boolean

Flag to use $.ajax instead of window.fetch