훅 레퍼런스

이 페이지

훅을 사용하면 플러그인이 콘텐츠, 미디어, 이메일, 댓글 및 페이지 수명 주기의 특정 지점에서 EmDash 동작을 가로채고 수정할 수 있습니다.

훅 개요

다음 표는 각 훅, 트리거 요인, 수정 가능한 항목 및 독점 여부를 나열합니다.

트리거수정 가능독점
content:beforeSave콘텐츠 저장 전콘텐츠 데이터아니요
content:afterSave콘텐츠 저장 후없음아니요
content:beforeDelete콘텐츠 삭제 전취소 가능아니요
content:afterDelete콘텐츠 삭제 후없음아니요
media:beforeUpload파일 업로드 전파일 메타데이터아니요
media:afterUpload파일 업로드 후없음아니요
cron예약된 작업 실행없음아니요
email:beforeSend이메일 전송 전메시지, 취소 가능아니요
email:deliver전송을 통한 이메일 전달없음
email:afterSend이메일 전송 성공 후없음아니요
comment:beforeCreate댓글 저장 전댓글, 취소 가능아니요
comment:moderate댓글 승인 상태 결정상태
comment:afterCreate댓글 저장 후없음아니요
comment:afterModerate관리자가 댓글 상태 변경 후없음아니요
page:metadata공개 페이지 헤드 렌더링태그 기여아니요
page:fragments공개 페이지 본문 렌더링스크립트 주입아니요
plugin:install플러그인 최초 설치 시없음아니요
plugin:activate플러그인 활성화 시없음아니요
plugin:deactivate플러그인 비활성화 시없음아니요
plugin:uninstall플러그인 제거 시없음아니요

콘텐츠 훅

content:beforeSave

데이터베이스에 콘텐츠를 저장하기 전에 실행됩니다. 콘텐츠를 검증, 변환 또는 강화하는 데 사용합니다.

import { definePlugin } from "emdash";

export default definePlugin({
	id: "my-plugin",
	version: "1.0.0",
	hooks: {
		"content:beforeSave": async (event, ctx) => {
			const { content, collection, isNew } = event;

			// Add timestamps
			if (isNew) {
				content.createdBy = "system";
			}
			content.modifiedAt = new Date().toISOString();

			// Return modified content
			return content;
		},
	},
});

Event

interface ContentHookEvent {
	content: Record<string, unknown>; // Content data
	collection: string; // Collection slug
	isNew: boolean; // True for creates, false for updates
}

반환 값

  • 수정된 콘텐츠 객체를 반환하여 변경 사항 적용
  • void를 반환하여 변경 없이 통과

content:afterSave

콘텐츠 저장 후에 실행됩니다. 알림, 캐시 무효화 또는 외부 동기화와 같은 부작용에 사용합니다.

hooks: {
  "content:afterSave": async (event, ctx) => {
    const { content, collection, isNew } = event;

    if (collection === "posts" && content.status === "published") {
      // Notify external service
      await ctx.http?.fetch("https://api.example.com/notify", {
        method: "POST",
        body: JSON.stringify({ postId: content.id }),
      });
    }
  },
}

반환 값

반환 값이 필요하지 않습니다.

content:beforeDelete

콘텐츠를 삭제하기 전에 실행됩니다. 삭제를 검증하거나 방지하는 데 사용합니다.

hooks: {
  "content:beforeDelete": async (event, ctx) => {
    const { id, collection } = event;

    // Prevent deletion of protected content
    const item = await ctx.content?.get(collection, id);
    if (item?.data.protected) {
      return false; // Cancel deletion
    }

    // Allow deletion
    return true;
  },
}

Event

interface ContentDeleteEvent {
	id: string; // Entry ID
	collection: string; // Collection slug
}

반환 값

  • false를 반환하여 삭제 취소
  • true 또는 void를 반환하여 허용

content:afterDelete

콘텐츠 삭제 후에 실행됩니다. 정리 작업에 사용합니다.

hooks: {
  "content:afterDelete": async (event, ctx) => {
    const { id, collection } = event;

    // Clean up related data
    await ctx.storage.relatedItems.delete(`${collection}:${id}`);
  },
}

미디어 훅

media:beforeUpload

파일 업로드 전에 실행됩니다. 파일을 검증, 이름 변경 또는 거부하는 데 사용합니다.

hooks: {
  "media:beforeUpload": async (event, ctx) => {
    const { file } = event;

    // Reject files over 10MB
    if (file.size > 10 * 1024 * 1024) {
      throw new Error("File too large");
    }

    // Rename file
    return {
      name: `${Date.now()}-${file.name}`,
      type: file.type,
      size: file.size,
    };
  },
}

Event

interface MediaUploadEvent {
	file: {
		name: string; // Original filename
		type: string; // MIME type
		size: number; // Size in bytes
	};
}

반환 값

  • 수정된 파일 메타데이터를 반환하여 변경 사항 적용
  • void를 반환하여 변경 없이 통과
  • 예외를 발생시켜 업로드 거부

media:afterUpload

파일 업로드 후에 실행됩니다. 처리, 썸네일 또는 메타데이터 추출에 사용합니다.

hooks: {
  "media:afterUpload": async (event, ctx) => {
    const { media } = event;

    if (media.mimeType.startsWith("image/")) {
      // Store image metadata
      await ctx.kv.set(`media:${media.id}:analyzed`, {
        processedAt: new Date().toISOString(),
      });
    }
  },
}

Event

interface MediaAfterUploadEvent {
	media: {
		id: string;
		filename: string;
		mimeType: string;
		size: number | null;
		url: string;
		createdAt: string;
	};
}

수명 주기 훅

plugin:install

플러그인이 처음 설치될 때 실행됩니다. 초기 설정, 저장소 컬렉션 생성 또는 시드 데이터에 사용합니다.

hooks: {
  "plugin:install": async (event, ctx) => {
    // Initialize default settings
    await ctx.kv.set("settings:enabled", true);
    await ctx.kv.set("settings:threshold", 100);

    ctx.log.info("Plugin installed successfully");
  },
}

plugin:activate

플러그인이 활성화될 때 실행됩니다(설치 후 또는 재활성화 후).

hooks: {
  "plugin:activate": async (event, ctx) => {
    ctx.log.info("Plugin activated");
  },
}

plugin:deactivate

플러그인이 비활성화될 때 실행됩니다.

hooks: {
  "plugin:deactivate": async (event, ctx) => {
    ctx.log.info("Plugin deactivated");
  },
}

plugin:uninstall

플러그인이 제거될 때 실행됩니다. 정리에 사용합니다.

hooks: {
  "plugin:uninstall": async (event, ctx) => {
    const { deleteData } = event;

    if (deleteData) {
      // Clean up all plugin data
      const items = await ctx.kv.list("settings:");
      for (const { key } of items) {
        await ctx.kv.delete(key);
      }
    }

    ctx.log.info("Plugin uninstalled");
  },
}

Event

interface UninstallEvent {
	deleteData: boolean; // User chose to delete data
}

Cron 훅

cron

예약된 작업이 실행될 때 발생합니다. ctx.cron.schedule()로 작업을 예약합니다.

hooks: {
  "cron": async (event, ctx) => {
    if (event.name === "daily-sync") {
      const data = await ctx.http?.fetch("https://api.example.com/data");
      ctx.log.info("Sync complete");
    }
  },
}

Event

interface CronEvent {
	name: string;
	data?: Record<string, unknown>;
	scheduledAt: string;
}

이메일 훅

이메일 훅은 순서대로 실행됩니다: email:beforeSend, 그 다음 email:deliver, 그 다음 email:afterSend.

email:beforeSend

권한: hooks.email-events:register

전달 전에 실행되는 미들웨어 훅입니다. 메시지를 변환하거나 전달을 취소합니다.

hooks: {
  "email:beforeSend": async (event, ctx) => {
    // Add footer to all emails
    return {
      ...event.message,
      text: event.message.text + "\n\n—Sent from My Site",
    };

    // Or return false to cancel delivery
  },
}

Event

interface EmailBeforeSendEvent {
	message: { to: string; subject: string; text: string; html?: string };
	source: string;
}

반환 값

  • 수정된 메시지를 반환하여 변환
  • false를 반환하여 전달 취소
  • void를 반환하여 변경 없이 통과

email:deliver

권한: hooks.email-transport:register | 독점:

전송 제공자입니다. 하나의 플러그인만 이메일을 전달할 수 있습니다. 이메일 서비스를 통해 실제로 메시지를 보내는 책임이 있습니다.

hooks: {
  "email:deliver": {
    exclusive: true,
    handler: async (event, ctx) => {
      await sendViaSES(event.message);
    },
  },
}

email:afterSend

권한: hooks.email-events:register

전송 성공 후의 fire-and-forget 훅입니다. 오류는 기록되지만 전파되지 않습니다.

hooks: {
  "email:afterSend": async (event, ctx) => {
    await ctx.kv.set(`email:log:${Date.now()}`, {
      to: event.message.to,
      subject: event.message.subject,
    });
  },
}

댓글 훅

댓글 훅은 순서대로 실행됩니다: comment:beforeCreate, 그 다음 comment:moderate, 그 다음 comment:afterCreate. comment:afterModerate 훅은 관리자가 댓글 상태를 변경할 때 별도로 발생합니다.

comment:beforeCreate

권한: users:read

댓글 저장 전의 미들웨어 훅입니다. 댓글을 강화, 검증 또는 거부합니다.

hooks: {
  "comment:beforeCreate": async (event, ctx) => {
    // Reject comments with links
    if (event.comment.body.includes("http")) {
      return false;
    }
  },
}

Event

interface CommentBeforeCreateEvent {
	comment: {
		collection: string;
		contentId: string;
		parentId: string | null;
		authorName: string;
		authorEmail: string;
		authorUserId: string | null;
		body: string;
		ipHash: string | null;
		userAgent: string | null;
	};
	metadata: Record<string, unknown>;
}

반환 값

  • 수정된 이벤트를 반환하여 변환
  • false를 반환하여 거부
  • void를 반환하여 통과

comment:moderate

권한: users:read | 독점:

댓글이 승인됨, 보류 중 또는 스팸인지 결정합니다. 하나의 중재 제공자만 활성화됩니다.

hooks: {
  "comment:moderate": {
    exclusive: true,
    handler: async (event, ctx) => {
      const score = await checkSpam(event.comment);
      return {
        status: score > 0.8 ? "spam" : score > 0.5 ? "pending" : "approved",
        reason: `Spam score: ${score}`,
      };
    },
  },
}

Event

interface CommentModerateEvent {
	comment: { /* same as beforeCreate */ };
	metadata: Record<string, unknown>;
	collectionSettings: {
		commentsEnabled: boolean;
		commentsModeration: "all" | "first_time" | "none";
		commentsClosedAfterDays: number;
		commentsAutoApproveUsers: boolean;
	};
	priorApprovedCount: number;
}

반환 값

{ status: "approved" | "pending" | "spam"; reason?: string }

comment:afterCreate

권한: users:read

댓글 저장 후의 fire-and-forget 훅입니다. 알림에 사용합니다.

hooks: {
  "comment:afterCreate": async (event, ctx) => {
    if (event.comment.status === "approved") {
      await ctx.email?.send({
        to: event.contentAuthor?.email,
        subject: `New comment on "${event.content.title}"`,
        text: `${event.comment.authorName} commented: ${event.comment.body}`,
      });
    }
  },
}

comment:afterModerate

권한: users:read

관리자가 댓글 상태를 수동으로 변경할 때의 fire-and-forget 훅입니다.

Event

interface CommentAfterModerateEvent {
	comment: { id: string; /* ... */ };
	previousStatus: string;
	newStatus: string;
	moderator: { id: string; name: string | null };
}

페이지 훅

페이지 훅은 공개 페이지를 렌더링할 때 실행됩니다. 플러그인이 메타데이터와 스크립트를 주입할 수 있습니다.

page:metadata

권한: 필요 없음

메타 태그, Open Graph 속성, JSON-LD 구조화된 데이터 또는 링크 태그를 페이지 헤드에 기여합니다.

hooks: {
  "page:metadata": async (event, ctx) => {
    return [
      { kind: "meta", name: "generator", content: "EmDash" },
      { kind: "property", property: "og:site_name", content: event.page.siteName },
      { kind: "jsonld", graph: { "@type": "WebSite", name: event.page.siteName } },
    ];
  },
}

기여 유형

type PageMetadataContribution =
	| { kind: "meta"; name: string; content: string; key?: string }
	| { kind: "property"; property: string; content: string; key?: string }
	| {
			kind: "link";
			rel: "canonical" | "alternate" | "author" | "license" | "nlweb" | "site.standard.document";
			href: string;
			hreflang?: string;
			key?: string;
	  }
	| {
			kind: "jsonld";
			id?: string;
			graph: Record<string, unknown> | Array<Record<string, unknown>>;
	  };

key 필드는 기여를 중복 제거합니다 — 주어진 키를 가진 마지막 기여만 사용됩니다.

page:fragments

권한: hooks.page-fragments:register

페이지에 스크립트 또는 HTML을 주입합니다. 네이티브 플러그인에만 사용 가능합니다.

hooks: {
  "page:fragments": async (event, ctx) => {
    return [
      {
        kind: "external-script",
        placement: "body:end",
        src: "https://analytics.example.com/script.js",
        async: true,
      },
      {
        kind: "inline-script",
        placement: "head",
        code: `window.siteId = "abc123";`,
      },
    ];
  },
}

기여 유형

type PageFragmentContribution =
	| {
			kind: "external-script";
			placement: "head" | "body:start" | "body:end";
			src: string;
			async?: boolean;
			defer?: boolean;
			attributes?: Record<string, string>;
			key?: string;
		}
	| {
			kind: "inline-script";
			placement: "head" | "body:start" | "body:end";
			code: string;
			attributes?: Record<string, string>;
			key?: string;
		}
	| {
			kind: "html";
			placement: "head" | "body:start" | "body:end";
			html: string;
			key?: string;
		};

훅 구성

훅은 핸들러 함수 또는 구성 객체를 허용합니다.

hooks: {
  // Simple handler
  "content:afterSave": async (event, ctx) => { ... },

  // With configuration
  "content:beforeSave": {
    priority: 50,        // Lower runs first (default: 100)
    timeout: 10000,      // Max execution time in ms (default: 5000)
    dependencies: [],    // Run after these plugins
    errorPolicy: "abort", // "continue" or "abort" (default)
    handler: async (event, ctx) => { ... },
  },
}

구성 옵션

옵션유형기본값설명
prioritynumber100실행 순서(낮을수록 먼저 실행)
timeoutnumber5000최대 실행 시간(밀리초)
dependenciesstring[][]먼저 실행해야 하는 플러그인 ID
errorPolicystring"abort"오류를 무시하려면 "continue"
exclusivebooleanfalse하나의 플러그인만 활성 제공자가 될 수 있음(email:deliver, comment:moderate와 같은 제공자 패턴 훅용)

플러그인 컨텍스트

모든 훅은 플러그인 API에 대한 액세스 권한이 있는 컨텍스트 객체를 받습니다.

interface PluginContext {
	plugin: { id: string; version: string };
	storage: PluginStorage;
	kv: KVAccess;
	content?: ContentAccess;
	media?: MediaAccess;
	http?: HttpAccess;
	log: LogAccess;
	site: { name: string; url: string; locale: string };
	url(path: string): string;
	users?: UserAccess;
	cron?: CronAccess;
	email?: EmailAccess;
}

권한 요구 사항 및 메서드 세부 정보는 플러그인 개요 — 플러그인 컨텍스트를 참조하세요.

오류 처리

훅의 오류는 기록되고 errorPolicy에 따라 처리됩니다.

  • "abort"(기본값) — 실행 중지, 해당하는 경우 트랜잭션 롤백
  • "continue" — 오류 기록하고 다음 훅으로 계속
hooks: {
  "content:beforeSave": {
    errorPolicy: "continue", // Don't block save if this fails
    handler: async (event, ctx) => {
      try {
        await ctx.http?.fetch("https://api.example.com/validate");
      } catch (error) {
        ctx.log.warn("Validation service unavailable", error);
      }
    },
  },
}

실행 순서

훅은 다음 순서로 실행됩니다.

  1. priority로 정렬(오름차순)
  2. dependencies가 있는 플러그인은 종속성 이후에 실행
  3. 동일한 우선순위 내에서 순서는 결정적이지만 지정되지 않음
// This runs first (priority 10)
{ priority: 10, handler: ... }

// This runs second (priority 50)
{ priority: 50, handler: ... }

// This runs last (default priority 100)
{ handler: ... }