@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,325 @@
1
+ import React from "react";
2
+
3
+ //#region src/helpers.d.ts
4
+ /**
5
+ * Render React component to HTML string
6
+ *
7
+ * Converts a React email template component to HTML string that can be sent via email.
8
+ *
9
+ * @param Template - React component to render
10
+ * @param props - Props to pass to the component
11
+ * @returns Promise resolving to HTML string
12
+ * @example
13
+ * ```typescript
14
+ * const WelcomeEmail = ({ name }: { name: string }) => <h1>Welcome {name}!</h1>;
15
+ *
16
+ * const html = await renderEmailTemplate(WelcomeEmail, { name: 'John' });
17
+ * console.log(html); // "<h1>Welcome John!</h1>"
18
+ * ```
19
+ */
20
+ declare function renderEmailTemplate<T extends Record<string, unknown>>(Template: React.ComponentType<T>, props: T): Promise<string>;
21
+ /**
22
+ * Generate plain text from HTML
23
+ *
24
+ * Converts HTML content to plain text by removing tags and normalizing whitespace.
25
+ * Useful for creating text versions of HTML emails.
26
+ *
27
+ * @param html - HTML string to convert
28
+ * @returns Plain text version of the HTML
29
+ * @example
30
+ * ```typescript
31
+ * const html = '<h1>Hello</h1><p>Welcome to our <strong>platform</strong>!</p>';
32
+ * const text = htmlToText(html);
33
+ * console.log(text); // "Hello Welcome to our platform!"
34
+ * ```
35
+ */
36
+ declare function htmlToText(html: string): string;
37
+ /**
38
+ * Validate email address format
39
+ *
40
+ * Checks if an email address has a valid format using RFC 5322 compliant regex.
41
+ *
42
+ * @param email - Email address to validate
43
+ * @returns true if email format is valid, false otherwise
44
+ * @example
45
+ * ```typescript
46
+ * console.log(isValidEmail('user@example.com')); // true
47
+ * console.log(isValidEmail('invalid-email')); // false
48
+ * console.log(isValidEmail('user+tag@example.co.uk')); // true
49
+ * ```
50
+ */
51
+ declare function isValidEmail(email: string): boolean;
52
+ /**
53
+ * Validate multiple email addresses
54
+ *
55
+ * Validates an array of email addresses or a single email address.
56
+ *
57
+ * @param emails - Single email or array of emails to validate
58
+ * @returns true if all emails are valid, false if any are invalid
59
+ * @example
60
+ * ```typescript
61
+ * console.log(validateEmails('user@example.com')); // true
62
+ * console.log(validateEmails(['user1@example.com', 'user2@example.com'])); // true
63
+ * console.log(validateEmails(['valid@example.com', 'invalid-email'])); // false
64
+ * ```
65
+ */
66
+ declare function validateEmails(emails: string | string[]): boolean;
67
+ /**
68
+ * Filter valid emails from a list
69
+ *
70
+ * Returns only the valid email addresses from a list, filtering out invalid ones.
71
+ *
72
+ * @param emails - Array of email addresses to filter
73
+ * @returns Array containing only valid email addresses
74
+ * @example
75
+ * ```typescript
76
+ * const emails = ['valid@example.com', 'invalid-email', 'another@example.com'];
77
+ * const validEmails = filterValidEmails(emails);
78
+ * console.log(validEmails); // ['valid@example.com', 'another@example.com']
79
+ * ```
80
+ */
81
+ declare function filterValidEmails(emails: string[]): string[];
82
+ /**
83
+ * Format email address with display name
84
+ *
85
+ * Combines a display name with an email address in the standard format.
86
+ *
87
+ * @param name - Display name for the email address
88
+ * @param email - Email address
89
+ * @returns Formatted email address string
90
+ * @example
91
+ * ```typescript
92
+ * const formatted = formatEmailAddress('John Doe', 'john@example.com');
93
+ * console.log(formatted); // "John Doe <john@example.com>"
94
+ * ```
95
+ */
96
+ declare function formatEmailAddress(name: string, email: string): string;
97
+ /**
98
+ * Extract email address from formatted string
99
+ *
100
+ * Extracts the email address from a formatted "Name <email>" string.
101
+ *
102
+ * @param formattedAddress - Formatted email address string
103
+ * @returns Just the email address part
104
+ * @example
105
+ * ```typescript
106
+ * const email = extractEmail('John Doe <john@example.com>');
107
+ * console.log(email); // "john@example.com"
108
+ *
109
+ * const email2 = extractEmail('simple@example.com');
110
+ * console.log(email2); // "simple@example.com"
111
+ * ```
112
+ */
113
+ declare function extractEmail(formattedAddress: string): string;
114
+ /**
115
+ * Extract display name from formatted email address
116
+ *
117
+ * Extracts the display name from a formatted "Name <email>" string.
118
+ *
119
+ * @param formattedAddress - Formatted email address string
120
+ * @returns Display name or empty string if none found
121
+ * @example
122
+ * ```typescript
123
+ * const name = extractDisplayName('John Doe <john@example.com>');
124
+ * console.log(name); // "John Doe"
125
+ *
126
+ * const name2 = extractDisplayName('simple@example.com');
127
+ * console.log(name2); // ""
128
+ * ```
129
+ */
130
+ declare function extractDisplayName(formattedAddress: string): string;
131
+ /**
132
+ * Sanitize HTML content for email
133
+ *
134
+ * Removes potentially dangerous HTML elements and attributes while preserving
135
+ * email-safe formatting.
136
+ *
137
+ * @param html - HTML content to sanitize
138
+ * @returns Sanitized HTML safe for email
139
+ * @example
140
+ * ```typescript
141
+ * const unsafeHtml = '<script>alert("xss")</script><p>Safe content</p>';
142
+ * const safeHtml = sanitizeHtml(unsafeHtml);
143
+ * console.log(safeHtml); // "<p>Safe content</p>"
144
+ * ```
145
+ */
146
+ declare function sanitizeHtml(html: string): string;
147
+ /**
148
+ * Truncate text with ellipsis
149
+ *
150
+ * Truncates text to a specified length and adds ellipsis if needed.
151
+ *
152
+ * @param text - Text to truncate
153
+ * @param maxLength - Maximum length before truncation
154
+ * @param ellipsis - String to append when truncated (default: '...')
155
+ * @returns Truncated text
156
+ * @example
157
+ * ```typescript
158
+ * const text = 'This is a very long email subject line';
159
+ * const truncated = truncateText(text, 20);
160
+ * console.log(truncated); // "This is a very long..."
161
+ * ```
162
+ */
163
+ declare function truncateText(text: string, maxLength: number, ellipsis?: string): string;
164
+ /**
165
+ * Generate email preview text
166
+ *
167
+ * Creates a preview text from HTML content, suitable for email preview panes.
168
+ *
169
+ * @param html - HTML content to generate preview from
170
+ * @param maxLength - Maximum length of preview text (default: 150)
171
+ * @returns Preview text
172
+ * @example
173
+ * ```typescript
174
+ * const html = '<h1>Welcome!</h1><p>Thank you for joining our platform.</p>';
175
+ * const preview = generatePreviewText(html);
176
+ * console.log(preview); // "Welcome! Thank you for joining our platform."
177
+ * ```
178
+ */
179
+ declare function generatePreviewText(html: string, maxLength?: number): string;
180
+ /**
181
+ * Generate unsubscribe link
182
+ *
183
+ * Creates an unsubscribe URL with proper parameters.
184
+ *
185
+ * @param baseUrl - Base URL for unsubscribe endpoint
186
+ * @param email - Email address to unsubscribe
187
+ * @param token - Optional security token
188
+ * @returns Complete unsubscribe URL
189
+ * @example
190
+ * ```typescript
191
+ * const link = generateUnsubscribeLink(
192
+ * 'https://example.com/unsubscribe',
193
+ * 'user@example.com',
194
+ * 'secure-token-123'
195
+ * );
196
+ * console.log(link); // "https://example.com/unsubscribe?email=user%40example.com&token=secure-token-123"
197
+ * ```
198
+ */
199
+ declare function generateUnsubscribeLink(baseUrl: string, email: string, token?: string): string;
200
+ /**
201
+ * Generate tracking pixel URL
202
+ *
203
+ * Creates a tracking pixel URL for email open tracking.
204
+ *
205
+ * @param baseUrl - Base URL for tracking endpoint
206
+ * @param emailId - Unique identifier for the email
207
+ * @param recipientId - Unique identifier for the recipient
208
+ * @returns Tracking pixel URL
209
+ * @example
210
+ * ```typescript
211
+ * const pixelUrl = generateTrackingPixel(
212
+ * 'https://example.com/track',
213
+ * 'email-123',
214
+ * 'user-456'
215
+ * );
216
+ * // Use in email: <img src="${pixelUrl}" width="1" height="1" />
217
+ * ```
218
+ */
219
+ declare function generateTrackingPixel(baseUrl: string, emailId: string, recipientId: string): string;
220
+ /**
221
+ * Add UTM parameters to URL
222
+ *
223
+ * Adds UTM tracking parameters to a URL for campaign tracking.
224
+ *
225
+ * @param url - Original URL
226
+ * @param utmParams - UTM parameters object
227
+ * @returns URL with UTM parameters added
228
+ * @example
229
+ * ```typescript
230
+ * const trackedUrl = addUtmParams('https://example.com/product', {
231
+ * source: 'email',
232
+ * medium: 'newsletter',
233
+ * campaign: 'summer_sale',
234
+ * content: 'header_button'
235
+ * });
236
+ * ```
237
+ */
238
+ declare function addUtmParams(url: string, utmParams: {
239
+ source?: string;
240
+ medium?: string;
241
+ campaign?: string;
242
+ term?: string;
243
+ content?: string;
244
+ }): string;
245
+ /**
246
+ * Parse email list from string
247
+ *
248
+ * Parses a comma or semicolon separated string of email addresses.
249
+ *
250
+ * @param emailString - String containing multiple email addresses
251
+ * @returns Array of trimmed email addresses
252
+ * @example
253
+ * ```typescript
254
+ * const emails = parseEmailList('user1@example.com, user2@example.com; user3@example.com');
255
+ * console.log(emails); // ['user1@example.com', 'user2@example.com', 'user3@example.com']
256
+ * ```
257
+ */
258
+ declare function parseEmailList(emailString: string): string[];
259
+ /**
260
+ * Deduplicate email list
261
+ *
262
+ * Removes duplicate email addresses from a list (case-insensitive).
263
+ *
264
+ * @param emails - Array of email addresses
265
+ * @returns Array with duplicates removed
266
+ * @example
267
+ * ```typescript
268
+ * const emails = ['user@example.com', 'USER@EXAMPLE.COM', 'other@example.com'];
269
+ * const unique = deduplicateEmails(emails);
270
+ * console.log(unique); // ['user@example.com', 'other@example.com']
271
+ * ```
272
+ */
273
+ declare function deduplicateEmails(emails: string[]): string[];
274
+ /**
275
+ * Chunk email list for batch sending
276
+ *
277
+ * Splits a large email list into smaller chunks for batch processing.
278
+ *
279
+ * @param emails - Array of email addresses
280
+ * @param chunkSize - Size of each chunk (default: 100)
281
+ * @returns Array of email chunks
282
+ * @example
283
+ * ```typescript
284
+ * const emails = ['user1@example.com', 'user2@example.com', ...]; // 250 emails
285
+ * const chunks = chunkEmails(emails, 100);
286
+ * console.log(chunks.length); // 3 chunks
287
+ * console.log(chunks[0].length); // 100
288
+ * console.log(chunks[2].length); // 50
289
+ * ```
290
+ */
291
+ declare function chunkEmails(emails: string[], chunkSize?: number): string[][];
292
+ /**
293
+ * Get MIME type from file extension
294
+ *
295
+ * Returns the appropriate MIME type for common file extensions.
296
+ *
297
+ * @param filename - Filename or extension
298
+ * @returns MIME type string
299
+ * @example
300
+ * ```typescript
301
+ * console.log(getMimeType('document.pdf')); // 'application/pdf'
302
+ * console.log(getMimeType('image.jpg')); // 'image/jpeg'
303
+ * console.log(getMimeType('.png')); // 'image/png'
304
+ * ```
305
+ */
306
+ declare function getMimeType(filename: string): string;
307
+ /**
308
+ * Format file size for display
309
+ *
310
+ * Converts file size in bytes to human-readable format.
311
+ *
312
+ * @param bytes - File size in bytes
313
+ * @param decimals - Number of decimal places (default: 2)
314
+ * @returns Formatted file size string
315
+ * @example
316
+ * ```typescript
317
+ * console.log(formatFileSize(1024)); // "1.00 KB"
318
+ * console.log(formatFileSize(1048576)); // "1.00 MB"
319
+ * console.log(formatFileSize(1073741824, 1)); // "1.0 GB"
320
+ * ```
321
+ */
322
+ declare function formatFileSize(bytes: number, decimals?: number): string;
323
+ //#endregion
324
+ export { addUtmParams, chunkEmails, deduplicateEmails, extractDisplayName, extractEmail, filterValidEmails, formatEmailAddress, formatFileSize, generatePreviewText, generateTrackingPixel, generateUnsubscribeLink, getMimeType, htmlToText, isValidEmail, parseEmailList, renderEmailTemplate, sanitizeHtml, truncateText, validateEmails };
325
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;;AAsEuC;AA6BvC;;;;AAA0C;AAoB1C;;;;AAAwD;AAmBxD;;iBA1FsB,mBAAA,WAA8B,MAAA,mBAClD,QAAA,EAAU,KAAA,CAAM,aAAA,CAAc,CAAA,GAC9B,KAAA,EAAO,CAAA,GACN,OAAA;;AAuF+C;AAsBlD;;;;AAA8D;AAqB9D;;;;AAAqD;AAqBrD;;;iBApIgB,UAAA,CAAW,IAAY;AAoIoB;AAwB3D;;;;AAAyC;AA8BzC;;;;;;;;AAtD2D,iBAvG3C,YAAA,CAAa,KAAa;AAiL1C;;;;AAAyE;AA4BzE;;;;;;;;AAAsF;AA5BtF,iBA7JgB,cAAA,CAAe,MAAyB;;;;;;;;AAwNnC;AA2BrB;;;;;;iBAhOgB,iBAAA,CAAkB,MAAgB;;;;;;AAwO/C;AA8BH;;;;AAAkD;AAqBlD;;;iBArQgB,kBAAA,CAAmB,IAAA,UAAc,KAAa;AAqQZ;AA6BlD;;;;AAAqE;AA0BrE;;;;AAA4C;AAwD5C;;;;AAAkE;AA/GhB,iBAhPlC,YAAA,CAAa,gBAAwB;;;;;;;;;;;;;;;;;iBAqBrC,kBAAA,CAAmB,gBAAwB;;;;;;;;;;;;;;;;iBAwB3C,YAAA,CAAa,IAAY;;;;;;;;;;;;;;;;;iBA8BzB,YAAA,CAAa,IAAA,UAAc,SAAA,UAAmB,QAAA;;;;;;;;;;;;;;;;iBAoB9C,mBAAA,CAAoB,IAAA,UAAc,SAAuB;;;;;;;;;;;;;;;;;;;;iBA4BzD,uBAAA,CAAwB,OAAA,UAAiB,KAAA,UAAe,KAAA;;;;;;;;;;;;;;;;;;;;iBA4BxD,qBAAA,CACd,OAAA,UACA,OAAA,UACA,WAAA;;;;;;;;;;;;;;;;;;;iBA2Bc,YAAA,CACd,GAAA,UACA,SAAA;EACE,MAAA;EACA,MAAA;EACA,QAAA;EACA,IAAA;EACA,OAAA;AAAA;;;;;;;;;;;;;;iBA+BY,cAAA,CAAe,WAAmB;;;;;;;;;;;;;;;iBAqBlC,iBAAA,CAAkB,MAAgB;;;;;;;;;;;;;;;;;;iBA6BlC,WAAA,CAAY,MAAA,YAAkB,SAAuB;;;;;;;;;;;;;;;iBA0BrD,WAAA,CAAY,QAAgB;;;;;;;;;;;;;;;;iBAwD5B,cAAA,CAAe,KAAA,UAAe,QAAoB"}