API 路由

本頁內容

外掛程式可以為其管理介面和外部整合公開 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 會推斷 routeCtxctx——無需參數註解。沙盒路由處理器接受兩個參數(routeCtx, ctx)

  • routeCtx 攜帶請求形狀的資料:{ input, request, requestMeta }
  • ctx 是您在鉤子內獲得的相同 PluginContext——ctx.storagectx.kvctx.contentctx.httpctx.log 等。

路由 URL

路由掛載在 /_emdash/api/plugins/<slug>/<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() 鉤子會自動傳送;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.requestSandboxedRequest:一個可攜式的 { 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 參數——如果您選擇該路線,請參閱建立原生外掛程式