Class NextDrupalBase

The base class for NextDrupal clients.

Hierarchy (View Summary)

Constructors

  • Instantiates a new NextDrupalBase.

    const client = new NextDrupalBase(baseUrl)

    Parameters

    • baseUrl: string

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

    • options: NextDrupalBaseOptions = {}

      Options for NextDrupalBase.

    Returns NextDrupalBase

Properties

accessToken?: AccessToken
baseUrl: string
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
withAuth: boolean

Accessors

  • get apiPrefix(): string
  • Returns string

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

    • apiPrefix: string

    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 with the given options.

    Parameters

    • options: { locale?: string; path?: string; searchParams?: EndpointSearchParams } = {}

      The options for building the endpoint.

      • Optionallocale?: string

        The locale.

      • Optionalpath?: string

        The path.

      • OptionalsearchParams?: EndpointSearchParams

        The search parameters.

    Returns Promise<string>

    The constructed endpoint URL.

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

    Parameters

    • path: string

      The path for the url. Example: "/example"

    • OptionalsearchParams: EndpointSearchParams

      Optional query parameters.

    Returns URL

    The constructed URL.

    const drupal = new DrupalClient("https://example.com")

    // https://drupal.org
    drupal.buildUrl("https://drupal.org").toString()

    // https://example.com/foo
    drupal.buildUrl("/foo").toString()

    // https://example.com/foo?bar=baz
    client.buildUrl("/foo", { bar: "baz" }).toString()

    Build a URL from DrupalJsonApiParams

    const params = {
    getQueryObject: () => ({
    sort: "-created",
    "fields[node--article]": "title,path",
    }),
    }

    // https://example.com/jsonapi/node/article?sort=-created&fields%5Bnode--article%5D=title%2Cpath
    drupal.buildUrl("/jsonapi/node/article", params).toString()
  • 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

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

    Parameters

    • input: RequestInfo

      The url to fetch from.

    • init: FetchOptions = {}

      The fetch options with withAuth. If withAuth is set, fetch will fetch an Authorization header before making the request.

    Returns Promise<Response>

    The fetch response.

    To provide your own custom fetcher, see the fetcher docs.

    const url = drupal.buildUrl("/jsonapi/node/article", {
    sort: "-created",
    "fields[node--article]": "title,path",
    })

    const response = await drupal.fetch(url.toString())
  • Retrieve an access token.

    Parameters

    Returns Promise<AccessToken>

    The access token.

    If options is not provided, DrupalClient will use the clientId and clientSecret configured in auth.

    const accessToken = await drupal.getAccessToken({
    clientId: "7034f4db-7151-466f-a711-8384bddb9e60",
    clientSecret: "d92Fm^ds",
    })
  • 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.

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