Class NextDrupal

The NextDrupal class extends the NextDrupalBase class and provides methods for interacting with a Drupal backend.

Hierarchy (View Summary)

Constructors

Properties

accessToken?: AccessToken
baseUrl: string
cache?: DataCache
deserializer: JsonDeserializer
fetcher?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>

Type declaration

    • (input: RequestInfo | URL, init?: RequestInit): Promise<Response>
    • Parameters

      • input: RequestInfo | URL
      • Optionalinit: RequestInit

      Returns Promise<Response>

frontPage: string
isDebugEnabled: boolean
logger: Logger
throwJsonApiErrors: boolean
useDefaultEndpoints: boolean
withAuth: boolean

Accessors

  • get apiPrefix(): string
  • Returns string

  • set apiPrefix(apiPrefix: string): void
  • Parameters

    • apiPrefix: string

    Returns void

  • get auth(): NextDrupalAuth
  • Returns NextDrupalAuth

  • set auth(auth: NextDrupalAuth): void
  • Parameters

    Returns void

  • get headers(): HeadersInit
  • Returns HeadersInit

  • set headers(headers: HeadersInit): void
  • Parameters

    • headers: HeadersInit

    Returns void

  • get token(): AccessToken
  • Returns AccessToken

  • set token(token: AccessToken): void
  • Parameters

    Returns void

Methods

  • Adds a locale prefix to the given path.

    Parameters

    • path: string

      The path.

    • options: { defaultLocale?: string; locale?: string } = {}

      The options for adding the locale prefix.

      • OptionaldefaultLocale?: string

        The default locale.

      • Optionallocale?: string

        The locale.

    Returns string

    The path with the locale prefix.

  • Builds an endpoint URL for the specified parameters.

    Parameters

    • params: { locale?: string; path?: string; searchParams?: EndpointSearchParams } & {
          resourceType?: string;
      } = {}

      The parameters for the endpoint.

    Returns Promise<string>

    The built endpoint URL.

  • Builds a URL with the given path and search parameters.

    Parameters

    Returns URL

    The constructed URL.

  • Constructs a path from the given segment and options.

    Parameters

    • segment: string | string[]

      The path segment.

    • options: { defaultLocale?: string; locale?: string; pathPrefix?: string } = {}

      The options for constructing the path.

      • OptionaldefaultLocale?: string

        The default locale.

      • Optionallocale?: string

        The locale.

      • OptionalpathPrefix?: string

        The path prefix.

    Returns string

    The constructed path.

  • Logs a debug message if debug mode is enabled.

    Parameters

    • message: any

      The debug message.

    Returns void

  • Deletes an existing resource of the specified type.

    Parameters

    Returns Promise<boolean>

    True if the resource was deleted, false otherwise.

  • Deserializes the response body.

    Parameters

    • body: any

      The response body.

    • Optionaloptions: any

      Options for deserialization.

    Returns TJsonaModel | TJsonaModel[]

    The deserialized response body.

  • Fetches a resource from the given input URL or path.

    Parameters

    • input: RequestInfo

      The input URL or path.

    • init: FetchOptions = {}

      The fetch options.

    Returns Promise<Response>

    The fetch response.

  • Fetches the endpoint URL for the specified resource type.

    Parameters

    • type: string

      The type of the resource.

    • Optionallocale: string

      The locale for the request.

    Returns Promise<URL>

    The fetched endpoint URL.

  • Gets the authorization header value based on the provided auth configuration.

    Parameters

    Returns Promise<string>

    The authorization header value.

  • Extracts errors from the fetch response.

    Parameters

    • response: Response

      The fetch response.

    Returns Promise<string | JsonApiError[]>

    The extracted errors.

  • Fetches a resource of the specified type by its UUID.

    Type Parameters

    Parameters

    Returns Promise<T>

    The fetched resource.

    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 article = await drupal.getResource("node--article", id, {
    withCache: true,
    cacheKey: `node--article:${id}`,
    })

    Get a page resource with time-based revalidation.

    const node = await drupal.getResource(
    "node--page",
    "07464e9f-9221-4a4f-b7f2-01389408e6c8",
    { next: { revalidate: 3600 } }
    )

    Get a page resource with tag-based revalidation.

    const {slug} = params;
    const path = drupal.translatePath(slug)

    const type = path.jsonapi.resourceName
    const tag = `${path.entity.type}:${path.entity.id}`

    const node = await drupal.getResource(path, path.entity.uuid, {
    params: params.getQueryObject(),
    tags: [tag]
    })

    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"
    )
  • Fetches path segments for a collection of resources of the specified types.

    Parameters

    • types: string | string[]

      The types of the resources.

    • Optionaloptions: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOption & (
          JsonApiWithNextFetchOptions & ({ locales: string[]; defaultLocale: string; } | { locales?: undefined; defaultLocale?: never; })
      )

      Options for the request.

    Returns Promise<{ locale: string; path: string; segments: string[]; type: string }[]>

    The fetched path segments.

  • Logs or throws an error based on the throwJsonApiErrors flag.

    Parameters

    • error: Error

      The error to log or throw.

    Returns void

  • Throws an error if the response contains JSON:API errors.

    Parameters

    • response: Response

      The fetch response.

    • messagePrefix: string = ""

      The error message prefix.

    Returns Promise<void>

    The JSON:API errors.

  • Validates the draft URL using the provided search parameters.

    Parameters

    • searchParams: URLSearchParams

      The search parameters.

    Returns Promise<Response>

    The validation response.