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