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, or block_content--basic.
  • uuid: string
    • Required
    • The id of the resource. Example: 15486935-24bf-4be7-b858-a5b2de78d09d.
  • options
    • Optional
    • params: JsonApiParams: JSON:API params such as filter, fields, include or sort.
    • withAuth: boolean | DrupalClientAuth:
      • Set the authentication method to use. See the authentication docs.
      • Set to true to 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: Set withCache if 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 es translation 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--article resource using cache.
const id = "07464e9f-9221-4a4f-b7f2-01389408e6c8"
const menu = await drupal.getResource("node--article", id, {
withCache: true,
cacheKey: `node--article:${id}`,
})

TypeScript

  • Using DrupalNode for a node entity type.
import { DrupalNode } from "next-drupal"
const node = await drupal.getResource<DrupalNode>(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
)
  • Using DrupalTaxonomyTerm for 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.