@jsondrop/drupal

中文文档
0.1.0发布于 2026/11/01MITTypeScriptES Module零依赖

Drupal core entity types and helpers (decoupled_router, menus, views, images) for @jsondrop/core.

原始使用文档
文档目录3 节
  1. Types
  2. Helpers
  3. Users

@jsondrop/drupal

Drupal 11 core entity types and helpers for @jsondrop/core.

npm install @jsondrop/core @jsondrop/drupal

Types

Importing the package registers Drupal core's resource types with the client:

  • fixed types: user--user, file--file, menu_link_content--menu_link_content, path_alias--path_alias, and the config entities (node_type, taxonomy_vocabulary, media_type, user_role, block_content_type);
  • per-entity-type fallbacks: any node--* is a Node, taxonomy_term--* a Term, media--* a Media, block_content--* a BlockContent, paragraph--* a Paragraph, typed with the base fields.

Add your bundles' fields by extending them:

import type { ToMany, ToOne } from '@jsondrop/core';
import type { File, ImageMeta, MediaImage, Node, Term, TextField } from '@jsondrop/drupal';

export interface Article extends Node<'article'> {
  body: TextField | null;
  field_image: ToOne<File<ImageMeta>>;
  field_tags: ToMany<Term<'tags'>>;
  field_hero: ToOne<MediaImage>;
}

declare module '@jsondrop/core' {
  interface ResourceTypes {
    'node--article': Article;
  }
}

Field value types: TextField, PathField, LinkField, FileUri, ImageMeta, FileMeta, DateRangeField, AddressField. Standard-profile media types: MediaImage, MediaDocument, MediaAudio, MediaVideo, MediaRemoteVideo.

Helpers

HelperNeeds
translatePath(client, path)decoupled_routerResolves a path; NULL when not found
getResourceByPath(client, path, options)decoupled_routerResolves a path and fetches its entity, with redirects
getMenu(client, menu) / getMenuItemsjsonapi_menu_itemsA menu as a sorted tree / flat list
buildMenuTree(items)Nests links by parent; also takes core menu_link_content
getView(client, view, display, { filters, arguments, sort, page })jsonapi_viewsA view display's results and count
image(client, field)An image field as { url, alt, title, width, height }
fileUrl(client, file)Absolute file URL
imageStyleUrl(file, style)consumer_image_stylesAn image style derivative's URL

Users

HelperNeeds
getCurrentUser(client) / getCurrentUserIdThe logged-in user (any auth), from the entry point's me link; NULL when anonymous
login(client, { name, pass })Drupal session login (/user/login), server-side: returns the session cookie, CSRF and logout tokens
logout(client, session)Ends the session with its logout token
isLoggedIn(client, cookie)Whether a session is still valid
requestPasswordReset(client, { mail })Mails a reset link; same answer for unknown accounts

For OAuth logins (simple_oauth) see OAuthClient in @jsondrop/core. Both are shown in example 7.

const page = await getResourceByPath<Article>(drupal, '/blog/hello', {
  query: { include: ['field_image', 'field_tags'] },
});
if (!page?.resource) notFound();

版本与发布记录

@jsondrop/drupal 共发布 2 个版本,首次发布于 2026/11/01;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.1.0latest2026/11/011 天前
0.0.0-stage2026/11/011 天前

关于 @jsondrop/drupal 的常见问题

@jsondrop/drupal 是什么?

Drupal核心实体类型和@jsondrop/core的帮助器(decpled_router, menus, views, images)。

如何安装 @jsondrop/drupal?

在项目目录运行 npm install @jsondrop/drupal,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@jsondrop/core(^0.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@jsondrop/drupal 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@jsondrop/drupal 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@jsondrop/drupal 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@jsondrop/drupal 可以商用吗?

@jsondrop/drupal 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@jsondrop/drupal 还在维护吗?

最新版本 0.1.0 发布于 2026/11/01(1 天前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/11/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。