@z_06/relay-temp-mail 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +357 -226
- package/README.zh-CN.md +357 -195
- package/dist/index.cjs +190 -108
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +79 -4
- package/dist/index.d.ts +79 -4
- package/dist/index.js +188 -108
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.zh-CN.md
CHANGED
|
@@ -1,195 +1,357 @@
|
|
|
1
|
-
# relay-temp-mail
|
|
2
|
-
|
|
3
|
-
[English](README.md) | [中文](README.zh-CN.md)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
1
|
+
# relay-temp-mail
|
|
2
|
+
|
|
3
|
+
[English](README.md) | [中文](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
一个模块化的 TypeScript/JavaScript 包,通过可插拔的 Provider 管理邮箱别名和接收临时邮件。
|
|
6
|
+
|
|
7
|
+
基于 Provider 架构 — 自由组合**别名提供商**与**邮件提供商**。当前内置 Firefox Relay、DuckDuckGo 邮件保护和 CloudFlare 临时邮箱支持。
|
|
8
|
+
|
|
9
|
+
## 功能特性
|
|
10
|
+
|
|
11
|
+
- **Provider 架构** — 自由组合别名与邮件提供商
|
|
12
|
+
- **Firefox Relay** — 创建、列出、删除邮箱别名
|
|
13
|
+
- **DuckDuckGo 邮件保护** — 创建邮箱别名,支持本地存储
|
|
14
|
+
- **CloudFlare 临时邮箱** — 通过 API 获取并解析邮件
|
|
15
|
+
- **TypeScript 支持** — 所有 API 均有完整类型定义,包括 Provider 接口
|
|
16
|
+
- **ESM + CommonJS 支持** — 兼容两种模块系统
|
|
17
|
+
- **可扩展** — 实现 `AliasProvider` 或 `MailProvider` 即可接入新服务
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @z_06/relay-temp-mail
|
|
23
|
+
# 或
|
|
24
|
+
pnpm add @z_06/relay-temp-mail
|
|
25
|
+
# 或
|
|
26
|
+
bun add @z_06/relay-temp-mail
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 快速开始
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { TempMailClient } from '@z_06/relay-temp-mail';
|
|
33
|
+
|
|
34
|
+
const client = new TempMailClient({
|
|
35
|
+
aliasProvider: {
|
|
36
|
+
type: 'firefox-relay',
|
|
37
|
+
csrfToken: 'your-csrf-token',
|
|
38
|
+
sessionId: 'your-session-id',
|
|
39
|
+
},
|
|
40
|
+
mailProvider: {
|
|
41
|
+
type: 'cf-temp-mail',
|
|
42
|
+
apiUrl: 'https://your-cf-api.com',
|
|
43
|
+
token: 'your-cf-token',
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// 创建新别名
|
|
48
|
+
const alias = await client.createAlias();
|
|
49
|
+
console.log('新别名:', alias.fullAddress);
|
|
50
|
+
|
|
51
|
+
// 列出所有别名
|
|
52
|
+
const aliases = await client.listAliases();
|
|
53
|
+
|
|
54
|
+
// 获取指定别名的邮件
|
|
55
|
+
const emails = await client.getEmails(alias.fullAddress, { limit: 10 });
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Providers
|
|
59
|
+
|
|
60
|
+
本库使用两种可独立组合的 Provider:
|
|
61
|
+
|
|
62
|
+
| Provider 类型 | 接口 | 当前实现 |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| **别名提供商** | `AliasProvider` | `firefox-relay`, `duckduckgo-email` |
|
|
65
|
+
| **邮件提供商** | `MailProvider` | `cf-temp-mail` |
|
|
66
|
+
|
|
67
|
+
### 别名提供商
|
|
68
|
+
|
|
69
|
+
#### `firefox-relay`
|
|
70
|
+
|
|
71
|
+
通过 [Firefox Relay](https://relay.firefox.com) 管理邮箱别名。
|
|
72
|
+
|
|
73
|
+
**配置:**
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
{
|
|
77
|
+
type: 'firefox-relay',
|
|
78
|
+
csrfToken: string; // relay.firefox.com 的 CSRF token
|
|
79
|
+
sessionId: string; // relay.firefox.com 的 Session ID
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**获取 Token:**
|
|
84
|
+
|
|
85
|
+
1. 登录 [relay.firefox.com](https://relay.firefox.com)
|
|
86
|
+
2. 打开浏览器开发者工具(F12)
|
|
87
|
+
3. 切换到 Application/Storage 标签页
|
|
88
|
+
4. 找到 `relay.firefox.com` 的 Cookies
|
|
89
|
+
5. 复制 `csrftoken` 和 `sessionid` 的值
|
|
90
|
+
|
|
91
|
+
#### `duckduckgo-email`
|
|
92
|
+
|
|
93
|
+
通过 [DuckDuckGo 邮件保护](https://duckduckgo.com/email/) 管理邮箱别名。
|
|
94
|
+
|
|
95
|
+
由于 DuckDuckGo API 不提供列出或删除别名的接口,该提供商使用本地存储。内置内存存储;可通过实现 `DuckDuckGoAliasStore` 接口自定义持久化(如文件、数据库)。
|
|
96
|
+
|
|
97
|
+
**配置:**
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
{
|
|
101
|
+
type: 'duckduckgo-email',
|
|
102
|
+
jwtToken: string; // DuckDuckGo 邮件保护的 JWT token
|
|
103
|
+
store?: DuckDuckGoAliasStore; // 可选自定义存储(默认: 内存存储)
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**获取 JWT Token:**
|
|
108
|
+
|
|
109
|
+
1. 访问 [duckduckgo.com/email](https://duckduckgo.com/email/) 并注册账户
|
|
110
|
+
2. 打开浏览器开发者工具(F12)
|
|
111
|
+
3. 在 DuckDuckGo 邮件界面点击"生成新地址"
|
|
112
|
+
4. 在网络请求栏中找到发往 `quack.duckduckgo.com` 的请求
|
|
113
|
+
5. 从 `Authorization` 请求头中复制 Bearer token
|
|
114
|
+
|
|
115
|
+
**自定义持久化:**
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import type { DuckDuckGoAliasStore, RelayAlias } from '@z_06/relay-temp-mail';
|
|
119
|
+
|
|
120
|
+
class MyFileStore implements DuckDuckGoAliasStore {
|
|
121
|
+
getAll(): RelayAlias[] { /* 从文件读取 */ }
|
|
122
|
+
add(alias: RelayAlias): void { /* 追加到文件 */ }
|
|
123
|
+
remove(id: number): void { /* 从文件中删除 */ }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const client = new TempMailClient({
|
|
127
|
+
aliasProvider: {
|
|
128
|
+
type: 'duckduckgo-email',
|
|
129
|
+
jwtToken: 'your-jwt-token',
|
|
130
|
+
store: new MyFileStore(),
|
|
131
|
+
},
|
|
132
|
+
mailProvider: { /* ... */ },
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**重复检测:** DuckDuckGo API 偶尔会返回之前已生成过的地址(仍返回 201)。该提供商会检测这种情况并抛出 `RelayTempMailError`,错误代码为 `DUPLICATE_ALIAS`。
|
|
137
|
+
|
|
138
|
+
### 邮件提供商
|
|
139
|
+
|
|
140
|
+
#### `cf-temp-mail`
|
|
141
|
+
|
|
142
|
+
从 [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email) 实例获取邮件。
|
|
143
|
+
|
|
144
|
+
**配置:**
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
{
|
|
148
|
+
type: 'cf-temp-mail',
|
|
149
|
+
apiUrl: string; // CF 临时邮箱 API 的基础 URL
|
|
150
|
+
token: string; // API Bearer Token
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**部署后端:**
|
|
155
|
+
|
|
156
|
+
1. Fork [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email)
|
|
157
|
+
2. 部署到 Cloudflare Workers(一键部署或参考[部署文档](https://temp-mail-docs.awsl.uk)手动部署)
|
|
158
|
+
3. 在 Cloudflare Dashboard 中配置域名和 Email Routing 的 catch-all 规则
|
|
159
|
+
4. 从管理后台或用户设置中生成 API Token
|
|
160
|
+
|
|
161
|
+
## API 文档
|
|
162
|
+
|
|
163
|
+
### TempMailClient
|
|
164
|
+
|
|
165
|
+
主客户端类。接受 `aliasProvider` 和 `mailProvider` 配置,提供统一接口。
|
|
166
|
+
|
|
167
|
+
#### 构造函数
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
new TempMailClient(config: TempMailConfig)
|
|
171
|
+
|
|
172
|
+
interface TempMailConfig {
|
|
173
|
+
aliasProvider: AliasProviderConfig; // 别名提供商配置(鉴别联合类型)
|
|
174
|
+
mailProvider: MailProviderConfig; // 邮件提供商配置(鉴别联合类型)
|
|
175
|
+
timeout?: number; // 请求超时时间,毫秒(默认: 30000)
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### 方法
|
|
180
|
+
|
|
181
|
+
##### `listAliases()`
|
|
182
|
+
|
|
183
|
+
列出配置的别名提供商中的所有邮箱别名。
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
const aliases = await client.listAliases();
|
|
187
|
+
// 返回: RelayAlias[]
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
##### `createAlias()`
|
|
191
|
+
|
|
192
|
+
通过配置的别名提供商创建新的邮箱别名。
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
const alias = await client.createAlias();
|
|
196
|
+
// 返回: RelayAlias
|
|
197
|
+
console.log(alias.fullAddress); // 例如: "random123@mozmail.com"
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
##### `deleteAlias(id)`
|
|
201
|
+
|
|
202
|
+
根据 ID 删除别名。
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
await client.deleteAlias(12345);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
##### `getEmails(aliasAddress?, options?)`
|
|
209
|
+
|
|
210
|
+
从配置的邮件提供商获取并解析邮件。如果提供了 `aliasAddress`,则只返回发送到该地址的邮件。
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
// 获取所有邮件(默认数量限制)
|
|
214
|
+
const allEmails = await client.getEmails();
|
|
215
|
+
|
|
216
|
+
// 获取指定别名的邮件
|
|
217
|
+
const emails = await client.getEmails('alias@mozmail.com', { limit: 10 });
|
|
218
|
+
|
|
219
|
+
// 分页获取
|
|
220
|
+
const page2 = await client.getEmails('alias@mozmail.com', { limit: 10, offset: 10 });
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
选项:
|
|
224
|
+
|
|
225
|
+
- `limit` - 返回的最大邮件数量(默认: 20)
|
|
226
|
+
- `offset` - 分页偏移量,从 0 开始(默认: 0)
|
|
227
|
+
|
|
228
|
+
## 自定义 Provider
|
|
229
|
+
|
|
230
|
+
实现 `AliasProvider` 或 `MailProvider` 接口即可接入新服务:
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
import type { AliasProvider, RelayAlias } from '@z_06/relay-temp-mail';
|
|
234
|
+
|
|
235
|
+
class MyAliasProvider implements AliasProvider {
|
|
236
|
+
async listAliases(): Promise<RelayAlias[]> { /* ... */ }
|
|
237
|
+
async createAlias(): Promise<RelayAlias> { /* ... */ }
|
|
238
|
+
async deleteAlias(id: number): Promise<void> { /* ... */ }
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
import type { MailProvider, Email } from '@z_06/relay-temp-mail';
|
|
244
|
+
|
|
245
|
+
class MyMailProvider implements MailProvider {
|
|
246
|
+
async getMails(limit: number, offset: number): Promise<Email[]> { /* ... */ }
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## 错误处理
|
|
251
|
+
|
|
252
|
+
本包导出了多个错误类,用于处理不同的失败场景:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import {
|
|
256
|
+
RelayTempMailError,
|
|
257
|
+
NetworkError,
|
|
258
|
+
AuthError,
|
|
259
|
+
NotFoundError,
|
|
260
|
+
ParseError,
|
|
261
|
+
RateLimitError,
|
|
262
|
+
} from '@z_06/relay-temp-mail';
|
|
263
|
+
|
|
264
|
+
try {
|
|
265
|
+
const alias = await client.createAlias();
|
|
266
|
+
} catch (error) {
|
|
267
|
+
if (error instanceof AuthError) {
|
|
268
|
+
console.error('认证失败:', error.message);
|
|
269
|
+
} else if (error instanceof NetworkError) {
|
|
270
|
+
console.error('网络问题:', error.message);
|
|
271
|
+
} else if (error instanceof RateLimitError) {
|
|
272
|
+
console.error('请求过于频繁,请稍后重试:', error.response?.retryAfter);
|
|
273
|
+
} else if (error instanceof RelayTempMailError) {
|
|
274
|
+
console.error('错误:', error.code, error.message);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### 错误类
|
|
280
|
+
|
|
281
|
+
| 类名 | 描述 | 状态码 |
|
|
282
|
+
|------|------|--------|
|
|
283
|
+
| `RelayTempMailError` | 所有包错误的基类 | - |
|
|
284
|
+
| `NetworkError` | 网络连接问题 | - |
|
|
285
|
+
| `AuthError` | 认证或授权失败 | 401/403 |
|
|
286
|
+
| `NotFoundError` | 请求的资源不存在 | 404 |
|
|
287
|
+
| `ParseError` | 邮件 MIME 解析失败 | - |
|
|
288
|
+
| `RateLimitError` | API 请求频率超限 | 429 |
|
|
289
|
+
|
|
290
|
+
所有错误类都继承自 `RelayTempMailError`,并提供以下属性:
|
|
291
|
+
|
|
292
|
+
- `code` - 机器可读的错误代码
|
|
293
|
+
- `statusCode` - HTTP 状态码(如适用)
|
|
294
|
+
- `response` - API 返回的原始响应数据(如可用)
|
|
295
|
+
|
|
296
|
+
## TypeScript
|
|
297
|
+
|
|
298
|
+
所有类型均已导出,包括 Provider 接口:
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import type {
|
|
302
|
+
AliasProvider,
|
|
303
|
+
MailProvider,
|
|
304
|
+
TempMailConfig,
|
|
305
|
+
FirefoxRelayConfig,
|
|
306
|
+
DuckDuckGoEmailConfig,
|
|
307
|
+
DuckDuckGoAliasStore,
|
|
308
|
+
CFTempMailConfig,
|
|
309
|
+
RelayAlias,
|
|
310
|
+
Email,
|
|
311
|
+
ParsedEmail,
|
|
312
|
+
GetEmailsOptions,
|
|
313
|
+
} from '@z_06/relay-temp-mail';
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## 从 v1 迁移
|
|
317
|
+
|
|
318
|
+
<details>
|
|
319
|
+
<summary>v1 → v2 迁移指南</summary>
|
|
320
|
+
|
|
321
|
+
**`RelayClient` → `TempMailClient`**
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
// v1(已废弃)
|
|
325
|
+
import { RelayClient } from '@z_06/relay-temp-mail';
|
|
326
|
+
const client = new RelayClient({
|
|
327
|
+
csrfToken: '...',
|
|
328
|
+
sessionId: '...',
|
|
329
|
+
cfApiUrl: 'https://...',
|
|
330
|
+
cfToken: '...',
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// v2
|
|
334
|
+
import { TempMailClient } from '@z_06/relay-temp-mail';
|
|
335
|
+
const client = new TempMailClient({
|
|
336
|
+
aliasProvider: {
|
|
337
|
+
type: 'firefox-relay',
|
|
338
|
+
csrfToken: '...',
|
|
339
|
+
sessionId: '...',
|
|
340
|
+
},
|
|
341
|
+
mailProvider: {
|
|
342
|
+
type: 'cf-temp-mail',
|
|
343
|
+
apiUrl: 'https://...',
|
|
344
|
+
token: '...',
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
**`RelayAPIClient` → `FirefoxRelayProvider`**,**`CFEmailClient` → `CFTempMailProvider`**
|
|
350
|
+
|
|
351
|
+
旧名称仍作为已废弃别名导出。方法签名不变。
|
|
352
|
+
|
|
353
|
+
</details>
|
|
354
|
+
|
|
355
|
+
## 许可证
|
|
356
|
+
|
|
357
|
+
MIT
|