@z_06/relay-temp-mail 1.0.2 → 2.0.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/dist/index.d.ts CHANGED
@@ -2,13 +2,124 @@
2
2
  * TypeScript type definitions for the relay-temp-mail package.
3
3
  *
4
4
  * These interfaces define the core data structures used throughout the package,
5
- * ensuring type safety when interacting with the Firefox Relay and CF temp email APIs.
5
+ * including the provider abstraction layer for extensible email alias and mail
6
+ * access providers.
6
7
  */
7
8
  /**
8
- * Configuration interface for initializing the RelayClient.
9
+ * Abstract interface for alias email providers.
9
10
  *
10
- * This interface contains all required authentication tokens and API URLs
11
- * needed to communicate with both Firefox Relay and CloudFlare temp email services.
11
+ * An alias provider creates and manages email aliases that forward to a real
12
+ * email address. Different providers (Firefox Relay, SimpleLogin, DuckDuckGo
13
+ * Email Protection, etc.) can implement this interface.
14
+ */
15
+ interface AliasProvider {
16
+ /**
17
+ * Lists all email aliases managed by this provider.
18
+ *
19
+ * @returns Promise resolving to an array of RelayAlias objects
20
+ */
21
+ listAliases(): Promise<RelayAlias[]>;
22
+ /**
23
+ * Creates a new email alias.
24
+ *
25
+ * @returns Promise resolving to the newly created RelayAlias
26
+ */
27
+ createAlias(): Promise<RelayAlias>;
28
+ /**
29
+ * Deletes an email alias by its ID.
30
+ *
31
+ * @param id - The unique identifier of the alias to delete
32
+ */
33
+ deleteAlias(id: number): Promise<void>;
34
+ }
35
+ /**
36
+ * Abstract interface for mail access providers.
37
+ *
38
+ * A mail provider retrieves raw emails from a mailbox via API. Different
39
+ * providers (CF Temp Mail, IMAP, etc.) can implement this interface.
40
+ */
41
+ interface MailProvider {
42
+ /**
43
+ * Retrieves emails from the mail provider.
44
+ *
45
+ * @param limit - Maximum number of emails to return
46
+ * @param offset - Pagination offset (0-indexed)
47
+ * @returns Promise resolving to an array of Email objects
48
+ */
49
+ getMails(limit: number, offset: number): Promise<Email[]>;
50
+ }
51
+ /**
52
+ * Configuration for the Firefox Relay alias provider.
53
+ */
54
+ interface FirefoxRelayConfig {
55
+ /** Discriminant identifying this provider type */
56
+ type: 'firefox-relay';
57
+ /** CSRF token for Firefox Relay API authentication */
58
+ csrfToken: string;
59
+ /** Session ID for Firefox Relay API authentication */
60
+ sessionId: string;
61
+ }
62
+ /**
63
+ * Union type for all supported alias provider configurations.
64
+ *
65
+ * Currently only supports 'firefox-relay'. More providers (simplelogin,
66
+ * duckduckgo, etc.) can be added here in the future.
67
+ */
68
+ type AliasProviderConfig = FirefoxRelayConfig;
69
+ /**
70
+ * Configuration for the CloudFlare temp mail provider.
71
+ */
72
+ interface CFTempMailConfig {
73
+ /** Discriminant identifying this provider type */
74
+ type: 'cf-temp-mail';
75
+ /** Base URL for the CloudFlare temp email API */
76
+ apiUrl: string;
77
+ /** Bearer token for CloudFlare temp email API authentication */
78
+ token: string;
79
+ }
80
+ /**
81
+ * Union type for all supported mail provider configurations.
82
+ *
83
+ * Currently only supports 'cf-temp-mail'. More providers (imap, pop3, etc.)
84
+ * can be added here in the future.
85
+ */
86
+ type MailProviderConfig = CFTempMailConfig;
87
+ /**
88
+ * Configuration interface for initializing the TempMailClient.
89
+ *
90
+ * Uses a provider-based architecture where alias and mail providers are
91
+ * specified by their type and provider-specific configuration. This allows
92
+ * the library to grow to support additional providers without breaking changes.
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * const client = new TempMailClient({
97
+ * aliasProvider: {
98
+ * type: 'firefox-relay',
99
+ * csrfToken: '...',
100
+ * sessionId: '...',
101
+ * },
102
+ * mailProvider: {
103
+ * type: 'cf-temp-mail',
104
+ * apiUrl: 'https://...',
105
+ * token: '...',
106
+ * },
107
+ * timeout: 30000,
108
+ * });
109
+ * ```
110
+ */
111
+ interface TempMailConfig {
112
+ /** Alias email provider configuration */
113
+ aliasProvider: AliasProviderConfig;
114
+ /** Mail access provider configuration */
115
+ mailProvider: MailProviderConfig;
116
+ /** Optional timeout for HTTP requests in milliseconds (default: 30000) */
117
+ timeout?: number;
118
+ }
119
+ /**
120
+ * @deprecated Use TempMailConfig instead. Will be removed in the next major version.
121
+ *
122
+ * Legacy configuration interface kept for backward compatibility.
12
123
  */
13
124
  interface RelayConfig {
14
125
  /** CSRF token for Firefox Relay API authentication */
@@ -23,13 +134,13 @@ interface RelayConfig {
23
134
  timeout?: number;
24
135
  }
25
136
  /**
26
- * Represents a Firefox Relay email alias.
137
+ * Represents an email alias.
27
138
  *
28
- * This interface maps to the JSON structure returned by the Firefox Relay API
139
+ * This interface maps to the JSON structure returned by alias providers
29
140
  * when listing or creating email aliases.
30
141
  *
31
- * Note: Property names use camelCase even though the API returns snake_case.
32
- * The package will handle the conversion internally.
142
+ * Note: Property names use camelCase even though the underlying API may
143
+ * return snake_case. The provider implementation handles the conversion.
33
144
  */
34
145
  interface RelayAlias {
35
146
  /** Unique identifier for the alias */
@@ -70,10 +181,10 @@ interface RelayAlias {
70
181
  usedOn?: string | null;
71
182
  }
72
183
  /**
73
- * Email object returned by the CloudFlare temp email API.
184
+ * Email object returned by mail providers.
74
185
  *
75
- * This interface represents the raw email data as returned from the CF API,
76
- * including the complete MIME message in the `raw` field.
186
+ * This interface represents the raw email data as returned from a mail
187
+ * provider, including the complete MIME message in the `raw` field.
77
188
  */
78
189
  interface Email {
79
190
  /** Unique identifier for the email */
@@ -95,17 +206,17 @@ interface Email {
95
206
  * Email object with parsed alias information.
96
207
  *
97
208
  * This interface extends the base Email interface and adds the parsed
98
- * Firefox Relay alias information extracted from the MIME content.
209
+ * alias information extracted from the MIME content.
99
210
  */
100
211
  interface ParsedEmail extends Email {
101
212
  /**
102
- * The Firefox Relay alias that this email was sent to.
213
+ * The alias that this email was sent to.
103
214
  * This is extracted from the `raw` MIME content by parsing the `To:` header.
104
215
  */
105
216
  relayAlias?: string;
106
217
  }
107
218
  /**
108
- * Query options for listing Firefox Relay aliases.
219
+ * Query options for listing aliases.
109
220
  */
110
221
  interface ListAliasesOptions {
111
222
  /** Maximum number of aliases to return */
@@ -114,7 +225,7 @@ interface ListAliasesOptions {
114
225
  offset?: number;
115
226
  }
116
227
  /**
117
- * Query options for retrieving emails from the CF temp email API.
228
+ * Query options for retrieving emails from a mail provider.
118
229
  */
119
230
  interface GetEmailsOptions {
120
231
  /** Maximum number of emails to return */
@@ -237,81 +348,116 @@ declare class RateLimitError extends RelayTempMailError {
237
348
  constructor(message: string, response?: any);
238
349
  }
239
350
 
351
+ declare class TempMailClient {
352
+ private readonly aliasProvider;
353
+ private readonly mailProvider;
354
+ private readonly parser;
355
+ constructor(config: TempMailConfig);
356
+ listAliases(): Promise<RelayAlias[]>;
357
+ createAlias(): Promise<RelayAlias>;
358
+ deleteAlias(id: number): Promise<void>;
359
+ getEmails(aliasAddress?: string, options?: GetEmailsOptions): Promise<ParsedEmail[]>;
360
+ }
361
+ /** @deprecated Use TempMailClient instead */
362
+ declare const RelayClient: typeof TempMailClient;
363
+
240
364
  /**
241
- * Main client for the relay-temp-mail package.
365
+ * HTTP client utilities for the relay-temp-mail package.
242
366
  *
243
- * This module provides the RelayClient class which integrates all components
244
- * (CF API, Relay API, Parser) into a unified interface.
367
+ * This module provides a configurable HTTP client with timeout support,
368
+ * automatic retry logic, and proper error classification for API responses.
245
369
  */
246
-
247
370
  /**
248
- * Main client for interacting with Firefox Relay and CloudFlare temp email services.
249
- *
250
- * RelayClient integrates all components to provide a unified interface for:
251
- * - Managing Firefox Relay email aliases
252
- * - Retrieving and parsing emails from CloudFlare temp email API
253
- *
254
- * @example
255
- * ```typescript
256
- * const client = new RelayClient({
257
- * csrfToken: '...',
258
- * sessionId: '...',
259
- * cfApiUrl: 'https://...',
260
- * cfToken: '...',
261
- * timeout: 30000
262
- * });
371
+ * Options for individual HTTP requests.
372
+ */
373
+ interface RequestOptions {
374
+ /** Custom headers to include in the request */
375
+ headers?: Record<string, string>;
376
+ /** Request body (will be JSON serialized) */
377
+ body?: unknown;
378
+ /** Request timeout in milliseconds (overrides client default) */
379
+ timeout?: number;
380
+ /** Number of retries on failure (overrides client default) */
381
+ retries?: number;
382
+ }
383
+ /**
384
+ * HTTP client for making requests to the relay-temp-mail API.
263
385
  *
264
- * const aliases = await client.listAliases();
265
- * const emails = await client.getEmails('alias@mozmail.com', { limit: 10 });
266
- * ```
386
+ * Provides a configurable base URL with timeout, retry, and automatic
387
+ * JSON parsing capabilities.
267
388
  */
268
- declare class RelayClient {
269
- private readonly relayApi;
270
- private readonly cfApi;
271
- private readonly parser;
389
+ declare class HttpClient$1 {
390
+ private baseUrl;
391
+ private defaultTimeout;
392
+ private defaultRetries;
272
393
  /**
273
- * Creates a new RelayClient instance.
394
+ * Creates a new HttpClient instance.
274
395
  *
275
- * @param config - Configuration object containing authentication tokens and API URLs
396
+ * @param baseUrl - Base URL for all requests (e.g., 'https://api.example.com')
397
+ * @param defaultTimeout - Default timeout in milliseconds (default: 30000)
398
+ * @param defaultRetries - Default number of retries on failure (default: 0)
276
399
  */
277
- constructor(config: RelayConfig);
400
+ constructor(baseUrl: string, defaultTimeout?: number, defaultRetries?: number);
278
401
  /**
279
- * Lists all Firefox Relay email aliases.
402
+ * Makes an HTTP request to the specified path.
280
403
  *
281
- * @returns Promise resolving to an array of RelayAlias objects
282
- * @throws AuthError if authentication fails
283
- * @throws NetworkError if there's a network problem
404
+ * @param method - HTTP method (GET, POST, PUT, DELETE, etc.)
405
+ * @param path - API path (will be appended to baseUrl)
406
+ * @param options - Optional request configuration
407
+ * @returns Promise resolving to the parsed JSON response
284
408
  */
285
- listAliases(): Promise<RelayAlias[]>;
409
+ request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
286
410
  /**
287
- * Creates a new Firefox Relay email alias.
288
- *
289
- * @returns Promise resolving to the newly created RelayAlias
290
- * @throws AuthError if authentication fails
291
- * @throws NetworkError if there's a network problem
411
+ * Executes the actual HTTP request with timeout support.
292
412
  */
293
- createAlias(): Promise<RelayAlias>;
413
+ private executeRequest;
294
414
  /**
295
- * Deletes a Firefox Relay email alias.
296
- *
297
- * @param id - The ID of the alias to delete
298
- * @throws AuthError if authentication fails
299
- * @throws NotFoundError if the alias doesn't exist
300
- * @throws NetworkError if there's a network problem
415
+ * Handles the HTTP response, parsing JSON and checking for errors.
301
416
  */
302
- deleteAlias(id: number): Promise<void>;
417
+ private handleResponse;
303
418
  /**
304
- * Retrieves and parses emails from the CloudFlare temp email API.
305
- *
306
- * If aliasAddress is provided, only emails sent to that address are returned.
307
- *
308
- * @param aliasAddress - Optional email address to filter by
309
- * @param options - Query options for pagination
310
- * @returns Promise resolving to an array of ParsedEmail objects
311
- * @throws AuthError if authentication fails
312
- * @throws NetworkError if there's a network problem
419
+ * Classifies an error based on the error type and HTTP response.
313
420
  */
314
- getEmails(aliasAddress?: string, options?: GetEmailsOptions): Promise<ParsedEmail[]>;
421
+ private classifyError;
422
+ /**
423
+ * Determines if a request should be retried based on the error.
424
+ */
425
+ private shouldRetry;
426
+ /**
427
+ * Sleep for a specified duration.
428
+ */
429
+ private sleep;
430
+ }
431
+
432
+ declare class FirefoxRelayProvider implements AliasProvider {
433
+ private csrfToken;
434
+ private sessionId;
435
+ private httpClient;
436
+ constructor(csrfToken: string, sessionId: string, httpClient?: HttpClient$1);
437
+ private getAuthHeaders;
438
+ listAliases(): Promise<RelayAlias[]>;
439
+ createAlias(): Promise<RelayAlias>;
440
+ deleteAlias(id: number): Promise<void>;
441
+ private mapAliasResponse;
442
+ }
443
+ /** @deprecated Use FirefoxRelayProvider instead */
444
+ declare const RelayAPIClient: typeof FirefoxRelayProvider;
445
+
446
+ declare class CFTempMailProvider implements MailProvider {
447
+ private readonly apiUrl;
448
+ private readonly token;
449
+ private readonly httpClient;
450
+ constructor(apiUrl: string, token: string, httpClient?: HttpClient);
451
+ getMails(limit?: number, offset?: number): Promise<Email[]>;
452
+ private mapCFResponse;
453
+ private handleError;
454
+ }
455
+ /** @deprecated Use CFTempMailProvider instead */
456
+ declare const CFEmailClient: typeof CFTempMailProvider;
457
+ interface HttpClient {
458
+ get(url: string, options?: {
459
+ headers?: Record<string, string>;
460
+ }): Promise<unknown>;
315
461
  }
316
462
 
317
- export { AuthError, type CFMailsResponse, type CreateAliasResponse, type Email, type GetEmailsOptions, type ListAliasesOptions, NetworkError, NotFoundError, ParseError, type ParsedEmail, RateLimitError, type RelayAddressesResponse, type RelayAlias, RelayClient, type RelayConfig, RelayTempMailError };
463
+ export { type AliasProvider, type AliasProviderConfig, AuthError, CFEmailClient, type CFMailsResponse, type CFTempMailConfig, CFTempMailProvider, type CreateAliasResponse, type Email, type FirefoxRelayConfig, FirefoxRelayProvider, type GetEmailsOptions, type ListAliasesOptions, type MailProvider, type MailProviderConfig, NetworkError, NotFoundError, ParseError, type ParsedEmail, RateLimitError, RelayAPIClient, type RelayAddressesResponse, type RelayAlias, RelayClient, type RelayConfig, RelayTempMailError, TempMailClient, type TempMailConfig };