# 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
- DruxtClient
- new DruxtClient(baseUrl, [options])
- .axios :
object
- .addHeaders(headers)
- .buildQueryUrl(url, [query]) ⇒
string
- .getCollection(type, [query]) ⇒
object
- .getCollectionAll(type, [query]) ⇒
Array.<object>
- .getIndex(resource) ⇒
object
- .getResource(type, id, [query]) ⇒
object
# 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', {})
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}` })
# .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)
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')
# .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')
# .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')
# .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)
# 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',
}
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
Example
new DrupalJsonApiParams().addPageLimit(5)