unitpost 0.0.0 → 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/README.md +170 -7
- package/dist/index.cjs +677 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2456 -0
- package/dist/index.d.ts +2456 -0
- package/dist/index.js +671 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -12
package/dist/index.js
ADDED
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual, randomUUID } from 'crypto';
|
|
2
|
+
|
|
3
|
+
// src/generated/operations.ts
|
|
4
|
+
var API_VERSION = "v1";
|
|
5
|
+
|
|
6
|
+
// src/http.ts
|
|
7
|
+
var SDK_VERSION = "0.1.0";
|
|
8
|
+
var DEFAULT_BASE_URL = "https://www.unitpost.com";
|
|
9
|
+
var UnitpostError = class extends Error {
|
|
10
|
+
code;
|
|
11
|
+
status;
|
|
12
|
+
requestId;
|
|
13
|
+
/** Per-field validation problems (present on 422 validation_error). */
|
|
14
|
+
details;
|
|
15
|
+
constructor(args) {
|
|
16
|
+
super(args.message);
|
|
17
|
+
this.name = "UnitpostError";
|
|
18
|
+
this.code = args.code;
|
|
19
|
+
this.status = args.status;
|
|
20
|
+
this.requestId = args.requestId;
|
|
21
|
+
this.details = args.details ?? [];
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function isRetryableStatus(status) {
|
|
25
|
+
return status === 429 || status >= 500 && status <= 599;
|
|
26
|
+
}
|
|
27
|
+
function parseRetryAfter(value) {
|
|
28
|
+
if (!value) return null;
|
|
29
|
+
const seconds = Number(value);
|
|
30
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
|
|
31
|
+
const date = Date.parse(value);
|
|
32
|
+
if (!Number.isNaN(date)) return Math.max(0, date - Date.now());
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
var HttpClient = class {
|
|
36
|
+
apiKey;
|
|
37
|
+
baseUrl;
|
|
38
|
+
timeout;
|
|
39
|
+
fetchImpl;
|
|
40
|
+
extraHeaders;
|
|
41
|
+
maxRetries;
|
|
42
|
+
retryBaseDelay;
|
|
43
|
+
maxRetryDelay;
|
|
44
|
+
sleep;
|
|
45
|
+
constructor(options = {}) {
|
|
46
|
+
const apiKey = options.apiKey ?? getEnv("UNITPOST_API_KEY");
|
|
47
|
+
if (!apiKey) {
|
|
48
|
+
throw new UnitpostError({
|
|
49
|
+
code: "missing_api_key",
|
|
50
|
+
message: "No API key provided. Pass `new Unitpost(apiKey)` or set the UNITPOST_API_KEY environment variable. Create a key at https://www.unitpost.com \u2192 Settings \u2192 API keys. Full reference: https://www.unitpost.com/docs",
|
|
51
|
+
status: 0,
|
|
52
|
+
requestId: null
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
this.apiKey = apiKey;
|
|
56
|
+
this.baseUrl = (options.baseUrl ?? getEnv("UNITPOST_BASE_URL") ?? DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
57
|
+
this.timeout = options.timeout ?? 3e4;
|
|
58
|
+
this.fetchImpl = options.fetch ?? globalThis.fetch;
|
|
59
|
+
this.extraHeaders = options.headers ?? {};
|
|
60
|
+
this.maxRetries = Math.max(0, options.maxRetries ?? 2);
|
|
61
|
+
this.retryBaseDelay = options.retryBaseDelay ?? 500;
|
|
62
|
+
this.maxRetryDelay = options.maxRetryDelay ?? 2e4;
|
|
63
|
+
this.sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
64
|
+
if (typeof this.fetchImpl !== "function") {
|
|
65
|
+
throw new UnitpostError({
|
|
66
|
+
code: "no_fetch",
|
|
67
|
+
message: "No global fetch available. Use Node 18+ or pass a `fetch` implementation.",
|
|
68
|
+
status: 0,
|
|
69
|
+
requestId: null
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns `{ data, error }` — never throws for an API-level error.
|
|
75
|
+
*
|
|
76
|
+
* Transient failures (429, 5xx, timeouts, network errors) are retried up to
|
|
77
|
+
* `maxRetries` times with backoff, but ONLY when the request is safe to
|
|
78
|
+
* replay: a GET, or a write that carries an `Idempotency-Key` (deduped
|
|
79
|
+
* server-side). A keyless write is never auto-retried. A 429/503 `Retry-After`
|
|
80
|
+
* is honored (clamped to `maxRetryDelay`); otherwise we use exponential
|
|
81
|
+
* backoff with full jitter.
|
|
82
|
+
*/
|
|
83
|
+
async request(args) {
|
|
84
|
+
const retrySafe = args.method === "GET" || Boolean(args.idempotencyKey);
|
|
85
|
+
let attempt = 0;
|
|
86
|
+
while (true) {
|
|
87
|
+
const { result, retryAfterMs } = await this.attempt(args);
|
|
88
|
+
const err = result.error;
|
|
89
|
+
const retryable = err !== null && (err.status === 0 || isRetryableStatus(err.status));
|
|
90
|
+
if (!err || !retryable || !retrySafe || attempt >= this.maxRetries) {
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
await this.sleep(this.backoffDelay(attempt, retryAfterMs));
|
|
94
|
+
attempt += 1;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Wait before the next retry: honor a clamped `Retry-After` if the server sent
|
|
98
|
+
// one, else exponential backoff (base · 2^attempt) with full jitter — both
|
|
99
|
+
// capped at `maxRetryDelay`.
|
|
100
|
+
backoffDelay(attempt, retryAfterMs) {
|
|
101
|
+
if (retryAfterMs !== null) return Math.min(retryAfterMs, this.maxRetryDelay);
|
|
102
|
+
const ceiling = Math.min(this.retryBaseDelay * 2 ** attempt, this.maxRetryDelay);
|
|
103
|
+
return Math.random() * ceiling;
|
|
104
|
+
}
|
|
105
|
+
// One HTTP attempt. Returns the `{ data, error }` result plus any parsed
|
|
106
|
+
// `Retry-After` (ms) so the retry loop can honor server-directed backoff.
|
|
107
|
+
async attempt(args) {
|
|
108
|
+
const url = this.buildUrl(args.path, args.query);
|
|
109
|
+
const headers = {
|
|
110
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
111
|
+
"User-Agent": `unitpost-node/${SDK_VERSION}`,
|
|
112
|
+
Accept: "application/json",
|
|
113
|
+
...this.extraHeaders
|
|
114
|
+
};
|
|
115
|
+
if (args.idempotencyKey) headers["Idempotency-Key"] = args.idempotencyKey;
|
|
116
|
+
let bodyText;
|
|
117
|
+
if (args.body !== void 0 && args.method !== "GET") {
|
|
118
|
+
headers["Content-Type"] = "application/json";
|
|
119
|
+
bodyText = JSON.stringify(args.body);
|
|
120
|
+
}
|
|
121
|
+
const controller = new AbortController();
|
|
122
|
+
const timer = setTimeout(() => controller.abort(), this.timeout);
|
|
123
|
+
try {
|
|
124
|
+
const res = await this.fetchImpl(url, {
|
|
125
|
+
method: args.method,
|
|
126
|
+
headers,
|
|
127
|
+
body: bodyText,
|
|
128
|
+
signal: controller.signal
|
|
129
|
+
});
|
|
130
|
+
const requestId = res.headers.get("x-request-id");
|
|
131
|
+
if (res.status === 204) return { result: { data: null, error: null }, retryAfterMs: null };
|
|
132
|
+
const raw = await res.text();
|
|
133
|
+
const parsed = raw ? safeJsonParse(raw) : null;
|
|
134
|
+
if (!res.ok) {
|
|
135
|
+
const errBody = parsed;
|
|
136
|
+
return {
|
|
137
|
+
result: {
|
|
138
|
+
data: null,
|
|
139
|
+
error: new UnitpostError({
|
|
140
|
+
code: errBody?.error?.code ?? "http_error",
|
|
141
|
+
message: errBody?.error?.message ?? `Request failed with status ${res.status}.`,
|
|
142
|
+
status: res.status,
|
|
143
|
+
requestId,
|
|
144
|
+
details: errBody?.error?.details
|
|
145
|
+
})
|
|
146
|
+
},
|
|
147
|
+
retryAfterMs: parseRetryAfter(res.headers.get("retry-after"))
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return { result: { data: parsed, error: null }, retryAfterMs: null };
|
|
151
|
+
} catch (err) {
|
|
152
|
+
const aborted = err instanceof Error && err.name === "AbortError";
|
|
153
|
+
return {
|
|
154
|
+
result: {
|
|
155
|
+
data: null,
|
|
156
|
+
error: new UnitpostError({
|
|
157
|
+
code: aborted ? "timeout" : "network_error",
|
|
158
|
+
message: aborted ? `Request timed out after ${this.timeout}ms.` : err instanceof Error ? err.message : "Network request failed.",
|
|
159
|
+
status: 0,
|
|
160
|
+
requestId: null
|
|
161
|
+
})
|
|
162
|
+
},
|
|
163
|
+
retryAfterMs: null
|
|
164
|
+
};
|
|
165
|
+
} finally {
|
|
166
|
+
clearTimeout(timer);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
buildUrl(path, query) {
|
|
170
|
+
const url = new URL(`${this.baseUrl}/api/${API_VERSION}${path}`);
|
|
171
|
+
if (query) {
|
|
172
|
+
for (const [key, value] of Object.entries(query)) {
|
|
173
|
+
if (value === void 0 || value === null) continue;
|
|
174
|
+
url.searchParams.set(key, String(value));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return url.toString();
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
function getEnv(name) {
|
|
181
|
+
return typeof process !== "undefined" && process.env ? process.env[name] : void 0;
|
|
182
|
+
}
|
|
183
|
+
function safeJsonParse(text) {
|
|
184
|
+
try {
|
|
185
|
+
return JSON.parse(text);
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// src/pagination.ts
|
|
192
|
+
async function* paginate(client, path, params = {}) {
|
|
193
|
+
let after = params.after;
|
|
194
|
+
for (; ; ) {
|
|
195
|
+
const { data, error } = await client.request({
|
|
196
|
+
method: "GET",
|
|
197
|
+
path,
|
|
198
|
+
query: { limit: params.limit, after, before: params.before }
|
|
199
|
+
});
|
|
200
|
+
if (error) throw error;
|
|
201
|
+
const page = data;
|
|
202
|
+
for (const row of page.data) yield row;
|
|
203
|
+
if (!page.has_more || page.data.length === 0) return;
|
|
204
|
+
after = page.data[page.data.length - 1].id;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
var SECRET_PREFIX = "whsec_";
|
|
208
|
+
var WebhookVerificationError = class extends Error {
|
|
209
|
+
constructor(message) {
|
|
210
|
+
super(message);
|
|
211
|
+
this.name = "WebhookVerificationError";
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
function header(headers, name) {
|
|
215
|
+
const v = headers[name] ?? headers[name.toLowerCase()];
|
|
216
|
+
return Array.isArray(v) ? v[0] : v;
|
|
217
|
+
}
|
|
218
|
+
function secretKeyBytes(secret) {
|
|
219
|
+
const raw = secret.startsWith(SECRET_PREFIX) ? secret.slice(SECRET_PREFIX.length) : secret;
|
|
220
|
+
return Buffer.from(raw, "base64url");
|
|
221
|
+
}
|
|
222
|
+
function verifyWebhook(args) {
|
|
223
|
+
const id = header(args.headers, "svix-id");
|
|
224
|
+
const timestamp = header(args.headers, "svix-timestamp");
|
|
225
|
+
const signature = header(args.headers, "svix-signature");
|
|
226
|
+
if (!id || !timestamp || !signature) {
|
|
227
|
+
throw new WebhookVerificationError("Missing svix-id, svix-timestamp, or svix-signature header.");
|
|
228
|
+
}
|
|
229
|
+
const ts = Number(timestamp);
|
|
230
|
+
const tolerance = args.toleranceSeconds ?? 300;
|
|
231
|
+
if (!Number.isFinite(ts)) {
|
|
232
|
+
throw new WebhookVerificationError("Invalid svix-timestamp header.");
|
|
233
|
+
}
|
|
234
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
235
|
+
if (Math.abs(now - ts) > tolerance) {
|
|
236
|
+
throw new WebhookVerificationError("Webhook timestamp is outside the tolerance window (possible replay).");
|
|
237
|
+
}
|
|
238
|
+
const signedContent = `${id}.${ts}.${args.payload}`;
|
|
239
|
+
const expected = createHmac("sha256", secretKeyBytes(args.secret)).update(signedContent).digest("base64");
|
|
240
|
+
const expectedBuf = Buffer.from(expected);
|
|
241
|
+
const matched = signature.split(" ").some((part) => {
|
|
242
|
+
const [, sig] = part.split(",", 2);
|
|
243
|
+
if (!sig) return false;
|
|
244
|
+
const candidate = Buffer.from(sig);
|
|
245
|
+
return candidate.length === expectedBuf.length && timingSafeEqual(candidate, expectedBuf);
|
|
246
|
+
});
|
|
247
|
+
if (!matched) {
|
|
248
|
+
throw new WebhookVerificationError("Webhook signature verification failed.");
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
return JSON.parse(args.payload);
|
|
252
|
+
} catch {
|
|
253
|
+
throw new WebhookVerificationError("Webhook payload is not valid JSON.");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
var Resource = class {
|
|
257
|
+
constructor(http) {
|
|
258
|
+
this.http = http;
|
|
259
|
+
}
|
|
260
|
+
http;
|
|
261
|
+
};
|
|
262
|
+
var Emails = class extends Resource {
|
|
263
|
+
/**
|
|
264
|
+
* Send or schedule a transactional email.
|
|
265
|
+
*
|
|
266
|
+
* If you don't pass `idempotencyKey`, the SDK generates one automatically so
|
|
267
|
+
* the request is safe to auto-retry (a network blip can't double-send). Pass
|
|
268
|
+
* your own to dedupe across separate `send()` calls.
|
|
269
|
+
*/
|
|
270
|
+
send(body, opts = {}) {
|
|
271
|
+
return this.http.request({
|
|
272
|
+
method: "POST",
|
|
273
|
+
path: "/emails",
|
|
274
|
+
body,
|
|
275
|
+
idempotencyKey: opts.idempotencyKey ?? randomUUID()
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Send up to 100 emails in one request (all-or-nothing validation).
|
|
280
|
+
*
|
|
281
|
+
* Like `send()`, if you don't pass `idempotencyKey` the SDK generates one so a
|
|
282
|
+
* transient failure can be safely auto-retried (the server dedupes the batch
|
|
283
|
+
* on the key and replays the original result instead of re-sending). Pass your
|
|
284
|
+
* own to dedupe across separate `batch()` calls.
|
|
285
|
+
*/
|
|
286
|
+
batch(body, opts = {}) {
|
|
287
|
+
return this.http.request({
|
|
288
|
+
method: "POST",
|
|
289
|
+
path: "/emails/batch",
|
|
290
|
+
body,
|
|
291
|
+
idempotencyKey: opts.idempotencyKey ?? randomUUID()
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/** Retrieve a batch by id. */
|
|
295
|
+
getBatch(id) {
|
|
296
|
+
return this.http.request({ method: "GET", path: `/emails/batches/${enc(id)}` });
|
|
297
|
+
}
|
|
298
|
+
/** Cancel every still-cancelable message in a batch. */
|
|
299
|
+
cancelBatch(id) {
|
|
300
|
+
return this.http.request({ method: "POST", path: `/emails/batches/${enc(id)}/cancel` });
|
|
301
|
+
}
|
|
302
|
+
/** List sent/scheduled emails (cursor-paginated). */
|
|
303
|
+
list(params = {}) {
|
|
304
|
+
return this.http.request({ method: "GET", path: "/emails", query: params });
|
|
305
|
+
}
|
|
306
|
+
/** Async-iterate every sent/scheduled email. */
|
|
307
|
+
listAll(params = {}) {
|
|
308
|
+
return paginate(this.http, "/emails", params);
|
|
309
|
+
}
|
|
310
|
+
/** Retrieve a sent or scheduled email. */
|
|
311
|
+
get(id) {
|
|
312
|
+
return this.http.request({ method: "GET", path: `/emails/${enc(id)}` });
|
|
313
|
+
}
|
|
314
|
+
/** Cancel or reschedule a scheduled/queued email. */
|
|
315
|
+
update(id, body) {
|
|
316
|
+
return this.http.request({ method: "PATCH", path: `/emails/${enc(id)}`, body });
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Deliverability / engagement stats over a rolling window.
|
|
320
|
+
*
|
|
321
|
+
* `days` is the window size (1–365); defaults to 30 server-side.
|
|
322
|
+
*/
|
|
323
|
+
stats(params = {}) {
|
|
324
|
+
return this.http.request({ method: "GET", path: "/emails/stats", query: params });
|
|
325
|
+
}
|
|
326
|
+
// --- Inbound (received) emails ---
|
|
327
|
+
receivedList(params = {}) {
|
|
328
|
+
return this.http.request({ method: "GET", path: "/emails/received", query: params });
|
|
329
|
+
}
|
|
330
|
+
receivedGet(id) {
|
|
331
|
+
return this.http.request({ method: "GET", path: `/emails/received/${enc(id)}` });
|
|
332
|
+
}
|
|
333
|
+
receivedAttachmentUrl(id, attachmentId) {
|
|
334
|
+
return this.http.request({ method: "GET", path: `/emails/received/${enc(id)}/attachments/${enc(attachmentId)}` });
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
var Contacts = class extends Resource {
|
|
338
|
+
list(params = {}) {
|
|
339
|
+
return this.http.request({ method: "GET", path: "/contacts", query: params });
|
|
340
|
+
}
|
|
341
|
+
listAll(params = {}) {
|
|
342
|
+
return paginate(this.http, "/contacts", params);
|
|
343
|
+
}
|
|
344
|
+
create(body) {
|
|
345
|
+
return this.http.request({ method: "POST", path: "/contacts", body });
|
|
346
|
+
}
|
|
347
|
+
/** Retrieve by contact id or email. */
|
|
348
|
+
get(idOrEmail) {
|
|
349
|
+
return this.http.request({ method: "GET", path: `/contacts/${enc(idOrEmail)}` });
|
|
350
|
+
}
|
|
351
|
+
update(idOrEmail, body) {
|
|
352
|
+
return this.http.request({ method: "PATCH", path: `/contacts/${enc(idOrEmail)}`, body });
|
|
353
|
+
}
|
|
354
|
+
delete(idOrEmail) {
|
|
355
|
+
return this.http.request({ method: "DELETE", path: `/contacts/${enc(idOrEmail)}` });
|
|
356
|
+
}
|
|
357
|
+
/** Start an async contact import. */
|
|
358
|
+
import(body) {
|
|
359
|
+
return this.http.request({ method: "POST", path: "/contacts/imports", body });
|
|
360
|
+
}
|
|
361
|
+
listImports(params = {}) {
|
|
362
|
+
return this.http.request({ method: "GET", path: "/contacts/imports", query: params });
|
|
363
|
+
}
|
|
364
|
+
getImport(id) {
|
|
365
|
+
return this.http.request({ method: "GET", path: `/contacts/imports/${enc(id)}` });
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
var ContactFields = class extends Resource {
|
|
369
|
+
list(params = {}) {
|
|
370
|
+
return this.http.request({ method: "GET", path: "/contact-fields", query: params });
|
|
371
|
+
}
|
|
372
|
+
listAll(params = {}) {
|
|
373
|
+
return paginate(this.http, "/contact-fields", params);
|
|
374
|
+
}
|
|
375
|
+
create(body) {
|
|
376
|
+
return this.http.request({ method: "POST", path: "/contact-fields", body });
|
|
377
|
+
}
|
|
378
|
+
get(id) {
|
|
379
|
+
return this.http.request({ method: "GET", path: `/contact-fields/${enc(id)}` });
|
|
380
|
+
}
|
|
381
|
+
update(id, body) {
|
|
382
|
+
return this.http.request({ method: "PATCH", path: `/contact-fields/${enc(id)}`, body });
|
|
383
|
+
}
|
|
384
|
+
delete(id) {
|
|
385
|
+
return this.http.request({ method: "DELETE", path: `/contact-fields/${enc(id)}` });
|
|
386
|
+
}
|
|
387
|
+
rename(id, body) {
|
|
388
|
+
return this.http.request({ method: "POST", path: `/contact-fields/${enc(id)}/rename`, body });
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
var Segments = class extends Resource {
|
|
392
|
+
list(params = {}) {
|
|
393
|
+
return this.http.request({ method: "GET", path: "/segments", query: params });
|
|
394
|
+
}
|
|
395
|
+
listAll(params = {}) {
|
|
396
|
+
return paginate(this.http, "/segments", params);
|
|
397
|
+
}
|
|
398
|
+
create(body) {
|
|
399
|
+
return this.http.request({ method: "POST", path: "/segments", body });
|
|
400
|
+
}
|
|
401
|
+
get(id) {
|
|
402
|
+
return this.http.request({ method: "GET", path: `/segments/${enc(id)}` });
|
|
403
|
+
}
|
|
404
|
+
update(id, body) {
|
|
405
|
+
return this.http.request({ method: "PATCH", path: `/segments/${enc(id)}`, body });
|
|
406
|
+
}
|
|
407
|
+
delete(id) {
|
|
408
|
+
return this.http.request({ method: "DELETE", path: `/segments/${enc(id)}` });
|
|
409
|
+
}
|
|
410
|
+
listMembers(id, params = {}) {
|
|
411
|
+
return this.http.request({ method: "GET", path: `/segments/${enc(id)}/contacts`, query: params });
|
|
412
|
+
}
|
|
413
|
+
/** Add a contact (by id or email) to the segment. Idempotent. */
|
|
414
|
+
addMember(id, body) {
|
|
415
|
+
return this.http.request({ method: "POST", path: `/segments/${enc(id)}/contacts`, body });
|
|
416
|
+
}
|
|
417
|
+
/** Remove a contact (by id or email) from the segment. */
|
|
418
|
+
removeMember(id, contact) {
|
|
419
|
+
return this.http.request({ method: "DELETE", path: `/segments/${enc(id)}/contacts/${enc(contact)}` });
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
var Topics = class extends Resource {
|
|
423
|
+
list(params = {}) {
|
|
424
|
+
return this.http.request({ method: "GET", path: "/topics", query: params });
|
|
425
|
+
}
|
|
426
|
+
listAll(params = {}) {
|
|
427
|
+
return paginate(this.http, "/topics", params);
|
|
428
|
+
}
|
|
429
|
+
create(body) {
|
|
430
|
+
return this.http.request({ method: "POST", path: "/topics", body });
|
|
431
|
+
}
|
|
432
|
+
get(id) {
|
|
433
|
+
return this.http.request({ method: "GET", path: `/topics/${enc(id)}` });
|
|
434
|
+
}
|
|
435
|
+
update(id, body) {
|
|
436
|
+
return this.http.request({ method: "PATCH", path: `/topics/${enc(id)}`, body });
|
|
437
|
+
}
|
|
438
|
+
delete(id) {
|
|
439
|
+
return this.http.request({ method: "DELETE", path: `/topics/${enc(id)}` });
|
|
440
|
+
}
|
|
441
|
+
/** List a contact's effective topic subscriptions. */
|
|
442
|
+
listTopics(contactId, params = {}) {
|
|
443
|
+
return this.http.request({ method: "GET", path: `/contacts/${enc(contactId)}/topics`, query: params });
|
|
444
|
+
}
|
|
445
|
+
/** Set a contact's preference for one topic. */
|
|
446
|
+
setTopic(contactId, body) {
|
|
447
|
+
return this.http.request({ method: "POST", path: `/contacts/${enc(contactId)}/topics`, body });
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
var Campaigns = class extends Resource {
|
|
451
|
+
list(params = {}) {
|
|
452
|
+
return this.http.request({ method: "GET", path: "/campaigns", query: params });
|
|
453
|
+
}
|
|
454
|
+
listAll(params = {}) {
|
|
455
|
+
return paginate(this.http, "/campaigns", params);
|
|
456
|
+
}
|
|
457
|
+
create(body) {
|
|
458
|
+
return this.http.request({ method: "POST", path: "/campaigns", body });
|
|
459
|
+
}
|
|
460
|
+
get(id) {
|
|
461
|
+
return this.http.request({ method: "GET", path: `/campaigns/${enc(id)}` });
|
|
462
|
+
}
|
|
463
|
+
update(id, body) {
|
|
464
|
+
return this.http.request({ method: "PATCH", path: `/campaigns/${enc(id)}`, body });
|
|
465
|
+
}
|
|
466
|
+
delete(id) {
|
|
467
|
+
return this.http.request({ method: "DELETE", path: `/campaigns/${enc(id)}` });
|
|
468
|
+
}
|
|
469
|
+
send(id) {
|
|
470
|
+
return this.http.request({ method: "POST", path: `/campaigns/${enc(id)}/send` });
|
|
471
|
+
}
|
|
472
|
+
cancel(id) {
|
|
473
|
+
return this.http.request({ method: "POST", path: `/campaigns/${enc(id)}/cancel` });
|
|
474
|
+
}
|
|
475
|
+
/** Pause a scheduled/sending campaign; already-sent messages are untouched. */
|
|
476
|
+
pause(id) {
|
|
477
|
+
return this.http.request({ method: "POST", path: `/campaigns/${enc(id)}/pause` });
|
|
478
|
+
}
|
|
479
|
+
/** Resume a paused campaign; sending continues with the remaining recipients. */
|
|
480
|
+
resume(id) {
|
|
481
|
+
return this.http.request({ method: "POST", path: `/campaigns/${enc(id)}/resume` });
|
|
482
|
+
}
|
|
483
|
+
reschedule(id, body) {
|
|
484
|
+
return this.http.request({ method: "POST", path: `/campaigns/${enc(id)}/reschedule`, body });
|
|
485
|
+
}
|
|
486
|
+
/** Non-mutating pre-send validation report. */
|
|
487
|
+
validate(id) {
|
|
488
|
+
return this.http.request({ method: "GET", path: `/campaigns/${enc(id)}/validate` });
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
var Templates = class extends Resource {
|
|
492
|
+
list(params = {}) {
|
|
493
|
+
return this.http.request({ method: "GET", path: "/templates", query: params });
|
|
494
|
+
}
|
|
495
|
+
listAll(params = {}) {
|
|
496
|
+
return paginate(this.http, "/templates", params);
|
|
497
|
+
}
|
|
498
|
+
create(body) {
|
|
499
|
+
return this.http.request({ method: "POST", path: "/templates", body });
|
|
500
|
+
}
|
|
501
|
+
get(id) {
|
|
502
|
+
return this.http.request({ method: "GET", path: `/templates/${enc(id)}` });
|
|
503
|
+
}
|
|
504
|
+
update(id, body) {
|
|
505
|
+
return this.http.request({ method: "PATCH", path: `/templates/${enc(id)}`, body });
|
|
506
|
+
}
|
|
507
|
+
delete(id) {
|
|
508
|
+
return this.http.request({ method: "DELETE", path: `/templates/${enc(id)}` });
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
var BrandKits = class extends Resource {
|
|
512
|
+
/** List profiles (max 5). Optional `name` filters by display name. */
|
|
513
|
+
list(params = {}) {
|
|
514
|
+
return this.http.request({ method: "GET", path: "/brand-kits", query: params });
|
|
515
|
+
}
|
|
516
|
+
/** Retrieve one profile by `bk_…` id. */
|
|
517
|
+
get(id) {
|
|
518
|
+
return this.http.request({ method: "GET", path: `/brand-kits/${enc(id)}` });
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
var Domains = class extends Resource {
|
|
522
|
+
list(params = {}) {
|
|
523
|
+
return this.http.request({ method: "GET", path: "/domains", query: params });
|
|
524
|
+
}
|
|
525
|
+
listAll(params = {}) {
|
|
526
|
+
return paginate(this.http, "/domains", params);
|
|
527
|
+
}
|
|
528
|
+
create(body) {
|
|
529
|
+
return this.http.request({ method: "POST", path: "/domains", body });
|
|
530
|
+
}
|
|
531
|
+
get(id) {
|
|
532
|
+
return this.http.request({ method: "GET", path: `/domains/${enc(id)}` });
|
|
533
|
+
}
|
|
534
|
+
delete(id) {
|
|
535
|
+
return this.http.request({ method: "DELETE", path: `/domains/${enc(id)}` });
|
|
536
|
+
}
|
|
537
|
+
update(id, body) {
|
|
538
|
+
return this.http.request({ method: "PATCH", path: `/domains/${enc(id)}`, body });
|
|
539
|
+
}
|
|
540
|
+
/** Run an on-demand DNS/SES verification check. */
|
|
541
|
+
verify(id) {
|
|
542
|
+
return this.http.request({ method: "POST", path: `/domains/${enc(id)}/verify` });
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
var Webhooks = class extends Resource {
|
|
546
|
+
list(params = {}) {
|
|
547
|
+
return this.http.request({ method: "GET", path: "/webhooks", query: params });
|
|
548
|
+
}
|
|
549
|
+
listAll(params = {}) {
|
|
550
|
+
return paginate(this.http, "/webhooks", params);
|
|
551
|
+
}
|
|
552
|
+
/** Create an endpoint. The response includes `signing_secret` exactly once. */
|
|
553
|
+
create(body) {
|
|
554
|
+
return this.http.request({ method: "POST", path: "/webhooks", body });
|
|
555
|
+
}
|
|
556
|
+
get(id) {
|
|
557
|
+
return this.http.request({ method: "GET", path: `/webhooks/${enc(id)}` });
|
|
558
|
+
}
|
|
559
|
+
update(id, body) {
|
|
560
|
+
return this.http.request({ method: "PATCH", path: `/webhooks/${enc(id)}`, body });
|
|
561
|
+
}
|
|
562
|
+
delete(id) {
|
|
563
|
+
return this.http.request({ method: "DELETE", path: `/webhooks/${enc(id)}` });
|
|
564
|
+
}
|
|
565
|
+
/** Fire a synchronous sample event at the endpoint. */
|
|
566
|
+
test(id) {
|
|
567
|
+
return this.http.request({ method: "POST", path: `/webhooks/${enc(id)}/test` });
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Verify a webhook delivery's signature and return the parsed event. Throws
|
|
571
|
+
* `WebhookVerificationError` on a missing/invalid/stale signature. Alias of the
|
|
572
|
+
* top-level `verifyWebhook` for a Resend-style `unitpost.webhooks.verify(...)`.
|
|
573
|
+
*/
|
|
574
|
+
verify(args) {
|
|
575
|
+
return verifyWebhook(args);
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
var ApiKeys = class extends Resource {
|
|
579
|
+
list(params = {}) {
|
|
580
|
+
return this.http.request({ method: "GET", path: "/api-keys", query: params });
|
|
581
|
+
}
|
|
582
|
+
listAll(params = {}) {
|
|
583
|
+
return paginate(this.http, "/api-keys", params);
|
|
584
|
+
}
|
|
585
|
+
/** Create a key. The response includes the plaintext `key` exactly once. */
|
|
586
|
+
create(body) {
|
|
587
|
+
return this.http.request({ method: "POST", path: "/api-keys", body });
|
|
588
|
+
}
|
|
589
|
+
delete(id) {
|
|
590
|
+
return this.http.request({ method: "DELETE", path: `/api-keys/${enc(id)}` });
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
var Suppressions = class extends Resource {
|
|
594
|
+
/** List suppressed addresses (your own + read-only platform rows). */
|
|
595
|
+
list(params = {}) {
|
|
596
|
+
return this.http.request({ method: "GET", path: "/suppressions", query: params });
|
|
597
|
+
}
|
|
598
|
+
/** Async-iterate every suppressed address. */
|
|
599
|
+
listAll(params = {}) {
|
|
600
|
+
return paginate(this.http, "/suppressions", params);
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Suppress one address (`{ email }`) or many (`{ emails: [...] }`). Idempotent
|
|
604
|
+
* — re-suppressing an existing address is a no-op. The bulk form resolves to a
|
|
605
|
+
* `{ object: "list", added, skipped, total }` summary.
|
|
606
|
+
*/
|
|
607
|
+
create(body) {
|
|
608
|
+
return this.http.request({ method: "POST", path: "/suppressions", body });
|
|
609
|
+
}
|
|
610
|
+
/** Retrieve a suppression by id (`supp_…`) or email. */
|
|
611
|
+
get(idOrEmail) {
|
|
612
|
+
return this.http.request({ method: "GET", path: `/suppressions/${enc(idOrEmail)}` });
|
|
613
|
+
}
|
|
614
|
+
/** Un-suppress an address by id or email. Idempotent on the email form. */
|
|
615
|
+
delete(idOrEmail) {
|
|
616
|
+
return this.http.request({ method: "DELETE", path: `/suppressions/${enc(idOrEmail)}` });
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
var Usage = class extends Resource {
|
|
620
|
+
/**
|
|
621
|
+
* Retrieve the current billing-period usage snapshot: plan, period window,
|
|
622
|
+
* emails sent, and (on paid plans) the dollar sending wallet — allowance,
|
|
623
|
+
* spend, purchased credit, and overage, all in integer USD cents.
|
|
624
|
+
*/
|
|
625
|
+
get() {
|
|
626
|
+
return this.http.request({ method: "GET", path: "/usage" });
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
function enc(segment) {
|
|
630
|
+
return encodeURIComponent(segment);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// src/client.ts
|
|
634
|
+
var Unitpost = class {
|
|
635
|
+
emails;
|
|
636
|
+
contacts;
|
|
637
|
+
contactFields;
|
|
638
|
+
segments;
|
|
639
|
+
topics;
|
|
640
|
+
campaigns;
|
|
641
|
+
templates;
|
|
642
|
+
brandKits;
|
|
643
|
+
domains;
|
|
644
|
+
webhooks;
|
|
645
|
+
apiKeys;
|
|
646
|
+
suppressions;
|
|
647
|
+
// NOTE (pre-launch): `events` (POST /v1/events) is intentionally absent
|
|
648
|
+
// while the custom-events surface is behind the launch gate.
|
|
649
|
+
usage;
|
|
650
|
+
constructor(apiKeyOrOptions) {
|
|
651
|
+
const options = typeof apiKeyOrOptions === "string" ? { apiKey: apiKeyOrOptions } : apiKeyOrOptions ?? {};
|
|
652
|
+
const http = new HttpClient(options);
|
|
653
|
+
this.emails = new Emails(http);
|
|
654
|
+
this.contacts = new Contacts(http);
|
|
655
|
+
this.contactFields = new ContactFields(http);
|
|
656
|
+
this.segments = new Segments(http);
|
|
657
|
+
this.topics = new Topics(http);
|
|
658
|
+
this.campaigns = new Campaigns(http);
|
|
659
|
+
this.templates = new Templates(http);
|
|
660
|
+
this.brandKits = new BrandKits(http);
|
|
661
|
+
this.domains = new Domains(http);
|
|
662
|
+
this.webhooks = new Webhooks(http);
|
|
663
|
+
this.apiKeys = new ApiKeys(http);
|
|
664
|
+
this.suppressions = new Suppressions(http);
|
|
665
|
+
this.usage = new Usage(http);
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
export { SDK_VERSION, Unitpost, UnitpostError, WebhookVerificationError, verifyWebhook };
|
|
670
|
+
//# sourceMappingURL=index.js.map
|
|
671
|
+
//# sourceMappingURL=index.js.map
|