ApplicationAdapter Class
Adapter for a JSON API endpoint, use as a service for your backend
Item Index
Methods
Methods
_urlForRelationship
-
resource -
relationship
Parameters:
-
resourceResourceinstance, has URLs via it's relationships property
-
relationshipStringname
Returns:
url
cacheRemove
-
resource
Noop as a hook to remove a resource from cached data
Parameters:
-
resourceResource
cacheResource
-
resp
Noop as a hook for defining how deserialized resource objects are cached, e.g. in memory
Parameters:
-
respObjectw/ 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:
-
respObjectw/ 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:
-
resourceResourceinstance, has URLs via it's relationships property
-
relationshipStringname
-
idStringof 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:
-
resourceResourcethe 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:
-
resourceResourceinstance, has URLs via it's relationships property
-
relationshipStringname
-
idStringof the related resource
Returns:
deleteResource
-
resource
Delete an existing resource, sends a DELETE request
Parameters:
-
resourceString | Resourcename (plural) or instance w/ self link
Returns:
deserialize
-
json -
headers -
options
Parameters:
-
jsonObjectpayload from response
-
headersObjectreceived from response
-
optionsObjectpassed into original request
fetch
-
url -
options
Fetches data using Fetch API or XMLHttpRequest
Parameters:
-
urlString -
optionsObjectmay include a query object or an update flag
Returns:
fetchAuthorizationHeader
-
options
Sets Authorization header if accessible in the authorizationCredential property
Parameters:
-
optionsObject
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
authorizationCredentialproperty
Parameters:
-
optionsObject
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:
-
urlString
Returns:
find
-
options
Find resource(s) using an id or a using a query {id: '', query: {}}
Parameters:
-
optionsObject | 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:
-
idString -
queryObject
Returns:
findQuery
-
options
Find resources using an optional query object, e.g. w/ pagination params
Parameters:
-
optionsObject
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:
-
relationString | Objectname to lookup the service object w/ serializer
-
relationStringthe name of the relationship
-
-
resource.typeStringthe name of the resource
-
urlString
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:
-
resourceResourceinstance, has URLs via it's relationships property
-
relationshipString
Returns:
updateResource
-
resource -
includeRelationships
Patch an existing resource, sends a PATCH request.
Parameters:
-
resourceResourceinstance to serialize the changed attributes
-
includeRelationshipsArray(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
