Class NextDrupalPages

The NextDrupalPages class extends the NextDrupal class and provides methods for interacting with a Drupal backend in the context of Next.js pages.

Hierarchy (View Summary)

Constructors

  • Instantiates a new NextDrupalPages.

    const client = new NextDrupalPages(baseUrl)

    Parameters

    • baseUrl: string

      The baseUrl of your Drupal site. Do not add the /jsonapi suffix.

    • options: DrupalClientOptions = {}

      Options for the client. See Experiment_DrupalClientOptions.

    Returns NextDrupalPages

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
getPathsFromContext: (
    types: string | string[],
    context: GetStaticPathsContext,
    options?: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOption,
) => Promise<(string | { locale?: string; params: { slug: string[] } })[]> = ...

Type declaration

    • (
          types: string | string[],
          context: GetStaticPathsContext,
          options?: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOption,
      ): Promise<(string | { locale?: string; params: { slug: string[] } })[]>
    • Gets static paths from the context.

      Parameters

      • types: string | string[]

        The types of the resources.

      • context: GetStaticPathsContext

        The static paths context.

      • Optionaloptions: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOption

        Options for the request.

      Returns Promise<(string | { locale?: string; params: { slug: string[] } })[]>

      The fetched static paths.

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.

  • Parameters

    Returns DrupalMenuTree

  • Builds static paths from resources.

    Parameters

    • resources: { path: DrupalPathAlias }[]

      The resources.

    • Optionaloptions: { locale?: string; pathPrefix?: string }

      Options for the request.

    Returns { params: { slug: string[] } }[]

    The built static paths.

  • Builds static paths parameters from paths.

    Parameters

    • paths: string[]

      The paths.

    • Optionaloptions: { locale?: string; pathPrefix?: string }

      Options for the request.

    Returns { params: { slug: string[] } }[]

    The built static paths parameters.

  • 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 authentication configuration from the context and options.

    Parameters

    • context: GetStaticPropsContext

      The static props context.

    • options: JsonApiWithAuthOption

      Options for the request.

    Returns boolean | NextDrupalAuth

    The authentication configuration.

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

    Parameters

    Returns Promise<string>

    The authorization header value.

  • Gets the entry point for a given resource type.

    Parameters

    • resourceType: string

      The resource type.

    • Optionallocale: string

      The locale.

    Returns Promise<string>

    The entry point URL.

  • Extracts errors from the fetch response.

    Parameters

    • response: Response

      The fetch response.

    Returns Promise<string | JsonApiError[]>

    The extracted errors.

  • Gets the path from the context.

    Parameters

    • context: GetStaticPropsContext

      The static props context.

    • Optionaloptions: { pathPrefix?: string }

      Options for the request.

    Returns string

    The constructed path.

  • 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"
    )
  • Gets a collection of resources from the context.

    Type Parameters

    Parameters

    • type: string

      The type of the resources.

    • context: GetStaticPropsContext

      The static props context.

    • Optionaloptions: { deserialize?: boolean } & JsonApiOptions

      Options for the request.

    Returns Promise<T>

    The fetched collection of resources.

  • 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.

  • Gets a resource from the context.

    Type Parameters

    Parameters

    • input: string | DrupalTranslatedPath

      The input path or translated path.

    • context: GetStaticPropsContext

      The static props context.

    • Optionaloptions: { isVersionable?: boolean; pathPrefix?: string } & JsonApiOptions

      Options for the request.

    Returns Promise<T>

    The fetched resource.

  • Gets a search index from the context.

    Type Parameters

    Parameters

    • name: string

      The name of the search index.

    • context: GetStaticPropsContext

      The static props context.

    • Optionaloptions: JsonApiOptions

      Options for the request.

    Returns Promise<T>

    The fetched search index.

  • Gets static paths from the context.

    Parameters

    • types: string | string[]

      The types of the resources.

    • context: GetStaticPathsContext

      The static paths context.

    • Optionaloptions: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOption

      Options for the request.

    Returns Promise<(string | { locale?: string; params: { slug: string[] } })[]>

    The fetched static paths.

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

    Parameters

    • error: Error

      The error to log or throw.

    Returns void

  • Handles preview mode.

    Parameters

    • request: NextApiRequest

      The API request.

    • response: NextApiResponse

      The API response.

    • Optionaloptions: { enable: boolean }

      Options for the request.

    Returns Promise<void | NextApiResponse>

  • Disables preview mode.

    Parameters

    • request: NextApiRequest

      The API request.

    • response: NextApiResponse

      The API response.

    Returns Promise<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.

  • Translates a path from the context.

    Parameters

    • context: GetStaticPropsContext

      The static props context.

    • Optionaloptions: { pathPrefix?: string } & JsonApiWithAuthOption

      Options for the request.

    Returns Promise<DrupalTranslatedPath>

    The translated path.

  • Validates the draft URL using the provided search parameters.

    Parameters

    • searchParams: URLSearchParams

      The search parameters.

    Returns Promise<Response>

    The validation response.