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