@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.
- package/README.md +307 -226
- package/README.zh-CN.md +307 -195
- 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
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
# or
|
|
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
|
-
```typescript
|
|
138
|
-
await client.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
-
|
|
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 和 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
|