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


.index : object

JSON:API Index.

Kind: instance property of DruxtClient


.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, [prefix]) ⇒ object

Create a JSON:API resource.

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

ParamTypeDescription
resourceobjectThe JSON:API resource object
[prefix]string(Optional) The JSON:API endpoint prefix or langcode.

Example

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

.error(err)

Throw a formatted error.

Kind: instance method of DruxtClient
Throws:

  • Error A formatted error.
ParamTypeDescription
errobjectThe error object

.get(url, options) ⇒ object

Execute an Axios GET request, with permission checking and error handling.

Kind: instance method of DruxtClient
Returns: object - The Axios response.

ParamTypeDescription
urlstringThe URL to GET.
optionsobjectAn Axios options object.

.getCollection(type, [query], [prefix]) ⇒ 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.
[prefix]string(Optional) The JSON:API endpoint prefix or langcode.

Example

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

.getCollectionAll(type, [query], [prefix]) ⇒ 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.
[prefix]string(Optional) The JSON:API endpoint prefix or langcode.

Example

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

.getIndex([resource], [prefix]) ⇒ 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
[resource]string(Optional) A specific resource to query.
[prefix]string(Optional) The JSON:API endpoint prefix or langcode.

Example

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

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.
[prefix]string(Optional) The JSON:API endpoint prefix or langcode.

Example

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

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

Get a JSON:API resource by type and ID.

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

  • update method to take a context object instead of 4 parameters.
ParamTypeDescription
typestringThe JSON:API Resource type.
idstringThe Drupal resource UUID.
[query]DruxtClientQueryA correctly formatted JSON:API query string or object.
[prefix]string(Optional) The JSON:API endpoint prefix or langcode.

Example

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

.updateResource(resource, [prefix]) ⇒ object

Update a JSON:API resource.

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

ParamTypeDescription
resourceobjectThe JSON:API resource object
[prefix]string(Optional) The JSON:API endpoint prefix or langcode.

Example

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

DruxtClientOptions : object

DruxtClient options object.

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

ParamTypeDefaultDescription
[axios]objectAxios instance settings.
[debug]booleanfalseEnable Debug mode for verbose console log messages.
[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 instance settings.
  axios: {
    // Set axios headers.
    headers: { 'X-Custom-Header': true },
  },
  // Enable debug console log messages.
  debug: true,
  // JSON:API endpoint.
  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)