TypeScript
Using built-in types with NextDrupal client.
Every helper method for the NextDrupal
client has full TypeScript support.
The next-drupal
module ships with some default basic types you can use.
Built-in Types
DrupalNode
export interface DrupalNode extends JsonApiResourceWithPath { drupal_internal__nid: number drupal_internal__vid: number changed: string created: string title: string default_langcode: boolean sticky: boolean}
DrupalBlock
export interface DrupalBlock extends JsonApiResource { info: string}
DrupalMedia
export interface DrupalMedia extends JsonApiResource { drupal_internal__mid: string drupal_internal__vid: string changed: string created: string name: string}
DrupalFile
export interface DrupalFile extends JsonApiResource { drupal_internal__fid: string changed: string created: string filename: string uri: { value: string url: string } filesize: number filemime: string resourceIdObjMeta?: DrupalFileMeta}
export interface DrupalFileMeta { alt?: string title?: string width: number height: number}
DrupalTaxonomyTerm
export interface DrupalTaxonomyTerm extends JsonApiResourceWithPath { drupal_internal__tid: string changed: string default_langcode: boolean name: string description: string weight: number}
DrupalUser
export interface DrupalUser extends JsonApiResourceWithPath { drupal_internal__uid: string changed: string created: string default_langcode: boolean name: string}
DrupalView
/* eslint-disable @typescript-eslint/no-explicit-any */export interface DrupalView<T = Record<string, any>[]> { id: string results: T meta: JsonApiResponse["meta"] links: JsonApiResponse["links"]}
DrupalParagraph
export interface DrupalParagraph extends JsonApiResource { drupal_internal__id: number drupal_internal__revision_id: number}
DrupalPathAlias
export type DrupalPathAlias = { alias: string pid: number langcode: string}
DrupalMenuItem
export interface DrupalMenuItem { description: string enabled: boolean expanded: boolean id: DrupalMenuItemId menu_name: string meta: Record<string, unknown> options: Record<string, unknown> parent: DrupalMenuItemId provider: string route: { name: string parameters: Record<string, unknown> } title: string type: string url: string weight: string items?: DrupalMenuItem[]}
export type DrupalMenuItemId = string
DrupalTranslatedPath
export interface DrupalTranslatedPath { resolved: string isHomePath: boolean entity: { canonical: string type: string bundle: string id: string uuid: string langcode?: string } label?: string jsonapi?: { individual: string resourceName: string pathPrefix: string basePath: string entryPoint: string } meta?: Record<string, unknown> redirect?: { from: string to: string status: string }[]}
DrupalSearchApiJsonApiResponse
export interface DrupalSearchApiJsonApiResponse extends JsonApiResponse { meta: JsonApiResponse["meta"] & { facets?: DrupalSearchApiFacet[] }}
export interface DrupalSearchApiFacet { id: string label?: string path?: string terms?: { url: string values: { value: string label: string active?: boolean count?: number } }[]}
Extending Types
To extend the built-in types, create a file at types/drupal.d.ts
and extend the types as follows.
types/drupal.d.ts
interface Article extends DrupalNode { field_foo: string}