ApplicationAdapter Class
Adapter for a JSON API endpoint, use as a service for your backend
Item Index
Methods
Methods
_urlForRelationship
-
resource
-
relationship
Parameters:
-
resource
Resourceinstance, has URLs via it's relationships property
-
relationship
Stringname
Returns:
url
cacheRemove
-
resource
Noop as a hook to remove a resource from cached data
Parameters:
-
resource
Resource
cacheResource
-
resp
Noop as a hook for defining how deserialized resource objects are cached, e.g. in memory
Parameters:
-
resp
Objectw/ 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
Objectw/ props: {Object} meta, {Array|Object} data, & {Object} headers
createRelationship
-
resource
-
relationship
-
id
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
Resourceinstance, has URLs via it's relationships property
-
relationship
Stringname
-
id
Stringof the related resource
Returns:
createResource
-
resource
Create a new resource, sends a POST request, updates resource instance with persisted data, and updates cache with persisted resource
Parameters:
-
resource
Resourcethe instance to serialize
Returns:
deleteRelationship
-
resource
-
relationship
-
id
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
Resourceinstance, has URLs via it's relationships property
-
relationship
Stringname
-
id
Stringof the related resource
Returns:
deleteResource
-
resource
Delete an existing resource, sends a DELETE request
Parameters:
-
resource
String | Resourcename (plural) or instance w/ self link
Returns:
deserialize
-
json
-
headers
-
options
Parameters:
-
json
Objectpayload from response
-
headers
Objectreceived from response
-
options
Objectpassed into original request
fetch
-
url
-
options
Fetches data using Fetch API or XMLHttpRequest
Parameters:
-
url
String -
options
Objectmay include a query object or an update flag
Returns:
fetchAuthorizationHeader
-
options
Sets Authorization header if accessible in the authorizationCredential
property
Parameters:
-
options
Object
fetchOptions
-
options
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:
fetchUrl
-
url
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:
find
-
options
Find resource(s) using an id or a using a query {id: '', query: {}}
Parameters:
-
options
Object | String | Numberuse a string for a single id or an object.
Returns:
findOne
-
id
-
query
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:
findQuery
-
options
Find resources using an optional query object, e.g. w/ pagination params
Parameters:
-
options
Object
Returns:
findRelated
-
relation
-
resource.type
-
url
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 | Objectname to lookup the service object w/ serializer
-
relation
Stringthe name of the relationship
-
-
resource.type
Stringthe name of the resource
-
url
String
Returns:
init
()
patchRelationship
-
resource
-
relationship
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
Resourceinstance, has URLs via it's relationships property
-
relationship
String
Returns:
updateResource
-
resource
-
includeRelationships
Patch an existing resource, sends a PATCH request.
Parameters:
-
resource
Resourceinstance to serialize the changed attributes
-
includeRelationships
Array(optional) list of {String} relationships to opt-into an update
Returns:
resolves with PATCH response or null
if nothing to update
Properties
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