@upyo/mailgun 0.1.0-dev.10

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.
@@ -0,0 +1,300 @@
1
+ import { Message, Receipt, Transport, TransportOptions } from "@upyo/core";
2
+
3
+ //#region src/config.d.ts
4
+
5
+ /**
6
+ * Configuration interface for Mailgun transport connection settings.
7
+ *
8
+ * This interface defines all available options for configuring a Mailgun
9
+ * API connection including authentication, server region, and HTTP options.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const config: MailgunConfig = {
14
+ * apiKey: 'your-api-key',
15
+ * domain: 'your-domain.com',
16
+ * region: 'us', // or 'eu'
17
+ * timeout: 30000,
18
+ * retries: 3
19
+ * };
20
+ * ```
21
+ */
22
+ interface MailgunConfig {
23
+ /**
24
+ * Your Mailgun API key.
25
+ *
26
+ * You can find your API key in the Mailgun Control Panel.
27
+ * It should start with 'key-' for private API keys.
28
+ */
29
+ readonly apiKey: string;
30
+ /**
31
+ * Your Mailgun domain.
32
+ *
33
+ * This is the domain you verified with Mailgun to send emails from.
34
+ */
35
+ readonly domain: string;
36
+ /**
37
+ * Mailgun region where your domain is hosted.
38
+ *
39
+ * @default "us"
40
+ */
41
+ readonly region?: "us" | "eu";
42
+ /**
43
+ * Base URL for the Mailgun API.
44
+ *
45
+ * If not provided, will be automatically determined based on the region.
46
+ * - US region: https://api.mailgun.net/v3
47
+ * - EU region: https://api.eu.mailgun.net/v3
48
+ */
49
+ readonly baseUrl?: string;
50
+ /**
51
+ * HTTP request timeout in milliseconds.
52
+ *
53
+ * @default 30000
54
+ */
55
+ readonly timeout?: number;
56
+ /**
57
+ * Number of retry attempts for failed requests.
58
+ *
59
+ * @default 3
60
+ */
61
+ readonly retries?: number;
62
+ /**
63
+ * Whether to validate SSL certificates.
64
+ *
65
+ * @default true
66
+ */
67
+ readonly validateSsl?: boolean;
68
+ /**
69
+ * Additional HTTP headers to include with requests.
70
+ */
71
+ readonly headers?: Record<string, string>;
72
+ /**
73
+ * Whether to enable tracking for sent messages.
74
+ *
75
+ * @default true
76
+ */
77
+ readonly tracking?: boolean;
78
+ /**
79
+ * Whether to enable click tracking for sent messages.
80
+ *
81
+ * @default true
82
+ */
83
+ readonly clickTracking?: boolean;
84
+ /**
85
+ * Whether to enable open tracking for sent messages.
86
+ *
87
+ * @default true
88
+ */
89
+ readonly openTracking?: boolean;
90
+ }
91
+ /**
92
+ * Resolved Mailgun configuration with all optional fields filled with default values.
93
+ *
94
+ * This type represents the final configuration after applying defaults,
95
+ * used internally by the Mailgun transport implementation.
96
+ */
97
+ type ResolvedMailgunConfig = Required<MailgunConfig>;
98
+ /**
99
+ * Creates a resolved Mailgun configuration by applying default values to optional fields.
100
+ *
101
+ * This function takes a partial Mailgun configuration and returns a complete
102
+ * configuration with all optional fields filled with sensible defaults.
103
+ *
104
+ * @param config - The Mailgun configuration with optional fields
105
+ * @returns A resolved configuration with all defaults applied
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * const resolved = createMailgunConfig({
110
+ * apiKey: 'your-api-key',
111
+ * domain: 'your-domain.com'
112
+ * });
113
+ *
114
+ * // resolved.region will be 'us' (default)
115
+ * // resolved.timeout will be 30000 (default)
116
+ * // resolved.retries will be 3 (default)
117
+ * ```
118
+ */
119
+ declare function createMailgunConfig(config: MailgunConfig): ResolvedMailgunConfig;
120
+ //#endregion
121
+ //#region src/http-client.d.ts
122
+ /**
123
+ * Response from Mailgun API for sending messages.
124
+ */
125
+ interface MailgunResponse {
126
+ /**
127
+ * The message ID returned by Mailgun.
128
+ */
129
+ id: string;
130
+ /**
131
+ * Success message from Mailgun.
132
+ */
133
+ message: string;
134
+ }
135
+ /**
136
+ * Error response from Mailgun API.
137
+ */
138
+
139
+ /**
140
+ * HTTP client wrapper for Mailgun API requests.
141
+ *
142
+ * This class handles authentication, request formatting, error handling,
143
+ * and retry logic for Mailgun API calls.
144
+ */
145
+ declare class MailgunHttpClient {
146
+ private config;
147
+ constructor(config: ResolvedMailgunConfig);
148
+ /**
149
+ * Sends a message via Mailgun API.
150
+ *
151
+ * @param formData The form data to send to Mailgun.
152
+ * @param signal Optional AbortSignal for cancellation.
153
+ * @returns Promise that resolves to the Mailgun response.
154
+ */
155
+ sendMessage(formData: FormData, signal?: AbortSignal): Promise<MailgunResponse>;
156
+ /**
157
+ * Makes an HTTP request to Mailgun API with retry logic.
158
+ *
159
+ * @param url The URL to make the request to.
160
+ * @param options Fetch options.
161
+ * @returns Promise that resolves to the parsed response.
162
+ */
163
+ private makeRequest;
164
+ /**
165
+ * Makes a fetch request with Mailgun authentication.
166
+ *
167
+ * @param url The URL to make the request to.
168
+ * @param options Fetch options.
169
+ * @returns Promise that resolves to the fetch response.
170
+ */
171
+ private fetchWithAuth;
172
+ }
173
+ /**
174
+ * Custom error class for Mailgun API errors.
175
+ */
176
+ declare class MailgunApiError extends Error {
177
+ statusCode?: number;
178
+ constructor(message: string, statusCode?: number);
179
+ }
180
+ //#endregion
181
+ //#region src/mailgun-transport.d.ts
182
+ /**
183
+ * Mailgun transport implementation for sending emails via Mailgun API.
184
+ *
185
+ * This transport provides efficient email delivery using Mailgun's HTTP API,
186
+ * with support for authentication, retry logic, and batch sending capabilities.
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * import { MailgunTransport } from '@upyo/mailgun';
191
+ *
192
+ * const transport = new MailgunTransport({
193
+ * apiKey: 'your-api-key',
194
+ * domain: 'your-domain.com',
195
+ * region: 'us' // or 'eu'
196
+ * });
197
+ *
198
+ * const receipt = await transport.send(message);
199
+ * console.log('Message sent:', receipt.messageId);
200
+ * ```
201
+ */
202
+ declare class MailgunTransport implements Transport {
203
+ config: ReturnType<typeof createMailgunConfig>;
204
+ httpClient: MailgunHttpClient;
205
+ /**
206
+ * Creates a new Mailgun transport instance.
207
+ *
208
+ * @param config Mailgun configuration including API key, domain, and options.
209
+ */
210
+ constructor(config: MailgunConfig);
211
+ /**
212
+ * Sends a single email message via Mailgun API.
213
+ *
214
+ * This method converts the message to Mailgun format, makes an HTTP request
215
+ * to the Mailgun API, and returns a receipt with the result.
216
+ *
217
+ * @example
218
+ * ```typescript
219
+ * const receipt = await transport.send({
220
+ * sender: { address: 'from@example.com' },
221
+ * recipients: [{ address: 'to@example.com' }],
222
+ * ccRecipients: [],
223
+ * bccRecipients: [],
224
+ * replyRecipients: [],
225
+ * subject: 'Hello',
226
+ * content: { text: 'Hello World!' },
227
+ * attachments: [],
228
+ * priority: 'normal',
229
+ * tags: [],
230
+ * headers: new Headers()
231
+ * });
232
+ *
233
+ * if (receipt.successful) {
234
+ * console.log('Message sent with ID:', receipt.messageId);
235
+ * }
236
+ * ```
237
+ *
238
+ * @param message The email message to send.
239
+ * @param options Optional transport options including `AbortSignal` for
240
+ * cancellation.
241
+ * @returns A promise that resolves to a receipt indicating success or
242
+ * failure.
243
+ */
244
+ send(message: Message, options?: TransportOptions): Promise<Receipt>;
245
+ /**
246
+ * Sends multiple email messages efficiently via Mailgun API.
247
+ *
248
+ * This method sends each message individually but provides a streamlined
249
+ * interface for processing multiple messages. Each message is sent as a
250
+ * separate API request to Mailgun.
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * const messages = [
255
+ * {
256
+ * sender: { address: 'from@example.com' },
257
+ * recipients: [{ address: 'user1@example.com' }],
258
+ * ccRecipients: [],
259
+ * bccRecipients: [],
260
+ * replyRecipients: [],
261
+ * subject: 'Message 1',
262
+ * content: { text: 'Hello User 1!' },
263
+ * attachments: [],
264
+ * priority: 'normal',
265
+ * tags: [],
266
+ * headers: new Headers()
267
+ * },
268
+ * {
269
+ * sender: { address: 'from@example.com' },
270
+ * recipients: [{ address: 'user2@example.com' }],
271
+ * ccRecipients: [],
272
+ * bccRecipients: [],
273
+ * replyRecipients: [],
274
+ * subject: 'Message 2',
275
+ * content: { text: 'Hello User 2!' },
276
+ * attachments: [],
277
+ * priority: 'normal',
278
+ * tags: [],
279
+ * headers: new Headers()
280
+ * }
281
+ * ];
282
+ *
283
+ * for await (const receipt of transport.sendMany(messages)) {
284
+ * if (receipt.successful) {
285
+ * console.log('Sent:', receipt.messageId);
286
+ * } else {
287
+ * console.error('Failed:', receipt.errorMessages);
288
+ * }
289
+ * }
290
+ * ```
291
+ *
292
+ * @param messages An iterable or async iterable of messages to send.
293
+ * @param options Optional transport options including `AbortSignal` for
294
+ * cancellation.
295
+ * @returns An async iterable of receipts, one for each message.
296
+ */
297
+ sendMany(messages: Iterable<Message> | AsyncIterable<Message>, options?: TransportOptions): AsyncIterable<Receipt>;
298
+ }
299
+ //#endregion
300
+ export { MailgunApiError, MailgunConfig, MailgunTransport, createMailgunConfig };
@@ -0,0 +1,300 @@
1
+ import { Message, Receipt, Transport, TransportOptions } from "@upyo/core";
2
+
3
+ //#region src/config.d.ts
4
+
5
+ /**
6
+ * Configuration interface for Mailgun transport connection settings.
7
+ *
8
+ * This interface defines all available options for configuring a Mailgun
9
+ * API connection including authentication, server region, and HTTP options.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const config: MailgunConfig = {
14
+ * apiKey: 'your-api-key',
15
+ * domain: 'your-domain.com',
16
+ * region: 'us', // or 'eu'
17
+ * timeout: 30000,
18
+ * retries: 3
19
+ * };
20
+ * ```
21
+ */
22
+ interface MailgunConfig {
23
+ /**
24
+ * Your Mailgun API key.
25
+ *
26
+ * You can find your API key in the Mailgun Control Panel.
27
+ * It should start with 'key-' for private API keys.
28
+ */
29
+ readonly apiKey: string;
30
+ /**
31
+ * Your Mailgun domain.
32
+ *
33
+ * This is the domain you verified with Mailgun to send emails from.
34
+ */
35
+ readonly domain: string;
36
+ /**
37
+ * Mailgun region where your domain is hosted.
38
+ *
39
+ * @default "us"
40
+ */
41
+ readonly region?: "us" | "eu";
42
+ /**
43
+ * Base URL for the Mailgun API.
44
+ *
45
+ * If not provided, will be automatically determined based on the region.
46
+ * - US region: https://api.mailgun.net/v3
47
+ * - EU region: https://api.eu.mailgun.net/v3
48
+ */
49
+ readonly baseUrl?: string;
50
+ /**
51
+ * HTTP request timeout in milliseconds.
52
+ *
53
+ * @default 30000
54
+ */
55
+ readonly timeout?: number;
56
+ /**
57
+ * Number of retry attempts for failed requests.
58
+ *
59
+ * @default 3
60
+ */
61
+ readonly retries?: number;
62
+ /**
63
+ * Whether to validate SSL certificates.
64
+ *
65
+ * @default true
66
+ */
67
+ readonly validateSsl?: boolean;
68
+ /**
69
+ * Additional HTTP headers to include with requests.
70
+ */
71
+ readonly headers?: Record<string, string>;
72
+ /**
73
+ * Whether to enable tracking for sent messages.
74
+ *
75
+ * @default true
76
+ */
77
+ readonly tracking?: boolean;
78
+ /**
79
+ * Whether to enable click tracking for sent messages.
80
+ *
81
+ * @default true
82
+ */
83
+ readonly clickTracking?: boolean;
84
+ /**
85
+ * Whether to enable open tracking for sent messages.
86
+ *
87
+ * @default true
88
+ */
89
+ readonly openTracking?: boolean;
90
+ }
91
+ /**
92
+ * Resolved Mailgun configuration with all optional fields filled with default values.
93
+ *
94
+ * This type represents the final configuration after applying defaults,
95
+ * used internally by the Mailgun transport implementation.
96
+ */
97
+ type ResolvedMailgunConfig = Required<MailgunConfig>;
98
+ /**
99
+ * Creates a resolved Mailgun configuration by applying default values to optional fields.
100
+ *
101
+ * This function takes a partial Mailgun configuration and returns a complete
102
+ * configuration with all optional fields filled with sensible defaults.
103
+ *
104
+ * @param config - The Mailgun configuration with optional fields
105
+ * @returns A resolved configuration with all defaults applied
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * const resolved = createMailgunConfig({
110
+ * apiKey: 'your-api-key',
111
+ * domain: 'your-domain.com'
112
+ * });
113
+ *
114
+ * // resolved.region will be 'us' (default)
115
+ * // resolved.timeout will be 30000 (default)
116
+ * // resolved.retries will be 3 (default)
117
+ * ```
118
+ */
119
+ declare function createMailgunConfig(config: MailgunConfig): ResolvedMailgunConfig;
120
+ //#endregion
121
+ //#region src/http-client.d.ts
122
+ /**
123
+ * Response from Mailgun API for sending messages.
124
+ */
125
+ interface MailgunResponse {
126
+ /**
127
+ * The message ID returned by Mailgun.
128
+ */
129
+ id: string;
130
+ /**
131
+ * Success message from Mailgun.
132
+ */
133
+ message: string;
134
+ }
135
+ /**
136
+ * Error response from Mailgun API.
137
+ */
138
+
139
+ /**
140
+ * HTTP client wrapper for Mailgun API requests.
141
+ *
142
+ * This class handles authentication, request formatting, error handling,
143
+ * and retry logic for Mailgun API calls.
144
+ */
145
+ declare class MailgunHttpClient {
146
+ private config;
147
+ constructor(config: ResolvedMailgunConfig);
148
+ /**
149
+ * Sends a message via Mailgun API.
150
+ *
151
+ * @param formData The form data to send to Mailgun.
152
+ * @param signal Optional AbortSignal for cancellation.
153
+ * @returns Promise that resolves to the Mailgun response.
154
+ */
155
+ sendMessage(formData: FormData, signal?: AbortSignal): Promise<MailgunResponse>;
156
+ /**
157
+ * Makes an HTTP request to Mailgun API with retry logic.
158
+ *
159
+ * @param url The URL to make the request to.
160
+ * @param options Fetch options.
161
+ * @returns Promise that resolves to the parsed response.
162
+ */
163
+ private makeRequest;
164
+ /**
165
+ * Makes a fetch request with Mailgun authentication.
166
+ *
167
+ * @param url The URL to make the request to.
168
+ * @param options Fetch options.
169
+ * @returns Promise that resolves to the fetch response.
170
+ */
171
+ private fetchWithAuth;
172
+ }
173
+ /**
174
+ * Custom error class for Mailgun API errors.
175
+ */
176
+ declare class MailgunApiError extends Error {
177
+ statusCode?: number;
178
+ constructor(message: string, statusCode?: number);
179
+ }
180
+ //#endregion
181
+ //#region src/mailgun-transport.d.ts
182
+ /**
183
+ * Mailgun transport implementation for sending emails via Mailgun API.
184
+ *
185
+ * This transport provides efficient email delivery using Mailgun's HTTP API,
186
+ * with support for authentication, retry logic, and batch sending capabilities.
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * import { MailgunTransport } from '@upyo/mailgun';
191
+ *
192
+ * const transport = new MailgunTransport({
193
+ * apiKey: 'your-api-key',
194
+ * domain: 'your-domain.com',
195
+ * region: 'us' // or 'eu'
196
+ * });
197
+ *
198
+ * const receipt = await transport.send(message);
199
+ * console.log('Message sent:', receipt.messageId);
200
+ * ```
201
+ */
202
+ declare class MailgunTransport implements Transport {
203
+ config: ReturnType<typeof createMailgunConfig>;
204
+ httpClient: MailgunHttpClient;
205
+ /**
206
+ * Creates a new Mailgun transport instance.
207
+ *
208
+ * @param config Mailgun configuration including API key, domain, and options.
209
+ */
210
+ constructor(config: MailgunConfig);
211
+ /**
212
+ * Sends a single email message via Mailgun API.
213
+ *
214
+ * This method converts the message to Mailgun format, makes an HTTP request
215
+ * to the Mailgun API, and returns a receipt with the result.
216
+ *
217
+ * @example
218
+ * ```typescript
219
+ * const receipt = await transport.send({
220
+ * sender: { address: 'from@example.com' },
221
+ * recipients: [{ address: 'to@example.com' }],
222
+ * ccRecipients: [],
223
+ * bccRecipients: [],
224
+ * replyRecipients: [],
225
+ * subject: 'Hello',
226
+ * content: { text: 'Hello World!' },
227
+ * attachments: [],
228
+ * priority: 'normal',
229
+ * tags: [],
230
+ * headers: new Headers()
231
+ * });
232
+ *
233
+ * if (receipt.successful) {
234
+ * console.log('Message sent with ID:', receipt.messageId);
235
+ * }
236
+ * ```
237
+ *
238
+ * @param message The email message to send.
239
+ * @param options Optional transport options including `AbortSignal` for
240
+ * cancellation.
241
+ * @returns A promise that resolves to a receipt indicating success or
242
+ * failure.
243
+ */
244
+ send(message: Message, options?: TransportOptions): Promise<Receipt>;
245
+ /**
246
+ * Sends multiple email messages efficiently via Mailgun API.
247
+ *
248
+ * This method sends each message individually but provides a streamlined
249
+ * interface for processing multiple messages. Each message is sent as a
250
+ * separate API request to Mailgun.
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * const messages = [
255
+ * {
256
+ * sender: { address: 'from@example.com' },
257
+ * recipients: [{ address: 'user1@example.com' }],
258
+ * ccRecipients: [],
259
+ * bccRecipients: [],
260
+ * replyRecipients: [],
261
+ * subject: 'Message 1',
262
+ * content: { text: 'Hello User 1!' },
263
+ * attachments: [],
264
+ * priority: 'normal',
265
+ * tags: [],
266
+ * headers: new Headers()
267
+ * },
268
+ * {
269
+ * sender: { address: 'from@example.com' },
270
+ * recipients: [{ address: 'user2@example.com' }],
271
+ * ccRecipients: [],
272
+ * bccRecipients: [],
273
+ * replyRecipients: [],
274
+ * subject: 'Message 2',
275
+ * content: { text: 'Hello User 2!' },
276
+ * attachments: [],
277
+ * priority: 'normal',
278
+ * tags: [],
279
+ * headers: new Headers()
280
+ * }
281
+ * ];
282
+ *
283
+ * for await (const receipt of transport.sendMany(messages)) {
284
+ * if (receipt.successful) {
285
+ * console.log('Sent:', receipt.messageId);
286
+ * } else {
287
+ * console.error('Failed:', receipt.errorMessages);
288
+ * }
289
+ * }
290
+ * ```
291
+ *
292
+ * @param messages An iterable or async iterable of messages to send.
293
+ * @param options Optional transport options including `AbortSignal` for
294
+ * cancellation.
295
+ * @returns An async iterable of receipts, one for each message.
296
+ */
297
+ sendMany(messages: Iterable<Message> | AsyncIterable<Message>, options?: TransportOptions): AsyncIterable<Receipt>;
298
+ }
299
+ //#endregion
300
+ export { MailgunApiError, MailgunConfig, MailgunTransport, createMailgunConfig };