DruxtRouter (druxt-router)
DruxtRouter
DruxtRouter class.
Provides core Drupal JSON:API query functionality.
Kind: global class
See: /modules/router
- DruxtRouter
- new DruxtRouter(baseUrl, [options])
- .druxt :
DruxtClient - .get(path) ⇒
object - .getRedirect(path, route) ⇒
boolean|string - .getResourceByRoute(route) ⇒
object - .getRoute(path) ⇒
object .addHeaders(headers).buildQueryUrl(url, [query]) ⇒string.getIndex(resource) ⇒object.getResource(query) ⇒object.getResources(resource, query, [options]) ⇒Array.<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.<object> | Druxt Router route type definitions. These are constructor-only: the Nuxt module's plugin does not forward a types option from nuxt.config.js. | |
| [options.types[].type] | string | The type id, e.g. 'entity' or 'views'. | |
| [options.types[].canonical] | function | string | The canonical path, or a function of the route data that returns it. | |
| [options.types[].component] | string | Name of the component used to render the route. | |
| [options.types[].property] | string | The route response property to extract; the type matches only when this property is present in the response. | |
| [options.types[].props] | function | A function of the route data that returns the props object for the component. |
Example (Inside a Nuxt component; the plugin injects a factory function, note the call.)
const router = this.$druxtRouter()
const { route } = await router.get('/node/1')
Example (Manual construction.)
import { DruxtRouter } from 'druxt-router'
const router = new DruxtRouter('https://example.com', {})
Example (Adding a custom route type.)
const router = new DruxtRouter('https://example.com', {
types: [{
type: 'entity',
canonical: (route) => route.entity.canonical,
component: 'my-entity-component',
property: 'entity',
props: (route) => ({ uuid: route.entity.uuid }),
}],
})
.druxt : DruxtClient
Instance of the Druxt Client.
Kind: instance property of DruxtRouter
See: DruxtClient
.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')
.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.
| 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)
.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('/')
.addHeaders(headers)
Deprecated in druxt-router:0.18.0, removed from druxt-router:2.0.0. Set headers via the DruxtClient axios options, or client.axios interceptors instead.
Add headers to the Axios instance.
Kind: instance method of DruxtRouter
See
| Param | Type | Description |
|---|---|---|
| headers | object | An object containing HTTP headers. |
Example
router.druxt.addHeaders({ 'Authorization': `Basic ${token}` })
.buildQueryUrl(url, [query]) ⇒ string
stringDeprecated in druxt-router:0.18.0, removed from druxt-router:2.0.0. Use client.buildQueryUrl(url, query) instead.
Build query URL.
Kind: instance method of DruxtRouter
Returns: string - The URL with query string.
See
| 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)
.getIndex(resource) ⇒ object
objectDeprecated in druxt-router:0.18.0, removed from druxt-router:2.0.0. Use client.getIndex(resource) instead.
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
| Param | Type | Description |
|---|---|---|
| resource | string | (Optional) A specific resource to query. |
Example
const { href } = await router.druxt.getIndex('node--article')
.getResource(query) ⇒ object
objectDeprecated in druxt-router:0.18.0, removed from druxt-router:2.0.0. Use client.getResource(type, id) instead.
Get a JSON:API resource by type and ID.
Kind: instance method of DruxtRouter
Returns: object - The JSON:API resource data.
See
| Param | Type | Description |
|---|---|---|
| query | object | The resource query. |
| query.type | string | The JSON:API resource type. |
| query.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 in druxt-router:0.18.0, removed from druxt-router:2.0.0. Use client.getCollectionAll(resource, query) instead.
Gets a collection of resources.
Kind: instance method of DruxtRouter
Returns: Array.<object> - Array of resources.
See
| 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)