@frigear-nu/通知
一般通知驱动/发送 Nuxt 模块 。 让功能模块(例如:. @frigear-nu/users(中文(简体) )
发送电子邮件/短信/简便通知,而从未决定其状况
交付 - 该选择(和配置)属于应用/消耗层.
快速设置
其他模块声明 @frigear-nu/notify 作为 moduleDependency,所以安装了
无论在哪里使用,你都不必加入 modules 除非你们
想要配置它.
配置
通过 notify 键入 nuxt.config.ts。每一种通知类型(email, (中文(简体) ),
sms, (中文(简体) ), simple) 内置默认值 log 驱动程序( 日志而不是发送) :
export default defineNuxtConfig({
notify: {
email: {
// Name of a built-in driver (`'log'`, the default - logs instead of sending) or a path to
// a function (default export) matching the `EmailDriver` signature, for a real provider
// (Resend, SMTP, ...).
driver: '~/server/utils/resend-email-driver',
},
sms: {
// Same idea, matching the `SMSDriver` signature, for a real provider (Twilio, ...).
driver: '~/server/utils/twilio-sms-driver',
},
simple: {
// Same idea, matching the `SimpleDriver` signature, for generic push notifications.
driver: 'log',
},
},
})
只有每个通知类型的已配置驱动程序才能解析/装入 .
苹果土司不属于这个模块:呼叫 useToast() 直接从 Nuxt UI.
示例自定义驱动程序
// server/utils/resend-email-driver.ts
import type { EmailDriver } from '@frigear-nu/notify/types'
const sendWithResend: EmailDriver = async (notification) => {
// ... call your provider's API with notification.to/subject/body/text
}
export default sendWithResend
使用量
服务器端发送
dispatchEmailNotification, (中文(简体) ), dispatchSMSNotification,以及 dispatchSimpleNotification 为:
安装此模块的自动导入服务器侧 :
await dispatchEmailNotification({
to: user.email,
subject: 'Welcome!',
body: '<p>Thanks for signing up.</p>',
text: 'Thanks for signing up.',
})
await dispatchSMSNotification({
to: user.phone,
body: 'Your verification code is 123456.',
})
await dispatchSimpleNotification({
title: 'New signup',
text: `${user.email} just signed up.`,
})
内建的电子邮件驱动 : resend, (中文(简体) ), ses, (中文(简体) ), cloudflare
环境变量以外的零依赖性和零配置 - 每个驱动程序用平面向提供者说话 fetch
(SES请求通过Web Crypto与AWS SigV4签署). 驾驶员及其证书在 运行时间这样
可以不重建每个环境而切换提供者 :
NUXT_NOTIFY_EMAIL_DRIVER=resend # log (default) | resend | ses | cloudflare
NUXT_NOTIFY_EMAIL_FROM="Frigear <[email protected]>"
# resend
NUXT_NOTIFY_EMAIL_RESEND_API_TOKEN=...
# ses (v2 API)
NUXT_NOTIFY_EMAIL_SES_REGION=eu-west-1
NUXT_NOTIFY_EMAIL_SES_ACCESS_KEY_ID=...
NUXT_NOTIFY_EMAIL_SES_SECRET_ACCESS_KEY=...
NUXT_NOTIFY_EMAIL_SES_SESSION_TOKEN=... # optional, for temporary credentials
# cloudflare (Email Service)
NUXT_NOTIFY_EMAIL_CLOUDFLARE_ACCOUNT_ID=...
NUXT_NOTIFY_EMAIL_CLOUDFLARE_API_TOKEN=...
设置 notify.email.driver 内 nuxt.config.ts 改为其中之一
赢了 缺少证书的发送失败, 并错误指定要设置的变量 。 任何其他 driver 值不变
作为通向习惯的途径 EmailDriver。 。 。 .