@btravstack/mailer

中文文档
0.58.0发布于 2026/11/08MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 6067

A Mailer port for @btravstack/core: a recording adapter a spec asserts against, an SMTP adapter, and a composition that spans, counts and logs every send

原始使用文档
文档目录4 节
  1. A worked example
  2. Options
  3. What it decides, and what it does not
  4. License

@btravstack/mailer

The mail port for @btravstack/core: one Mailer an application depends on, a recording adapter a spec asserts against, an SMTP adapter, and a span, a count and a log line per send.

📖 Documentation · API Reference

pnpm add @btravstack/mailer @btravstack/core @btravstack/config @btravstack/di unthrown

Four peer dependencies, plus nodemailer — optional, and needed only if you compose the SMTP adapter. Instrumentation needs no peer of its own: the Logger, Tracer and Meter it depends on are the kernel's ports. Node >=22.

A worked example

A notification, and the failure arm that makes it honest:

import { Mailer } from "@btravstack/mailer";
import { Port, Provider } from "@btravstack/di";
import type { AsyncResult } from "unthrown";

class Notifier extends Port("ReadmeNotifier")<{
  readonly placed: (
    to: string,
    orderId: string,
  ) => AsyncResult<void, RetryableError>;
}> {}

export const notifier = Provider(Notifier)({
  inject: { mailer: Mailer },
  sync: ({ mailer }) => ({
    placed: (to, orderId) =>
      mailer
        .send({
          from: "[email protected]",
          to: [to],
          subject: `order ${orderId} placed`,
          text: `Order ${orderId} is on its way.`,
        })
        // A failed send is YOUR transport's problem, which is why the port
        // models it instead of retrying: leaving the delivery un-acked
        // hands it to the next worker, on the broker's own budget.
        .mapErrCases((matcher) =>
          matcher.with(
            P.tag("MailNotSent"),
            (error) =>
              new RetryableError(
                `order ${orderId} was not notified: ${error.reason}`,
              ),
          ),
        ),
  }),
});

The composition — the adapter is the only decision, made here and nowhere else:

import { mailer } from "@btravstack/mailer";
import { smtpMailer } from "@btravstack/mailer/smtp";

export const NotifierApp = Module("NotifierApp")({
  imports: [
    // Instrumented by default: a span, a counter and a log line per send,
    // which is why `observability()` and `otel()` are below. Pass
    // Every call is reported through `Observers`. Drop the two modules
    // below and this composition is unchanged — it just reports to nobody.
    mailer({ adapter: smtpMailer() }),
    observability(),
    otel(),
  ],
  provides: [notifier],
  exports: [Notifier, Logger],
});

And in a test, the adapter that sends nothing and keeps everything:

import { mailRecorder, recordingMailerProvider } from "@btravstack/mailer";
import { overridden } from "@btravstack/testing";

const recorder = mailRecorder();
const app = overridden(NotifierApp, [recordingMailerProvider(recorder)]);
// … place an order through the real graph …
expect(recorder.only()).toEqual(
  expect.objectContaining({ subject: "order 42 placed" }),
);

Options

OptionWhereWhat it is
adaptermailer({ adapter })the adapter module providing MailerBackend — required
SMTP_URLenvironment, read by smtpMailer()the transport URL — required, validated at graph build

The full table — defaults, semantics and the reasoning — lives on the reference page, which is this list's one detailed home.

What it decides, and what it does not

It decides that every send is handed to whatever contributed to Observers — there is no flag and no opt-out, because a graph that composed no observability pays one inert call and reports to nobody. It decides that the SMTP adapter contributes a health check — verify(), a connection and an authentication, folded into the kernel's /healthz with nothing wired, while the recording adapter contributes none because it would report healthy for free. And it decides that a send is accepted, not delivered, and that a failure is a modeled MailNotSent carrying the envelope and never the body.

It does not decide what happens after a failure — retrying belongs to your transport, and the example answers a RetryableError so the broker's budget owns it. Mail carries the whole envelope — cc, bcc, attachments and headers beside to, subject and the two bodies — but there is no templating, no bulk send and no address validation — each reason is on the reference page, and the same reasoning is in AGENTS.md for contributors.

License

MIT

版本与发布记录

@btravstack/mailer 共发布 62 个版本,首次发布于 2026/08/26;过去 12 个月发布了 62 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.58.0latest2026/11/08今天
0.57.02026/11/071 天前
0.56.12026/11/062 天前
0.56.02026/11/062 天前
0.55.12026/11/035 天前
0.55.02026/11/035 天前

关于 @btravstack/mailer 的常见问题

@btravstack/mailer 是什么?

A Mailer port for @btravstack/core: a recording adapter a spec asserts against, an SMTP adapter, and a composition that spans, counts and logs every send

如何安装 @btravstack/mailer?

在项目目录运行 npm install @btravstack/mailer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:unthrown(^5.0.0)、@btravstack/di(^0.58.0)、@btravstack/core(^0.58.0)、@btravstack/config(^0.58.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@btravstack/mailer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@btravstack/mailer 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@btravstack/mailer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@btravstack/mailer 可以商用吗?

@btravstack/mailer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@btravstack/mailer 的下载量有多少?

根据 npm 官方下载统计,@btravstack/mailer 在 2026/10/09 – 2026/11/07 这 30 天内被下载 6,067 次,日均约 202 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@btravstack/mailer 还在维护吗?

最新版本 0.58.0 发布于 2026/11/08(今天);至今共发布 62 个版本,其中过去 12 个月发布了 62 个;首次发布于 2026/08/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。