@depup/resend 6.9.4-depup.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/dist/index.cjs ADDED
@@ -0,0 +1,1019 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let postal_mime = require("postal-mime");
25
+ postal_mime = __toESM(postal_mime);
26
+ let svix = require("svix");
27
+ //#region package.json
28
+ var version = "6.9.4";
29
+ //#endregion
30
+ //#region src/common/utils/build-pagination-query.ts
31
+ /**
32
+ * Builds a query string from pagination options
33
+ * @param options - Pagination options containing limit and either after or before (but not both)
34
+ * @returns Query string (without leading '?') or empty string if no options
35
+ */
36
+ function buildPaginationQuery(options) {
37
+ const searchParams = new URLSearchParams();
38
+ if (options.limit !== void 0) searchParams.set("limit", options.limit.toString());
39
+ if ("after" in options && options.after !== void 0) searchParams.set("after", options.after);
40
+ if ("before" in options && options.before !== void 0) searchParams.set("before", options.before);
41
+ return searchParams.toString();
42
+ }
43
+ //#endregion
44
+ //#region src/api-keys/api-keys.ts
45
+ var ApiKeys = class {
46
+ constructor(resend) {
47
+ this.resend = resend;
48
+ }
49
+ async create(payload, options = {}) {
50
+ return await this.resend.post("/api-keys", payload, options);
51
+ }
52
+ async list(options = {}) {
53
+ const queryString = buildPaginationQuery(options);
54
+ const url = queryString ? `/api-keys?${queryString}` : "/api-keys";
55
+ return await this.resend.get(url);
56
+ }
57
+ async remove(id) {
58
+ return await this.resend.delete(`/api-keys/${id}`);
59
+ }
60
+ };
61
+ //#endregion
62
+ //#region src/common/utils/parse-email-to-api-options.ts
63
+ function parseAttachments(attachments) {
64
+ return attachments?.map((attachment) => ({
65
+ content: attachment.content,
66
+ filename: attachment.filename,
67
+ path: attachment.path,
68
+ content_type: attachment.contentType,
69
+ content_id: attachment.contentId
70
+ }));
71
+ }
72
+ function parseEmailToApiOptions(email) {
73
+ return {
74
+ attachments: parseAttachments(email.attachments),
75
+ bcc: email.bcc,
76
+ cc: email.cc,
77
+ from: email.from,
78
+ headers: email.headers,
79
+ html: email.html,
80
+ reply_to: email.replyTo,
81
+ scheduled_at: email.scheduledAt,
82
+ subject: email.subject,
83
+ tags: email.tags,
84
+ text: email.text,
85
+ to: email.to,
86
+ template: email.template ? {
87
+ id: email.template.id,
88
+ variables: email.template.variables
89
+ } : void 0,
90
+ topic_id: email.topicId
91
+ };
92
+ }
93
+ //#endregion
94
+ //#region src/render.ts
95
+ async function render(node) {
96
+ let render;
97
+ try {
98
+ ({render} = await import("@react-email/render"));
99
+ } catch {
100
+ throw new Error("Failed to render React component. Make sure to install `@react-email/render` or `@react-email/components`.");
101
+ }
102
+ return render(node);
103
+ }
104
+ //#endregion
105
+ //#region src/batch/batch.ts
106
+ var Batch = class {
107
+ constructor(resend) {
108
+ this.resend = resend;
109
+ }
110
+ async send(payload, options) {
111
+ return this.create(payload, options);
112
+ }
113
+ async create(payload, options) {
114
+ const emails = [];
115
+ for (const email of payload) {
116
+ if (email.react) {
117
+ email.html = await render(email.react);
118
+ email.react = void 0;
119
+ }
120
+ emails.push(parseEmailToApiOptions(email));
121
+ }
122
+ return await this.resend.post("/emails/batch", emails, {
123
+ ...options,
124
+ headers: {
125
+ "x-batch-validation": options?.batchValidation ?? "strict",
126
+ ...options?.headers
127
+ }
128
+ });
129
+ }
130
+ };
131
+ //#endregion
132
+ //#region src/broadcasts/broadcasts.ts
133
+ var Broadcasts = class {
134
+ constructor(resend) {
135
+ this.resend = resend;
136
+ }
137
+ async create(payload, options = {}) {
138
+ if (payload.react) payload.html = await render(payload.react);
139
+ return await this.resend.post("/broadcasts", {
140
+ name: payload.name,
141
+ segment_id: payload.segmentId,
142
+ audience_id: payload.audienceId,
143
+ preview_text: payload.previewText,
144
+ from: payload.from,
145
+ html: payload.html,
146
+ reply_to: payload.replyTo,
147
+ subject: payload.subject,
148
+ text: payload.text,
149
+ topic_id: payload.topicId,
150
+ send: payload.send,
151
+ scheduled_at: payload.scheduledAt
152
+ }, options);
153
+ }
154
+ async send(id, payload) {
155
+ return await this.resend.post(`/broadcasts/${id}/send`, { scheduled_at: payload?.scheduledAt });
156
+ }
157
+ async list(options = {}) {
158
+ const queryString = buildPaginationQuery(options);
159
+ const url = queryString ? `/broadcasts?${queryString}` : "/broadcasts";
160
+ return await this.resend.get(url);
161
+ }
162
+ async get(id) {
163
+ return await this.resend.get(`/broadcasts/${id}`);
164
+ }
165
+ async remove(id) {
166
+ return await this.resend.delete(`/broadcasts/${id}`);
167
+ }
168
+ async update(id, payload) {
169
+ if (payload.react) payload.html = await render(payload.react);
170
+ return await this.resend.patch(`/broadcasts/${id}`, {
171
+ name: payload.name,
172
+ segment_id: payload.segmentId,
173
+ audience_id: payload.audienceId,
174
+ from: payload.from,
175
+ html: payload.html,
176
+ text: payload.text,
177
+ subject: payload.subject,
178
+ reply_to: payload.replyTo,
179
+ preview_text: payload.previewText,
180
+ topic_id: payload.topicId
181
+ });
182
+ }
183
+ };
184
+ //#endregion
185
+ //#region src/common/utils/parse-contact-properties-to-api-options.ts
186
+ function parseContactPropertyFromApi(contactProperty) {
187
+ return {
188
+ id: contactProperty.id,
189
+ key: contactProperty.key,
190
+ createdAt: contactProperty.created_at,
191
+ type: contactProperty.type,
192
+ fallbackValue: contactProperty.fallback_value
193
+ };
194
+ }
195
+ function parseContactPropertyToApiOptions(contactProperty) {
196
+ if ("key" in contactProperty) return {
197
+ key: contactProperty.key,
198
+ type: contactProperty.type,
199
+ fallback_value: contactProperty.fallbackValue
200
+ };
201
+ return { fallback_value: contactProperty.fallbackValue };
202
+ }
203
+ //#endregion
204
+ //#region src/contact-properties/contact-properties.ts
205
+ var ContactProperties = class {
206
+ constructor(resend) {
207
+ this.resend = resend;
208
+ }
209
+ async create(options) {
210
+ const apiOptions = parseContactPropertyToApiOptions(options);
211
+ return await this.resend.post("/contact-properties", apiOptions);
212
+ }
213
+ async list(options = {}) {
214
+ const queryString = buildPaginationQuery(options);
215
+ const url = queryString ? `/contact-properties?${queryString}` : "/contact-properties";
216
+ const response = await this.resend.get(url);
217
+ if (response.data) return {
218
+ data: {
219
+ ...response.data,
220
+ data: response.data.data.map((apiContactProperty) => parseContactPropertyFromApi(apiContactProperty))
221
+ },
222
+ headers: response.headers,
223
+ error: null
224
+ };
225
+ return response;
226
+ }
227
+ async get(id) {
228
+ if (!id) return {
229
+ data: null,
230
+ headers: null,
231
+ error: {
232
+ message: "Missing `id` field.",
233
+ statusCode: null,
234
+ name: "missing_required_field"
235
+ }
236
+ };
237
+ const response = await this.resend.get(`/contact-properties/${id}`);
238
+ if (response.data) return {
239
+ data: {
240
+ object: "contact_property",
241
+ ...parseContactPropertyFromApi(response.data)
242
+ },
243
+ headers: response.headers,
244
+ error: null
245
+ };
246
+ return response;
247
+ }
248
+ async update(payload) {
249
+ if (!payload.id) return {
250
+ data: null,
251
+ headers: null,
252
+ error: {
253
+ message: "Missing `id` field.",
254
+ statusCode: null,
255
+ name: "missing_required_field"
256
+ }
257
+ };
258
+ const apiOptions = parseContactPropertyToApiOptions(payload);
259
+ return await this.resend.patch(`/contact-properties/${payload.id}`, apiOptions);
260
+ }
261
+ async remove(id) {
262
+ if (!id) return {
263
+ data: null,
264
+ headers: null,
265
+ error: {
266
+ message: "Missing `id` field.",
267
+ statusCode: null,
268
+ name: "missing_required_field"
269
+ }
270
+ };
271
+ return await this.resend.delete(`/contact-properties/${id}`);
272
+ }
273
+ };
274
+ //#endregion
275
+ //#region src/contacts/segments/contact-segments.ts
276
+ var ContactSegments = class {
277
+ constructor(resend) {
278
+ this.resend = resend;
279
+ }
280
+ async list(options) {
281
+ if (!options.contactId && !options.email) return {
282
+ data: null,
283
+ headers: null,
284
+ error: {
285
+ message: "Missing `id` or `email` field.",
286
+ statusCode: null,
287
+ name: "missing_required_field"
288
+ }
289
+ };
290
+ const identifier = options.email ? options.email : options.contactId;
291
+ const queryString = buildPaginationQuery(options);
292
+ const url = queryString ? `/contacts/${identifier}/segments?${queryString}` : `/contacts/${identifier}/segments`;
293
+ return await this.resend.get(url);
294
+ }
295
+ async add(options) {
296
+ if (!options.contactId && !options.email) return {
297
+ data: null,
298
+ headers: null,
299
+ error: {
300
+ message: "Missing `id` or `email` field.",
301
+ statusCode: null,
302
+ name: "missing_required_field"
303
+ }
304
+ };
305
+ const identifier = options.email ? options.email : options.contactId;
306
+ return this.resend.post(`/contacts/${identifier}/segments/${options.segmentId}`);
307
+ }
308
+ async remove(options) {
309
+ if (!options.contactId && !options.email) return {
310
+ data: null,
311
+ headers: null,
312
+ error: {
313
+ message: "Missing `id` or `email` field.",
314
+ statusCode: null,
315
+ name: "missing_required_field"
316
+ }
317
+ };
318
+ const identifier = options.email ? options.email : options.contactId;
319
+ return this.resend.delete(`/contacts/${identifier}/segments/${options.segmentId}`);
320
+ }
321
+ };
322
+ //#endregion
323
+ //#region src/contacts/topics/contact-topics.ts
324
+ var ContactTopics = class {
325
+ constructor(resend) {
326
+ this.resend = resend;
327
+ }
328
+ async update(payload) {
329
+ if (!payload.id && !payload.email) return {
330
+ data: null,
331
+ headers: null,
332
+ error: {
333
+ message: "Missing `id` or `email` field.",
334
+ statusCode: null,
335
+ name: "missing_required_field"
336
+ }
337
+ };
338
+ const identifier = payload.email ? payload.email : payload.id;
339
+ return this.resend.patch(`/contacts/${identifier}/topics`, payload.topics);
340
+ }
341
+ async list(options) {
342
+ if (!options.id && !options.email) return {
343
+ data: null,
344
+ headers: null,
345
+ error: {
346
+ message: "Missing `id` or `email` field.",
347
+ statusCode: null,
348
+ name: "missing_required_field"
349
+ }
350
+ };
351
+ const identifier = options.email ? options.email : options.id;
352
+ const queryString = buildPaginationQuery(options);
353
+ const url = queryString ? `/contacts/${identifier}/topics?${queryString}` : `/contacts/${identifier}/topics`;
354
+ return this.resend.get(url);
355
+ }
356
+ };
357
+ //#endregion
358
+ //#region src/contacts/contacts.ts
359
+ var Contacts = class {
360
+ constructor(resend) {
361
+ this.resend = resend;
362
+ this.topics = new ContactTopics(this.resend);
363
+ this.segments = new ContactSegments(this.resend);
364
+ }
365
+ async create(payload, options = {}) {
366
+ if ("audienceId" in payload) {
367
+ if ("segments" in payload || "topics" in payload) return {
368
+ data: null,
369
+ headers: null,
370
+ error: {
371
+ message: "`audienceId` is deprecated, and cannot be used together with `segments` or `topics`. Use `segments` instead to add one or more segments to the new contact.",
372
+ statusCode: null,
373
+ name: "invalid_parameter"
374
+ }
375
+ };
376
+ return await this.resend.post(`/audiences/${payload.audienceId}/contacts`, {
377
+ unsubscribed: payload.unsubscribed,
378
+ email: payload.email,
379
+ first_name: payload.firstName,
380
+ last_name: payload.lastName,
381
+ properties: payload.properties
382
+ }, options);
383
+ }
384
+ return await this.resend.post("/contacts", {
385
+ unsubscribed: payload.unsubscribed,
386
+ email: payload.email,
387
+ first_name: payload.firstName,
388
+ last_name: payload.lastName,
389
+ properties: payload.properties,
390
+ segments: payload.segments,
391
+ topics: payload.topics
392
+ }, options);
393
+ }
394
+ async list(options = {}) {
395
+ const segmentId = options.segmentId ?? options.audienceId;
396
+ if (!segmentId) {
397
+ const queryString = buildPaginationQuery(options);
398
+ const url = queryString ? `/contacts?${queryString}` : "/contacts";
399
+ return await this.resend.get(url);
400
+ }
401
+ const queryString = buildPaginationQuery(options);
402
+ const url = queryString ? `/segments/${segmentId}/contacts?${queryString}` : `/segments/${segmentId}/contacts`;
403
+ return await this.resend.get(url);
404
+ }
405
+ async get(options) {
406
+ if (typeof options === "string") return this.resend.get(`/contacts/${options}`);
407
+ if (!options.id && !options.email) return {
408
+ data: null,
409
+ headers: null,
410
+ error: {
411
+ message: "Missing `id` or `email` field.",
412
+ statusCode: null,
413
+ name: "missing_required_field"
414
+ }
415
+ };
416
+ if (!options.audienceId) return this.resend.get(`/contacts/${options?.email ? options?.email : options?.id}`);
417
+ return this.resend.get(`/audiences/${options.audienceId}/contacts/${options?.email ? options?.email : options?.id}`);
418
+ }
419
+ async update(options) {
420
+ if (!options.id && !options.email) return {
421
+ data: null,
422
+ headers: null,
423
+ error: {
424
+ message: "Missing `id` or `email` field.",
425
+ statusCode: null,
426
+ name: "missing_required_field"
427
+ }
428
+ };
429
+ if (!options.audienceId) return await this.resend.patch(`/contacts/${options?.email ? options?.email : options?.id}`, {
430
+ unsubscribed: options.unsubscribed,
431
+ first_name: options.firstName,
432
+ last_name: options.lastName,
433
+ properties: options.properties
434
+ });
435
+ return await this.resend.patch(`/audiences/${options.audienceId}/contacts/${options?.email ? options?.email : options?.id}`, {
436
+ unsubscribed: options.unsubscribed,
437
+ first_name: options.firstName,
438
+ last_name: options.lastName,
439
+ properties: options.properties
440
+ });
441
+ }
442
+ async remove(payload) {
443
+ if (typeof payload === "string") return this.resend.delete(`/contacts/${payload}`);
444
+ if (!payload.id && !payload.email) return {
445
+ data: null,
446
+ headers: null,
447
+ error: {
448
+ message: "Missing `id` or `email` field.",
449
+ statusCode: null,
450
+ name: "missing_required_field"
451
+ }
452
+ };
453
+ if (!payload.audienceId) return this.resend.delete(`/contacts/${payload?.email ? payload?.email : payload?.id}`);
454
+ return this.resend.delete(`/audiences/${payload.audienceId}/contacts/${payload?.email ? payload?.email : payload?.id}`);
455
+ }
456
+ };
457
+ //#endregion
458
+ //#region src/common/utils/parse-domain-to-api-options.ts
459
+ function parseDomainToApiOptions(domain) {
460
+ return {
461
+ name: domain.name,
462
+ region: domain.region,
463
+ custom_return_path: domain.customReturnPath,
464
+ capabilities: domain.capabilities,
465
+ open_tracking: domain.openTracking,
466
+ click_tracking: domain.clickTracking,
467
+ tls: domain.tls
468
+ };
469
+ }
470
+ //#endregion
471
+ //#region src/domains/domains.ts
472
+ var Domains = class {
473
+ constructor(resend) {
474
+ this.resend = resend;
475
+ }
476
+ async create(payload, options = {}) {
477
+ return await this.resend.post("/domains", parseDomainToApiOptions(payload), options);
478
+ }
479
+ async list(options = {}) {
480
+ const queryString = buildPaginationQuery(options);
481
+ const url = queryString ? `/domains?${queryString}` : "/domains";
482
+ return await this.resend.get(url);
483
+ }
484
+ async get(id) {
485
+ return await this.resend.get(`/domains/${id}`);
486
+ }
487
+ async update(payload) {
488
+ return await this.resend.patch(`/domains/${payload.id}`, {
489
+ click_tracking: payload.clickTracking,
490
+ open_tracking: payload.openTracking,
491
+ tls: payload.tls,
492
+ capabilities: payload.capabilities
493
+ });
494
+ }
495
+ async remove(id) {
496
+ return await this.resend.delete(`/domains/${id}`);
497
+ }
498
+ async verify(id) {
499
+ return await this.resend.post(`/domains/${id}/verify`);
500
+ }
501
+ };
502
+ //#endregion
503
+ //#region src/emails/attachments/attachments.ts
504
+ var Attachments$1 = class {
505
+ constructor(resend) {
506
+ this.resend = resend;
507
+ }
508
+ async get(options) {
509
+ const { emailId, id } = options;
510
+ return await this.resend.get(`/emails/${emailId}/attachments/${id}`);
511
+ }
512
+ async list(options) {
513
+ const { emailId } = options;
514
+ const queryString = buildPaginationQuery(options);
515
+ const url = queryString ? `/emails/${emailId}/attachments?${queryString}` : `/emails/${emailId}/attachments`;
516
+ return await this.resend.get(url);
517
+ }
518
+ };
519
+ //#endregion
520
+ //#region src/emails/receiving/attachments/attachments.ts
521
+ var Attachments = class {
522
+ constructor(resend) {
523
+ this.resend = resend;
524
+ }
525
+ async get(options) {
526
+ const { emailId, id } = options;
527
+ return await this.resend.get(`/emails/receiving/${emailId}/attachments/${id}`);
528
+ }
529
+ async list(options) {
530
+ const { emailId } = options;
531
+ const queryString = buildPaginationQuery(options);
532
+ const url = queryString ? `/emails/receiving/${emailId}/attachments?${queryString}` : `/emails/receiving/${emailId}/attachments`;
533
+ return await this.resend.get(url);
534
+ }
535
+ };
536
+ //#endregion
537
+ //#region src/emails/receiving/receiving.ts
538
+ var Receiving = class {
539
+ constructor(resend) {
540
+ this.resend = resend;
541
+ this.attachments = new Attachments(resend);
542
+ }
543
+ async get(id) {
544
+ return await this.resend.get(`/emails/receiving/${id}`);
545
+ }
546
+ async list(options = {}) {
547
+ const queryString = buildPaginationQuery(options);
548
+ const url = queryString ? `/emails/receiving?${queryString}` : "/emails/receiving";
549
+ return await this.resend.get(url);
550
+ }
551
+ async forward(options) {
552
+ const { emailId, to, from } = options;
553
+ const passthrough = options.passthrough !== false;
554
+ const emailResponse = await this.get(emailId);
555
+ if (emailResponse.error) return {
556
+ data: null,
557
+ error: emailResponse.error,
558
+ headers: emailResponse.headers
559
+ };
560
+ const email = emailResponse.data;
561
+ const originalSubject = email.subject || "(no subject)";
562
+ if (passthrough) return this.forwardPassthrough(email, {
563
+ to,
564
+ from,
565
+ subject: originalSubject
566
+ });
567
+ const forwardSubject = originalSubject.startsWith("Fwd:") ? originalSubject : `Fwd: ${originalSubject}`;
568
+ return this.forwardWrapped(email, {
569
+ to,
570
+ from,
571
+ subject: forwardSubject,
572
+ text: "text" in options ? options.text : void 0,
573
+ html: "html" in options ? options.html : void 0
574
+ });
575
+ }
576
+ async forwardPassthrough(email, options) {
577
+ const { to, from, subject } = options;
578
+ if (!email.raw?.download_url) return {
579
+ data: null,
580
+ error: {
581
+ name: "validation_error",
582
+ message: "Raw email content is not available for this email",
583
+ statusCode: 400
584
+ },
585
+ headers: null
586
+ };
587
+ const rawResponse = await fetch(email.raw.download_url);
588
+ if (!rawResponse.ok) return {
589
+ data: null,
590
+ error: {
591
+ name: "application_error",
592
+ message: "Failed to download raw email content",
593
+ statusCode: rawResponse.status
594
+ },
595
+ headers: null
596
+ };
597
+ const rawEmailContent = await rawResponse.text();
598
+ const parsed = await postal_mime.default.parse(rawEmailContent, { attachmentEncoding: "base64" });
599
+ const attachments = parsed.attachments.map((attachment) => {
600
+ const contentId = attachment.contentId ? attachment.contentId.replace(/^<|>$/g, "") : void 0;
601
+ return {
602
+ filename: attachment.filename,
603
+ content: attachment.content.toString(),
604
+ content_type: attachment.mimeType,
605
+ content_id: contentId || void 0
606
+ };
607
+ });
608
+ return await this.resend.post("/emails", {
609
+ from,
610
+ to,
611
+ subject,
612
+ text: parsed.text || void 0,
613
+ html: parsed.html || void 0,
614
+ attachments: attachments.length > 0 ? attachments : void 0
615
+ });
616
+ }
617
+ async forwardWrapped(email, options) {
618
+ const { to, from, subject, text, html } = options;
619
+ if (!email.raw?.download_url) return {
620
+ data: null,
621
+ error: {
622
+ name: "validation_error",
623
+ message: "Raw email content is not available for this email",
624
+ statusCode: 400
625
+ },
626
+ headers: null
627
+ };
628
+ const rawResponse = await fetch(email.raw.download_url);
629
+ if (!rawResponse.ok) return {
630
+ data: null,
631
+ error: {
632
+ name: "application_error",
633
+ message: "Failed to download raw email content",
634
+ statusCode: rawResponse.status
635
+ },
636
+ headers: null
637
+ };
638
+ const rawEmailContent = await rawResponse.text();
639
+ return await this.resend.post("/emails", {
640
+ from,
641
+ to,
642
+ subject,
643
+ text,
644
+ html,
645
+ attachments: [{
646
+ filename: "forwarded_message.eml",
647
+ content: Buffer.from(rawEmailContent).toString("base64"),
648
+ content_type: "message/rfc822"
649
+ }]
650
+ });
651
+ }
652
+ };
653
+ //#endregion
654
+ //#region src/emails/emails.ts
655
+ var Emails = class {
656
+ constructor(resend) {
657
+ this.resend = resend;
658
+ this.attachments = new Attachments$1(resend);
659
+ this.receiving = new Receiving(resend);
660
+ }
661
+ async send(payload, options = {}) {
662
+ return this.create(payload, options);
663
+ }
664
+ async create(payload, options = {}) {
665
+ if (payload.react) payload.html = await render(payload.react);
666
+ return await this.resend.post("/emails", parseEmailToApiOptions(payload), options);
667
+ }
668
+ async get(id) {
669
+ return await this.resend.get(`/emails/${id}`);
670
+ }
671
+ async list(options = {}) {
672
+ const queryString = buildPaginationQuery(options);
673
+ const url = queryString ? `/emails?${queryString}` : "/emails";
674
+ return await this.resend.get(url);
675
+ }
676
+ async update(payload) {
677
+ return await this.resend.patch(`/emails/${payload.id}`, { scheduled_at: payload.scheduledAt });
678
+ }
679
+ async cancel(id) {
680
+ return await this.resend.post(`/emails/${id}/cancel`);
681
+ }
682
+ };
683
+ //#endregion
684
+ //#region src/segments/segments.ts
685
+ var Segments = class {
686
+ constructor(resend) {
687
+ this.resend = resend;
688
+ }
689
+ async create(payload, options = {}) {
690
+ return await this.resend.post("/segments", payload, options);
691
+ }
692
+ async list(options = {}) {
693
+ const queryString = buildPaginationQuery(options);
694
+ const url = queryString ? `/segments?${queryString}` : "/segments";
695
+ return await this.resend.get(url);
696
+ }
697
+ async get(id) {
698
+ return await this.resend.get(`/segments/${id}`);
699
+ }
700
+ async remove(id) {
701
+ return await this.resend.delete(`/segments/${id}`);
702
+ }
703
+ };
704
+ //#endregion
705
+ //#region src/common/utils/get-pagination-query-properties.ts
706
+ function getPaginationQueryProperties(options = {}) {
707
+ const query = new URLSearchParams();
708
+ if (options.before) query.set("before", options.before);
709
+ if (options.after) query.set("after", options.after);
710
+ if (options.limit) query.set("limit", options.limit.toString());
711
+ return query.size > 0 ? `?${query.toString()}` : "";
712
+ }
713
+ //#endregion
714
+ //#region src/common/utils/parse-template-to-api-options.ts
715
+ function parseVariables(variables) {
716
+ return variables?.map((variable) => ({
717
+ key: variable.key,
718
+ type: variable.type,
719
+ fallback_value: variable.fallbackValue
720
+ }));
721
+ }
722
+ function parseTemplateToApiOptions(template) {
723
+ return {
724
+ name: "name" in template ? template.name : void 0,
725
+ subject: template.subject,
726
+ html: template.html,
727
+ text: template.text,
728
+ alias: template.alias,
729
+ from: template.from,
730
+ reply_to: template.replyTo,
731
+ variables: parseVariables(template.variables)
732
+ };
733
+ }
734
+ //#endregion
735
+ //#region src/templates/chainable-template-result.ts
736
+ var ChainableTemplateResult = class {
737
+ constructor(promise, publishFn) {
738
+ this.promise = promise;
739
+ this.publishFn = publishFn;
740
+ }
741
+ then(onfulfilled, onrejected) {
742
+ return this.promise.then(onfulfilled, onrejected);
743
+ }
744
+ async publish() {
745
+ const { data, error } = await this.promise;
746
+ if (error) return {
747
+ data: null,
748
+ headers: null,
749
+ error
750
+ };
751
+ return this.publishFn(data.id);
752
+ }
753
+ };
754
+ //#endregion
755
+ //#region src/templates/templates.ts
756
+ var Templates = class {
757
+ constructor(resend) {
758
+ this.resend = resend;
759
+ }
760
+ create(payload) {
761
+ return new ChainableTemplateResult(this.performCreate(payload), this.publish.bind(this));
762
+ }
763
+ async performCreate(payload) {
764
+ if (payload.react) {
765
+ if (!this.renderAsync) try {
766
+ const { renderAsync } = await import("@react-email/render");
767
+ this.renderAsync = renderAsync;
768
+ } catch {
769
+ throw new Error("Failed to render React component. Make sure to install `@react-email/render`");
770
+ }
771
+ payload.html = await this.renderAsync(payload.react);
772
+ }
773
+ return this.resend.post("/templates", parseTemplateToApiOptions(payload));
774
+ }
775
+ async remove(identifier) {
776
+ return await this.resend.delete(`/templates/${identifier}`);
777
+ }
778
+ async get(identifier) {
779
+ return await this.resend.get(`/templates/${identifier}`);
780
+ }
781
+ async list(options = {}) {
782
+ return this.resend.get(`/templates${getPaginationQueryProperties(options)}`);
783
+ }
784
+ duplicate(identifier) {
785
+ return new ChainableTemplateResult(this.resend.post(`/templates/${identifier}/duplicate`), this.publish.bind(this));
786
+ }
787
+ async publish(identifier) {
788
+ return await this.resend.post(`/templates/${identifier}/publish`);
789
+ }
790
+ async update(identifier, payload) {
791
+ return await this.resend.patch(`/templates/${identifier}`, parseTemplateToApiOptions(payload));
792
+ }
793
+ };
794
+ //#endregion
795
+ //#region src/topics/topics.ts
796
+ var Topics = class {
797
+ constructor(resend) {
798
+ this.resend = resend;
799
+ }
800
+ async create(payload) {
801
+ const { defaultSubscription, ...body } = payload;
802
+ return await this.resend.post("/topics", {
803
+ ...body,
804
+ default_subscription: defaultSubscription
805
+ });
806
+ }
807
+ async list() {
808
+ return await this.resend.get("/topics");
809
+ }
810
+ async get(id) {
811
+ if (!id) return {
812
+ data: null,
813
+ headers: null,
814
+ error: {
815
+ message: "Missing `id` field.",
816
+ statusCode: null,
817
+ name: "missing_required_field"
818
+ }
819
+ };
820
+ return await this.resend.get(`/topics/${id}`);
821
+ }
822
+ async update(payload) {
823
+ if (!payload.id) return {
824
+ data: null,
825
+ headers: null,
826
+ error: {
827
+ message: "Missing `id` field.",
828
+ statusCode: null,
829
+ name: "missing_required_field"
830
+ }
831
+ };
832
+ return await this.resend.patch(`/topics/${payload.id}`, payload);
833
+ }
834
+ async remove(id) {
835
+ if (!id) return {
836
+ data: null,
837
+ headers: null,
838
+ error: {
839
+ message: "Missing `id` field.",
840
+ statusCode: null,
841
+ name: "missing_required_field"
842
+ }
843
+ };
844
+ return await this.resend.delete(`/topics/${id}`);
845
+ }
846
+ };
847
+ //#endregion
848
+ //#region src/webhooks/webhooks.ts
849
+ var Webhooks = class {
850
+ constructor(resend) {
851
+ this.resend = resend;
852
+ }
853
+ async create(payload, options = {}) {
854
+ return await this.resend.post("/webhooks", payload, options);
855
+ }
856
+ async get(id) {
857
+ return await this.resend.get(`/webhooks/${id}`);
858
+ }
859
+ async list(options = {}) {
860
+ const queryString = buildPaginationQuery(options);
861
+ const url = queryString ? `/webhooks?${queryString}` : "/webhooks";
862
+ return await this.resend.get(url);
863
+ }
864
+ async update(id, payload) {
865
+ return await this.resend.patch(`/webhooks/${id}`, payload);
866
+ }
867
+ async remove(id) {
868
+ return await this.resend.delete(`/webhooks/${id}`);
869
+ }
870
+ verify(payload) {
871
+ return new svix.Webhook(payload.webhookSecret).verify(payload.payload, {
872
+ "svix-id": payload.headers.id,
873
+ "svix-timestamp": payload.headers.timestamp,
874
+ "svix-signature": payload.headers.signature
875
+ });
876
+ }
877
+ };
878
+ //#endregion
879
+ //#region src/resend.ts
880
+ const defaultBaseUrl = "https://api.resend.com";
881
+ const defaultUserAgent = `resend-node:${version}`;
882
+ const baseUrl = typeof process !== "undefined" && process.env ? process.env.RESEND_BASE_URL || defaultBaseUrl : defaultBaseUrl;
883
+ const userAgent = typeof process !== "undefined" && process.env ? process.env.RESEND_USER_AGENT || defaultUserAgent : defaultUserAgent;
884
+ var Resend = class {
885
+ constructor(key) {
886
+ this.key = key;
887
+ this.apiKeys = new ApiKeys(this);
888
+ this.segments = new Segments(this);
889
+ this.audiences = this.segments;
890
+ this.batch = new Batch(this);
891
+ this.broadcasts = new Broadcasts(this);
892
+ this.contacts = new Contacts(this);
893
+ this.contactProperties = new ContactProperties(this);
894
+ this.domains = new Domains(this);
895
+ this.emails = new Emails(this);
896
+ this.webhooks = new Webhooks(this);
897
+ this.templates = new Templates(this);
898
+ this.topics = new Topics(this);
899
+ if (!key) {
900
+ if (typeof process !== "undefined" && process.env) this.key = process.env.RESEND_API_KEY;
901
+ if (!this.key) throw new Error("Missing API key. Pass it to the constructor `new Resend(\"re_123\")`");
902
+ }
903
+ this.headers = new Headers({
904
+ Authorization: `Bearer ${this.key}`,
905
+ "User-Agent": userAgent,
906
+ "Content-Type": "application/json"
907
+ });
908
+ }
909
+ async fetchRequest(path, options = {}) {
910
+ try {
911
+ const response = await fetch(`${baseUrl}${path}`, options);
912
+ if (!response.ok) try {
913
+ const rawError = await response.text();
914
+ return {
915
+ data: null,
916
+ error: JSON.parse(rawError),
917
+ headers: Object.fromEntries(response.headers.entries())
918
+ };
919
+ } catch (err) {
920
+ if (err instanceof SyntaxError) return {
921
+ data: null,
922
+ error: {
923
+ name: "application_error",
924
+ statusCode: response.status,
925
+ message: "Internal server error. We are unable to process your request right now, please try again later."
926
+ },
927
+ headers: Object.fromEntries(response.headers.entries())
928
+ };
929
+ const error = {
930
+ message: response.statusText,
931
+ statusCode: response.status,
932
+ name: "application_error"
933
+ };
934
+ if (err instanceof Error) return {
935
+ data: null,
936
+ error: {
937
+ ...error,
938
+ message: err.message
939
+ },
940
+ headers: Object.fromEntries(response.headers.entries())
941
+ };
942
+ return {
943
+ data: null,
944
+ error,
945
+ headers: Object.fromEntries(response.headers.entries())
946
+ };
947
+ }
948
+ return {
949
+ data: await response.json(),
950
+ error: null,
951
+ headers: Object.fromEntries(response.headers.entries())
952
+ };
953
+ } catch {
954
+ return {
955
+ data: null,
956
+ error: {
957
+ name: "application_error",
958
+ statusCode: null,
959
+ message: "Unable to fetch data. The request could not be resolved."
960
+ },
961
+ headers: null
962
+ };
963
+ }
964
+ }
965
+ async post(path, entity, options = {}) {
966
+ const headers = new Headers(this.headers);
967
+ if (options.headers) for (const [key, value] of new Headers(options.headers).entries()) headers.set(key, value);
968
+ if (options.idempotencyKey) headers.set("Idempotency-Key", options.idempotencyKey);
969
+ const requestOptions = {
970
+ method: "POST",
971
+ body: JSON.stringify(entity),
972
+ ...options,
973
+ headers
974
+ };
975
+ return this.fetchRequest(path, requestOptions);
976
+ }
977
+ async get(path, options = {}) {
978
+ const headers = new Headers(this.headers);
979
+ if (options.headers) for (const [key, value] of new Headers(options.headers).entries()) headers.set(key, value);
980
+ const requestOptions = {
981
+ method: "GET",
982
+ ...options,
983
+ headers
984
+ };
985
+ return this.fetchRequest(path, requestOptions);
986
+ }
987
+ async put(path, entity, options = {}) {
988
+ const headers = new Headers(this.headers);
989
+ if (options.headers) for (const [key, value] of new Headers(options.headers).entries()) headers.set(key, value);
990
+ const requestOptions = {
991
+ method: "PUT",
992
+ body: JSON.stringify(entity),
993
+ ...options,
994
+ headers
995
+ };
996
+ return this.fetchRequest(path, requestOptions);
997
+ }
998
+ async patch(path, entity, options = {}) {
999
+ const headers = new Headers(this.headers);
1000
+ if (options.headers) for (const [key, value] of new Headers(options.headers).entries()) headers.set(key, value);
1001
+ const requestOptions = {
1002
+ method: "PATCH",
1003
+ body: JSON.stringify(entity),
1004
+ ...options,
1005
+ headers
1006
+ };
1007
+ return this.fetchRequest(path, requestOptions);
1008
+ }
1009
+ async delete(path, query) {
1010
+ const requestOptions = {
1011
+ method: "DELETE",
1012
+ body: JSON.stringify(query),
1013
+ headers: this.headers
1014
+ };
1015
+ return this.fetchRequest(path, requestOptions);
1016
+ }
1017
+ };
1018
+ //#endregion
1019
+ exports.Resend = Resend;