插件可以为其管理界面和外部集成公开 API 路由。路由挂载在 /_emdash/api/plugins/<slug>/<route-name> 下(<slug> 是 emdash-plugin.jsonc 中的 slug 字段——在运行时作为 ctx.plugin.id 公开),并在沙箱运行时内以钩子接收的相同 PluginContext 运行。
本页介绍沙箱插件。原生插件的 API 表面相同;唯一的区别是处理器签名——有关详细信息,请参阅原生插件中的说明。
定义路由
在 src/plugin.ts 的默认导出中声明路由:
import type { SandboxedPlugin } from "emdash/plugin";
import { z } from "astro/zod";
export default {
routes: {
status: {
handler: async (_routeCtx, ctx) => {
return { ok: true, plugin: ctx.plugin.id };
},
},
submissions: {
input: z.object({
formId: z.string().optional(),
limit: z.number().default(50),
cursor: z.string().optional(),
}),
handler: async (routeCtx, ctx) => {
const { formId, limit, cursor } = routeCtx.input;
const result = await ctx.storage.submissions.query({
where: formId ? { formId } : undefined,
orderBy: { createdAt: "desc" },
limit,
cursor,
});
return result;
},
},
},
} satisfies SandboxedPlugin;
satisfies SandboxedPlugin 会推断 routeCtx 和 ctx——无需参数注释。沙箱路由处理器接受两个参数:(routeCtx, ctx)。
routeCtx携带请求形状的数据:{ input, request, requestMeta }。ctx是您在钩子内获得的相同PluginContext——ctx.storage、ctx.kv、ctx.content、ctx.http、ctx.log等。
路由 URL
路由挂载在 /_emdash/api/plugins/<slug>/<route-name>。路由名称可以包含斜杠以表示嵌套路径。
| 插件 ID | 路由名称 | URL |
|---|---|---|
forms | status | /_emdash/api/plugins/forms/status |
forms | submissions | /_emdash/api/plugins/forms/submissions |
seo | settings/save | /_emdash/api/plugins/seo/settings/save |
analytics | events/recent | /_emdash/api/plugins/analytics/events/recent |
认证和 CSRF
**插件路由默认经过认证。**调度程序在调用您的处理器之前需要会话(或具有 admin 范围的令牌):
- 读取方法(
GET、HEAD、OPTIONS)需要plugins:read权限。 - 写入方法(
POST、PUT、PATCH、DELETE)需要plugins:manage。 - 私有路由上的状态更改方法还需要
X-EmDash-Request: 1CSRF 头(管理界面的usePluginAPI()钩子会自动发送;cookie 认证的外部调用者需要自己设置;令牌认证的请求豁免)。
要让路由退出认证和 CSRF,请将其标记为 public: true:
routes: {
track: {
public: true,
input: z.object({ event: z.string() }),
handler: async (routeCtx, ctx) => {
ctx.log.info("Tracked", { event: routeCtx.input.event });
return { ok: true };
},
},
},
输入验证
input 接受 Zod 模式。调度程序解析请求正文(POST/PUT/PATCH)或查询字符串(GET/DELETE),对其进行验证,并将类型化结果作为 routeCtx.input 传递给您的处理器。无效输入在处理器运行之前返回 400。
routes: {
create: {
input: z.object({
title: z.string().min(1).max(200),
email: z.string().email(),
priority: z.enum(["low", "medium", "high"]).default("medium"),
tags: z.array(z.string()).optional(),
}),
handler: async (routeCtx, ctx) => {
const { title, email, priority, tags } = routeCtx.input;
await ctx.storage.items.put(`item_${Date.now()}`, {
title,
email,
priority,
tags: tags ?? [],
createdAt: new Date().toISOString(),
});
return { success: true };
},
},
},
返回值
返回任何 JSON 可序列化的值。调度程序将其包装在 EmDash 的标准信封({ success: true, data: <your value> })中,并作为 application/json 提供。
return { id: "abc", count: 42 }; // wrapped to { success: true, data: { id, count } }
return [1, 2, 3]; // wrapped to { success: true, data: [1, 2, 3] }
错误
抛出异常以返回错误响应。任何不是已知插件错误的内容都会返回通用消息——内部异常被屏蔽,而不是泄漏堆栈跟踪或数据库错误:
handler: async (routeCtx, ctx) => {
const item = await ctx.storage.items.get(routeCtx.input.id);
if (!item) {
throw new Error("Item not found");
}
return item;
},
对于特定状态代码,抛出 Response:
handler: async (routeCtx, ctx) => {
const item = await ctx.storage.items.get(routeCtx.input.id);
if (!item) {
throw new Response(JSON.stringify({ error: "Not found" }), {
status: 404,
headers: { "Content-Type": "application/json" },
});
}
return item;
},
HTTP 方法
路由响应所有方法。如果需要按方法的行为,请在 routeCtx.request.method 上分支:
routes: {
item: {
input: z.object({ id: z.string() }),
handler: async (routeCtx, ctx) => {
const { id } = routeCtx.input;
switch (routeCtx.request.method) {
case "GET":
return await ctx.storage.items.get(id);
case "DELETE":
await ctx.storage.items.delete(id);
return { deleted: true };
default:
throw new Response("Method not allowed", { status: 405 });
}
},
},
},
访问请求
routeCtx.request 是 SandboxedRequest:一个可移植的 { url, method, headers } 记录,在进程内和隔离内的行为相同。headers 是一个按小写头名称索引的 Record<string, string>——通过小写名称索引或使用 Object.entries 迭代。url 是一个字符串,因此 new URL(request.url) 解析查询参数。routeCtx.requestMeta 携带在可用时跨平台规范化的 IP、用户代理和地理数据。
handler: async (routeCtx, ctx) => {
const { request, requestMeta } = routeCtx;
const auth = request.headers["authorization"]; // lowercased key, no .get()
const url = new URL(request.url);
const page = url.searchParams.get("page");
ctx.log.info("Request", { meta: requestMeta });
if (request.method !== "POST") {
throw new Response("POST required", { status: 405 });
}
},
常见模式
通过 KV 设置
沙箱插件通过 KV 存储读写设置,通常在 settings: 前缀下。自动生成的 settingsSchema 表单仅适用于原生插件——对于沙箱插件,通过路由公开读/写,并在 Block Kit 中渲染表单。
routes: {
settings: {
handler: async (_routeCtx, ctx) => {
const settings = await ctx.kv.list("settings:");
const result: Record<string, unknown> = {};
for (const entry of settings) {
result[entry.key.replace("settings:", "")] = entry.value;
}
return result;
},
},
"settings/save": {
input: z.object({
enabled: z.boolean().optional(),
apiKey: z.string().optional(),
maxItems: z.number().optional(),
}),
handler: async (routeCtx, ctx) => {
for (const [key, value] of Object.entries(routeCtx.input)) {
if (value !== undefined) {
await ctx.kv.set(`settings:${key}`, value);
}
}
return { success: true };
},
},
},
分页列表
从存储查询返回基于游标的分页——响应形状与 EmDash 的其余部分使用的相匹配:
routes: {
list: {
input: z.object({
limit: z.number().min(1).max(100).default(50),
cursor: z.string().optional(),
status: z.string().optional(),
}),
handler: async (routeCtx, ctx) => {
const { limit, cursor, status } = routeCtx.input;
const result = await ctx.storage.items.query({
where: status ? { status } : undefined,
orderBy: { createdAt: "desc" },
limit,
cursor,
});
return {
items: result.items.map((item) => ({ id: item.id, ...item.data })),
cursor: result.cursor,
hasMore: result.hasMore,
};
},
},
},
外部 API 代理
通过 ctx.http 代理对外部服务的请求(需要 network:request 能力和 allowedHosts 中的条目):
routes: {
forecast: {
input: z.object({ city: z.string() }),
handler: async (routeCtx, ctx) => {
if (!ctx.http) throw new Error("Network capability not granted");
const apiKey = await ctx.kv.get<string>("settings:apiKey");
if (!apiKey) throw new Error("API key not configured");
const response = await ctx.http.fetch(
`https://api.weather.example.com/forecast?city=${routeCtx.input.city}`,
{ headers: { "X-API-Key": apiKey } },
);
if (!response.ok) {
throw new Error(`Weather API error: ${response.status}`);
}
return response.json();
},
},
},
从管理界面调用路由
使用管理包中的 usePluginAPI()——它会自动添加 X-EmDash-Request CSRF 头和插件 ID 前缀:
import { usePluginAPI } from "@emdash-cms/admin";
function SettingsPage() {
const api = usePluginAPI();
const handleSave = async (settings) => {
await api.post("settings/save", settings);
};
const loadSettings = async () => {
return api.get("settings");
};
}
外部调用路由
公共路由可直接调用:
curl -X POST https://your-site.com/_emdash/api/plugins/forms/track \
-H "Content-Type: application/json" \
-d '{"event": "pageview"}'
私有路由需要会话凭据或具有 admin 范围的 API 令牌:
curl -X POST https://your-site.com/_emdash/api/plugins/forms/create \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{"title": "Hello", "email": "user@example.com"}'
路由上下文参考
// 沙箱路由处理器作为两个参数接收的内容
interface SandboxedRequest {
url: string;
method: string;
headers: Record<string, string>; // lowercased keys
}
interface SandboxedRouteContext {
input: unknown; // narrow with the `input` Zod schema at the route level
request: SandboxedRequest;
requestMeta?: unknown;
}
interface PluginContext {
plugin: { id: string; version: string };
storage: PluginStorage;
kv: KVAccess;
log: LogAccess;
site: SiteInfo;
url(path: string): string;
cron?: CronAccess;
content?: ContentAccess; // when content:read or content:write declared
media?: MediaAccess; // when media:read or media:write declared
http?: HttpAccess; // when network:request declared
users?: UserAccess; // when users:read declared
email?: EmailAccess; // when email:send declared and provider configured
}
原生插件接收一个组合两者的单个 RouteContext 参数——如果您选择该路线,请参阅创建原生插件。