# Classes

DruxtClient

Druxt JSON:API client.

# Typedefs

DruxtClientOptions : object

DruxtClient options object.

DruxtClientQuery : string | object

A correctly formatted JSON:API query string or object.

# DruxtClient

Druxt JSON:API client.

Kind: global class


# new DruxtClient(baseUrl, [options])

DruxtClient constructor.

  • Validates module options.
  • Sets up Axios instance.
  • Sets up options.
Param Type Description
baseUrl string The Drupal base URL.
[options] DruxtClientOptions The DruxtClient options object.

Example

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

# .axios : object

The Axios instance.

Kind: instance property of DruxtClient
See: https://github.com/axios/axios#instance-methods (opens new window)


# .addHeaders(headers)

Add headers to the Axios instance.

Kind: instance method of DruxtClient

Param Type Description
headers object An object containing HTTP headers.

Example

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

# .buildQueryUrl(url, [query]) ⇒ string

Build query URL.

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

Param Type Description
url string The base query URL.
[query] DruxtClientQuery A correctly formatted JSON:API query string or object.

Example

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

# .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.

Param Type Description
type string The JSON:API Resource type.
[query] DruxtClientQuery A correctly formatted JSON:API query string or object.

Example

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

# .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.

Param Type Description
type string The JSON:API Resource type.
[query] DruxtClientQuery A correctly formatted JSON:API query string or object.

Example

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

# .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.

Param Type Description
resource string (Optional) A specific resource to query.

Example

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

# .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.

Param Type Description
type string The JSON:API Resource type.
id string The Drupal resource UUID.
[query] DruxtClientQuery A correctly formatted JSON:API query string or object.

Example

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

# DruxtClientOptions : object

DruxtClient options object.

Kind: global typedef
See: https://github.com/axios/axios#request-config (opens new window)

Param Type Default Description
[axios] object Axios 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 (opens new window) support.

Example

{
  axios: {
    headers: { 'X-Custom-Header': true },
  },
  endpoint: 'api',
}
1
2
3
4
5
6

# DruxtClientQuery : string | object

A correctly formatted JSON:API query string or object.

Kind: global typedef
See: https://www.npmjs.com/package/drupal-jsonapi-params (opens new window)
Example

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

Example

new DrupalJsonApiParams().addPageLimit(5)
1