Next-Drupal API Reference

website·Indexed 34 days ago

https://next-drupal.org/

API documentation for Next-Drupal, providing tools to integrate Drupal backends with Next.js. Includes functions for resource fetching (getResource, getResourceCollection), path translation (translatePath), authentication checks (isBasicAuth, isAccessTokenAuth), and the NextDrupalPages class for managing Drupal interactions within Next.js page contexts.

Tokens
44.8K
Snippets
296
Records
374
Agent score
69%

What's inside Next-Drupal

  1. Overview of NextDrupal Client features

    The NextDrupal client is a JSON:API client designed to fetch Drupal data for static page generation or on-demand server rendering. It supports both read and write operations (POST, PATCH, DELETE) and includes the following features:

    • Helpers for fetching resources, menus, views, and search indices.
    • Support for custom authentication (Bearer, Basic, Next-Auth, or custom).
    • Support for custom serializers and fetchers.
    • Support for cached resources (e.g., memory cache, Redis).
    • Human-readable error messages.
  2. Overview of Next.js On-demand Revalidation in Drupal

    The next module uses Revalidator plugins to trigger on-demand revalidation of Next.js sites whenever a Drupal entity is inserted, updated, or deleted. By default, the module provides a Path revalidator that revalidates sites based on the entity's path.
  3. Use the DrupalMenuItem interface

    The DrupalMenuItem interface defines the structure of a menu item retrieved from Drupal. It includes metadata about the item's position in the menu hierarchy, its routing information, and display properties.
    interface DrupalMenuItem {
      description: string;
      enabled: boolean;
      expanded: boolean;
      id: string;
      items?: DrupalMenuItem[];
      menu_name: string;
      meta: Record<string, unknown>;
      options: Record<string, unknown>;
      parent: string;
      provider: string;
      route: { name: string; parameters: Record<string, unknown> };
      title: string;
      type: string;
      url: string;
      weight: string;
    }
  4. Use Preview URL Generators for encrypted preview secrets

    Next-Drupal 1.5 and Next 1.3 introduce Preview URL Generators to create URLs for preview mode using encrypted secrets. Unlike previous versions, these secrets are generated per request, encrypted, non-reusable, scoped to the specific URL and user, and include expiration dates. Validation is handled by Drupal, and the system is pluggable, allowing developers to implement their own secret generators.
  5. Use the JsonApiResponse interface for Drupal JSON:API responses

    The JsonApiResponse interface defines the standard structure for responses returned by the Drupal JSON:API. It includes the primary data payload, error collections, included related resources, and metadata about the response.
    interface JsonApiResponse {
      data: Record<string, any>[];
      errors: JsonApiError[];
      included?: Record<string, any>[];
      jsonapi?: {
        meta: Record<string, any>[];
        version: string;
      };
      links?: JsonApiLinks;
      meta: {
        count: number;
        [key: string]: any;
      };
      [key: string]: any;
    }
  6. Reference DrupalView<T> properties

    The DrupalView<T> interface contains the following properties:

    • id: A string identifier for the view.
    • links: An object of type JsonApiLinks containing API navigation links.
    • meta: An object containing metadata about the response, including a mandatory count (number) and any other dynamic key-value pairs.
    • results: The actual data returned by the view, typed as T (which defaults to an array of records).
  7. Implement on-demand ISR for Drupal content types

    Next.js for Drupal supports On-demand Revalidation, allowing the Next.js site to update automatically when content in Drupal is created, updated, or deleted. This can be configured per entity type using two primary methods: revalidation by path or revalidation by tag. Tag-based invalidation (introduced in Next Drupal 2.0) leverages Drupal's native cache tag system to invalidate specific content on the Next.js frontend.
  8. Implement user-level access control with Next.js JWT

    The Next.js JWT module (introduced as experimental in Next 1.3) allows the use of JSON Web Tokens for authentication. It includes a Preview URL generator that enables more granular, user-level access control for preview modes.
  9. Implement Next.js Draft Mode with Drupal

    To enable draft mode for content types in a Next.js site, you must use the Next.js module in conjunction with the next-drupal plugin. This setup allows the creation of Next.js draft routes, which require a configured Next.js site on the Drupal side and established authentication.
  10. Initialize a Next-Drupal project with App Router or Pages Router

    Next-Drupal 2.0 provides different starters depending on the Next.js routing architecture you wish to use. The basic starter now defaults to the App Router.
    # Create a project using the App Router (Default)
    npx create-next-app -e https://github.com/chapter-three/next-drupal-basic-starter
    
    # Create a project using the Pages Router
    npx create-next-app -e https://github.com/chapter-three/next-drupal-pages-starter
  11. Use the DrupalTranslatedPath interface

    The DrupalTranslatedPath interface defines the structure of a translated path object in next-drupal. It provides details about the associated Drupal entity, whether the path is the home page, JSON:API routing information, and any applicable redirects.
    interface DrupalTranslatedPath {
      entity: {
        bundle: string;
        canonical: string;
        id: string;
        langcode?: string;
        path?: string;
        type: string;
        uuid: string;
      };
      isHomePath: boolean;
      jsonapi?: {
        basePath: string;
        entryPoint: string;
        individual: string;
        pathPrefix: string;
        resourceName: string;
      };
      label?: string;
      meta?: Record<string, unknown>;
      redirect?: { from: string; status: string; to: string }[];
      resolved: string;
    }
  12. DrupalFile interface definition

    The DrupalFile interface extends JsonApiResource and represents a file entity in Drupal. It contains metadata about the file, including its URI, MIME type, and size.
    interface DrupalFile {
      changed: string;
      created: string;
      drupal_internal__fid: string;
      filemime: string;
      filename: string;
      filesize: number;
      id: string;
      langcode: string;
      resourceIdObjMeta?: DrupalFileMeta;
      status: boolean;
      type: string;
      uri: { url: string; value: string };
      [key: string]: any;
    }