플러그인은 관리 UI와 외부 통합을 위한 API 라우트를 노출할 수 있습니다. 라우트는 /_emdash/api/plugins/<plugin-id>/<route-name> 아래에 마운트되며, 훅이 받는 것과 동일한 PluginContext를 가진 샌드박스 런타임 내에서 실행됩니다.
이 페이지는 샌드박스(표준 형식) 플러그인을 다룹니다. 네이티브 플러그인의 API 표면은 동일하며, 유일한 차이점은 핸들러 시그니처입니다 — 자세한 내용은 네이티브 플러그인의 참고 사항을 참조하세요.
라우트 정의
sandbox-entry.ts의 definePlugin()에서 라우트를 선언합니다:
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는 훅 내부에서 얻는 것과 동일한PluginContext입니다 —ctx.storage,ctx.kv,ctx.content,ctx.http,ctx.log등.
라우트 URL
라우트는 /_emdash/api/plugins/<plugin-id>/<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 헤더도 필요합니다(관리 UI의usePluginAPI()훅이 자동으로 보냅니다. 쿠키 인증 외부 호출자는 직접 설정해야 합니다. 토큰 인증 요청은 면제됩니다).
인증 및 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] }로 래핑됨
오류
오류 응답을 반환하려면 throw합니다. 알려진 플러그인 오류가 아닌 것은 일반 메시지를 반환합니다 — 내부 예외는 스택 트레이스나 데이터베이스 오류를 유출하는 대신 마스킹됩니다:
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를 throw합니다:
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에서 라우트 호출
관리 패키지의 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 인수를 받습니다 — 그 경로를 가는 경우 네이티브 플러그인 만들기를 참조하세요.