getResource
Fetch a resource by id.
const resource = await drupal.getResource<T = JsonApiResource>( type, uuid, options?: { params, withAuth, deserialize, locale, defaultLocale, withCache, }): Promise<T>type: string- Required
- The resource type. Example:
node--article,taxonomy_term--tags, orblock_content--basic.
uuid: string- Required
- The id of the resource. Example:
15486935-24bf-4be7-b858-a5b2de78d09d.
options- Optional
params: JsonApiParams: JSON:API params such asfilter,fields,includeorsort.withAuth: boolean | NextDrupalAuth:- Set the authentication method to use. See the authentication docs.
- Set to
trueto use the authentication method configured on the client.
deserialize: boolean: Set to false to return the raw JSON:API response.locale: string: The locale to fetch the resource in.defaultLocale: string: The default locale of the site.withCache: boolean: SetwithCacheif you want to store and retrieve the resource from cache.cacheKey: string: The cache key to use.
Examples
- Get a page by uuid.
const node = await drupal.getResource( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8")- Get the
estranslation for a page by uuid.
const node = await drupal.getResource( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8", { locale: "es", defaultLocale: "en", })- Get the raw JSON:API response.
const { data, meta, links } = await drupal.getResource( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8", { deserialize: false, })- Get a
node--articleresource using cache.
const id = "07464e9f-9221-4a4f-b7f2-01389408e6c8"
const article = await drupal.getResource("node--article", id, { withCache: true, cacheKey: `node--article:${id}`,})TypeScript
- Using
DrupalNodefor a node entity type.
import { DrupalNode } from "next-drupal"
const node = await drupal.getResource<DrupalNode>( "node--page", "07464e9f-9221-4a4f-b7f2-01389408e6c8")- Using
DrupalTaxonomyTermfor a taxonomy term entity type.
import { DrupalTaxonomyTerm } from "next-drupal"
const term = await drupal.getResource<DrupalTaxonomyTerm>( "taxonomy_term--tags", "7b47d7cc-9b1b-4867-a909-75dc1d61dfd3")See the TypeScript docs for more built-in types.