Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-eels-pick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"emdash": patch
---

Passes emailPipeline to plugin route handler context so plugins with email:send capability can send email from route handlers.
5 changes: 4 additions & 1 deletion packages/core/src/emdash-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,10 @@ export class EmDashRuntime {
// resolution order in getPluginRouteMeta to avoid auth/execution mismatches.
const trustedPlugin = this.configuredPlugins.find((p) => p.id === pluginId);
if (trustedPlugin && this.enabledPlugins.has(trustedPlugin.id)) {
const routeRegistry = new PluginRouteRegistry({ db: this.db });
const routeRegistry = new PluginRouteRegistry({
db: this.db,
emailPipeline: this.email ?? undefined,
});
routeRegistry.register(trustedPlugin);

const routeKey = path.replace(LEADING_SLASH_PATTERN, "");
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/plugins/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { Storage } from "../storage/types.js";
import type { PluginContextFactoryOptions } from "./context.js";
import { setCronTasksEnabled } from "./cron.js";
import { definePlugin } from "./define-plugin.js";
import type { EmailPipeline } from "./email.js";
import {
HookPipeline,
type HookResult,
Expand Down Expand Up @@ -83,6 +84,17 @@ export class PluginManager {
};
}

/**
* Set the email pipeline used when creating plugin contexts.
* Reinitializes routes/hooks if already initialized so ctx.email is available immediately.
*/
setEmailPipeline(pipeline: EmailPipeline): void {
this.factoryOptions.emailPipeline = pipeline;
if (this.initialized) {
this.reinitialize();
}
}

// =========================================================================
// Plugin Registration
// =========================================================================
Expand Down
32 changes: 32 additions & 0 deletions packages/core/tests/unit/plugins/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { describe, it, expect, vi } from "vitest";
import { z } from "zod";

import type { PluginContextFactoryOptions } from "../../../src/plugins/context.js";
import { EmailPipeline } from "../../../src/plugins/email.js";
import { HookPipeline } from "../../../src/plugins/hooks.js";
import {
PluginRouteHandler,
PluginRouteRegistry,
Expand Down Expand Up @@ -301,6 +303,36 @@ describe("PluginRouteHandler", () => {
expect(result.error?.code).toBe("INTERNAL_ERROR");
expect(result.error?.message).toContain("Unexpected error");
});

it("includes ctx.email when email pipeline is configured", async () => {
const hookPipeline = new HookPipeline([], createMockFactoryOptions());
hookPipeline.setExclusiveSelection("email:deliver", "provider");
const emailPipeline = new EmailPipeline(hookPipeline);

const plugin = createTestPlugin({
capabilities: ["email:send"],
routes: {
checkEmail: {
handler: async (ctx) => ({
hasEmail: !!ctx.email,
hasSend: typeof ctx.email?.send === "function",
}),
},
},
});

const handler = new PluginRouteHandler(plugin, {
...createMockFactoryOptions(),
emailPipeline,
});

const result = await handler.invoke("checkEmail", {
request: new Request("http://test.com"),
});

expect(result.success).toBe(true);
expect(result.data).toEqual({ hasEmail: true, hasSend: true });
});
});
});

Expand Down
Loading