API 路由

本頁內容

外掛可以為其管理介面和外部整合公開 API 路由。路由掛載在 /_emdash/api/plugins/<plugin-id>/<route-name> 下,並在沙盒執行時期內執行,使用與 hooks 相同的 PluginContext

本頁面介紹沙盒化(標準格式)外掛。原生外掛的 API 表面相同;唯一的差異是處理器簽章 — 詳見原生外掛中的說明。

定義路由

sandbox-entry.tsdefinePlugin() 中宣告路由:

import { definePlugin } from "emdash";
import type { PluginContext } from "emdash";
import { z } from "astro/zod";

export default definePlugin({
	routes: {
		status: {
			handler: async (_routeCtx, ctx: PluginContext) => {
				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: PluginContext) => {
				const { formId, limit, cursor } = routeCtx.input;

				const result = await ctx.storage.submissions.query({
					where: formId ? { formId } : undefined,
					orderBy: { createdAt: "desc" },
					limit,
					cursor,
				});

				return result;
			},
		},
	},
});

標準格式的路由處理器接受兩個參數(routeCtx, ctx)

  • routeCtx 攜帶請求形式的資料:{ input, request, requestMeta }
  • ctx 是您在 hooks 內部獲得的相同 PluginContextctx.storagectx.kvctx.contentctx.httpctx.log 等。

路由 URL

路由掛載在 /_emdash/api/plugins/<plugin-id>/<route-name>。路由名稱可以包含斜線以支援巢狀路徑。

外掛 ID路由名稱URL
formsstatus/_emdash/api/plugins/forms/status
formssubmissions/_emdash/api/plugins/forms/submissions
seosettings/save/_emdash/api/plugins/seo/settings/save
analyticsevents/recent/_emdash/api/plugins/analytics/events/recent

驗證與 CSRF

外掛路由預設經過驗證。 調度器在呼叫您的處理器之前需要工作階段(或具有 admin 作用域的權杖):

  • 讀取方法(GETHEADOPTIONS)需要 plugins:read 權限。
  • 寫入方法(POSTPUTPATCHDELETE)需要 plugins:manage
  • 私有路由上的狀態變更方法還需要 X-EmDash-Request: 1 CSRF 標頭(管理介面的 usePluginAPI() hook 會自動傳送;使用 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: <你的值> })並作為 application/json 提供。

return { id: "abc", count: 42 };  // 包裝為 { success: true, data: { id, count } }
return [1, 2, 3];                 // 包裝為 { 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 });
			}
		},
	},
},

存取請求

完整的 Request 物件可作為 routeCtx.request 用於標頭、原始 body 存取與 URL 解析。routeCtx.requestMeta 攜帶跨平台標準化的 IP、使用者代理與地理資料。

handler: async (routeCtx, ctx) => {
	const { request, requestMeta } = routeCtx;

	const auth = request.headers.get("Authorization");
	const url = new URL(request.url);
	const page = url.searchParams.get("page");

	ctx.log.info("Request", { ip: requestMeta.ip, ua: requestMeta.userAgent });

	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();
		},
	},
},

從管理介面呼叫路由

使用 admin 套件中的 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": "[email protected]"}'

路由內容參考

// 標準格式處理器作為其兩個參數接收的內容

interface StandardRouteContext<TInput = unknown> {
	input: TInput;
	request: Request;
	requestMeta: { ip: string | null; userAgent: string | null; geo?: GeoData };
}

interface PluginContext {
	plugin: { id: string; version: string };
	storage: PluginStorage;
	kv: KVAccess;
	log: LogAccess;
	site: SiteInfo;
	url(path: string): string;
	cron?: CronAccess;
	content?: ContentAccess;       // 當宣告 content:read 或 content:write 時
	media?: MediaAccess;           // 當宣告 media:read 或 media:write 時
	http?: HttpAccess;             // 當宣告 network:request 時
	users?: UserAccess;            // 當宣告 users:read 時
	email?: EmailAccess;           // 當宣告 email:send 且設定提供者時
}

原生外掛接收一個組合兩者的單一 RouteContext 參數 — 如果您走那條路,請參閱建立原生外掛