@kumix/email 0.1.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.
@@ -0,0 +1,401 @@
1
+ import React from "react";
2
+
3
+ //#region src/types.d.ts
4
+ /**
5
+ * Email service type definitions and interfaces
6
+ * Provides comprehensive TypeScript types for email configuration, sending options, and results
7
+ */
8
+ /**
9
+ * Supported email service providers
10
+ * @public
11
+ */
12
+ type EmailProvider = "resend" | "nodemailer";
13
+ /**
14
+ * Base email configuration interface
15
+ * Common properties shared by all email providers
16
+ * @public
17
+ */
18
+ interface BaseEmailConfig {
19
+ /** Email service provider */
20
+ provider: EmailProvider;
21
+ /** Default sender information */
22
+ from: {
23
+ /** Sender name */name: string; /** Sender email address */
24
+ email: string;
25
+ };
26
+ /** Reply-to email address */
27
+ replyTo?: string;
28
+ }
29
+ /**
30
+ * Resend email service configuration
31
+ * @public
32
+ */
33
+ interface ResendConfig extends BaseEmailConfig {
34
+ provider: "resend";
35
+ /** Resend API key */
36
+ apiKey: string;
37
+ }
38
+ /**
39
+ * Nodemailer/SMTP email service configuration
40
+ * @public
41
+ */
42
+ interface NodemailerConfig extends BaseEmailConfig {
43
+ provider: "nodemailer";
44
+ /** SMTP server configuration */
45
+ smtp: {
46
+ /** SMTP server hostname */host: string; /** SMTP server port */
47
+ port: number; /** Use secure connection (TLS) */
48
+ secure: boolean; /** Authentication credentials */
49
+ auth: {
50
+ /** SMTP username */user: string; /** SMTP password */
51
+ pass: string;
52
+ };
53
+ };
54
+ }
55
+ /**
56
+ * Union type for all email configurations
57
+ * @public
58
+ */
59
+ type EmailConfig = ResendConfig | NodemailerConfig;
60
+ /**
61
+ * Email attachment configuration
62
+ * @public
63
+ */
64
+ interface EmailAttachment {
65
+ /** Attachment filename */
66
+ filename: string;
67
+ /** Attachment content as Uint8Array, Buffer, or string */
68
+ content: Uint8Array | string;
69
+ /** MIME type of the attachment */
70
+ contentType?: string;
71
+ /** Content-ID for inline images */
72
+ cid?: string;
73
+ }
74
+ /**
75
+ * Email sending options
76
+ * @public
77
+ */
78
+ interface SendEmailOptions {
79
+ /** Recipient email address(es) */
80
+ to: string | string[];
81
+ /** CC recipient email address(es) */
82
+ cc?: string | string[];
83
+ /** BCC recipient email address(es) */
84
+ bcc?: string | string[];
85
+ /** Email subject line */
86
+ subject: string;
87
+ /** HTML content of the email */
88
+ html?: string;
89
+ /** Plain text content of the email */
90
+ text?: string;
91
+ /** Email attachments */
92
+ attachments?: EmailAttachment[];
93
+ /** Custom email headers */
94
+ headers?: Record<string, string>;
95
+ /** Email tags for tracking and analytics */
96
+ tags?: Record<string, string>;
97
+ /** Email priority (1=highest, 3=normal, 5=lowest) */
98
+ priority?: 1 | 2 | 3 | 4 | 5;
99
+ /** Schedule email delivery for later */
100
+ scheduledAt?: Date;
101
+ }
102
+ /**
103
+ * Email template data interface
104
+ * @public
105
+ */
106
+ interface EmailTemplateData {
107
+ /** Template data properties */
108
+ [key: string]: unknown;
109
+ }
110
+ /**
111
+ * Email sending result
112
+ * @public
113
+ */
114
+ interface EmailResult {
115
+ /** Whether the email was sent successfully */
116
+ success: boolean;
117
+ /** Unique message ID from the email provider */
118
+ messageId?: string;
119
+ /** Error message if sending failed */
120
+ error?: string;
121
+ /** Additional metadata from the provider */
122
+ metadata?: Record<string, unknown>;
123
+ }
124
+ /**
125
+ * Email validation result
126
+ * @public
127
+ */
128
+ interface EmailValidationResult {
129
+ /** Whether the email address is valid */
130
+ valid: boolean;
131
+ /** Error message if validation failed */
132
+ error?: string;
133
+ }
134
+ /**
135
+ * Email service provider interface
136
+ * @public
137
+ */
138
+ interface IEmailProvider {
139
+ /** Send an email */
140
+ send(options: SendEmailOptions): Promise<EmailResult>;
141
+ /** Validate provider configuration */
142
+ validateConfig?(): Promise<EmailValidationResult>;
143
+ }
144
+ /**
145
+ * Email template options
146
+ * @public
147
+ */
148
+ interface EmailTemplateOptions {
149
+ /** Template name or ID */
150
+ template: string;
151
+ /** Template data/variables */
152
+ data: EmailTemplateData;
153
+ /** Recipient email address(es) */
154
+ to: string | string[];
155
+ /** CC recipient email address(es) */
156
+ cc?: string | string[];
157
+ /** BCC recipient email address(es) */
158
+ bcc?: string | string[];
159
+ /** Email subject (if not defined in template) */
160
+ subject?: string;
161
+ /** Email attachments */
162
+ attachments?: EmailAttachment[];
163
+ /** Custom email headers */
164
+ headers?: Record<string, string>;
165
+ /** Email tags for tracking and analytics */
166
+ tags?: Record<string, string>;
167
+ }
168
+ /**
169
+ * Bulk email sending options
170
+ * @public
171
+ */
172
+ interface BulkEmailOptions {
173
+ /** Array of email sending options */
174
+ emails: SendEmailOptions[];
175
+ /** Batch size for sending (default: 100) */
176
+ batchSize?: number;
177
+ /** Delay between batches in milliseconds (default: 1000) */
178
+ batchDelay?: number;
179
+ }
180
+ /**
181
+ * Bulk email sending result
182
+ * @public
183
+ */
184
+ interface BulkEmailResult {
185
+ /** Whether all emails were processed */
186
+ success: boolean;
187
+ /** Total number of emails processed */
188
+ total: number;
189
+ /** Number of successfully sent emails */
190
+ sent: number;
191
+ /** Number of failed emails */
192
+ failed: number;
193
+ /** Array of individual email results */
194
+ results: EmailResult[];
195
+ /** Array of errors for failed emails */
196
+ errors: Array<{
197
+ index: number;
198
+ error: string;
199
+ }>;
200
+ }
201
+ /**
202
+ * Email configuration validation result
203
+ * @public
204
+ */
205
+ interface ConfigValidationResult {
206
+ /** Whether the configuration is valid */
207
+ valid: boolean;
208
+ /** Array of missing required fields */
209
+ missing: string[];
210
+ /** Array of validation errors */
211
+ errors: string[];
212
+ }
213
+ /**
214
+ * Email analytics data
215
+ * @public
216
+ */
217
+ interface EmailAnalytics {
218
+ /** Number of emails sent */
219
+ sent: number;
220
+ /** Number of emails delivered */
221
+ delivered: number;
222
+ /** Number of emails opened */
223
+ opened: number;
224
+ /** Number of emails clicked */
225
+ clicked: number;
226
+ /** Number of emails bounced */
227
+ bounced: number;
228
+ /** Number of emails marked as spam */
229
+ spam: number;
230
+ /** Delivery rate percentage */
231
+ deliveryRate: number;
232
+ /** Open rate percentage */
233
+ openRate: number;
234
+ /** Click rate percentage */
235
+ clickRate: number;
236
+ }
237
+ /**
238
+ * Email webhook event types
239
+ * @public
240
+ */
241
+ type EmailWebhookEvent = "sent" | "delivered" | "opened" | "clicked" | "bounced" | "spam" | "unsubscribed";
242
+ /**
243
+ * Email webhook payload
244
+ * @public
245
+ */
246
+ interface EmailWebhookPayload {
247
+ /** Event type */
248
+ event: EmailWebhookEvent;
249
+ /** Message ID */
250
+ messageId: string;
251
+ /** Recipient email */
252
+ email: string;
253
+ /** Timestamp of the event */
254
+ timestamp: Date;
255
+ /** Additional event data */
256
+ data?: Record<string, unknown>;
257
+ }
258
+ //#endregion
259
+ //#region src/config.d.ts
260
+ /**
261
+ * Cross-runtime environment record.
262
+ * Pass `process.env` on Node.js, `ctx.env` on Cloudflare Workers,
263
+ * `Deno.env.toObject()` on Deno, or a plain object in the browser.
264
+ */
265
+ type EnvRecord = Record<string, string | undefined>;
266
+ declare function loadResendConfig(env?: EnvRecord): ResendConfig | null;
267
+ declare function loadNodemailerConfig(env?: EnvRecord): NodemailerConfig | null;
268
+ declare function loadEmailConfig(env?: EnvRecord): EmailConfig | null;
269
+ declare function hasEmailConfig(env?: EnvRecord): boolean;
270
+ declare function getEmailEnvVars(env?: EnvRecord): Record<string, string | undefined>;
271
+ declare function validateResendEnvVars(env?: EnvRecord): ConfigValidationResult;
272
+ declare function validateNodemailerEnvVars(env?: EnvRecord): ConfigValidationResult;
273
+ declare function validateEmailEnvVars(env?: EnvRecord): ConfigValidationResult;
274
+ //#endregion
275
+ //#region src/services.d.ts
276
+ declare class EmailService {
277
+ private provider;
278
+ private config;
279
+ constructor(config: EmailConfig);
280
+ private createProvider;
281
+ sendEmail(options: SendEmailOptions): Promise<EmailResult>;
282
+ sendTemplate<T extends EmailTemplateData>(Template: React.ComponentType<T>, props: T, options: Omit<SendEmailOptions, "html" | "text">): Promise<EmailResult>;
283
+ getConfig(): EmailConfig;
284
+ updateConfig(config: EmailConfig): void;
285
+ validateConfig(): Promise<EmailValidationResult>;
286
+ }
287
+ //#endregion
288
+ //#region src/factory.d.ts
289
+ declare function createEmail(config?: EmailConfig, env?: EnvRecord): EmailService;
290
+ declare function createResend(config?: Omit<ResendConfig, "provider"> & {
291
+ provider?: "resend";
292
+ }, env?: EnvRecord): EmailService;
293
+ declare function createNodemailer(config?: Omit<NodemailerConfig, "provider"> & {
294
+ provider?: "nodemailer";
295
+ }, env?: EnvRecord): EmailService;
296
+ declare function isEmailConfigured(env?: EnvRecord): boolean;
297
+ declare function getConfiguredProvider(env?: EnvRecord): EmailProvider | null;
298
+ //#endregion
299
+ //#region src/providers/nodemailer.d.ts
300
+ declare class NodemailerProvider implements IEmailProvider {
301
+ private config;
302
+ private _transporter;
303
+ constructor(config: NodemailerConfig);
304
+ private getTransporter;
305
+ validateConfig(): Promise<EmailValidationResult>;
306
+ send(options: SendEmailOptions): Promise<EmailResult>;
307
+ }
308
+ //#endregion
309
+ //#region src/providers/resend.d.ts
310
+ /**
311
+ * Resend email provider implementation
312
+ * @public
313
+ *
314
+ * @example
315
+ * ```typescript
316
+ * import { ResendProvider } from '@kumix/email';
317
+ *
318
+ * const provider = new ResendProvider({
319
+ * provider: 'resend',
320
+ * apiKey: 'your-resend-api-key',
321
+ * from: {
322
+ * name: 'Your App',
323
+ * email: 'noreply@yourapp.com'
324
+ * }
325
+ * });
326
+ *
327
+ * const result = await provider.send({
328
+ * to: 'user@example.com',
329
+ * subject: 'Hello',
330
+ * html: '<h1>Hello World</h1>'
331
+ * });
332
+ *
333
+ * if (result.success) {
334
+ * console.log('Email sent with ID:', result.messageId);
335
+ * } else {
336
+ * console.error('Failed to send email:', result.error);
337
+ * }
338
+ * ```
339
+ */
340
+ declare class ResendProvider implements IEmailProvider {
341
+ private resend;
342
+ private config;
343
+ /**
344
+ * Create a new Resend provider instance
345
+ * @param config Resend configuration including API key and sender info
346
+ * @throws Error if API key is missing
347
+ */
348
+ constructor(config: ResendConfig);
349
+ /**
350
+ * Validate Resend configuration
351
+ * @returns Promise resolving to validation result
352
+ * @public
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * const validation = await provider.validateConfig();
357
+ * if (!validation.valid) {
358
+ * console.error('Configuration error:', validation.error);
359
+ * } else {
360
+ * console.log('Resend configuration is valid');
361
+ * }
362
+ * ```
363
+ */
364
+ validateConfig(): Promise<EmailValidationResult>;
365
+ /**
366
+ * Send an email using Resend
367
+ * @param options Email sending options
368
+ * @returns Promise resolving to email sending result
369
+ * @public
370
+ *
371
+ * @example
372
+ * ```typescript
373
+ * const result = await provider.send({
374
+ * to: ['user1@example.com', 'user2@example.com'],
375
+ * cc: 'manager@example.com',
376
+ * subject: 'Important Update',
377
+ * html: '<h1>Hello</h1><p>This is an important update.</p>',
378
+ * text: 'Hello\n\nThis is an important update.',
379
+ * attachments: [{
380
+ * filename: 'document.pdf',
381
+ * content: pdfBuffer,
382
+ * contentType: 'application/pdf'
383
+ * }],
384
+ * tags: {
385
+ * category: 'notification',
386
+ * priority: 'high'
387
+ * }
388
+ * });
389
+ *
390
+ * if (result.success) {
391
+ * console.log('Email sent with ID:', result.messageId);
392
+ * } else {
393
+ * console.error('Failed to send email:', result.error);
394
+ * }
395
+ * ```
396
+ */
397
+ send(options: SendEmailOptions): Promise<EmailResult>;
398
+ }
399
+ //#endregion
400
+ export { type BulkEmailOptions, type BulkEmailResult, type ConfigValidationResult, type EmailAnalytics, type EmailAttachment, type EmailConfig, type EmailProvider, type EmailResult, EmailService, type EmailTemplateData, type EmailTemplateOptions, type EmailValidationResult, type EmailWebhookEvent, type EmailWebhookPayload, type EnvRecord, type IEmailProvider, type NodemailerConfig, NodemailerProvider, type ResendConfig, ResendProvider, type SendEmailOptions, createEmail, createNodemailer, createResend, getConfiguredProvider, getEmailEnvVars, hasEmailConfig, isEmailConfigured, loadEmailConfig, loadNodemailerConfig, loadResendConfig, validateEmailEnvVars, validateNodemailerEnvVars, validateResendEnvVars };
401
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/config.ts","../src/services.ts","../src/factory.ts","../src/providers/nodemailer.ts","../src/providers/resend.ts"],"mappings":";;;;;;AASA;;;;AAAyB;KAAb,aAAA;;;;;;UAOK,eAAA;EAMb;EAJF,QAAA,EAAU,aAAa;EASvB;EAPA,IAAA;IAOO,kBALL,IAAA,UAY0B;IAV1B,KAAA;EAAA;EAUkC;EAPpC,OAAA;AAAA;;AAUM;AAOR;;UAViB,YAAA,SAAqB,eAAe;EACnD,QAAA;EASwC;EAPxC,MAAA;AAAA;;;;;UAOe,gBAAA,SAAyB,eAAe;EACvD,QAAA;EAcQ;EAZR,IAAA;IAqBU,2BAnBR,IAAA,UAmBsB;IAjBtB,IAAA,UAiBqD;IAfrD,MAAA,WAqB4B;IAnB5B,IAAA;MAuBiB,oBArBf,IAAA,UAqBJ;MAnBI,IAAA;IAAA;EAAA;AAAA;AAuBD;AAOL;;;AAPK,KAdO,WAAA,GAAc,YAAA,GAAe,gBAAgB;;;;;UAMxC,eAAA;EAiBf;EAfA,QAAA;EAmBA;EAjBA,OAAA,EAAS,UAAU;EAqBnB;EAnBA,WAAA;EAuBA;EArBA,GAAA;AAAA;;;;;UAOe,gBAAA;EAsBD;EApBd,EAAA;EAoBkB;EAlBlB,EAAA;EAyBgC;EAvBhC,GAAA;EAyBY;EAvBZ,OAAA;EA8Be;EA5Bf,IAAA;;EAEA,IAAA;EA4BA;EA1BA,WAAA,GAAc,eAAA;EA8Bd;EA5BA,OAAA,GAAU,MAAA;EA8BC;EA5BX,IAAA,GAAO,MAAA;EA4BU;EA1BjB,QAAA;EAiCoC;EA/BpC,WAAA,GAAc,IAAA;AAAA;AAmCT;AAOP;;;AAPO,UA5BU,iBAAA;EAqC0B;EAAA,CAnCxC,GAAW;AAAA;;;;;UAOG,WAAA;EA4BV;EA1BL,OAAA;EA0ByC;EAxBzC,SAAA;EA0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