@parsrun/email 0.1.30 → 0.1.31

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
@@ -38,27 +38,64 @@ export { EmailConfig, EmailSendResult, EmailAddress as ParsEmailAddress, EmailAt
38
38
 
39
39
  /**
40
40
  * Email Service
41
- * High-level email service with provider abstraction
41
+ *
42
+ * High-level email service that provides a unified interface for sending emails
43
+ * through various providers (Resend, SendGrid, Postmark, or Console for development).
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const service = new EmailService({
48
+ * provider: 'resend',
49
+ * apiKey: 'your-api-key',
50
+ * fromEmail: 'hello@example.com',
51
+ * fromName: 'My App',
52
+ * });
53
+ *
54
+ * await service.send({
55
+ * to: 'user@example.com',
56
+ * subject: 'Hello',
57
+ * html: '<p>Welcome!</p>',
58
+ * });
59
+ * ```
42
60
  */
43
61
  declare class EmailService {
44
62
  private provider;
45
63
  private debug;
64
+ /**
65
+ * Creates a new EmailService instance.
66
+ *
67
+ * @param config - The email service configuration including provider type, API key, and default sender info
68
+ */
46
69
  constructor(config: EmailServiceConfig);
47
70
  private createProvider;
48
71
  /**
49
- * Get the provider type
72
+ * Gets the type of email provider being used.
73
+ *
74
+ * @returns The provider type identifier (e.g., 'resend', 'sendgrid', 'postmark', 'console')
50
75
  */
51
76
  get providerType(): EmailProviderType;
52
77
  /**
53
- * Send a single email
78
+ * Sends a single email through the configured provider.
79
+ *
80
+ * @param options - The email options including recipient, subject, and content
81
+ * @returns A promise that resolves to the result of the send operation
82
+ * @throws {EmailError} If the email send fails due to provider error
54
83
  */
55
84
  send(options: EmailOptions): Promise<EmailResult>;
56
85
  /**
57
- * Send multiple emails
86
+ * Sends multiple emails in a batch.
87
+ *
88
+ * If the provider supports native batch sending, it will be used. Otherwise,
89
+ * emails are sent sequentially.
90
+ *
91
+ * @param options - The batch email options including array of emails and error handling config
92
+ * @returns A promise that resolves to the batch result with success/failure counts
58
93
  */
59
94
  sendBatch(options: BatchEmailOptions): Promise<BatchEmailResult>;
60
95
  /**
61
- * Verify provider configuration
96
+ * Verifies the provider configuration by testing the API connection.
97
+ *
98
+ * @returns A promise that resolves to true if the configuration is valid, false otherwise
62
99
  */
63
100
  verify(): Promise<boolean>;
64
101
  }
@@ -91,7 +128,28 @@ declare class EmailService {
91
128
  */
92
129
  declare function createEmailService(config: EmailServiceConfig): EmailService;
93
130
  /**
94
- * Create an email provider directly
131
+ * Creates an email provider instance directly.
132
+ *
133
+ * Use this when you need direct access to a provider without the EmailService wrapper.
134
+ *
135
+ * @param type - The type of email provider to create
136
+ * @param config - The provider configuration including API key and sender info
137
+ * @returns A new email provider instance
138
+ * @throws {EmailError} If an unknown provider type is specified
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * const provider = createEmailProvider('resend', {
143
+ * apiKey: 'your-api-key',
144
+ * fromEmail: 'hello@example.com',
145
+ * });
146
+ *
147
+ * await provider.send({
148
+ * to: 'user@example.com',
149
+ * subject: 'Hello',
150
+ * html: '<p>Hi!</p>',
151
+ * });
152
+ * ```
95
153
  */
96
154
  declare function createEmailProvider(type: EmailProviderType, config: EmailProviderConfig): EmailProvider;
97
155
  declare const _default: {
package/dist/index.js CHANGED
@@ -14,6 +14,13 @@ import {
14
14
  emailConfig
15
15
  } from "@parsrun/types";
16
16
  var EmailError = class extends Error {
17
+ /**
18
+ * Creates a new EmailError instance.
19
+ *
20
+ * @param message - Human-readable error description
21
+ * @param code - Error code from EmailErrorCodes for programmatic handling
22
+ * @param cause - The underlying error that caused this error, if any
23
+ */
17
24
  constructor(message, code, cause) {
18
25
  super(message);
19
26
  this.code = code;
@@ -34,11 +41,17 @@ var EmailErrorCodes = {
34
41
 
35
42
  // src/providers/resend.ts
36
43
  var ResendProvider = class {
44
+ /** Provider type identifier */
37
45
  type = "resend";
38
46
  apiKey;
39
47
  fromEmail;
40
48
  fromName;
41
49
  baseUrl = "https://api.resend.com";
50
+ /**
51
+ * Creates a new ResendProvider instance.
52
+ *
53
+ * @param config - The provider configuration including API key and sender info
54
+ */
42
55
  constructor(config) {
43
56
  this.apiKey = config.apiKey;
44
57
  this.fromEmail = config.fromEmail;
@@ -59,6 +72,13 @@ var ResendProvider = class {
59
72
  }
60
73
  return [this.formatAddress(addresses)];
61
74
  }
75
+ /**
76
+ * Sends an email via the Resend API.
77
+ *
78
+ * @param options - The email options including recipient, subject, and content
79
+ * @returns A promise that resolves to the send result with message ID if successful
80
+ * @throws {EmailError} If the Resend API request fails
81
+ */
62
82
  async send(options) {
63
83
  const from = options.from ? this.formatAddress(options.from) : this.fromName ? `${this.fromName} <${this.fromEmail}>` : this.fromEmail;
64
84
  const payload = {
@@ -111,6 +131,12 @@ var ResendProvider = class {
111
131
  );
112
132
  }
113
133
  }
134
+ /**
135
+ * Sends multiple emails via the Resend API sequentially.
136
+ *
137
+ * @param options - The batch email options containing emails and error handling config
138
+ * @returns A promise that resolves to the batch result with success/failure counts
139
+ */
114
140
  async sendBatch(options) {
115
141
  const results = [];
116
142
  let successful = 0;
@@ -141,6 +167,11 @@ var ResendProvider = class {
141
167
  results
142
168
  };
143
169
  }
170
+ /**
171
+ * Verifies the Resend API key by checking configured domains.
172
+ *
173
+ * @returns A promise that resolves to true if the API key is valid, false otherwise
174
+ */
144
175
  async verify() {
145
176
  try {
146
177
  const response = await fetch(`${this.baseUrl}/domains`, {
@@ -153,6 +184,14 @@ var ResendProvider = class {
153
184
  return false;
154
185
  }
155
186
  }
187
+ /**
188
+ * Converts a Uint8Array to a base64-encoded string.
189
+ *
190
+ * Edge-compatible base64 encoding that works in all JavaScript runtimes.
191
+ *
192
+ * @param data - The binary data to encode
193
+ * @returns The base64-encoded string
194
+ */
156
195
  uint8ArrayToBase64(data) {
157
196
  let binary = "";
158
197
  for (let i = 0; i < data.length; i++) {
@@ -170,11 +209,17 @@ function createResendProvider(config) {
170
209
 
171
210
  // src/providers/sendgrid.ts
172
211
  var SendGridProvider = class {
212
+ /** Provider type identifier */
173
213
  type = "sendgrid";
174
214
  apiKey;
175
215
  fromEmail;
176
216
  fromName;
177
217
  baseUrl = "https://api.sendgrid.com/v3";
218
+ /**
219
+ * Creates a new SendGridProvider instance.
220
+ *
221
+ * @param config - The provider configuration including API key and sender info
222
+ */
178
223
  constructor(config) {
179
224
  this.apiKey = config.apiKey;
180
225
  this.fromEmail = config.fromEmail;
@@ -192,6 +237,13 @@ var SendGridProvider = class {
192
237
  }
193
238
  return [this.formatAddress(addresses)];
194
239
  }
240
+ /**
241
+ * Sends an email via the SendGrid API.
242
+ *
243
+ * @param options - The email options including recipient, subject, and content
244
+ * @returns A promise that resolves to the send result with message ID if successful
245
+ * @throws {EmailError} If the SendGrid API request fails
246
+ */
195
247
  async send(options) {
196
248
  const from = options.from ? this.formatAddress(options.from) : this.fromName ? { email: this.fromEmail, name: this.fromName } : { email: this.fromEmail };
197
249
  const personalization = {
@@ -266,6 +318,15 @@ var SendGridProvider = class {
266
318
  );
267
319
  }
268
320
  }
321
+ /**
322
+ * Sends multiple emails via the SendGrid API sequentially.
323
+ *
324
+ * Note: SendGrid does not support true batch sending via the standard API,
325
+ * so emails are sent one at a time.
326
+ *
327
+ * @param options - The batch email options containing emails and error handling config
328
+ * @returns A promise that resolves to the batch result with success/failure counts
329
+ */
269
330
  async sendBatch(options) {
270
331
  const results = [];
271
332
  let successful = 0;
@@ -296,6 +357,11 @@ var SendGridProvider = class {
296
357
  results
297
358
  };
298
359
  }
360
+ /**
361
+ * Verifies the SendGrid API key by checking API scopes.
362
+ *
363
+ * @returns A promise that resolves to true if the API key is valid, false otherwise
364
+ */
299
365
  async verify() {
300
366
  try {
301
367
  const response = await fetch(`${this.baseUrl}/scopes`, {
@@ -308,6 +374,12 @@ var SendGridProvider = class {
308
374
  return false;
309
375
  }
310
376
  }
377
+ /**
378
+ * Converts a Uint8Array to a base64-encoded string.
379
+ *
380
+ * @param data - The binary data to encode
381
+ * @returns The base64-encoded string
382
+ */
311
383
  uint8ArrayToBase64(data) {
312
384
  let binary = "";
313
385
  for (let i = 0; i < data.length; i++) {
@@ -325,11 +397,17 @@ function createSendGridProvider(config) {
325
397
 
326
398
  // src/providers/postmark.ts
327
399
  var PostmarkProvider = class {
400
+ /** Provider type identifier */
328
401
  type = "postmark";
329
402
  apiKey;
330
403
  fromEmail;
331
404
  fromName;
332
405
  baseUrl = "https://api.postmarkapp.com";
406
+ /**
407
+ * Creates a new PostmarkProvider instance.
408
+ *
409
+ * @param config - The provider configuration including API key (server token) and sender info
410
+ */
333
411
  constructor(config) {
334
412
  this.apiKey = config.apiKey;
335
413
  this.fromEmail = config.fromEmail;
@@ -350,6 +428,13 @@ var PostmarkProvider = class {
350
428
  }
351
429
  return this.formatAddress(addresses);
352
430
  }
431
+ /**
432
+ * Sends an email via the Postmark API.
433
+ *
434
+ * @param options - The email options including recipient, subject, and content
435
+ * @returns A promise that resolves to the send result with message ID if successful
436
+ * @throws {EmailError} If the Postmark API request fails
437
+ */
353
438
  async send(options) {
354
439
  const from = options.from ? this.formatAddress(options.from) : this.fromName ? `${this.fromName} <${this.fromEmail}>` : this.fromEmail;
355
440
  const payload = {
@@ -411,6 +496,15 @@ var PostmarkProvider = class {
411
496
  );
412
497
  }
413
498
  }
499
+ /**
500
+ * Sends multiple emails via Postmark's batch API.
501
+ *
502
+ * Postmark supports native batch sending of up to 500 emails in a single request.
503
+ *
504
+ * @param options - The batch email options containing emails and error handling config
505
+ * @returns A promise that resolves to the batch result with success/failure counts
506
+ * @throws {EmailError} If the Postmark batch API request fails
507
+ */
414
508
  async sendBatch(options) {
415
509
  const batchPayload = options.emails.map((email) => {
416
510
  const from = email.from ? this.formatAddress(email.from) : this.fromName ? `${this.fromName} <${this.fromEmail}>` : this.fromEmail;
@@ -459,6 +553,11 @@ var PostmarkProvider = class {
459
553
  );
460
554
  }
461
555
  }
556
+ /**
557
+ * Verifies the Postmark server token by checking server info.
558
+ *
559
+ * @returns A promise that resolves to true if the server token is valid, false otherwise
560
+ */
462
561
  async verify() {
463
562
  try {
464
563
  const response = await fetch(`${this.baseUrl}/server`, {
@@ -472,6 +571,12 @@ var PostmarkProvider = class {
472
571
  return false;
473
572
  }
474
573
  }
574
+ /**
575
+ * Converts a Uint8Array to a base64-encoded string.
576
+ *
577
+ * @param data - The binary data to encode
578
+ * @returns The base64-encoded string
579
+ */
475
580
  uint8ArrayToBase64(data) {
476
581
  let binary = "";
477
582
  for (let i = 0; i < data.length; i++) {
@@ -489,10 +594,16 @@ function createPostmarkProvider(config) {
489
594
 
490
595
  // src/providers/console.ts
491
596
  var ConsoleProvider = class {
597
+ /** Provider type identifier */
492
598
  type = "console";
493
599
  fromEmail;
494
600
  fromName;
495
601
  messageCounter = 0;
602
+ /**
603
+ * Creates a new ConsoleProvider instance.
604
+ *
605
+ * @param config - The provider configuration (apiKey is not required for console provider)
606
+ */
496
607
  constructor(config) {
497
608
  this.fromEmail = config.fromEmail;
498
609
  this.fromName = config.fromName;
@@ -512,6 +623,12 @@ var ConsoleProvider = class {
512
623
  }
513
624
  return this.formatAddress(addresses);
514
625
  }
626
+ /**
627
+ * Logs an email to the console instead of sending it.
628
+ *
629
+ * @param options - The email options to log
630
+ * @returns A promise that resolves to a successful result with a generated message ID
631
+ */
515
632
  async send(options) {
516
633
  this.messageCounter++;
517
634
  const messageId = `console-${Date.now()}-${this.messageCounter}`;
@@ -566,6 +683,12 @@ ${separator}`);
566
683
  messageId
567
684
  };
568
685
  }
686
+ /**
687
+ * Logs multiple emails to the console.
688
+ *
689
+ * @param options - The batch email options containing emails to log
690
+ * @returns A promise that resolves to the batch result with success/failure counts
691
+ */
569
692
  async sendBatch(options) {
570
693
  const results = [];
571
694
  let successful = 0;
@@ -600,6 +723,13 @@ ${separator}`);
600
723
  results
601
724
  };
602
725
  }
726
+ /**
727
+ * Verifies the provider configuration.
728
+ *
729
+ * For the console provider, this always returns true and logs a message.
730
+ *
731
+ * @returns A promise that always resolves to true
732
+ */
603
733
  async verify() {
604
734
  console.log("\u{1F4E7} Console email provider verified (always returns true)");
605
735
  return true;
@@ -1010,6 +1140,11 @@ var renderFunctions = {
1010
1140
  var EmailService = class {
1011
1141
  provider;
1012
1142
  debug;
1143
+ /**
1144
+ * Creates a new EmailService instance.
1145
+ *
1146
+ * @param config - The email service configuration including provider type, API key, and default sender info
1147
+ */
1013
1148
  constructor(config) {
1014
1149
  this.debug = config.debug ?? false;
1015
1150
  this.provider = this.createProvider(config);
@@ -1038,13 +1173,19 @@ var EmailService = class {
1038
1173
  }
1039
1174
  }
1040
1175
  /**
1041
- * Get the provider type
1176
+ * Gets the type of email provider being used.
1177
+ *
1178
+ * @returns The provider type identifier (e.g., 'resend', 'sendgrid', 'postmark', 'console')
1042
1179
  */
1043
1180
  get providerType() {
1044
1181
  return this.provider.type;
1045
1182
  }
1046
1183
  /**
1047
- * Send a single email
1184
+ * Sends a single email through the configured provider.
1185
+ *
1186
+ * @param options - The email options including recipient, subject, and content
1187
+ * @returns A promise that resolves to the result of the send operation
1188
+ * @throws {EmailError} If the email send fails due to provider error
1048
1189
  */
1049
1190
  async send(options) {
1050
1191
  if (this.debug) {
@@ -1061,7 +1202,13 @@ var EmailService = class {
1061
1202
  return result;
1062
1203
  }
1063
1204
  /**
1064
- * Send multiple emails
1205
+ * Sends multiple emails in a batch.
1206
+ *
1207
+ * If the provider supports native batch sending, it will be used. Otherwise,
1208
+ * emails are sent sequentially.
1209
+ *
1210
+ * @param options - The batch email options including array of emails and error handling config
1211
+ * @returns A promise that resolves to the batch result with success/failure counts
1065
1212
  */
1066
1213
  async sendBatch(options) {
1067
1214
  if (this.debug) {
@@ -1111,7 +1258,9 @@ var EmailService = class {
1111
1258
  };
1112
1259
  }
1113
1260
  /**
1114
- * Verify provider configuration
1261
+ * Verifies the provider configuration by testing the API connection.
1262
+ *
1263
+ * @returns A promise that resolves to true if the configuration is valid, false otherwise
1115
1264
  */
1116
1265
  async verify() {
1117
1266
  if (this.provider.verify) {