플러그인은 관리 UI 및 외부 통합을 위해 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 헤더도 필요합니다(관리 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: <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] }
오류
오류 응답을 반환하려면 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 });
}
},
},
},
요청에 액세스하기
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를 통한 설정
샌드박스 플러그인은 일반적으로 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": "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 인수를 받습니다 — 그 경로를 선택하는 경우 네이티브 플러그인 생성을 참조하세요.