DruxtRouter()
View source on GitHubDruxtRouter
DruxtRouter class.
Provides core Drupal JSON:API query functionality.
Kind: global class
- DruxtRouter
- new DruxtRouter(baseURL, [options])
- .druxt :
DruxtClient .addHeaders(headers).buildQueryUrl(url, [query]) ⇒string- .get(path) ⇒
object .getIndex(resource) ⇒object- .getRedirect(path, route) ⇒
boolean|string .getResource(type, id) ⇒object.getResources(resource, query, [options]) ⇒Array.<object>- .getResourceByRoute(route) ⇒
object - .getRoute(path) ⇒
object
new DruxtRouter(baseURL, [options])
DruxtRouter constructor.
- Validates module options.
- Sets up Axios instance.
- Sets up options.
| Param | Type | Default | Description |
|---|---|---|---|
| baseURL | string | The Drupal base URL. | |
| [options] | object | Druxt Router options. | |
| [options.axios] | object | Axios instance settings. | |
| [options.endpoint] | string | "jsonapi" | The JSON:API endpoint. |
| [options.types] | array | Array of Druxt Router type definitions. |
Example
const router = new DruxtRouter('https://example.com', {})
.druxt : DruxtClient
Instance of the Druxt Client.
Kind: instance property of DruxtRouter
See: http://druxtjs.org/api/client
.addHeaders(headers)
Deprecated
Add headers to the Axios instance.
Kind: instance method of DruxtRouter
See: https://druxtjs.org/api/client
| Param | Type | Description |
|---|---|---|
| headers | object | An object containing HTTP headers. |
Example
router.druxt.addHeaders({ 'Authorization': `Basic ${token}` })
.buildQueryUrl(url, [query]) ⇒ string
stringDeprecated
Build query URL.
Kind: instance method of DruxtRouter
Returns: string - The URL with query string.
See: https://druxtjs.org/api/client
| Param | Type | Description |
|---|---|---|
| url | string | The base query URL. |
| [query] | string | object | A correctly formatted JSON:API query string or object. |
Example
const query = new DrupalJsonApiParams()
query.addFilter('status', '1')
const queryUrl = router.druxt.buildQueryUrl(resourceUrl, query)
.get(path) ⇒ object
Returns route and redirect data for a given path.
Kind: instance method of DruxtRouter
Returns: object - The route and redirect data.
| Param | Type | Description |
|---|---|---|
| path | string | The route path. |
Example
const { redirect, route } = await router.get('/node/1')
.getIndex(resource) ⇒ object
objectDeprecated
Get index of all available resources, or the optionally specified resource.
Kind: instance method of DruxtRouter
Returns: object - The resource index object or the specified resource.
See: https://druxtjs.org/api/client
| Param | Type | Description |
|---|---|---|
| resource | string | (Optional) A specific resource to query. |
Example
const { href } = await router.druxt.getIndex('node--article')
.getRedirect(path, route) ⇒ boolean | string
Get redirect data for a given route.
Kind: instance method of DruxtRouter
Returns: boolean | string - The redirect path or false.
Todo
- Move this to a DruxtRouterRedirect class.
- Remove the path parameter.
| Param | Type | Description |
|---|---|---|
| path | string | The route path. |
| route | object | Druxt route object. |
Example
const route = await router.getRoute(path)
const redirect = router.getRedirect(path, route)
.getResource(type, id) ⇒ object
objectDeprecated
Get a JSON:API resource by type and ID.
Kind: instance method of DruxtRouter
Returns: object - The JSON:API resource data.
See: https://druxtjs.org/api/client
| Param | Type | Description |
|---|---|---|
| type | string | The JSON:API resource type. |
| id | string | The Drupal resource UUID. |
Example
const data = await router.druxt.getResource('node--article', id)
.getResources(resource, query, [options]) ⇒ Array.<object>
Array.<object>Deprecated
Gets a collection of resources.
Kind: instance method of DruxtRouter
Returns: Array.<object> - Array of resources.
See: https://druxtjs.org/api/client
Todo
- Add granular pagination.
| Param | Type | Default | Description |
|---|---|---|---|
| resource | string | The JSON:API resource type. | |
| query | string | object | A JSON:API query string or object. | |
| [options] | object | Resource-loading options. | |
| [options.all] | boolean | false | Load all results. |
Example
// Load all currently published Articles.
const query = new DrupalJsonApiParams()
query.addFilter('status', '1')
const resources = await router.druxt.getCollection('node--article', query)
.getResourceByRoute(route) ⇒ object
Get a JSON:API resource by Drupal route.
Kind: instance method of DruxtRouter
Returns: object - The JSON:API resource data.
| Param | Type | Description |
|---|---|---|
| route | object | Druxt Router route object. |
Example
const route = await router.getRoute('/')
const data = await router.getResourceByRoute(route)
.getRoute(path) ⇒ object
Get routing data from Decoupled Router.
Kind: instance method of DruxtRouter
Returns: object - The route object.
| Param | Type | Default | Description |
|---|---|---|---|
| path | string | "/" | The route path. |
Example
const route = await router.getRoute('/')