@upyo/sendgrid 0.1.0-dev.13

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