@impruthvi/nodemail 0.5.0 → 0.6.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.
- package/README.md +1 -1
- package/dist/core/MailFacade.d.ts +17 -10
- package/dist/core/MailFacade.d.ts.map +1 -1
- package/dist/core/MailFacade.js +104 -5
- package/dist/core/MailFacade.js.map +1 -1
- package/dist/core/MailManager.d.ts +13 -1
- package/dist/core/MailManager.d.ts.map +1 -1
- package/dist/core/MailManager.js +64 -0
- package/dist/core/MailManager.js.map +1 -1
- package/dist/core/Mailable.d.ts +1 -0
- package/dist/core/Mailable.d.ts.map +1 -1
- package/dist/core/Mailable.js +4 -0
- package/dist/core/Mailable.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/queue/QueueManager.d.ts +21 -0
- package/dist/queue/QueueManager.d.ts.map +1 -0
- package/dist/queue/QueueManager.js +131 -0
- package/dist/queue/QueueManager.js.map +1 -0
- package/dist/queue/drivers/BullDriver.d.ts +16 -0
- package/dist/queue/drivers/BullDriver.d.ts.map +1 -0
- package/dist/queue/drivers/BullDriver.js +137 -0
- package/dist/queue/drivers/BullDriver.js.map +1 -0
- package/dist/queue/drivers/BullMQDriver.d.ts +18 -0
- package/dist/queue/drivers/BullMQDriver.d.ts.map +1 -0
- package/dist/queue/drivers/BullMQDriver.js +150 -0
- package/dist/queue/drivers/BullMQDriver.js.map +1 -0
- package/dist/queue/index.d.ts +4 -0
- package/dist/queue/index.d.ts.map +1 -0
- package/dist/queue/index.js +10 -0
- package/dist/queue/index.js.map +1 -0
- package/dist/testing/MailFake.d.ts +10 -8
- package/dist/testing/MailFake.d.ts.map +1 -1
- package/dist/testing/MailFake.js +6 -0
- package/dist/testing/MailFake.js.map +1 -1
- package/dist/types/index.d.ts +45 -0
- package/dist/types/index.d.ts.map +1 -1
- package/examples/queue-example.ts +313 -0
- package/package.json +1 -1
package/dist/types/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface MailConfig {
|
|
|
7
7
|
};
|
|
8
8
|
mailers: Record<string, MailerConfig>;
|
|
9
9
|
templates?: TemplateConfig;
|
|
10
|
+
queue?: QueueConfig;
|
|
10
11
|
}
|
|
11
12
|
export interface TemplateConfig {
|
|
12
13
|
engine?: 'handlebars' | 'ejs' | 'pug' | TemplateEngine;
|
|
@@ -98,4 +99,48 @@ export interface MailResponse {
|
|
|
98
99
|
response?: string;
|
|
99
100
|
error?: string;
|
|
100
101
|
}
|
|
102
|
+
export interface QueueConfig {
|
|
103
|
+
driver: 'bull' | 'bullmq' | 'sync';
|
|
104
|
+
connection?: RedisConnectionConfig;
|
|
105
|
+
defaultQueue?: string;
|
|
106
|
+
prefix?: string;
|
|
107
|
+
retries?: number;
|
|
108
|
+
backoff?: BackoffConfig;
|
|
109
|
+
}
|
|
110
|
+
export interface RedisConnectionConfig {
|
|
111
|
+
host?: string;
|
|
112
|
+
port?: number;
|
|
113
|
+
password?: string;
|
|
114
|
+
db?: number;
|
|
115
|
+
url?: string;
|
|
116
|
+
}
|
|
117
|
+
export interface BackoffConfig {
|
|
118
|
+
type: 'fixed' | 'exponential';
|
|
119
|
+
delay: number;
|
|
120
|
+
}
|
|
121
|
+
export interface QueuedMailJob {
|
|
122
|
+
id: string;
|
|
123
|
+
mailOptions: MailOptions;
|
|
124
|
+
mailableClass?: string;
|
|
125
|
+
mailableData?: Record<string, unknown>;
|
|
126
|
+
attempts: number;
|
|
127
|
+
maxAttempts: number;
|
|
128
|
+
delay?: number;
|
|
129
|
+
scheduledAt?: Date;
|
|
130
|
+
createdAt: Date;
|
|
131
|
+
}
|
|
132
|
+
export interface QueueJobResult {
|
|
133
|
+
success: boolean;
|
|
134
|
+
jobId: string;
|
|
135
|
+
queue: string;
|
|
136
|
+
scheduledAt?: Date;
|
|
137
|
+
error?: string;
|
|
138
|
+
}
|
|
139
|
+
export interface QueueDriver {
|
|
140
|
+
add(job: QueuedMailJob, queueName?: string): Promise<QueueJobResult>;
|
|
141
|
+
addDelayed(job: QueuedMailJob, delay: number, queueName?: string): Promise<QueueJobResult>;
|
|
142
|
+
addScheduled(job: QueuedMailJob, date: Date, queueName?: string): Promise<QueueJobResult>;
|
|
143
|
+
process(queueName: string, handler: (job: QueuedMailJob) => Promise<MailResponse>): void;
|
|
144
|
+
close(): Promise<void>;
|
|
145
|
+
}
|
|
101
146
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAElE,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IACF,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IACtC,SAAS,CAAC,EAAE,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAElE,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IACF,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IACtC,SAAS,CAAC,EAAE,cAAc,CAAC;IAC3B,KAAK,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,YAAY,GAAG,KAAK,GAAG,KAAK,GAAG,cAAc,CAAC;IACvD,SAAS,CAAC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AAC,CAAC;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,WAAW;IAI1B,GAAG,CAAC,GAAG,EAAE,aAAa,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IAKrE,UAAU,CAAC,GAAG,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IAK3F,YAAY,CAAC,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IAK1F,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,aAAa,KAAK,OAAO,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC;IAKzF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Queue Example - Demonstrates email queuing functionality
|
|
3
|
+
*
|
|
4
|
+
* This example shows how to:
|
|
5
|
+
* - Configure queue with different drivers (sync, bull, bullmq)
|
|
6
|
+
* - Queue emails for background processing
|
|
7
|
+
* - Delay emails by a specific number of seconds
|
|
8
|
+
* - Schedule emails for a specific time
|
|
9
|
+
* - Process queued emails
|
|
10
|
+
*
|
|
11
|
+
* Note: For Bull/BullMQ drivers, you need Redis running:
|
|
12
|
+
* docker run -d -p 6379:6379 redis:alpine
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Mail, Mailable, QueueManager } from '../src';
|
|
16
|
+
import type { QueueConfig, MailOptions } from '../src/types';
|
|
17
|
+
|
|
18
|
+
// Example Mailable class
|
|
19
|
+
class WelcomeMailable extends Mailable {
|
|
20
|
+
constructor(
|
|
21
|
+
private userName: string,
|
|
22
|
+
private activationLink: string
|
|
23
|
+
) {
|
|
24
|
+
super();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
build(): this {
|
|
28
|
+
return this.from('noreply@example.com')
|
|
29
|
+
.subject(`Welcome ${this.userName}!`)
|
|
30
|
+
.html(`
|
|
31
|
+
<h1>Welcome to Our Platform!</h1>
|
|
32
|
+
<p>Hi ${this.userName},</p>
|
|
33
|
+
<p>Thank you for signing up. Please click the link below to activate your account:</p>
|
|
34
|
+
<a href="${this.activationLink}">Activate Account</a>
|
|
35
|
+
`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class OrderConfirmationMailable extends Mailable {
|
|
40
|
+
constructor(
|
|
41
|
+
private orderId: string,
|
|
42
|
+
private total: number
|
|
43
|
+
) {
|
|
44
|
+
super();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
build(): this {
|
|
48
|
+
return this.from('orders@example.com')
|
|
49
|
+
.subject(`Order #${this.orderId} Confirmed`)
|
|
50
|
+
.html(`
|
|
51
|
+
<h1>Order Confirmation</h1>
|
|
52
|
+
<p>Your order #${this.orderId} has been confirmed.</p>
|
|
53
|
+
<p>Total: $${this.total.toFixed(2)}</p>
|
|
54
|
+
<p>Thank you for your purchase!</p>
|
|
55
|
+
`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function syncDriverExample(): Promise<void> {
|
|
60
|
+
console.log('=== Sync Driver Example (for development/testing) ===\n');
|
|
61
|
+
|
|
62
|
+
// Configure with sync driver (emails are queued but not processed in background)
|
|
63
|
+
Mail.configure({
|
|
64
|
+
default: 'smtp',
|
|
65
|
+
from: { address: 'noreply@example.com', name: 'My App' },
|
|
66
|
+
mailers: {
|
|
67
|
+
smtp: {
|
|
68
|
+
driver: 'smtp',
|
|
69
|
+
host: 'smtp.example.com',
|
|
70
|
+
port: 587,
|
|
71
|
+
auth: { user: 'user', pass: 'pass' },
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
queue: {
|
|
75
|
+
driver: 'sync',
|
|
76
|
+
defaultQueue: 'mail',
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Queue an email immediately
|
|
81
|
+
const result1 = await Mail.to('user@example.com')
|
|
82
|
+
.subject('Quick Update')
|
|
83
|
+
.html('<p>This is a quick update!</p>')
|
|
84
|
+
.queue();
|
|
85
|
+
|
|
86
|
+
console.log('Queued email:', result1);
|
|
87
|
+
|
|
88
|
+
// Queue an email with 60 second delay
|
|
89
|
+
const result2 = await Mail.to('user@example.com')
|
|
90
|
+
.subject('Delayed Message')
|
|
91
|
+
.html('<p>This message was delayed by 60 seconds.</p>')
|
|
92
|
+
.later(60);
|
|
93
|
+
|
|
94
|
+
console.log('Delayed email:', result2);
|
|
95
|
+
|
|
96
|
+
// Schedule an email for a specific time
|
|
97
|
+
const futureTime = new Date(Date.now() + 3600 * 1000); // 1 hour from now
|
|
98
|
+
const result3 = await Mail.to('user@example.com')
|
|
99
|
+
.subject('Scheduled Message')
|
|
100
|
+
.html('<p>This message was scheduled for a specific time.</p>')
|
|
101
|
+
.at(futureTime);
|
|
102
|
+
|
|
103
|
+
console.log('Scheduled email:', result3);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function mailableQueueExample(): Promise<void> {
|
|
107
|
+
console.log('\n=== Mailable Queue Example ===\n');
|
|
108
|
+
|
|
109
|
+
Mail.configure({
|
|
110
|
+
default: 'smtp',
|
|
111
|
+
from: { address: 'noreply@example.com', name: 'My App' },
|
|
112
|
+
mailers: {
|
|
113
|
+
smtp: {
|
|
114
|
+
driver: 'smtp',
|
|
115
|
+
host: 'smtp.example.com',
|
|
116
|
+
port: 587,
|
|
117
|
+
auth: { user: 'user', pass: 'pass' },
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
queue: {
|
|
121
|
+
driver: 'sync',
|
|
122
|
+
defaultQueue: 'mail',
|
|
123
|
+
retries: 3,
|
|
124
|
+
backoff: { type: 'exponential', delay: 1000 },
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Queue a mailable
|
|
129
|
+
const welcome = new WelcomeMailable('John Doe', 'https://example.com/activate/abc123');
|
|
130
|
+
const result1 = await Mail.to('john@example.com').queue(welcome);
|
|
131
|
+
console.log('Queued welcome email:', result1);
|
|
132
|
+
|
|
133
|
+
// Queue with delay
|
|
134
|
+
const order = new OrderConfirmationMailable('ORD-12345', 99.99);
|
|
135
|
+
const result2 = await Mail.to('john@example.com').later(120, order);
|
|
136
|
+
console.log('Delayed order confirmation:', result2);
|
|
137
|
+
|
|
138
|
+
// Direct queue via Mail.queue()
|
|
139
|
+
const welcome2 = new WelcomeMailable('Jane Doe', 'https://example.com/activate/xyz789');
|
|
140
|
+
const result3 = await Mail.queue(welcome2);
|
|
141
|
+
console.log('Direct queue result:', result3);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function queueManagerDirectExample(): Promise<void> {
|
|
145
|
+
console.log('\n=== QueueManager Direct Usage ===\n');
|
|
146
|
+
|
|
147
|
+
const queueConfig: QueueConfig = {
|
|
148
|
+
driver: 'sync',
|
|
149
|
+
defaultQueue: 'priority-mail',
|
|
150
|
+
prefix: 'myapp',
|
|
151
|
+
retries: 5,
|
|
152
|
+
backoff: { type: 'fixed', delay: 2000 },
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const queueManager = new QueueManager(queueConfig);
|
|
156
|
+
|
|
157
|
+
const mailOptions: MailOptions = {
|
|
158
|
+
from: 'noreply@example.com',
|
|
159
|
+
to: 'recipient@example.com',
|
|
160
|
+
subject: 'Direct Queue Test',
|
|
161
|
+
html: '<p>This email was queued directly via QueueManager.</p>',
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// Queue immediately
|
|
165
|
+
const result1 = await queueManager.queue(mailOptions);
|
|
166
|
+
console.log('Queued:', result1);
|
|
167
|
+
|
|
168
|
+
// Queue with delay
|
|
169
|
+
const result2 = await queueManager.later(mailOptions, 30);
|
|
170
|
+
console.log('Delayed 30s:', result2);
|
|
171
|
+
|
|
172
|
+
// Schedule for specific time
|
|
173
|
+
const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
|
174
|
+
const result3 = await queueManager.at(mailOptions, tomorrow);
|
|
175
|
+
console.log('Scheduled for tomorrow:', result3);
|
|
176
|
+
|
|
177
|
+
await queueManager.close();
|
|
178
|
+
console.log('Queue manager closed');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function bullMQConfigExample(): void {
|
|
182
|
+
console.log('\n=== BullMQ Configuration Example ===\n');
|
|
183
|
+
|
|
184
|
+
// This is just showing the configuration - requires Redis running
|
|
185
|
+
const config = {
|
|
186
|
+
default: 'smtp',
|
|
187
|
+
from: { address: 'noreply@example.com', name: 'My App' },
|
|
188
|
+
mailers: {
|
|
189
|
+
smtp: {
|
|
190
|
+
driver: 'smtp' as const,
|
|
191
|
+
host: 'smtp.example.com',
|
|
192
|
+
port: 587,
|
|
193
|
+
auth: { user: 'user', pass: 'pass' },
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
queue: {
|
|
197
|
+
driver: 'bullmq' as const,
|
|
198
|
+
connection: {
|
|
199
|
+
host: 'localhost',
|
|
200
|
+
port: 6379,
|
|
201
|
+
// password: 'your-redis-password',
|
|
202
|
+
// db: 0,
|
|
203
|
+
},
|
|
204
|
+
defaultQueue: 'mail',
|
|
205
|
+
prefix: 'myapp',
|
|
206
|
+
retries: 3,
|
|
207
|
+
backoff: { type: 'exponential' as const, delay: 1000 },
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
console.log('BullMQ configuration:');
|
|
212
|
+
console.log(JSON.stringify(config, null, 2));
|
|
213
|
+
console.log('\nTo use BullMQ:');
|
|
214
|
+
console.log('1. Install: npm install bullmq ioredis');
|
|
215
|
+
console.log('2. Start Redis: docker run -d -p 6379:6379 redis:alpine');
|
|
216
|
+
console.log('3. Configure Mail with the above config');
|
|
217
|
+
console.log('4. Start a worker to process queued emails');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function bullConfigExample(): void {
|
|
221
|
+
console.log('\n=== Bull (Legacy) Configuration Example ===\n');
|
|
222
|
+
|
|
223
|
+
// This is just showing the configuration - requires Redis running
|
|
224
|
+
const config = {
|
|
225
|
+
default: 'smtp',
|
|
226
|
+
from: { address: 'noreply@example.com', name: 'My App' },
|
|
227
|
+
mailers: {
|
|
228
|
+
smtp: {
|
|
229
|
+
driver: 'smtp' as const,
|
|
230
|
+
host: 'smtp.example.com',
|
|
231
|
+
port: 587,
|
|
232
|
+
auth: { user: 'user', pass: 'pass' },
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
queue: {
|
|
236
|
+
driver: 'bull' as const,
|
|
237
|
+
connection: {
|
|
238
|
+
host: 'localhost',
|
|
239
|
+
port: 6379,
|
|
240
|
+
},
|
|
241
|
+
defaultQueue: 'mail',
|
|
242
|
+
prefix: 'myapp',
|
|
243
|
+
retries: 3,
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
console.log('Bull configuration:');
|
|
248
|
+
console.log(JSON.stringify(config, null, 2));
|
|
249
|
+
console.log('\nTo use Bull:');
|
|
250
|
+
console.log('1. Install: npm install bull');
|
|
251
|
+
console.log('2. Start Redis: docker run -d -p 6379:6379 redis:alpine');
|
|
252
|
+
console.log('3. Configure Mail with the above config');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function testingQueueExample(): Promise<void> {
|
|
256
|
+
console.log('\n=== Testing Queue with Mail.fake() ===\n');
|
|
257
|
+
|
|
258
|
+
// Enable fake mode for testing
|
|
259
|
+
Mail.fake();
|
|
260
|
+
|
|
261
|
+
// Queue some emails
|
|
262
|
+
await Mail.to('user1@example.com')
|
|
263
|
+
.subject('Test Queue 1')
|
|
264
|
+
.html('<p>Test 1</p>')
|
|
265
|
+
.queue();
|
|
266
|
+
|
|
267
|
+
await Mail.to('user2@example.com')
|
|
268
|
+
.subject('Test Queue 2')
|
|
269
|
+
.html('<p>Test 2</p>')
|
|
270
|
+
.later(60);
|
|
271
|
+
|
|
272
|
+
const mailable = new WelcomeMailable('Test User', 'https://example.com/activate');
|
|
273
|
+
await Mail.to('user3@example.com').queue(mailable);
|
|
274
|
+
|
|
275
|
+
// Assert emails were queued
|
|
276
|
+
try {
|
|
277
|
+
Mail.assertQueued(WelcomeMailable);
|
|
278
|
+
console.log('WelcomeMailable was queued');
|
|
279
|
+
|
|
280
|
+
const queuedCount = Mail.queued().length;
|
|
281
|
+
console.log(`Total queued emails: ${queuedCount}`);
|
|
282
|
+
|
|
283
|
+
// Check specific queued email
|
|
284
|
+
const queued = Mail.queued();
|
|
285
|
+
queued.forEach((msg, i) => {
|
|
286
|
+
console.log(`Queued email ${i + 1}: ${msg.getSubject()}`);
|
|
287
|
+
});
|
|
288
|
+
} catch (e) {
|
|
289
|
+
console.error('Assertion failed:', e);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Restore real mail functionality
|
|
293
|
+
Mail.restore();
|
|
294
|
+
console.log('\nMail.fake() restored');
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Run examples
|
|
298
|
+
async function main(): Promise<void> {
|
|
299
|
+
try {
|
|
300
|
+
await syncDriverExample();
|
|
301
|
+
await mailableQueueExample();
|
|
302
|
+
await queueManagerDirectExample();
|
|
303
|
+
bullMQConfigExample();
|
|
304
|
+
bullConfigExample();
|
|
305
|
+
await testingQueueExample();
|
|
306
|
+
|
|
307
|
+
console.log('\n=== All examples completed! ===');
|
|
308
|
+
} catch (error) {
|
|
309
|
+
console.error('Example failed:', error);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
void main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@impruthvi/nodemail",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A unified mail service for Node.js/TypeScript with Laravel-like simplicity. Support for multiple providers (SMTP, SendGrid, AWS SES, Mailgun, Resend, Postmark) and template engines (Handlebars, EJS, Pug) - just change environment variables, no code changes needed.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|