@basenative/admin

中文文档
3.0.0发布于 2026/10/31Apache-2.0TypeScriptES Module零依赖构建来源已验证

Pluggable admin & moderation surface — protected-route helpers, role/permission primitives, audit-log middleware, and queue-review UI components for BaseNative apps

原始使用文档
文档目录11 节
  1. Install
  2. Roles
  3. Seeding admins from env
  4. Submission queue
  5. Drop-in handlers
  6. Audit log
  7. UI components
  8. Migration
  9. Adopting incrementally
  10. Links
  11. License

@basenative/admin

A pluggable admin & moderation surface for BaseNative apps. Brings together the four pieces that every "users-can-submit-stuff, mods-can-approve-it" app rebuilds from scratch:

  1. Roles — hierarchical (user < moderator < admin) with seedable admin promotion from a deployment-time allowlist.
  2. Queues — submission table → approved target table, with per-row decide audit columns populated automatically.
  3. Audit log — every protected action records who did what, when, and to which target — without ever crashing the request when the audit write fails.
  4. UI — server-rendered building blocks (<AdminQueueList>, <AdminUserList>) that compose with @basenative/components and theme via CSS custom properties.

Worker-runtime first; works fine on Node where storage adapters allow.

Install

pnpm add @basenative/admin
# Optional — gets you sessions/cookies for `getCurrentUser`
pnpm add @basenative/auth

Roles

import { defineRoles, requireRole, roleSeed } from '@basenative/admin/roles';

const roles = defineRoles({ hierarchy: ['user', 'moderator', 'admin'] });

roles.isAdmin(user);        // top tier?
roles.isModerator(user);    // ≥ moderator (admin counts)
roles.hasRole(user, 'moderator');

// Inside a handler:
const guard = requireRole('moderator');
const { user, error } = guard(currentUser);
if (error) return error;

Seeding admins from env

Promote handles listed in ADMIN_HANDLES (comma-separated) on first sight:

user = await roleSeed({
  env, envKey: 'ADMIN_HANDLES',
  user,
  setRole: (id, role, by) => d1Users(env.DB).setRole(id, role, by),
});

Submission queue

import { defineQueue } from '@basenative/admin/queue';

const queue = defineQueue({
  db: env.DB,
  tables: { submissions: 'submissions', target: 'phrases', columns: ['category', 'phrase'] },
});

await queue.submit({ category: 'Foods', phrase: 'apple pie', submittedBy: 'alice' });
const pending = await queue.listPending();
await queue.decide(pending[0].id, 'approved', 'mod1');

On approval the row is copied into the target table with created_at / created_by populated from the moderator's decision. Override onApprove if you need bespoke promotion logic.

Drop-in handlers

import { defineAdminHandlers } from '@basenative/admin/handlers';
import { currentUser } from '@basenative/auth';

const admin = defineAdminHandlers({
  queue,
  users: d1Users(env.DB),
  getCurrentUser: currentUser,
});

// functions/api/moderate/pending.js
export const onRequestGet = admin.listPending;

// functions/api/moderate/decide.js
export const onRequestPost = admin.decide;

// functions/api/moderate/users.js
export const onRequestGet = admin.users;

// functions/api/moderate/promote.js
export const onRequestPost = admin.promote;

Audit log

import { auditAction } from '@basenative/admin/audit';

await auditAction(env, {
  user,
  action: 'submission.approved',
  target: { type: 'submission', id: 42 },
  meta: { reason: 'duplicate' },
});

Audit failures are caught and logged — they never bubble up to the user.

UI components

import { renderAdminQueueList, renderAdminUserList } from '@basenative/admin/components';

const html = renderAdminQueueList({ items: pending });
const usersHtml = renderAdminUserList({
  users: elevated,
  results,
  query: q,
  currentHandle: me.handle,
});

Themable via CSS variables — --bn-admin-bg, --bn-admin-fg, --bn-admin-accent, --bn-admin-ok, --bn-admin-bad, --bn-admin-danger.

Action delegation: each button carries data-action="bn-admin-decide" (or bn-admin-set-role) plus data-id / data-decision / data-role. Wire a single document-level click handler that dispatches to your API.

Migration

Apply migrations/0001_audit_and_roles.sql (also exported as AUDIT_MIGRATION).

Adopting incrementally

You can pull in the bits in any order:

  1. Start with roles — the cheapest surface area; just wraps role checks.
  2. Add audit next to start recording protected actions you already have.
  3. Replace your queue endpoints with handlers once the data shape lines up.
  4. Swap your queue UI for components last.

License

Apache-2.0.

版本与发布记录

@basenative/admin 共发布 11 个版本,首次发布于 2026/10/18;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/312 天前
2.4.02026/10/268 天前
2.3.22026/10/2212 天前
2.3.12026/10/2212 天前
2.3.02026/10/2212 天前
2.2.02026/10/2113 天前

关于 @basenative/admin 的常见问题

@basenative/admin 是什么?

可插拔的管理和审核表面保护路由助手、角色/权限原语、审计日志中间件和用于BaseNative应用程序的队列审查UI组件

如何安装 @basenative/admin?

在项目目录运行 npm install @basenative/admin,或使用 pnpm add / yarn add / bun add。

@basenative/admin 支持 TypeScript 吗?

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

@basenative/admin 支持 ESM 还是 CommonJS?

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

@basenative/admin 可以商用吗?

@basenative/admin 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@basenative/admin 还在维护吗?

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