@krizaka/notifications

中文文档
0.2.3发布于 2026/11/01Apache-2.0TypeScriptES Module构建来源已验证

The bell, channels and preferences for every Krizaka product: one notify() writes the in-app item and appends one outbox message per enabled channel (e-mail, push per device, live), respecting the account's matrix of categories by channels and per-categor

原始使用文档

@krizaka/notifications

The bell, the channels and the preferences for every Krizaka product. One notify writes the in-app item and appends one outbox message per enabled channel, respecting the account's matrix and a burst cap per category. No push or realtime import (the workers that deliver a channel are the product's), no database client, no wall clock.

  • Channels. app (the bell and the live event of the open pages, one switch), push (one message per registered device) and email. The outbox topics are notifications.live, notifications.push and notifications.email; the event is evt.notification.requested.v1, the name and the shape of Java's krizaka-notifications-api request (channel, recipient, template, locale, variables, plus the ids; channel is EMAIL, and PUSH / LIVE for the web). A product delivers e-mail with its own worker (@krizaka/email to render, @krizaka/mail to queue) or lets the Java service consume it.
  • The product declares its categories and their defaults (notificationSettings(declared) is the registry fragment); the admin tunes the default channels and the caps without a redeploy. A locked category (security, the account) cannot be switched off by anyone.
  • Burst caps. After burstMax notifications of one category to one account within burstWindowSeconds, the next ones are bell-only: the item is kept, no channel is used. The count and the insert run under a transaction-level advisory lock per account and category, so the cap holds under concurrency when notify gets the caller's tx.
  • Idempotent. dedupeKey makes one fact one notification; the outbox message ids derive from the item, so a retried append is a no-op.
  • The bell. list (newest first, cursor), unreadCount, markRead (the account's own items; the first read_at stays). An item whose app channel was off is kept for the history and the cap, never shown.
  • Preferences. preferences (every category by channel with its source: default, account, locked), setPreference, setMatrix (a preset applied).
  • Devices. registerDevice (a token belongs to one account; registering it again moves it), revokeDevice when the push service refuses it.
  • Privacy. The module holds no address (the product passes email per call); erase(accountId) removes the items, choices and devices; purge applies the retention.
import { createOutbox } from "@krizaka/outbox";
import { createNotifications, dbNotificationStore } from "@krizaka/notifications";
import { notificationSettings, readNotificationConfig } from "@krizaka/notifications/settings";

const declared = {
  messages: { defaults: { app: true, push: true, email: true }, burst: { max: 5, windowSeconds: 3600 } },
  follows: { defaults: { app: true, push: false, email: false } },
  account: { locked: true, defaults: { app: true, push: true, email: true } },
};
// settingsRegistry = { ...otherSettings, ...notificationSettings(declared) }

const notifications = createNotifications({
  clock,
  outbox: createOutbox({ clock, settings: outboxSettings }),
  config: () => readNotificationConfig(declared, (key) => settings.get(key)),
});

// in the use case's transaction
await notifications.notify(tx, { accountId, category: "messages", template: "new-message", variables: { name }, locale: "fr", email, dedupeKey: `message:${messageId}` });

Tables (schema notifications, notificationsSchema): item, preference, device. notificationStoreContract (@krizaka/notifications/testkit) is the contract every store runs.

Apache-2.0.

版本与发布记录

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

版本发布日期距今
0.2.3latest2026/11/011 天前
0.2.22026/10/304 天前
0.2.12026/10/295 天前
0.2.02026/10/277 天前
0.0.0-stage2026/10/277 天前

关于 @krizaka/notifications 的常见问题

@krizaka/notifications 是什么?

每个Krizaka产品的铃声、渠道和偏好:一个notify()会写入应用内项目,并为每个启用的渠道(电子邮件、每个设备推送、实时)添加一个发件箱消息,尊重账户按渠道和类别的分类矩阵

如何安装 @krizaka/notifications?

在项目目录运行 npm install @krizaka/notifications,或使用 pnpm add / yarn add / bun add。

@krizaka/notifications 支持 TypeScript 吗?

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

@krizaka/notifications 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@krizaka/notifications 需要什么版本的 Node.js?

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

@krizaka/notifications 可以商用吗?

@krizaka/notifications 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@krizaka/notifications 还在维护吗?

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