postbound 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1022 -2
- package/dist/index.cjs +411 -0
- package/dist/index.d.cts +614 -0
- package/dist/index.d.mts +614 -0
- package/dist/index.mjs +410 -0
- package/package.json +67 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let standardwebhooks = require("standardwebhooks");
|
|
3
|
+
// package.json
|
|
4
|
+
var version = "0.1.0";
|
|
5
|
+
// src/common/utils/build-path.ts
|
|
6
|
+
function buildPath(...segments) {
|
|
7
|
+
return `/${segments.map(encodeURIComponent).join("/")}`;
|
|
8
|
+
}
|
|
9
|
+
// src/common/utils/with-query.ts
|
|
10
|
+
function withQuery(path, params) {
|
|
11
|
+
const search = new URLSearchParams();
|
|
12
|
+
for (const [key, value] of Object.entries(params)) if (value !== void 0 && value !== "") search.set(key, String(value));
|
|
13
|
+
const query = search.toString();
|
|
14
|
+
return query ? `${path}?${query}` : path;
|
|
15
|
+
}
|
|
16
|
+
function withPagination(path, options, extra = {}) {
|
|
17
|
+
return withQuery(path, {
|
|
18
|
+
limit: options.limit,
|
|
19
|
+
after: options.after,
|
|
20
|
+
before: options.before,
|
|
21
|
+
...extra
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
// src/api-keys/api-keys.ts
|
|
25
|
+
var ApiKeys = class {
|
|
26
|
+
postbound;
|
|
27
|
+
constructor(postbound) {
|
|
28
|
+
this.postbound = postbound;
|
|
29
|
+
}
|
|
30
|
+
create(payload, requestOptions = {}) {
|
|
31
|
+
return this.postbound.post(buildPath("api-keys"), {
|
|
32
|
+
name: payload.name,
|
|
33
|
+
permission: payload.permission,
|
|
34
|
+
domain_id: payload.domainId
|
|
35
|
+
}, requestOptions);
|
|
36
|
+
}
|
|
37
|
+
list(options = {}, requestOptions = {}) {
|
|
38
|
+
return this.postbound.get(withPagination(buildPath("api-keys"), options), requestOptions);
|
|
39
|
+
}
|
|
40
|
+
remove(id, requestOptions = {}) {
|
|
41
|
+
return this.postbound.delete(buildPath("api-keys", id), requestOptions);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
// src/audiences/audiences.ts
|
|
45
|
+
var Audiences = class {
|
|
46
|
+
postbound;
|
|
47
|
+
constructor(postbound) {
|
|
48
|
+
this.postbound = postbound;
|
|
49
|
+
}
|
|
50
|
+
create(payload, requestOptions = {}) {
|
|
51
|
+
return this.postbound.post(buildPath("audiences"), payload, requestOptions);
|
|
52
|
+
}
|
|
53
|
+
get(id, requestOptions = {}) {
|
|
54
|
+
return this.postbound.get(buildPath("audiences", id), requestOptions);
|
|
55
|
+
}
|
|
56
|
+
list(options = {}, requestOptions = {}) {
|
|
57
|
+
return this.postbound.get(withPagination(buildPath("audiences"), options), requestOptions);
|
|
58
|
+
}
|
|
59
|
+
remove(id, requestOptions = {}) {
|
|
60
|
+
return this.postbound.delete(buildPath("audiences", id), requestOptions);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
// src/render.ts
|
|
64
|
+
async function render(node) {
|
|
65
|
+
let renderComponent;
|
|
66
|
+
try {
|
|
67
|
+
({render: renderComponent} = await import("@react-email/render"));
|
|
68
|
+
} catch {
|
|
69
|
+
throw new Error("Failed to render React component. Make sure to install `@react-email/render` or `@react-email/components`.");
|
|
70
|
+
}
|
|
71
|
+
return renderComponent(node);
|
|
72
|
+
}
|
|
73
|
+
// src/emails/to-email-api-options.ts
|
|
74
|
+
function encodeContent(content) {
|
|
75
|
+
if (content === void 0 || typeof content === "string") return content;
|
|
76
|
+
return (content instanceof ArrayBuffer ? Buffer.from(content) : Buffer.from(content.buffer, content.byteOffset, content.byteLength)).toString("base64");
|
|
77
|
+
}
|
|
78
|
+
function toAttachmentApiOptions(attachment) {
|
|
79
|
+
return {
|
|
80
|
+
content: encodeContent(attachment.content),
|
|
81
|
+
filename: attachment.filename,
|
|
82
|
+
path: attachment.path,
|
|
83
|
+
content_type: attachment.contentType,
|
|
84
|
+
content_id: attachment.contentId
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
async function toEmailApiOptions(email) {
|
|
88
|
+
const html = email.react ? await render(email.react) : email.html;
|
|
89
|
+
return {
|
|
90
|
+
from: email.from,
|
|
91
|
+
to: email.to,
|
|
92
|
+
subject: email.subject,
|
|
93
|
+
html,
|
|
94
|
+
text: email.text,
|
|
95
|
+
cc: email.cc,
|
|
96
|
+
bcc: email.bcc,
|
|
97
|
+
reply_to: email.replyTo,
|
|
98
|
+
headers: email.headers,
|
|
99
|
+
tags: email.tags,
|
|
100
|
+
attachments: email.attachments?.map(toAttachmentApiOptions),
|
|
101
|
+
scheduled_at: email.scheduledAt
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// src/batch/batch.ts
|
|
105
|
+
var Batch = class {
|
|
106
|
+
postbound;
|
|
107
|
+
constructor(postbound) {
|
|
108
|
+
this.postbound = postbound;
|
|
109
|
+
}
|
|
110
|
+
send(payload, requestOptions = {}) {
|
|
111
|
+
return this.create(payload, requestOptions);
|
|
112
|
+
}
|
|
113
|
+
async create(payload, requestOptions = {}) {
|
|
114
|
+
const emails = await Promise.all(payload.map(toEmailApiOptions));
|
|
115
|
+
return this.postbound.post(buildPath("emails", "batch"), emails, requestOptions);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
// src/common/utils/failure.ts
|
|
119
|
+
function failure(error) {
|
|
120
|
+
return {
|
|
121
|
+
data: null,
|
|
122
|
+
error,
|
|
123
|
+
headers: null
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// src/common/utils/has-dot-segment.ts
|
|
127
|
+
const dotSegment = /^\.{1,2}$/;
|
|
128
|
+
function hasDotSegment(requestPath) {
|
|
129
|
+
return (requestPath.split("?")[0] ?? "").split("/").some((segment) => dotSegment.test(segment));
|
|
130
|
+
}
|
|
131
|
+
// src/common/utils/parse-body.ts
|
|
132
|
+
function parseJson(body) {
|
|
133
|
+
if (body === "") return {};
|
|
134
|
+
try {
|
|
135
|
+
return JSON.parse(body);
|
|
136
|
+
} catch {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function toErrorResponse(parsed, statusCode) {
|
|
141
|
+
if (isErrorBody(parsed)) return parsed;
|
|
142
|
+
return {
|
|
143
|
+
name: "application_error",
|
|
144
|
+
statusCode,
|
|
145
|
+
message: "Internal server error. We are unable to process your request right now, please try again later."
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function isErrorBody(value) {
|
|
149
|
+
return typeof value === "object" && value !== null && "name" in value && typeof value.name === "string" && "message" in value && typeof value.message === "string";
|
|
150
|
+
}
|
|
151
|
+
// src/common/utils/read-env.ts
|
|
152
|
+
function readEnv(name) {
|
|
153
|
+
if (typeof process === "undefined") return;
|
|
154
|
+
return process.env?.[name] || void 0;
|
|
155
|
+
}
|
|
156
|
+
// src/contacts/contacts.ts
|
|
157
|
+
var Contacts = class {
|
|
158
|
+
postbound;
|
|
159
|
+
constructor(postbound) {
|
|
160
|
+
this.postbound = postbound;
|
|
161
|
+
}
|
|
162
|
+
create(payload, requestOptions = {}) {
|
|
163
|
+
return this.postbound.post(buildPath("audiences", payload.audienceId, "contacts"), {
|
|
164
|
+
email: payload.email,
|
|
165
|
+
first_name: payload.firstName,
|
|
166
|
+
last_name: payload.lastName,
|
|
167
|
+
unsubscribed: payload.unsubscribed
|
|
168
|
+
}, requestOptions);
|
|
169
|
+
}
|
|
170
|
+
get(options, requestOptions = {}) {
|
|
171
|
+
return this.withContactPath(options, (path) => this.postbound.get(path, requestOptions));
|
|
172
|
+
}
|
|
173
|
+
list(options, requestOptions = {}) {
|
|
174
|
+
return this.postbound.get(withPagination(buildPath("audiences", options.audienceId, "contacts"), options), requestOptions);
|
|
175
|
+
}
|
|
176
|
+
update(options, requestOptions = {}) {
|
|
177
|
+
return this.withContactPath(options, (path) => this.postbound.patch(path, {
|
|
178
|
+
first_name: options.firstName,
|
|
179
|
+
last_name: options.lastName,
|
|
180
|
+
unsubscribed: options.unsubscribed
|
|
181
|
+
}, requestOptions));
|
|
182
|
+
}
|
|
183
|
+
remove(options, requestOptions = {}) {
|
|
184
|
+
return this.withContactPath(options, (path) => this.postbound.delete(path, requestOptions));
|
|
185
|
+
}
|
|
186
|
+
withContactPath(reference, run) {
|
|
187
|
+
const contact = reference.id ?? reference.email;
|
|
188
|
+
if (!contact) return Promise.resolve(failure({
|
|
189
|
+
name: "missing_required_field",
|
|
190
|
+
statusCode: null,
|
|
191
|
+
message: "Missing `id` or `email` field."
|
|
192
|
+
}));
|
|
193
|
+
return run(buildPath("audiences", reference.audienceId, "contacts", contact));
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
// src/domains/domains.ts
|
|
197
|
+
var Domains = class {
|
|
198
|
+
postbound;
|
|
199
|
+
constructor(postbound) {
|
|
200
|
+
this.postbound = postbound;
|
|
201
|
+
}
|
|
202
|
+
create(payload, requestOptions = {}) {
|
|
203
|
+
return this.postbound.post(buildPath("domains"), {
|
|
204
|
+
name: payload.name,
|
|
205
|
+
region: payload.region,
|
|
206
|
+
custom_return_path: payload.customReturnPath
|
|
207
|
+
}, requestOptions);
|
|
208
|
+
}
|
|
209
|
+
get(id, requestOptions = {}) {
|
|
210
|
+
return this.postbound.get(buildPath("domains", id), requestOptions);
|
|
211
|
+
}
|
|
212
|
+
list(options = {}, requestOptions = {}) {
|
|
213
|
+
return this.postbound.get(withPagination(buildPath("domains"), options), requestOptions);
|
|
214
|
+
}
|
|
215
|
+
update(payload, requestOptions = {}) {
|
|
216
|
+
return this.postbound.patch(buildPath("domains", payload.id), {
|
|
217
|
+
open_tracking: payload.openTracking,
|
|
218
|
+
click_tracking: payload.clickTracking,
|
|
219
|
+
tls: payload.tls,
|
|
220
|
+
capabilities: payload.capabilities
|
|
221
|
+
}, requestOptions);
|
|
222
|
+
}
|
|
223
|
+
verify(id, requestOptions = {}) {
|
|
224
|
+
return this.postbound.post(buildPath("domains", id, "verify"), void 0, requestOptions);
|
|
225
|
+
}
|
|
226
|
+
remove(id, requestOptions = {}) {
|
|
227
|
+
return this.postbound.delete(buildPath("domains", id), requestOptions);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
// src/emails/receiving/attachments.ts
|
|
231
|
+
var ReceivingAttachments = class {
|
|
232
|
+
postbound;
|
|
233
|
+
constructor(postbound) {
|
|
234
|
+
this.postbound = postbound;
|
|
235
|
+
}
|
|
236
|
+
get(options, requestOptions = {}) {
|
|
237
|
+
return this.postbound.get(buildPath("emails", "receiving", options.emailId, "attachments", options.id), requestOptions);
|
|
238
|
+
}
|
|
239
|
+
list(options, requestOptions = {}) {
|
|
240
|
+
return this.postbound.get(withPagination(buildPath("emails", "receiving", options.emailId, "attachments"), options), requestOptions);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
// src/emails/receiving/receiving.ts
|
|
244
|
+
var Receiving = class {
|
|
245
|
+
postbound;
|
|
246
|
+
attachments;
|
|
247
|
+
constructor(postbound) {
|
|
248
|
+
this.postbound = postbound;
|
|
249
|
+
this.attachments = new ReceivingAttachments(postbound);
|
|
250
|
+
}
|
|
251
|
+
get(id, options = {}, requestOptions = {}) {
|
|
252
|
+
return this.postbound.get(withQuery(buildPath("emails", "receiving", id), { html_format: options.html_format }), requestOptions);
|
|
253
|
+
}
|
|
254
|
+
list(options = {}, requestOptions = {}) {
|
|
255
|
+
return this.postbound.get(withPagination(buildPath("emails", "receiving"), options), requestOptions);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
// src/emails/emails.ts
|
|
259
|
+
var Emails = class {
|
|
260
|
+
postbound;
|
|
261
|
+
receiving;
|
|
262
|
+
constructor(postbound) {
|
|
263
|
+
this.postbound = postbound;
|
|
264
|
+
this.receiving = new Receiving(postbound);
|
|
265
|
+
}
|
|
266
|
+
send(payload, requestOptions = {}) {
|
|
267
|
+
return this.create(payload, requestOptions);
|
|
268
|
+
}
|
|
269
|
+
async create(payload, requestOptions = {}) {
|
|
270
|
+
return this.postbound.post("/emails", await toEmailApiOptions(payload), requestOptions);
|
|
271
|
+
}
|
|
272
|
+
get(id, requestOptions = {}) {
|
|
273
|
+
return this.postbound.get(buildPath("emails", id), requestOptions);
|
|
274
|
+
}
|
|
275
|
+
list(options = {}, requestOptions = {}) {
|
|
276
|
+
return this.postbound.get(withPagination(buildPath("emails"), options, {
|
|
277
|
+
status: options.status,
|
|
278
|
+
q: options.q
|
|
279
|
+
}), requestOptions);
|
|
280
|
+
}
|
|
281
|
+
update(payload, requestOptions = {}) {
|
|
282
|
+
return this.postbound.patch(buildPath("emails", payload.id), { scheduled_at: payload.scheduledAt }, requestOptions);
|
|
283
|
+
}
|
|
284
|
+
cancel(id, requestOptions = {}) {
|
|
285
|
+
return this.postbound.post(buildPath("emails", id, "cancel"), void 0, requestOptions);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
// src/webhooks/webhooks.ts
|
|
289
|
+
var Webhooks = class {
|
|
290
|
+
postbound;
|
|
291
|
+
constructor(postbound) {
|
|
292
|
+
this.postbound = postbound;
|
|
293
|
+
}
|
|
294
|
+
create(payload, requestOptions = {}) {
|
|
295
|
+
return this.postbound.post(buildPath("webhooks"), payload, requestOptions);
|
|
296
|
+
}
|
|
297
|
+
get(id, requestOptions = {}) {
|
|
298
|
+
return this.postbound.get(buildPath("webhooks", id), requestOptions);
|
|
299
|
+
}
|
|
300
|
+
list(options = {}, requestOptions = {}) {
|
|
301
|
+
return this.postbound.get(withPagination(buildPath("webhooks"), options), requestOptions);
|
|
302
|
+
}
|
|
303
|
+
update(id, payload, requestOptions = {}) {
|
|
304
|
+
return this.postbound.patch(buildPath("webhooks", id), payload, requestOptions);
|
|
305
|
+
}
|
|
306
|
+
remove(id, requestOptions = {}) {
|
|
307
|
+
return this.postbound.delete(buildPath("webhooks", id), requestOptions);
|
|
308
|
+
}
|
|
309
|
+
test(id, requestOptions = {}) {
|
|
310
|
+
return this.postbound.post(buildPath("webhooks", id, "test"), void 0, requestOptions);
|
|
311
|
+
}
|
|
312
|
+
verify(options) {
|
|
313
|
+
return new standardwebhooks.Webhook(options.webhookSecret).verify(options.payload, {
|
|
314
|
+
"webhook-id": options.headers.id,
|
|
315
|
+
"webhook-timestamp": options.headers.timestamp,
|
|
316
|
+
"webhook-signature": options.headers.signature
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
// src/postbound.ts
|
|
321
|
+
const defaultBaseUrl = "https://api.postbound.app";
|
|
322
|
+
const defaultUserAgent = `postbound-node:${version}`;
|
|
323
|
+
var Postbound = class {
|
|
324
|
+
key;
|
|
325
|
+
baseUrl;
|
|
326
|
+
userAgent;
|
|
327
|
+
debug;
|
|
328
|
+
headers;
|
|
329
|
+
apiKeys = new ApiKeys(this);
|
|
330
|
+
audiences = new Audiences(this);
|
|
331
|
+
batch = new Batch(this);
|
|
332
|
+
contacts = new Contacts(this);
|
|
333
|
+
domains = new Domains(this);
|
|
334
|
+
emails = new Emails(this);
|
|
335
|
+
webhooks = new Webhooks(this);
|
|
336
|
+
constructor(key, options = {}) {
|
|
337
|
+
const resolvedKey = key || readEnv("POSTBOUND_API_KEY");
|
|
338
|
+
if (!resolvedKey) throw new Error("Missing API key. Pass it to the constructor `new Postbound(\"pb_123\")`");
|
|
339
|
+
this.key = resolvedKey;
|
|
340
|
+
this.baseUrl = options.baseUrl ?? readEnv("POSTBOUND_BASE_URL") ?? defaultBaseUrl;
|
|
341
|
+
this.userAgent = options.userAgent ?? readEnv("POSTBOUND_USER_AGENT") ?? defaultUserAgent;
|
|
342
|
+
this.debug = readEnv("POSTBOUND_DEBUG") === "true";
|
|
343
|
+
this.headers = new Headers({
|
|
344
|
+
Authorization: `Bearer ${this.key}`,
|
|
345
|
+
"User-Agent": this.userAgent,
|
|
346
|
+
"Content-Type": "application/json"
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
get(path, options = {}) {
|
|
350
|
+
return this.send("GET", path, void 0, options);
|
|
351
|
+
}
|
|
352
|
+
post(path, body, options = {}) {
|
|
353
|
+
return this.send("POST", path, body, options);
|
|
354
|
+
}
|
|
355
|
+
patch(path, body, options = {}) {
|
|
356
|
+
return this.send("PATCH", path, body, options);
|
|
357
|
+
}
|
|
358
|
+
delete(path, options = {}) {
|
|
359
|
+
return this.send("DELETE", path, void 0, options);
|
|
360
|
+
}
|
|
361
|
+
send(method, path, body, options) {
|
|
362
|
+
const headers = new Headers(this.headers);
|
|
363
|
+
new Headers(options.headers).forEach((value, name) => {
|
|
364
|
+
headers.set(name, value);
|
|
365
|
+
});
|
|
366
|
+
if (options.idempotencyKey) headers.set("Idempotency-Key", options.idempotencyKey);
|
|
367
|
+
return this.execute(path, {
|
|
368
|
+
method,
|
|
369
|
+
headers,
|
|
370
|
+
signal: options.signal,
|
|
371
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
async execute(path, init) {
|
|
375
|
+
if (hasDotSegment(path)) return this.fail({
|
|
376
|
+
name: "invalid_parameter",
|
|
377
|
+
statusCode: null,
|
|
378
|
+
message: "Path parameters cannot be `.` or `..`."
|
|
379
|
+
}, path);
|
|
380
|
+
try {
|
|
381
|
+
const response = await fetch(`${this.baseUrl}${path}`, init);
|
|
382
|
+
const headers = Object.fromEntries(response.headers.entries());
|
|
383
|
+
const parsed = parseJson(await response.text());
|
|
384
|
+
if (!response.ok || parsed === void 0) return this.fail(toErrorResponse(parsed, response.status), path, headers, response.status);
|
|
385
|
+
return {
|
|
386
|
+
data: parsed,
|
|
387
|
+
error: null,
|
|
388
|
+
headers
|
|
389
|
+
};
|
|
390
|
+
} catch (error) {
|
|
391
|
+
if (init.signal?.aborted) throw error;
|
|
392
|
+
return this.fail({
|
|
393
|
+
name: "application_error",
|
|
394
|
+
statusCode: null,
|
|
395
|
+
message: "Unable to fetch data. The request could not be resolved."
|
|
396
|
+
}, path);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
fail(error, path, headers = null, status) {
|
|
400
|
+
if (this.debug) console.error("[Postbound API Error]:", {
|
|
401
|
+
...status !== void 0 && { status },
|
|
402
|
+
error,
|
|
403
|
+
path
|
|
404
|
+
});
|
|
405
|
+
return {
|
|
406
|
+
...failure(error),
|
|
407
|
+
headers
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
exports.Postbound = Postbound;
|