API Documentation

DruxtClient()

druxt

Classes

DruxtClient

The Druxt JSON:API client.

Typedefs

DruxtClientOptions : object

DruxtClient options object.

DruxtClientQuery : string | object

A correctly formatted JSON:API query string or object.

DruxtClient

The Druxt JSON:API client.

Kind: global class


new DruxtClient(baseUrl, [options])

DruxtClient constructor.

  • Validates module options.
  • Sets up Axios instance.
  • Sets up options.
ParamTypeDescription
baseUrlstringThe Drupal base URL.
[options]DruxtClientOptionsThe DruxtClient options object.

Example

const DruxtClient = require('druxt').DruxtCllient
const druxt = new DruxtClient('https://demo-api.druxtjs.org', {})

.axios : object

The Axios instance.

Kind: instance property of DruxtClient
See: https://github.com/axios/axios#instance-methods


.addHeaders(headers)

Add headers to the Axios instance.

Kind: instance method of DruxtClient

ParamTypeDescription
headersobjectAn object containing HTTP headers.

Example

this.$druxt.addHeaders({ 'Authorization': `Basic ${token}` })

.buildQueryUrl(url, [query]) ⇒ string

Build query URL.

Kind: instance method of DruxtClient
Returns: string - The URL with query string.

ParamTypeDescription
urlstringThe base query URL.
[query]DruxtClientQueryA correctly formatted JSON:API query string or object.

Example

const query = new DrupalJsonApiParams()
query.addFilter('status', '1')
const queryUrl = this.$druxt.buildQueryUrl(resourceUrl, query)

.createResource(resource) ⇒ object

Create a JSON:API resource.

Kind: instance method of DruxtClient
Returns: object - The response data

ParamTypeDescription
resourceobjectThe JSON:API resource object

Example

await this.$druxt.createResource({ type: 'node--page', attributes: {}, relationships: {} })

.getCollection(type, [query]) ⇒ object

Get a collection of resources from the JSON:API server.

Kind: instance method of DruxtClient
Returns: object - The JSON:API collection response.

ParamTypeDescription
typestringThe JSON:API Resource type.
[query]DruxtClientQueryA correctly formatted JSON:API query string or object.

Example

const collection = await this.$druxt.getCollection('node--recipe')

.getCollectionAll(type, [query]) ⇒ Array.<object>

Get all resources of a collection.

Kind: instance method of DruxtClient
Returns: Array.<object> - An array of JSON:API collections.

ParamTypeDescription
typestringThe JSON:API Resource type.
[query]DruxtClientQueryA correctly formatted JSON:API query string or object.

Example

const collections = await this.$druxt.getCollectionAll('node--recipe', 'fields[node--recipe]=title')

.getIndex(resource) ⇒ object

Get index of all available resources, or the optionally specified resource.

Kind: instance method of DruxtClient
Returns: object - The resource index object or the specified resource.

ParamTypeDescription
resourcestring(Optional) A specific resource to query.

Example

const { href } = await this.$druxt.getIndex('node--article')

Get the related resources from a specified JSON:API resource.

Kind: instance method of DruxtClient
Returns: object - The related JSON:API resource(s).

ParamTypeDescription
typestringThe JSON:API Resource type.
idstringThe Drupal resource UUID.
relatedstringThe relationship name.
[query]DruxtClientQueryA correctly formatted JSON:API query string or object.

Example

const related = this.$druxt.getRelated('node--page', id, 'uid')

.getResource(type, id, [query]) ⇒ object

Get a JSON:API resource by type and ID.

Kind: instance method of DruxtClient
Returns: object - The JSON:API resource data.

ParamTypeDescription
typestringThe JSON:API Resource type.
idstringThe Drupal resource UUID.
[query]DruxtClientQueryA correctly formatted JSON:API query string or object.

Example

const data = await this.$druxt.getResource('node--article', id)

.updateResource(resource) ⇒ object

Update a JSON:API resource.

Kind: instance method of DruxtClient
Returns: object - The response data

ParamTypeDescription
resourceobjectThe JSON:API resource object

Example

await this.$druxt.updateResource({ type: 'node--page', id, attributes: {}, relationships: {} })

DruxtClientOptions : object

DruxtClient options object.

Kind: global typedef
See: https://github.com/axios/axios#request-config

ParamTypeDefaultDescription
[axios]objectAxios instance settings.
[endpoint]string"jsonapi"The JSON:API endpoint.
[jsonapiResourceConfig]string"jsonapi_resource_config--jsonapi_resource_config"The JSON:API resource config type, used for JSON:API Extras support.

Example

{
  axios: {
    headers: { 'X-Custom-Header': true },
  },
  endpoint: 'api',
}

DruxtClientQuery : string | object

A correctly formatted JSON:API query string or object.

Kind: global typedef
See: https://www.npmjs.com/package/drupal-jsonapi-params
Example

page[limit]=5&page[offset]=5

Example

new DrupalJsonApiParams().addPageLimit(5)