notification-service-sdk
v1.0.0
Published
A Node.js notification service SDK supporting email and SMS providers
Maintainers
Readme
Notification Service SDK
一个用于发送通知的Node.js SDK,支持多种通知渠道,包括电子邮件和短信服务。
特性
- 支持多种通知渠道
- 电子邮件 (使用Nodemailer)
- 阿里云短信服务
- 火山引擎短信服务
- 华为云短信服务
- TypeScript支持
- 简单易用的API
- 错误处理和日志记录
- 可扩展的架构
安装
npm install notification-service-sdk或者使用yarn:
yarn add notification-service-sdk使用示例
发送电子邮件
import { NotificationService, EmailConfig } from 'notification-service-sdk';
// 配置邮件服务
const emailConfig: EmailConfig = {
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'your-password'
}
};
// 创建邮件提供者
const emailProvider = NotificationService.createEmailProvider(emailConfig);
// 发送邮件
async function sendEmail() {
const result = await emailProvider.send({
to: '[email protected]',
subject: '测试邮件',
text: '这是一封测试邮件',
html: '<p>这是一封测试邮件</p>'
});
if (result.success) {
console.log('邮件发送成功:', result.messageId);
} else {
console.error('邮件发送失败:', result.error);
}
}
sendEmail();发送阿里云短信
import { NotificationService, AliSmsConfig } from 'notification-service-sdk';
// 配置阿里云短信服务
const aliSmsConfig: AliSmsConfig = {
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
signName: 'your-sign-name'
};
// 创建阿里云短信提供者
const aliSmsProvider = NotificationService.createAliSmsProvider(aliSmsConfig);
// 发送短信
async function sendSms() {
const result = await aliSmsProvider.send({
phoneNumbers: '13800138000',
templateId: 'SMS_123456789',
templateParams: {
code: '123456'
}
});
if (result.success) {
console.log('短信发送成功:', result.messageId);
} else {
console.error('短信发送失败:', result.error);
}
}
sendSms();发送火山引擎短信
import { NotificationService, VolcSmsConfig } from 'notification-service-sdk';
// 配置火山引擎短信服务
const volcSmsConfig: VolcSmsConfig = {
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
signName: 'your-sign-name',
region: 'cn-north-1'
};
// 创建火山引擎短信提供者
const volcSmsProvider = NotificationService.createVolcSmsProvider(volcSmsConfig);
// 发送短信
async function sendSms() {
const result = await volcSmsProvider.send({
phoneNumbers: '13800138000',
templateId: 'SMS_123456789',
templateParams: {
code: '123456'
}
});
if (result.success) {
console.log('短信发送成功:', result.messageId);
} else {
console.error('短信发送失败:', result.error);
}
}
sendSms();发送华为云短信
import { NotificationService, HuaweiSmsConfig } from 'notification-service-sdk';
// 配置华为云短信服务
const huaweiSmsConfig: HuaweiSmsConfig = {
endpoint: 'https://sms-api.cn-north-4.myhuaweicloud.com',
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
signName: 'your-sign-name',
sender: 'your-sender-number'
};
// 创建华为云短信提供者
const huaweiSmsProvider = NotificationService.createHuaweiSmsProvider(huaweiSmsConfig);
// 发送短信
async function sendSms() {
const result = await huaweiSmsProvider.send({
phoneNumbers: '13800138000',
templateId: 'SMS_123456789',
templateParams: {
code: '123456'
}
});
if (result.success) {
console.log('短信发送成功:', result.messageId);
} else {
console.error('短信发送失败:', result.error);
}
}
sendSms();API文档
NotificationService
静态工厂类,用于创建不同的通知提供者。
方法
createEmailProvider(config: EmailConfig): EmailProvider- 创建邮件通知提供者createAliSmsProvider(config: AliSmsConfig): AliSmsProvider- 创建阿里云短信通知提供者createVolcSmsProvider(config: VolcSmsConfig): VolcSmsProvider- 创建火山引擎短信通知提供者createHuaweiSmsProvider(config: HuaweiSmsConfig): HuaweiSmsProvider- 创建华为云短信通知提供者
配置接口
EmailConfig
interface EmailConfig {
host: string;
port: number;
secure?: boolean;
auth: {
user: string;
pass: string;
};
}AliSmsConfig
interface AliSmsConfig {
accessKeyId: string;
accessKeySecret: string;
signName: string;
}VolcSmsConfig
interface VolcSmsConfig {
accessKeyId: string;
accessKeySecret: string;
signName: string;
region?: string;
}HuaweiSmsConfig
interface HuaweiSmsConfig {
endpoint: string;
accessKeyId: string;
accessKeySecret: string;
signName: string;
sender: string;
}消息接口
EmailMessage
interface EmailMessage {
from?: string;
to: string | string[];
subject: string;
text?: string;
html?: string;
}BaseSmsMessage
interface BaseSmsMessage {
phoneNumbers: string | string[];
templateId: string;
templateParams?: Record<string, string>;
}结果接口
SendResult
interface SendResult {
success: boolean;
messageId?: string;
error?: Error;
}许可证
MIT
