@z_06/relay-temp-mail 2.0.0 → 2.0.1

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.
Files changed (3) hide show
  1. package/README.md +307 -226
  2. package/README.zh-CN.md +307 -195
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,226 +1,307 @@
1
- # relay-temp-mail
2
-
3
- [English](README.md) | [中文](README.zh-CN.md)
4
-
5
- A TypeScript/JavaScript package for managing Firefox Relay email aliases and retrieving temporary emails via a CloudFlare temp email API.
6
-
7
- ## Features
8
-
9
- - **Create Firefox Relay aliases** - Generate new email aliases on demand
10
- - **List existing aliases** - View all your configured email aliases
11
- - **Get emails for specific aliases** - Fetch and parse emails sent to specific addresses
12
- - **Delete aliases** - Clean up unused aliases programmatically
13
- - **TypeScript support** - Full type definitions for all APIs
14
- - **ESM + CommonJS support** - Works with both module systems
15
-
16
- ## Installation
17
-
18
- ```bash
19
- npm install @z_06/relay-temp-mail
20
- # or
21
- pnpm add @z_06/relay-temp-mail
22
- # or
23
- bun add @z_06/relay-temp-mail
24
- ```
25
-
26
- ## Quick Start
27
-
28
- ```typescript
29
- import { RelayClient } from '@z_06/relay-temp-mail';
30
-
31
- const client = new RelayClient({
32
- csrfToken: 'your-csrf-token',
33
- sessionId: 'your-session-id',
34
- cfApiUrl: 'https://your-cf-api.com',
35
- cfToken: 'your-cf-token',
36
- });
37
-
38
- // Create a new alias
39
- const alias = await client.createAlias();
40
- console.log('New alias:', alias.fullAddress);
41
-
42
- // List all aliases
43
- const aliases = await client.listAliases();
44
-
45
- // Get emails for a specific alias
46
- const emails = await client.getEmails(alias.fullAddress, { limit: 10 });
47
- ```
48
-
49
- ## Configuration
50
-
51
- ### Firefox Relay Tokens
52
-
53
- To get your `csrfToken` and `sessionId`:
54
-
55
- 1. Login to [relay.firefox.com](https://relay.firefox.com)
56
- 2. Open your browser's developer tools (F12)
57
- 3. Go to the Application/Storage tab
58
- 4. Find Cookies for `relay.firefox.com`
59
- 5. Copy the values for `csrftoken` and `sessionid`
60
-
61
- ### CF Temp Email
62
-
63
- 本项目使�?[cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email) 作为临时邮箱后端,你需要先部署该服务才能使用�?
64
- #### 快速部署步�?
65
- 1. **Fork 仓库**
66
- - 访问 [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email)
67
- - 点击右上�?"Fork" 按钮,将仓库复制到你�?GitHub 账户
68
-
69
- 2. **一键部署到 Cloudflare**
70
- - 点击仓库 README 中的 "Deploy to Cloudflare Workers" 按钮
71
- - 或参�?[部署文档](https://temp-mail-docs.awsl.uk) 进行手动部署
72
-
73
- 3. **配置域名和邮件路�?*
74
- - �?Cloudflare Dashboard 中添加你的域�? - 配置 Email Routing(邮件路由)
75
- - 创建 catch-all 规则将所有邮件转发到 Worker
76
-
77
- 4. **获取 API 地址�?Token**
78
- - 部署完成后,你的 API 地址格式为:`https://<你的worker名称>.<你的子域>.workers.dev`
79
- - 登录前端界面(部署后会有 Pages 地址�? - 在用户设置或 Admin 后台生成 API Token
80
- - �?API 地址�?Token 填入 `RelayClient` 配置
81
-
82
- #### 获取 `cfApiUrl` �?`cfToken`
83
-
84
- ```typescript
85
- const client = new RelayClient({
86
- csrfToken: 'your-csrf-token',
87
- sessionId: 'your-session-id',
88
- cfApiUrl: 'https://your-worker-name.your-subdomain.workers.dev', // CF Worker API 地址
89
- cfToken: 'your-api-token', // �?Admin 后台或用户设置中生成
90
- });
91
- ```
92
-
93
- 更多详细配置请参�?[cloudflare_temp_email 官方文档](https://temp-mail-docs.awsl.uk)�?
94
- ## API Documentation
95
-
96
- ### RelayClient
97
-
98
- The main class for interacting with both Firefox Relay and CloudFlare temp email services.
99
-
100
- #### Constructor Options
101
-
102
- ```typescript
103
- interface RelayConfig {
104
- csrfToken: string; // Firefox Relay CSRF token
105
- sessionId: string; // Firefox Relay session ID
106
- cfApiUrl: string; // CloudFlare temp email API URL
107
- cfToken: string; // CloudFlare API token
108
- timeout?: number; // Request timeout in ms (default: 30000)
109
- }
110
- ```
111
-
112
- #### Methods
113
-
114
- ##### `listAliases()`
115
-
116
- Lists all Firefox Relay email aliases.
117
-
118
- ```typescript
119
- const aliases = await client.listAliases();
120
- // Returns: RelayAlias[]
121
- ```
122
-
123
- ##### `createAlias()`
124
-
125
- Creates a new random Firefox Relay email alias.
126
-
127
- ```typescript
128
- const alias = await client.createAlias();
129
- // Returns: RelayAlias
130
- console.log(alias.fullAddress); // e.g., "random123@mozmail.com"
131
- ```
132
-
133
- ##### `deleteAlias(id)`
134
-
135
- Deletes an alias by its ID.
136
-
137
- ```typescript
138
- await client.deleteAlias(12345);
139
- ```
140
-
141
- ##### `getEmails(aliasAddress?, options?)`
142
-
143
- Retrieves and parses emails from the CloudFlare temp email API. If `aliasAddress` is provided, only emails sent to that address are returned.
144
-
145
- ```typescript
146
- // Get all emails (up to default limit)
147
- const allEmails = await client.getEmails();
148
-
149
- // Get emails for a specific alias
150
- const emails = await client.getEmails('alias@mozmail.com', { limit: 10 });
151
-
152
- // With pagination
153
- const page2 = await client.getEmails('alias@mozmail.com', { limit: 10, offset: 10 });
154
- ```
155
-
156
- Options:
157
-
158
- - `limit` - Maximum number of emails to return (default: 20)
159
- - `offset` - Offset for pagination, 0-indexed (default: 0)
160
-
161
- ## Error Handling
162
-
163
- The package exports several error classes for handling different failure scenarios:
164
-
165
- ```typescript
166
- import {
167
- RelayTempMailError,
168
- NetworkError,
169
- AuthError,
170
- NotFoundError,
171
- ParseError,
172
- RateLimitError,
173
- } from '@z_06/relay-temp-mail';
174
-
175
- try {
176
- const alias = await client.createAlias();
177
- } catch (error) {
178
- if (error instanceof AuthError) {
179
- console.error('Authentication failed:', error.message);
180
- } else if (error instanceof NetworkError) {
181
- console.error('Network problem:', error.message);
182
- } else if (error instanceof RateLimitError) {
183
- console.error('Rate limited. Retry after:', error.response?.retryAfter);
184
- } else if (error instanceof RelayTempMailError) {
185
- console.error('Relay error:', error.code, error.message);
186
- }
187
- }
188
- ```
189
-
190
- ### Error Classes
191
-
192
- | Class | Description | Status Code |
193
- |-------|-------------|-------------|
194
- | `RelayTempMailError` | Base error class for all package errors | - |
195
- | `NetworkError` | Network connectivity issues | - |
196
- | `AuthError` | Authentication or authorization failures | 401/403 |
197
- | `NotFoundError` | Requested resource not found | 404 |
198
- | `ParseError` | Email MIME parsing failures | - |
199
- | `RateLimitError` | API rate limit exceeded | 429 |
200
-
201
- All error classes extend `RelayTempMailError` and provide:
202
-
203
- - `code` - Machine-readable error code
204
- - `statusCode` - HTTP status code (if applicable)
205
- - `response` - Raw response data from the API (if available)
206
-
207
- ## TypeScript
208
-
209
- All types are fully exported for use in your TypeScript projects:
210
-
211
- ```typescript
212
- import type {
213
- RelayConfig,
214
- RelayAlias,
215
- Email,
216
- ParsedEmail,
217
- ListAliasesOptions,
218
- GetEmailsOptions,
219
- } from '@z_06/relay-temp-mail';
220
- ```
221
-
222
- The package is built with strict TypeScript settings and provides comprehensive type definitions for all APIs.
223
-
224
- ## License
225
-
226
- MIT
1
+ # relay-temp-mail
2
+
3
+ [English](README.md) | [中文](README.zh-CN.md)
4
+
5
+ A modular TypeScript/JavaScript package for managing email aliases and retrieving temporary emails through pluggable providers.
6
+
7
+ Built on a provider architecture — combine any **alias provider** with any **mail provider** to fit your workflow. Currently ships with Firefox Relay and CloudFlare Temp Mail support, with more providers coming.
8
+
9
+ ## Features
10
+
11
+ - **Provider-based architecture** mix and match alias + mail providers
12
+ - **Firefox Relay** create, list, and delete email aliases
13
+ - **CloudFlare Temp Mail** retrieve and parse emails via API
14
+ - **TypeScript support** full type definitions for all APIs, including provider interfaces
15
+ - **ESM + CommonJS support** — works with both module systems
16
+ - **Extensible** — implement `AliasProvider` or `MailProvider` to add new services
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @z_06/relay-temp-mail
22
+ # or
23
+ pnpm add @z_06/relay-temp-mail
24
+ # or
25
+ bun add @z_06/relay-temp-mail
26
+ ```
27
+
28
+ ## Quick Start
29
+
30
+ ```typescript
31
+ import { TempMailClient } from '@z_06/relay-temp-mail';
32
+
33
+ const client = new TempMailClient({
34
+ aliasProvider: {
35
+ type: 'firefox-relay',
36
+ csrfToken: 'your-csrf-token',
37
+ sessionId: 'your-session-id',
38
+ },
39
+ mailProvider: {
40
+ type: 'cf-temp-mail',
41
+ apiUrl: 'https://your-cf-api.com',
42
+ token: 'your-cf-token',
43
+ },
44
+ });
45
+
46
+ // Create a new alias
47
+ const alias = await client.createAlias();
48
+ console.log('New alias:', alias.fullAddress);
49
+
50
+ // List all aliases
51
+ const aliases = await client.listAliases();
52
+
53
+ // Get emails for a specific alias
54
+ const emails = await client.getEmails(alias.fullAddress, { limit: 10 });
55
+ ```
56
+
57
+ ## Providers
58
+
59
+ The library uses two types of providers that can be combined independently:
60
+
61
+ | Provider Type | Interface | Current Implementations |
62
+ |---|---|---|
63
+ | **Alias Provider** | `AliasProvider` | `firefox-relay` |
64
+ | **Mail Provider** | `MailProvider` | `cf-temp-mail` |
65
+
66
+ ### Alias Providers
67
+
68
+ #### `firefox-relay`
69
+
70
+ Manages email aliases through [Firefox Relay](https://relay.firefox.com).
71
+
72
+ **Configuration:**
73
+
74
+ ```typescript
75
+ {
76
+ type: 'firefox-relay',
77
+ csrfToken: string; // CSRF token from relay.firefox.com cookies
78
+ sessionId: string; // Session ID from relay.firefox.com cookies
79
+ }
80
+ ```
81
+
82
+ **Getting your tokens:**
83
+
84
+ 1. Login to [relay.firefox.com](https://relay.firefox.com)
85
+ 2. Open your browser's developer tools (F12)
86
+ 3. Go to the Application/Storage tab
87
+ 4. Find Cookies for `relay.firefox.com`
88
+ 5. Copy the values for `csrftoken` and `sessionid`
89
+
90
+ ### Mail Providers
91
+
92
+ #### `cf-temp-mail`
93
+
94
+ Retrieves emails from a [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email) instance.
95
+
96
+ **Configuration:**
97
+
98
+ ```typescript
99
+ {
100
+ type: 'cf-temp-mail',
101
+ apiUrl: string; // Base URL of your CF temp email API
102
+ token: string; // Bearer token for API authentication
103
+ }
104
+ ```
105
+
106
+ **Deploying the backend:**
107
+
108
+ 1. Fork [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email)
109
+ 2. Deploy to Cloudflare Workers (one-click or manual via [docs](https://temp-mail-docs.awsl.uk))
110
+ 3. Configure Email Routing and catch-all rules in Cloudflare Dashboard
111
+ 4. Generate an API Token from the admin panel or user settings
112
+
113
+ ## API Documentation
114
+
115
+ ### TempMailClient
116
+
117
+ Main client class. Accepts an `aliasProvider` and `mailProvider` configuration and exposes a unified interface.
118
+
119
+ #### Constructor
120
+
121
+ ```typescript
122
+ new TempMailClient(config: TempMailConfig)
123
+
124
+ interface TempMailConfig {
125
+ aliasProvider: AliasProviderConfig; // Alias provider config (discriminated union)
126
+ mailProvider: MailProviderConfig; // Mail provider config (discriminated union)
127
+ timeout?: number; // Request timeout in ms (default: 30000)
128
+ }
129
+ ```
130
+
131
+ #### Methods
132
+
133
+ ##### `listAliases()`
134
+
135
+ Lists all email aliases from the configured alias provider.
136
+
137
+ ```typescript
138
+ const aliases = await client.listAliases();
139
+ // Returns: RelayAlias[]
140
+ ```
141
+
142
+ ##### `createAlias()`
143
+
144
+ Creates a new email alias via the configured alias provider.
145
+
146
+ ```typescript
147
+ const alias = await client.createAlias();
148
+ // Returns: RelayAlias
149
+ console.log(alias.fullAddress); // e.g., "random123@mozmail.com"
150
+ ```
151
+
152
+ ##### `deleteAlias(id)`
153
+
154
+ Deletes an alias by its ID.
155
+
156
+ ```typescript
157
+ await client.deleteAlias(12345);
158
+ ```
159
+
160
+ ##### `getEmails(aliasAddress?, options?)`
161
+
162
+ Retrieves and parses emails from the configured mail provider. If `aliasAddress` is provided, only emails sent to that address are returned.
163
+
164
+ ```typescript
165
+ // Get all emails (up to default limit)
166
+ const allEmails = await client.getEmails();
167
+
168
+ // Get emails for a specific alias
169
+ const emails = await client.getEmails('alias@mozmail.com', { limit: 10 });
170
+
171
+ // With pagination
172
+ const page2 = await client.getEmails('alias@mozmail.com', { limit: 10, offset: 10 });
173
+ ```
174
+
175
+ Options:
176
+
177
+ - `limit` - Maximum number of emails to return (default: 20)
178
+ - `offset` - Offset for pagination, 0-indexed (default: 0)
179
+
180
+ ## Custom Providers
181
+
182
+ Implement the `AliasProvider` or `MailProvider` interface to add support for new services:
183
+
184
+ ```typescript
185
+ import type { AliasProvider, RelayAlias } from '@z_06/relay-temp-mail';
186
+
187
+ class MyAliasProvider implements AliasProvider {
188
+ async listAliases(): Promise<RelayAlias[]> { /* ... */ }
189
+ async createAlias(): Promise<RelayAlias> { /* ... */ }
190
+ async deleteAlias(id: number): Promise<void> { /* ... */ }
191
+ }
192
+ ```
193
+
194
+ ```typescript
195
+ import type { MailProvider, Email } from '@z_06/relay-temp-mail';
196
+
197
+ class MyMailProvider implements MailProvider {
198
+ async getMails(limit: number, offset: number): Promise<Email[]> { /* ... */ }
199
+ }
200
+ ```
201
+
202
+ ## Error Handling
203
+
204
+ The package exports several error classes for handling different failure scenarios:
205
+
206
+ ```typescript
207
+ import {
208
+ RelayTempMailError,
209
+ NetworkError,
210
+ AuthError,
211
+ NotFoundError,
212
+ ParseError,
213
+ RateLimitError,
214
+ } from '@z_06/relay-temp-mail';
215
+
216
+ try {
217
+ const alias = await client.createAlias();
218
+ } catch (error) {
219
+ if (error instanceof AuthError) {
220
+ console.error('Authentication failed:', error.message);
221
+ } else if (error instanceof NetworkError) {
222
+ console.error('Network problem:', error.message);
223
+ } else if (error instanceof RateLimitError) {
224
+ console.error('Rate limited. Retry after:', error.response?.retryAfter);
225
+ } else if (error instanceof RelayTempMailError) {
226
+ console.error('Error:', error.code, error.message);
227
+ }
228
+ }
229
+ ```
230
+
231
+ ### Error Classes
232
+
233
+ | Class | Description | Status Code |
234
+ |-------|-------------|-------------|
235
+ | `RelayTempMailError` | Base error class for all package errors | - |
236
+ | `NetworkError` | Network connectivity issues | - |
237
+ | `AuthError` | Authentication or authorization failures | 401/403 |
238
+ | `NotFoundError` | Requested resource not found | 404 |
239
+ | `ParseError` | Email MIME parsing failures | - |
240
+ | `RateLimitError` | API rate limit exceeded | 429 |
241
+
242
+ All error classes extend `RelayTempMailError` and provide:
243
+
244
+ - `code` - Machine-readable error code
245
+ - `statusCode` - HTTP status code (if applicable)
246
+ - `response` - Raw response data from the API (if available)
247
+
248
+ ## TypeScript
249
+
250
+ All types are fully exported, including the provider interfaces:
251
+
252
+ ```typescript
253
+ import type {
254
+ AliasProvider,
255
+ MailProvider,
256
+ TempMailConfig,
257
+ FirefoxRelayConfig,
258
+ CFTempMailConfig,
259
+ RelayAlias,
260
+ Email,
261
+ ParsedEmail,
262
+ GetEmailsOptions,
263
+ } from '@z_06/relay-temp-mail';
264
+ ```
265
+
266
+ ## Migration from v1
267
+
268
+ <details>
269
+ <summary>v1 → v2 Migration Guide</summary>
270
+
271
+ **`RelayClient` → `TempMailClient`**
272
+
273
+ ```typescript
274
+ // v1 (deprecated)
275
+ import { RelayClient } from '@z_06/relay-temp-mail';
276
+ const client = new RelayClient({
277
+ csrfToken: '...',
278
+ sessionId: '...',
279
+ cfApiUrl: 'https://...',
280
+ cfToken: '...',
281
+ });
282
+
283
+ // v2
284
+ import { TempMailClient } from '@z_06/relay-temp-mail';
285
+ const client = new TempMailClient({
286
+ aliasProvider: {
287
+ type: 'firefox-relay',
288
+ csrfToken: '...',
289
+ sessionId: '...',
290
+ },
291
+ mailProvider: {
292
+ type: 'cf-temp-mail',
293
+ apiUrl: 'https://...',
294
+ token: '...',
295
+ },
296
+ });
297
+ ```
298
+
299
+ **`RelayAPIClient` → `FirefoxRelayProvider`**, **`CFEmailClient` → `CFTempMailProvider`**
300
+
301
+ The old names are still exported as deprecated aliases. The method signatures are unchanged.
302
+
303
+ </details>
304
+
305
+ ## License
306
+
307
+ MIT
package/README.zh-CN.md CHANGED
@@ -1,195 +1,307 @@
1
- # relay-temp-mail
2
-
3
- [English](README.md) | [中文](README.zh-CN.md)
4
-
5
- 一个用于管�?Firefox Relay 邮箱别名和通过 Cloudflare 临时邮箱 API 接收邮件�?TypeScript/JavaScript 包�?
6
- ## 功能特�?
7
- - **创建 Firefox Relay 别名** - 按需生成新的邮箱别名
8
- - **列出已有别名** - 查看所有已配置的邮箱别�?- **获取指定别名的邮�?* - 获取并解析发送到特定地址的邮�?- **删除别名** - 以编程方式清理未使用的别�?- **TypeScript 支持** - 所�?API 都有完整的类型定�?- **支持 ESM + CommonJS** - 兼容两种模块系统
9
-
10
- ## 安装
11
-
12
- ```bash
13
- npm install @z_06/relay-temp-mail
14
- # �?pnpm add @z_06/relay-temp-mail
15
- # �?bun add @z_06/relay-temp-mail
16
- ```
17
-
18
- ## 快速开�?
19
- ```typescript
20
- import { RelayClient } from '@z_06/relay-temp-mail';
21
-
22
- const client = new RelayClient({
23
- csrfToken: 'your-csrf-token',
24
- sessionId: 'your-session-id',
25
- cfApiUrl: 'https://your-cf-api.com',
26
- cfToken: 'your-cf-token',
27
- });
28
-
29
- // 创建新别�?const alias = await client.createAlias();
30
- console.log('新别�?', alias.fullAddress);
31
-
32
- // 列出所有别�?const aliases = await client.listAliases();
33
-
34
- // 获取指定别名的邮�?const emails = await client.getEmails(alias.fullAddress, { limit: 10 });
35
- ```
36
-
37
- ## 配置
38
-
39
- ### Firefox Relay Token
40
-
41
- 获取 `csrfToken` �?`sessionId` 的方法:
42
-
43
- 1. 登录 [relay.firefox.com](https://relay.firefox.com)
44
- 2. 打开浏览器开发者工�?(F12)
45
- 3. 切换�?Application/Storage 标签�?4. 找到 `relay.firefox.com` �?Cookies
46
- 5. 复制 `csrftoken` �?`sessionid` 的�?
47
- ### CF 临时邮箱
48
-
49
- 本项目使�?[cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email) 作为临时邮箱后端,你需要先部署该服务才能使用�?
50
- #### 快速部署步�?
51
- 1. **Fork 仓库**
52
- - 访问 [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email)
53
- - 点击右上�?"Fork" 按钮,将仓库复制到你�?GitHub 账户
54
-
55
- 2. **一键部署到 Cloudflare**
56
- - 点击仓库 README 中的 "Deploy to Cloudflare Workers" 按钮
57
- - 或参�?[部署文档](https://temp-mail-docs.awsl.uk) 进行手动部署
58
-
59
- 3. **配置域名和邮件路�?*
60
- - �?Cloudflare Dashboard 中添加你的域�? - 配置 Email Routing(邮件路由)
61
- - 创建 catch-all 规则将所有邮件转发到 Worker
62
-
63
- 4. **获取 API 地址�?Token**
64
- - 部署完成后,你的 API 地址格式为:`https://<你的worker名称>.<你的子域>.workers.dev`
65
- - 登录前端界面(部署后会有 Pages 地址�? - 在用户设置或 Admin 后台生成 API Token
66
- - �?API 地址�?Token 填入 `RelayClient` 配置
67
-
68
- #### 获取 `cfApiUrl` �?`cfToken`
69
-
70
- ```typescript
71
- const client = new RelayClient({
72
- csrfToken: 'your-csrf-token',
73
- sessionId: 'your-session-id',
74
- cfApiUrl: 'https://your-worker-name.your-subdomain.workers.dev', // CF Worker API 地址
75
- cfToken: 'your-api-token', // �?Admin 后台或用户设置中生成
76
- });
77
- ```
78
-
79
- 更多详细配置请参�?[cloudflare_temp_email 官方文档](https://temp-mail-docs.awsl.uk)�?
80
- ## API 文档
81
-
82
- ### RelayClient
83
-
84
- 用于�?Firefox Relay �?Cloudflare 临时邮箱服务交互的主类�?
85
- #### 构造函数选项
86
-
87
- ```typescript
88
- interface RelayConfig {
89
- csrfToken: string; // Firefox Relay CSRF token
90
- sessionId: string; // Firefox Relay session ID
91
- cfApiUrl: string; // Cloudflare 临时邮箱 API 地址
92
- cfToken: string; // Cloudflare API token
93
- timeout?: number; // 请求超时时间,毫秒(默认: 30000�?}
94
- ```
95
-
96
- #### 方法
97
-
98
- ##### `listAliases()`
99
-
100
- 列出所�?Firefox Relay 邮箱别名�?
101
- ```typescript
102
- const aliases = await client.listAliases();
103
- // 返回: RelayAlias[]
104
- ```
105
-
106
- ##### `createAlias()`
107
-
108
- 创建一个新的随�?Firefox Relay 邮箱别名�?
109
- ```typescript
110
- const alias = await client.createAlias();
111
- // 返回: RelayAlias
112
- console.log(alias.fullAddress); // 例如: "random123@mozmail.com"
113
- ```
114
-
115
- ##### `deleteAlias(id)`
116
-
117
- 根据 ID 删除别名�?
118
- ```typescript
119
- await client.deleteAlias(12345);
120
- ```
121
-
122
- ##### `getEmails(aliasAddress?, options?)`
123
-
124
- �?Cloudflare 临时邮箱 API 检索和解析邮件。如果提供了 `aliasAddress`,则只返回发送到该地址的邮件�?
125
- ```typescript
126
- // 获取所有邮件(默认数量限制�?const allEmails = await client.getEmails();
127
-
128
- // 获取指定别名的邮�?const emails = await client.getEmails('alias@mozmail.com', { limit: 10 });
129
-
130
- // 分页获取
131
- const page2 = await client.getEmails('alias@mozmail.com', { limit: 10, offset: 10 });
132
- ```
133
-
134
- 选项�?
135
- - `limit` - 返回的最大邮件数量(默认: 20�?- `offset` - 分页偏移量,�?0 开始(默认: 0�?
136
- ## 错误处理
137
-
138
- 本包导出了多个错误类,用于处理不同的失败场景�?
139
- ```typescript
140
- import {
141
- RelayTempMailError,
142
- NetworkError,
143
- AuthError,
144
- NotFoundError,
145
- ParseError,
146
- RateLimitError,
147
- } from '@z_06/relay-temp-mail';
148
-
149
- try {
150
- const alias = await client.createAlias();
151
- } catch (error) {
152
- if (error instanceof AuthError) {
153
- console.error('认证失败:', error.message);
154
- } else if (error instanceof NetworkError) {
155
- console.error('网络问题:', error.message);
156
- } else if (error instanceof RateLimitError) {
157
- console.error('请求过于频繁,请稍后重试:', error.response?.retryAfter);
158
- } else if (error instanceof RelayTempMailError) {
159
- console.error('Relay 错误:', error.code, error.message);
160
- }
161
- }
162
- ```
163
-
164
- ### 错误�?
165
- | 类名 | 描述 | 状态码 |
166
- |------|------|--------|
167
- | `RelayTempMailError` | 所有包错误的基�?| - |
168
- | `NetworkError` | 网络连接问题 | - |
169
- | `AuthError` | 认证或授权失�?| 401/403 |
170
- | `NotFoundError` | 请求的资源不存在 | 404 |
171
- | `ParseError` | 邮件 MIME 解析失败 | - |
172
- | `RateLimitError` | API 请求频率限制 exceeded | 429 |
173
-
174
- 所有错误类都继承自 `RelayTempMailError`,并提供以下属性:
175
-
176
- - `code` - 机器可读的错误代�?- `statusCode` - HTTP 状态码(如适用�?- `response` - API 返回的原始响应数据(如可用)
177
-
178
- ## TypeScript
179
-
180
- 所有类型都已导出,可在 TypeScript 项目中使用:
181
-
182
- ```typescript
183
- import type {
184
- RelayConfig,
185
- RelayAlias,
186
- Email,
187
- ParsedEmail,
188
- ListAliasesOptions,
189
- GetEmailsOptions,
190
- } from 'relay-temp-mail';
191
- ```
192
-
193
- 本包使用严格�?TypeScript 设置构建,为所�?API 提供全面的类型定义�?
194
- ## 许可�?
195
- MIT
1
+ # relay-temp-mail
2
+
3
+ [English](README.md) | [中文](README.zh-CN.md)
4
+
5
+ 一个模块化的 TypeScript/JavaScript 包,通过可插拔的 Provider 管理邮箱别名和接收临时邮件。
6
+
7
+ 基于 Provider 架构 — 自由组合**别名提供商**与**邮件提供商**。当前内置 Firefox Relay CloudFlare 临时邮箱支持,更多提供商即将推出。
8
+
9
+ ## 功能特性
10
+
11
+ - **Provider 架构** — 自由组合别名与邮件提供商
12
+ - **Firefox Relay** — 创建、列出、删除邮箱别名
13
+ - **CloudFlare 临时邮箱** — 通过 API 获取并解析邮件
14
+ - **TypeScript 支持** — 所有 API 均有完整类型定义,包括 Provider 接口
15
+ - **ESM + CommonJS 支持** — 兼容两种模块系统
16
+ - **可扩展** — 实现 `AliasProvider` 或 `MailProvider` 即可接入新服务
17
+
18
+ ## 安装
19
+
20
+ ```bash
21
+ npm install @z_06/relay-temp-mail
22
+ #
23
+ pnpm add @z_06/relay-temp-mail
24
+ #
25
+ bun add @z_06/relay-temp-mail
26
+ ```
27
+
28
+ ## 快速开始
29
+
30
+ ```typescript
31
+ import { TempMailClient } from '@z_06/relay-temp-mail';
32
+
33
+ const client = new TempMailClient({
34
+ aliasProvider: {
35
+ type: 'firefox-relay',
36
+ csrfToken: 'your-csrf-token',
37
+ sessionId: 'your-session-id',
38
+ },
39
+ mailProvider: {
40
+ type: 'cf-temp-mail',
41
+ apiUrl: 'https://your-cf-api.com',
42
+ token: 'your-cf-token',
43
+ },
44
+ });
45
+
46
+ // 创建新别名
47
+ const alias = await client.createAlias();
48
+ console.log('新别名:', alias.fullAddress);
49
+
50
+ // 列出所有别名
51
+ const aliases = await client.listAliases();
52
+
53
+ // 获取指定别名的邮件
54
+ const emails = await client.getEmails(alias.fullAddress, { limit: 10 });
55
+ ```
56
+
57
+ ## Providers
58
+
59
+ 本库使用两种可独立组合的 Provider:
60
+
61
+ | Provider 类型 | 接口 | 当前实现 |
62
+ |---|---|---|
63
+ | **别名提供商** | `AliasProvider` | `firefox-relay` |
64
+ | **邮件提供商** | `MailProvider` | `cf-temp-mail` |
65
+
66
+ ### 别名提供商
67
+
68
+ #### `firefox-relay`
69
+
70
+ 通过 [Firefox Relay](https://relay.firefox.com) 管理邮箱别名。
71
+
72
+ **配置:**
73
+
74
+ ```typescript
75
+ {
76
+ type: 'firefox-relay',
77
+ csrfToken: string; // relay.firefox.com 的 CSRF token
78
+ sessionId: string; // relay.firefox.com 的 Session ID
79
+ }
80
+ ```
81
+
82
+ **获取 Token:**
83
+
84
+ 1. 登录 [relay.firefox.com](https://relay.firefox.com)
85
+ 2. 打开浏览器开发者工具(F12)
86
+ 3. 切换到 Application/Storage 标签页
87
+ 4. 找到 `relay.firefox.com` 的 Cookies
88
+ 5. 复制 `csrftoken` 和 `sessionid` 的值
89
+
90
+ ### 邮件提供商
91
+
92
+ #### `cf-temp-mail`
93
+
94
+ 从 [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email) 实例获取邮件。
95
+
96
+ **配置:**
97
+
98
+ ```typescript
99
+ {
100
+ type: 'cf-temp-mail',
101
+ apiUrl: string; // CF 临时邮箱 API 的基础 URL
102
+ token: string; // API Bearer Token
103
+ }
104
+ ```
105
+
106
+ **部署后端:**
107
+
108
+ 1. Fork [cloudflare_temp_email](https://github.com/dreamhunter2333/cloudflare_temp_email)
109
+ 2. 部署到 Cloudflare Workers(一键部署或参考[部署文档](https://temp-mail-docs.awsl.uk)手动部署)
110
+ 3. Cloudflare Dashboard 中配置域名和 Email Routing 的 catch-all 规则
111
+ 4. 从管理后台或用户设置中生成 API Token
112
+
113
+ ## API 文档
114
+
115
+ ### TempMailClient
116
+
117
+ 主客户端类。接受 `aliasProvider` 和 `mailProvider` 配置,提供统一接口。
118
+
119
+ #### 构造函数
120
+
121
+ ```typescript
122
+ new TempMailClient(config: TempMailConfig)
123
+
124
+ interface TempMailConfig {
125
+ aliasProvider: AliasProviderConfig; // 别名提供商配置(鉴别联合类型)
126
+ mailProvider: MailProviderConfig; // 邮件提供商配置(鉴别联合类型)
127
+ timeout?: number; // 请求超时时间,毫秒(默认: 30000)
128
+ }
129
+ ```
130
+
131
+ #### 方法
132
+
133
+ ##### `listAliases()`
134
+
135
+ 列出配置的别名提供商中的所有邮箱别名。
136
+
137
+ ```typescript
138
+ const aliases = await client.listAliases();
139
+ // 返回: RelayAlias[]
140
+ ```
141
+
142
+ ##### `createAlias()`
143
+
144
+ 通过配置的别名提供商创建新的邮箱别名。
145
+
146
+ ```typescript
147
+ const alias = await client.createAlias();
148
+ // 返回: RelayAlias
149
+ console.log(alias.fullAddress); // 例如: "random123@mozmail.com"
150
+ ```
151
+
152
+ ##### `deleteAlias(id)`
153
+
154
+ 根据 ID 删除别名。
155
+
156
+ ```typescript
157
+ await client.deleteAlias(12345);
158
+ ```
159
+
160
+ ##### `getEmails(aliasAddress?, options?)`
161
+
162
+ 从配置的邮件提供商获取并解析邮件。如果提供了 `aliasAddress`,则只返回发送到该地址的邮件。
163
+
164
+ ```typescript
165
+ // 获取所有邮件(默认数量限制)
166
+ const allEmails = await client.getEmails();
167
+
168
+ // 获取指定别名的邮件
169
+ const emails = await client.getEmails('alias@mozmail.com', { limit: 10 });
170
+
171
+ // 分页获取
172
+ const page2 = await client.getEmails('alias@mozmail.com', { limit: 10, offset: 10 });
173
+ ```
174
+
175
+ 选项:
176
+
177
+ - `limit` - 返回的最大邮件数量(默认: 20)
178
+ - `offset` - 分页偏移量,从 0 开始(默认: 0)
179
+
180
+ ## 自定义 Provider
181
+
182
+ 实现 `AliasProvider` 或 `MailProvider` 接口即可接入新服务:
183
+
184
+ ```typescript
185
+ import type { AliasProvider, RelayAlias } from '@z_06/relay-temp-mail';
186
+
187
+ class MyAliasProvider implements AliasProvider {
188
+ async listAliases(): Promise<RelayAlias[]> { /* ... */ }
189
+ async createAlias(): Promise<RelayAlias> { /* ... */ }
190
+ async deleteAlias(id: number): Promise<void> { /* ... */ }
191
+ }
192
+ ```
193
+
194
+ ```typescript
195
+ import type { MailProvider, Email } from '@z_06/relay-temp-mail';
196
+
197
+ class MyMailProvider implements MailProvider {
198
+ async getMails(limit: number, offset: number): Promise<Email[]> { /* ... */ }
199
+ }
200
+ ```
201
+
202
+ ## 错误处理
203
+
204
+ 本包导出了多个错误类,用于处理不同的失败场景:
205
+
206
+ ```typescript
207
+ import {
208
+ RelayTempMailError,
209
+ NetworkError,
210
+ AuthError,
211
+ NotFoundError,
212
+ ParseError,
213
+ RateLimitError,
214
+ } from '@z_06/relay-temp-mail';
215
+
216
+ try {
217
+ const alias = await client.createAlias();
218
+ } catch (error) {
219
+ if (error instanceof AuthError) {
220
+ console.error('认证失败:', error.message);
221
+ } else if (error instanceof NetworkError) {
222
+ console.error('网络问题:', error.message);
223
+ } else if (error instanceof RateLimitError) {
224
+ console.error('请求过于频繁,请稍后重试:', error.response?.retryAfter);
225
+ } else if (error instanceof RelayTempMailError) {
226
+ console.error('错误:', error.code, error.message);
227
+ }
228
+ }
229
+ ```
230
+
231
+ ### 错误类
232
+
233
+ | 类名 | 描述 | 状态码 |
234
+ |------|------|--------|
235
+ | `RelayTempMailError` | 所有包错误的基类 | - |
236
+ | `NetworkError` | 网络连接问题 | - |
237
+ | `AuthError` | 认证或授权失败 | 401/403 |
238
+ | `NotFoundError` | 请求的资源不存在 | 404 |
239
+ | `ParseError` | 邮件 MIME 解析失败 | - |
240
+ | `RateLimitError` | API 请求频率超限 | 429 |
241
+
242
+ 所有错误类都继承自 `RelayTempMailError`,并提供以下属性:
243
+
244
+ - `code` - 机器可读的错误代码
245
+ - `statusCode` - HTTP 状态码(如适用)
246
+ - `response` - API 返回的原始响应数据(如可用)
247
+
248
+ ## TypeScript
249
+
250
+ 所有类型均已导出,包括 Provider 接口:
251
+
252
+ ```typescript
253
+ import type {
254
+ AliasProvider,
255
+ MailProvider,
256
+ TempMailConfig,
257
+ FirefoxRelayConfig,
258
+ CFTempMailConfig,
259
+ RelayAlias,
260
+ Email,
261
+ ParsedEmail,
262
+ GetEmailsOptions,
263
+ } from '@z_06/relay-temp-mail';
264
+ ```
265
+
266
+ ## 从 v1 迁移
267
+
268
+ <details>
269
+ <summary>v1 → v2 迁移指南</summary>
270
+
271
+ **`RelayClient` → `TempMailClient`**
272
+
273
+ ```typescript
274
+ // v1(已废弃)
275
+ import { RelayClient } from '@z_06/relay-temp-mail';
276
+ const client = new RelayClient({
277
+ csrfToken: '...',
278
+ sessionId: '...',
279
+ cfApiUrl: 'https://...',
280
+ cfToken: '...',
281
+ });
282
+
283
+ // v2
284
+ import { TempMailClient } from '@z_06/relay-temp-mail';
285
+ const client = new TempMailClient({
286
+ aliasProvider: {
287
+ type: 'firefox-relay',
288
+ csrfToken: '...',
289
+ sessionId: '...',
290
+ },
291
+ mailProvider: {
292
+ type: 'cf-temp-mail',
293
+ apiUrl: 'https://...',
294
+ token: '...',
295
+ },
296
+ });
297
+ ```
298
+
299
+ **`RelayAPIClient` → `FirefoxRelayProvider`**,**`CFEmailClient` → `CFTempMailProvider`**
300
+
301
+ 旧名称仍作为已废弃别名导出。方法签名不变。
302
+
303
+ </details>
304
+
305
+ ## 许可证
306
+
307
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@z_06/relay-temp-mail",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "type": "module",
5
5
  "description": "TypeScript npm package for Firefox Relay + CF temp email API",
6
6
  "main": "./dist/index.cjs",