Skip to content
router

DruxtRouter()

View source on GitHub

DruxtRouter

DruxtRouter class.

Provides core Drupal JSON:API query functionality.

Kind: global class


new DruxtRouter(baseURL, [options])

DruxtRouter constructor.

  • Validates module options.
  • Sets up Axios instance.
  • Sets up options.
ParamTypeDefaultDescription
baseURLstringThe Drupal base URL.
[options]objectDruxt Router options.
[options.axios]objectAxios instance settings.
[options.endpoint]string"jsonapi"The JSON:API endpoint.
[options.types]arrayArray 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

ParamTypeDescription
headersobjectAn object containing HTTP headers.

Example

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

.buildQueryUrl(url, [query]) ⇒ string

Deprecated

Build query URL.

Kind: instance method of DruxtRouter
Returns: string - The URL with query string.
See: https://druxtjs.org/api/client

ParamTypeDescription
urlstringThe base query URL.
[query]string | objectA 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.

ParamTypeDescription
pathstringThe route path.

Example

const { redirect, route } = await router.get('/node/1')

.getIndex(resource) ⇒ object

Deprecated

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

ParamTypeDescription
resourcestring(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.
ParamTypeDescription
pathstringThe route path.
routeobjectDruxt route object.

Example

const route = await router.getRoute(path)
const redirect = router.getRedirect(path, route)

.getResource(type, id) ⇒ object

Deprecated

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

ParamTypeDescription
typestringThe JSON:API resource type.
idstringThe Drupal resource UUID.

Example

const data = await router.druxt.getResource('node--article', id)

.getResources(resource, query, [options]) ⇒ 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.
ParamTypeDefaultDescription
resourcestringThe JSON:API resource type.
querystring | objectA JSON:API query string or object.
[options]objectResource-loading options.
[options.all]booleanfalseLoad 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.

ParamTypeDescription
routeobjectDruxt 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.

ParamTypeDefaultDescription
pathstring"/"The route path.

Example

const route = await router.getRoute('/')