notification-service-sdk
Version:
A Node.js notification service SDK supporting email and SMS providers
277 lines (217 loc) • 5.58 kB
Markdown
# Notification Service SDK
一个用于发送通知的Node.js SDK,支持多种通知渠道,包括电子邮件和短信服务。
## 特性
- 支持多种通知渠道
- 电子邮件 (使用Nodemailer)
- 阿里云短信服务
- 火山引擎短信服务
- 华为云短信服务
- TypeScript支持
- 简单易用的API
- 错误处理和日志记录
- 可扩展的架构
## 安装
```bash
npm install notification-service-sdk
```
或者使用yarn:
```bash
yarn add notification-service-sdk
```
## 使用示例
### 发送电子邮件
```typescript
import { NotificationService, EmailConfig } from 'notification-service-sdk';
// 配置邮件服务
const emailConfig: EmailConfig = {
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: 'your-email@example.com',
pass: 'your-password'
}
};
// 创建邮件提供者
const emailProvider = NotificationService.createEmailProvider(emailConfig);
// 发送邮件
async function sendEmail() {
const result = await emailProvider.send({
to: 'recipient@example.com',
subject: '测试邮件',
text: '这是一封测试邮件',
html: '<p>这是一封测试邮件</p>'
});
if (result.success) {
console.log('邮件发送成功:', result.messageId);
} else {
console.error('邮件发送失败:', result.error);
}
}
sendEmail();
```
### 发送阿里云短信
```typescript
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();
```
### 发送火山引擎短信
```typescript
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();
```
### 发送华为云短信
```typescript
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
```typescript
interface EmailConfig {
host: string;
port: number;
secure?: boolean;
auth: {
user: string;
pass: string;
};
}
```
#### AliSmsConfig
```typescript
interface AliSmsConfig {
accessKeyId: string;
accessKeySecret: string;
signName: string;
}
```
#### VolcSmsConfig
```typescript
interface VolcSmsConfig {
accessKeyId: string;
accessKeySecret: string;
signName: string;
region?: string;
}
```
#### HuaweiSmsConfig
```typescript
interface HuaweiSmsConfig {
endpoint: string;
accessKeyId: string;
accessKeySecret: string;
signName: string;
sender: string;
}
```
### 消息接口
#### EmailMessage
```typescript
interface EmailMessage {
from?: string;
to: string | string[];
subject: string;
text?: string;
html?: string;
}
```
#### BaseSmsMessage
```typescript
interface BaseSmsMessage {
phoneNumbers: string | string[];
templateId: string;
templateParams?: Record<string, string>;
}
```
### 结果接口
#### SendResult
```typescript
interface SendResult {
success: boolean;
messageId?: string;
error?: Error;
}
```
## 许可证
MIT