API Documentation
DruxtClient()
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
- DruxtClient
- new DruxtClient(baseUrl, [options])
- .axios :
object
- .index :
object
- .addHeaders(headers)
- .buildQueryUrl(url, [query]) ⇒
string
- .createResource(resource, [prefix]) ⇒
object
- .error(err)
- .get(url, options) ⇒
object
- .getCollection(type, [query], [prefix]) ⇒
object
- .getCollectionAll(type, [query], [prefix]) ⇒
Array.<object>
- .getIndex([resource], [prefix]) ⇒
object
- .getRelated(type, id, related, [query], [prefix]) ⇒
object
- .getResource(type, id, [query], [prefix]) ⇒
object
- .updateResource(resource, [prefix]) ⇒
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', {})
object
.axios : The Axios instance.
Kind: instance property of DruxtClient
See: https://github.com/axios/axios#instance-methods
object
.index : JSON:API Index.
Kind: instance property of DruxtClient
.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}` })
string
.buildQueryUrl(url, [query]) ⇒ 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)
object
.createResource(resource, [prefix]) ⇒ Create a JSON:API resource.
Kind: instance method of DruxtClient
Returns: object
- The response data
Param | Type | Description |
---|---|---|
resource | object | The 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.
Param | Type | Description |
---|---|---|
err | object | The error object |
object
.get(url, options) ⇒ Execute an Axios GET request, with permission checking and error handling.
Kind: instance method of DruxtClient
Returns: object
- The Axios response.
Param | Type | Description |
---|---|---|
url | string | The URL to GET. |
options | object | An Axios options object. |
object
.getCollection(type, [query], [prefix]) ⇒ 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. |
[prefix] | string | (Optional) The JSON:API endpoint prefix or langcode. |
Example
const collection = await this.$druxt.getCollection(
'node--recipe',
undefined,
'en'
)
Array.<object>
.getCollectionAll(type, [query], [prefix]) ⇒ 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. |
[prefix] | string | (Optional) The JSON:API endpoint prefix or langcode. |
Example
const collections = await this.$druxt.getCollectionAll(
'node--recipe',
'fields[node--recipe]=title',
'en'
)
object
.getIndex([resource], [prefix]) ⇒ 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. |
[prefix] | string | (Optional) The JSON:API endpoint prefix or langcode. |
Example
const { href } = await this.$druxt.getIndex('node--article', 'en')
object
.getRelated(type, id, related, [query], [prefix]) ⇒ Get the related resources from a specified JSON:API resource.
Kind: instance method of DruxtClient
Returns: object
- The related JSON:API resource(s).
Param | Type | Description |
---|---|---|
type | string | The JSON:API Resource type. |
id | string | The Drupal resource UUID. |
related | string | The relationship name. |
[query] | DruxtClientQuery | A 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'
)
object
.getResource(type, id, [query], [prefix]) ⇒ 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.
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. |
[prefix] | string | (Optional) The JSON:API endpoint prefix or langcode. |
Example
const data = await this.$druxt.getResource(
'node--article',
id,
undefined,
'en'
)
object
.updateResource(resource, [prefix]) ⇒ Update a JSON:API resource.
Kind: instance method of DruxtClient
Returns: object
- The response data
Param | Type | Description |
---|---|---|
resource | object | The 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'
)
object
DruxtClientOptions : DruxtClient options object.
Kind: global typedef
See: https://github.com/axios/axios#request-config
Param | Type | Default | Description |
---|---|---|---|
[axios] | object | Axios instance settings. | |
[debug] | boolean | false | Enable 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',
}
string
| object
DruxtClientQuery : 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)