@z_06/relay-temp-mail 1.0.2 → 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.
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