API ルート

このページ

プラグインは、管理UIと外部統合のためにAPIルートを公開できます。ルートは /_emdash/api/plugins/<plugin-id>/<route-name> にマウントされ、フックが受け取るのと同じ 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;
			},
		},
	},
});

標準フォーマットのルートハンドラーは 2つの引数 を取ります:(routeCtx, ctx)

  • routeCtx はリクエスト形式のデータを保持します:{ input, request, requestMeta }
  • ctx はフック内で取得するのと同じ PluginContext です — ctx.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 ヘッダーも必要です(管理UIの 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: <あなたの値> })でラップし、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 オブジェクトは、ヘッダー、生のボディアクセス、URL解析のために routeCtx.request として利用できます。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経由の設定

サンドボックス化されたプラグインは、慣例的に settings: プレフィックスの下でKVストアを通じて設定を読み書きします。自動生成された 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();
		},
	},
},

管理UIからルートを呼び出す

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]"}'

ルートコンテキストリファレンス

// 標準フォーマットハンドラーが2つの引数として受け取るもの

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 引数を受け取ります — その道を行く場合はネイティブプラグインの作成を参照してください。