Skip to content
You're viewing documentation for v0.6.1. It may be out of date.Go to latest
Email SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

Writing plugins

Build an EmailPlugin — choose hooks or middleware, register adapters, and add typed client extensions.

A plugin is a plain object: a stable id plus any combination of adapters, hooks, middleware, and extendClient. No base class, no registration ceremony — export a factory function that returns the object.

This page covers each capability and when to reach for it. For a guided build of a complete policy plugin, see Create your first plugin.

Hooks or middleware?

Both watch the send pipeline; only middleware can change it. Pick by intent:

You want to… Use Why
Add defaults or rewrite the message middleware.beforeSend Runs once per send, before validation; its return value replaces message/options.
Block a send (policy, guardrails) middleware.beforeSend Thrown errors propagate and stop the send — hooks errors are swallowed.
Log, count, or trace attempts hooks Per-attempt events including onRetry; failures never mask the send.
React to the final outcome middleware.afterSend / onError Fire on success and on per-route failure; observe-only, errors swallowed.

beforeSend can also transform: return { message?, options? } and the pipeline continues with your values. The returned message replaces the message wholesale; returned options shallow-merge over the current send options. The defaults plugin is exactly this pattern.

Plugin hooks use the same shape as the client hooks option and run before client hooks — see Hooks for the event fields.

Register adapters

Plugins can ship providers, which is how community adapter packages offer one-call setup:

For dynamic registration, adapters can be a function receiving an EmailPluginContext — inspect already-registered routes via ctx.adapters, read ctx.defaultAdapter, and call ctx.addAdapter():

Two rules, both enforced with an EmailValidationError: adapter factories must be synchronous (createEmailClient is sync — returning a Promise throws), and duplicate adapter names throw. Building the adapter itself is its own topic: Create an adapter.

Extend the client

extendClient returns properties merged onto the client. Type the plugin as EmailPlugin<TExtension> and the extension flows into the client type — createEmailClient infers the intersection of all plugin extensions, so callers get autocomplete with no casts:

Extensions run after the client is built. A key that already exists on the client — built-ins like send or adapters, or a key claimed by an earlier plugin — throws: Email plugin "<id>" tried to extend the client with reserved key "<key>". Keep extensions small: a store, a helper, a few readonly values. The capture plugin shows the pattern, including a configurable typed key.

Registration rules

createEmailClient processes plugins in array order and fails fast on conflicts:

  • Duplicate plugin id → EmailValidationError. Factories that can be mounted twice should accept an id option.
  • Duplicate adapter name (across direct adapters and all plugins) → EmailValidationError.
  • Async adapters factory → EmailValidationError.
  • extendClient key collision → EmailValidationError.
  • Plugin hooks run before client hooks; middleware runs in plugin order.

Next

Last updated on