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

Plugin API

Type reference for EmailPlugin, EmailPluginContext, EmailSendMiddleware events, and typed client extensions.

Reference for the plugin surface. For patterns and guidance, start with Writing plugins.

EmailPlugin

PropType
idstring

Stable plugin id. A duplicate id in the plugins array throws EmailValidationError.

Typestring
adapters?EmailProvider[] | ((ctx) => EmailProvider[])

Providers to register, as an array or a synchronous factory. An async factory or duplicate adapter name throws EmailValidationError.

TypeEmailProvider[] | ((ctx) => EmailProvider[])
hooks?EmailHooks

Per-attempt observability callbacks (beforeSend, afterSend, onError, onRetry). Run before client hooks; thrown errors are swallowed.

TypeEmailHooks
middleware?EmailSendMiddleware[]

Send-pipeline middleware. Only plugins can register middleware.

TypeEmailSendMiddleware[]
extendClient?(ctx: EmailPluginContext) => TExtension

Returns properties merged onto the client. Colliding with an existing client key throws EmailValidationError.

Type(ctx: EmailPluginContext) => TExtension

createEmailClient is synchronous: it registers direct adapters first, then processes plugins in array order, then applies extendClient extensions after the client object is built.

EmailPluginContext

Passed to adapters factories and extendClient.

PropType
adapters?ReadonlyMap<string, EmailProvider>

Live view of registered adapters at the time the plugin runs.

TypeReadonlyMap<string, EmailProvider>
defaultAdapter?string

The requested default adapter name.

Typestring
addAdapter?(adapter: EmailProvider) => void

Registers an adapter. Duplicate names throw EmailValidationError.

Type(adapter: EmailProvider) => void

If an adapters factory both calls ctx.addAdapter(adapter) and returns the same adapter object, it registers once.

EmailSendMiddleware

Callback Runs Errors
beforeSend Once per send, before validation and adapter routing. Stop the send
afterSend After a provider returns a normalized response. Swallowed
onError After an adapter route fails its last retry, before any fallback. Swallowed

beforeSend

No provider field — routing has not happened yet. Returning a result transforms the send: message replaces the message wholesale, options shallow-merges over the current send options. Return nothing to observe or to throw for policy. Middleware runs in plugin order, each seeing the previous one’s result.

afterSend and onError

Both extend the hook event shape:

After onError, fallback adapters may still run — it signals a failed route, not necessarily a failed send.

Typed client extensions

EmailPlugin<TExtension> carries the extension type, and createEmailClient infers the result from the plugins array:

Helper types behind the inference:

PropType
EmailPluginClientExtension<TPlugin>?object

Extracts TExtension from a single EmailPlugin<TExtension>.

Typeobject
EmailPluginClientExtensions<TPlugins>?object

Intersection of all extensions in a readonly plugins tuple — the type argument of the returned EmailClient.

Typeobject

Multiple extending plugins intersect: the client gains every extension, and a runtime key collision throws EmailValidationError.

Registration errors

All thrown by createEmailClient as EmailValidationError:

Condition Message
Duplicate plugin id Duplicate email plugin "<id>".
Duplicate adapter name Duplicate email adapter "<name>".
Async adapters factory Email plugin "<id>" returned async adapters. createEmailClient requires synchronous plugin adapters.
Extension key collision Email plugin "<id>" tried to extend the client with reserved key "<key>".

Last updated on