mailerbot 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 +290 -0
- package/dist/index.cjs +852 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +737 -0
- package/dist/index.d.ts +737 -0
- package/dist/index.js +842 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var MailerBotError = class extends Error {
|
|
3
|
+
statusCode;
|
|
4
|
+
response;
|
|
5
|
+
constructor(message, options) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "MailerBotError";
|
|
8
|
+
this.statusCode = options?.statusCode;
|
|
9
|
+
this.response = options?.response;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var AuthenticationError = class extends MailerBotError {
|
|
13
|
+
constructor(message, options) {
|
|
14
|
+
super(message, options);
|
|
15
|
+
this.name = "AuthenticationError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var PermissionError = class extends MailerBotError {
|
|
19
|
+
constructor(message, options) {
|
|
20
|
+
super(message, options);
|
|
21
|
+
this.name = "PermissionError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var NotFoundError = class extends MailerBotError {
|
|
25
|
+
constructor(message, options) {
|
|
26
|
+
super(message, options);
|
|
27
|
+
this.name = "NotFoundError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var ValidationError = class extends MailerBotError {
|
|
31
|
+
constructor(message, options) {
|
|
32
|
+
super(message, options);
|
|
33
|
+
this.name = "ValidationError";
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var RateLimitError = class extends MailerBotError {
|
|
37
|
+
constructor(message, options) {
|
|
38
|
+
super(message, options);
|
|
39
|
+
this.name = "RateLimitError";
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var ServerError = class extends MailerBotError {
|
|
43
|
+
constructor(message, options) {
|
|
44
|
+
super(message, options);
|
|
45
|
+
this.name = "ServerError";
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/http.ts
|
|
50
|
+
var DEFAULT_BASE_URL = "https://api.mailerbot.com/api/v1";
|
|
51
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
52
|
+
function buildHeaders(apiKey) {
|
|
53
|
+
return {
|
|
54
|
+
"X-API-Key": apiKey,
|
|
55
|
+
"Content-Type": "application/json",
|
|
56
|
+
Accept: "application/json"
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async function raiseForStatus(response) {
|
|
60
|
+
if (response.ok) return;
|
|
61
|
+
let body;
|
|
62
|
+
try {
|
|
63
|
+
body = await response.json();
|
|
64
|
+
} catch {
|
|
65
|
+
body = { detail: await response.text().catch(() => "Unknown error") };
|
|
66
|
+
}
|
|
67
|
+
const detail = typeof body.detail === "string" ? body.detail : JSON.stringify(body);
|
|
68
|
+
const status = response.status;
|
|
69
|
+
const opts = { statusCode: status, response: body };
|
|
70
|
+
if (status === 401) throw new AuthenticationError(detail, opts);
|
|
71
|
+
if (status === 403) throw new PermissionError(detail, opts);
|
|
72
|
+
if (status === 404) throw new NotFoundError(detail, opts);
|
|
73
|
+
if (status === 422) throw new ValidationError(detail, opts);
|
|
74
|
+
if (status === 429) throw new RateLimitError(detail, opts);
|
|
75
|
+
if (status >= 500) throw new ServerError(detail, opts);
|
|
76
|
+
throw new MailerBotError(detail, opts);
|
|
77
|
+
}
|
|
78
|
+
var HttpClient = class {
|
|
79
|
+
baseUrl;
|
|
80
|
+
headers;
|
|
81
|
+
timeout;
|
|
82
|
+
constructor(baseUrl, headers, timeout) {
|
|
83
|
+
this.baseUrl = baseUrl;
|
|
84
|
+
this.headers = headers;
|
|
85
|
+
this.timeout = timeout;
|
|
86
|
+
}
|
|
87
|
+
async request(options) {
|
|
88
|
+
const fullUrl = `${this.baseUrl}${options.path.startsWith("/") ? "" : "/"}${options.path}`;
|
|
89
|
+
const urlObj = new URL(fullUrl);
|
|
90
|
+
if (options.params) {
|
|
91
|
+
for (const [key, value] of Object.entries(options.params)) {
|
|
92
|
+
if (value !== void 0) {
|
|
93
|
+
urlObj.searchParams.set(key, String(value));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const mergedHeaders = { ...this.headers, ...options.headers };
|
|
98
|
+
const controller = new AbortController();
|
|
99
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
100
|
+
try {
|
|
101
|
+
const response = await fetch(urlObj.toString(), {
|
|
102
|
+
method: options.method ?? "GET",
|
|
103
|
+
headers: mergedHeaders,
|
|
104
|
+
body: options.body !== void 0 ? JSON.stringify(options.body) : void 0,
|
|
105
|
+
signal: controller.signal
|
|
106
|
+
});
|
|
107
|
+
await raiseForStatus(response);
|
|
108
|
+
const text = await response.text();
|
|
109
|
+
if (!text) return void 0;
|
|
110
|
+
return JSON.parse(text);
|
|
111
|
+
} finally {
|
|
112
|
+
clearTimeout(timeoutId);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async requestRaw(options) {
|
|
116
|
+
const fullUrl = `${this.baseUrl}${options.path.startsWith("/") ? "" : "/"}${options.path}`;
|
|
117
|
+
const urlObj = new URL(fullUrl);
|
|
118
|
+
if (options.params) {
|
|
119
|
+
for (const [key, value] of Object.entries(options.params)) {
|
|
120
|
+
if (value !== void 0) {
|
|
121
|
+
urlObj.searchParams.set(key, String(value));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const controller = new AbortController();
|
|
126
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
127
|
+
try {
|
|
128
|
+
const response = await fetch(urlObj.toString(), {
|
|
129
|
+
method: options.method ?? "POST",
|
|
130
|
+
headers: options.rawHeaders ?? this.headers,
|
|
131
|
+
body: options.rawBody,
|
|
132
|
+
signal: controller.signal
|
|
133
|
+
});
|
|
134
|
+
await raiseForStatus(response);
|
|
135
|
+
return response;
|
|
136
|
+
} finally {
|
|
137
|
+
clearTimeout(timeoutId);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/pagination.ts
|
|
143
|
+
var PageIterator = class {
|
|
144
|
+
fetch;
|
|
145
|
+
pageSize;
|
|
146
|
+
constructor(fetch2, pageSize = 100) {
|
|
147
|
+
this.fetch = fetch2;
|
|
148
|
+
this.pageSize = pageSize;
|
|
149
|
+
}
|
|
150
|
+
async *[Symbol.asyncIterator]() {
|
|
151
|
+
let page = 1;
|
|
152
|
+
while (true) {
|
|
153
|
+
const result = await this.fetch(page, this.pageSize);
|
|
154
|
+
for (const item of result.items) {
|
|
155
|
+
yield item;
|
|
156
|
+
}
|
|
157
|
+
if (page >= result.pages) break;
|
|
158
|
+
page++;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// src/resources/contacts.ts
|
|
164
|
+
var ContactsResource = class {
|
|
165
|
+
constructor(http) {
|
|
166
|
+
this.http = http;
|
|
167
|
+
}
|
|
168
|
+
async list(options) {
|
|
169
|
+
return this.http.request({
|
|
170
|
+
path: "/contacts",
|
|
171
|
+
params: {
|
|
172
|
+
page: options?.page ?? 1,
|
|
173
|
+
page_size: options?.pageSize ?? 50,
|
|
174
|
+
search: options?.search
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
iterAll(options) {
|
|
179
|
+
return new PageIterator(
|
|
180
|
+
(page, pageSize) => this.list({ page, pageSize, search: options?.search }),
|
|
181
|
+
options?.pageSize ?? 100
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
async get(contactId) {
|
|
185
|
+
return this.http.request({ path: `/contacts/${contactId}` });
|
|
186
|
+
}
|
|
187
|
+
async create(params) {
|
|
188
|
+
return this.http.request({
|
|
189
|
+
method: "POST",
|
|
190
|
+
path: "/contacts",
|
|
191
|
+
body: params
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
async update(contactId, params) {
|
|
195
|
+
return this.http.request({
|
|
196
|
+
method: "PUT",
|
|
197
|
+
path: `/contacts/${contactId}`,
|
|
198
|
+
body: params
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
async delete(contactId) {
|
|
202
|
+
await this.http.request({
|
|
203
|
+
method: "DELETE",
|
|
204
|
+
path: `/contacts/${contactId}`
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
async importCsv(contacts, options) {
|
|
208
|
+
return this.http.request({
|
|
209
|
+
method: "POST",
|
|
210
|
+
path: "/contacts/import",
|
|
211
|
+
body: { contacts, listName: options?.listName }
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
async validateAddresses(contactIds) {
|
|
215
|
+
return this.http.request({
|
|
216
|
+
method: "POST",
|
|
217
|
+
path: "/contacts/validate",
|
|
218
|
+
body: { contactIds }
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// src/resources/contact-lists.ts
|
|
224
|
+
var ContactListsResource = class {
|
|
225
|
+
constructor(http) {
|
|
226
|
+
this.http = http;
|
|
227
|
+
}
|
|
228
|
+
async list() {
|
|
229
|
+
return this.http.request({ path: "/contact-lists" });
|
|
230
|
+
}
|
|
231
|
+
async get(listId) {
|
|
232
|
+
return this.http.request({
|
|
233
|
+
path: `/contact-lists/${listId}`
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async create(name, options) {
|
|
237
|
+
return this.http.request({
|
|
238
|
+
method: "POST",
|
|
239
|
+
path: "/contact-lists",
|
|
240
|
+
body: { name, contactIds: options?.contactIds ?? [] }
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
async update(listId, name) {
|
|
244
|
+
return this.http.request({
|
|
245
|
+
method: "PUT",
|
|
246
|
+
path: `/contact-lists/${listId}`,
|
|
247
|
+
body: { name }
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
async delete(listId) {
|
|
251
|
+
await this.http.request({
|
|
252
|
+
method: "DELETE",
|
|
253
|
+
path: `/contact-lists/${listId}`
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
async addContacts(listId, contactIds) {
|
|
257
|
+
await this.http.request({
|
|
258
|
+
method: "POST",
|
|
259
|
+
path: `/contact-lists/${listId}/contacts`,
|
|
260
|
+
body: { contactIds }
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
async removeContacts(listId, contactIds) {
|
|
264
|
+
await this.http.request({
|
|
265
|
+
method: "DELETE",
|
|
266
|
+
path: `/contact-lists/${listId}/contacts`,
|
|
267
|
+
body: { contactIds }
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// src/resources/documents.ts
|
|
273
|
+
var DocumentsResource = class {
|
|
274
|
+
constructor(http) {
|
|
275
|
+
this.http = http;
|
|
276
|
+
}
|
|
277
|
+
async list(options) {
|
|
278
|
+
return this.http.request({
|
|
279
|
+
path: "/documents",
|
|
280
|
+
params: {
|
|
281
|
+
page: options?.page ?? 1,
|
|
282
|
+
page_size: options?.pageSize ?? 50,
|
|
283
|
+
type: options?.type
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
iterAll(options) {
|
|
288
|
+
return new PageIterator(
|
|
289
|
+
(page, pageSize) => this.list({ page, pageSize }),
|
|
290
|
+
options?.pageSize ?? 100
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
async get(documentId) {
|
|
294
|
+
return this.http.request({ path: `/documents/${documentId}` });
|
|
295
|
+
}
|
|
296
|
+
async create(params) {
|
|
297
|
+
return this.http.request({
|
|
298
|
+
method: "POST",
|
|
299
|
+
path: "/documents",
|
|
300
|
+
body: {
|
|
301
|
+
title: params?.title ?? "Untitled Document",
|
|
302
|
+
content: params?.content ?? ""
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
async update(documentId, params) {
|
|
307
|
+
return this.http.request({
|
|
308
|
+
method: "PUT",
|
|
309
|
+
path: `/documents/${documentId}`,
|
|
310
|
+
body: params
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
async delete(documentId) {
|
|
314
|
+
await this.http.request({
|
|
315
|
+
method: "DELETE",
|
|
316
|
+
path: `/documents/${documentId}`
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
async upload(file, filename) {
|
|
320
|
+
const formData = new FormData();
|
|
321
|
+
const blob = file instanceof Blob ? file : new Blob([file], { type: "application/pdf" });
|
|
322
|
+
formData.append("file", blob, filename);
|
|
323
|
+
const response = await this.http.requestRaw({
|
|
324
|
+
method: "POST",
|
|
325
|
+
path: "/documents/upload",
|
|
326
|
+
rawBody: formData,
|
|
327
|
+
rawHeaders: {}
|
|
328
|
+
// let fetch set Content-Type with boundary
|
|
329
|
+
});
|
|
330
|
+
return await response.json();
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// src/resources/postcards.ts
|
|
335
|
+
var PostcardsResource = class {
|
|
336
|
+
constructor(http) {
|
|
337
|
+
this.http = http;
|
|
338
|
+
}
|
|
339
|
+
async listTemplates() {
|
|
340
|
+
return this.http.request({
|
|
341
|
+
path: "/postcards/templates"
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
async getTemplate(templateId) {
|
|
345
|
+
return this.http.request({
|
|
346
|
+
path: `/postcards/templates/${templateId}`
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
async list(options) {
|
|
350
|
+
return this.http.request({
|
|
351
|
+
path: "/postcards",
|
|
352
|
+
params: {
|
|
353
|
+
page: options?.page ?? 1,
|
|
354
|
+
page_size: options?.pageSize ?? 50
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
iterAll(options) {
|
|
359
|
+
return new PageIterator(
|
|
360
|
+
(page, pageSize) => this.list({ page, pageSize }),
|
|
361
|
+
options?.pageSize ?? 100
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
async get(postcardId) {
|
|
365
|
+
return this.http.request({ path: `/postcards/${postcardId}` });
|
|
366
|
+
}
|
|
367
|
+
async create(params) {
|
|
368
|
+
return this.http.request({
|
|
369
|
+
method: "POST",
|
|
370
|
+
path: "/postcards",
|
|
371
|
+
body: {
|
|
372
|
+
title: params?.title ?? "Untitled Postcard",
|
|
373
|
+
front: params?.front,
|
|
374
|
+
back: params?.back
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
async update(postcardId, params) {
|
|
379
|
+
return this.http.request({
|
|
380
|
+
method: "PUT",
|
|
381
|
+
path: `/postcards/${postcardId}`,
|
|
382
|
+
body: params
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
async delete(postcardId) {
|
|
386
|
+
await this.http.request({
|
|
387
|
+
method: "DELETE",
|
|
388
|
+
path: `/postcards/${postcardId}`
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// src/resources/mailings.ts
|
|
394
|
+
function toISOString(date) {
|
|
395
|
+
if (date === void 0) return void 0;
|
|
396
|
+
return date instanceof Date ? date.toISOString() : date;
|
|
397
|
+
}
|
|
398
|
+
var MailingsResource = class {
|
|
399
|
+
constructor(http) {
|
|
400
|
+
this.http = http;
|
|
401
|
+
}
|
|
402
|
+
async list(options) {
|
|
403
|
+
return this.http.request({
|
|
404
|
+
path: "/mailings",
|
|
405
|
+
params: {
|
|
406
|
+
page: options?.page ?? 1,
|
|
407
|
+
page_size: options?.pageSize ?? 50
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
iterAll(options) {
|
|
412
|
+
return new PageIterator(
|
|
413
|
+
(page, pageSize) => this.list({ page, pageSize }),
|
|
414
|
+
options?.pageSize ?? 100
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
async get(mailingId) {
|
|
418
|
+
return this.http.request({ path: `/mailings/${mailingId}` });
|
|
419
|
+
}
|
|
420
|
+
async create(params) {
|
|
421
|
+
return this.http.request({
|
|
422
|
+
method: "POST",
|
|
423
|
+
path: "/mailings",
|
|
424
|
+
body: {
|
|
425
|
+
name: params.name,
|
|
426
|
+
type: params.type,
|
|
427
|
+
contactListId: params.contactListId,
|
|
428
|
+
documentId: params.documentId,
|
|
429
|
+
postcardId: params.postcardId,
|
|
430
|
+
couponListId: params.couponListId,
|
|
431
|
+
scheduledDate: toISOString(params.scheduledDate),
|
|
432
|
+
printColor: params.printColor ?? false,
|
|
433
|
+
postageSelections: params.postageSelections
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
async update(mailingId, params) {
|
|
438
|
+
return this.http.request({
|
|
439
|
+
method: "PUT",
|
|
440
|
+
path: `/mailings/${mailingId}`,
|
|
441
|
+
body: {
|
|
442
|
+
name: params.name,
|
|
443
|
+
scheduledDate: toISOString(params.scheduledDate)
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
async delete(mailingId) {
|
|
448
|
+
await this.http.request({
|
|
449
|
+
method: "DELETE",
|
|
450
|
+
path: `/mailings/${mailingId}`
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
async validateAddresses(mailingId) {
|
|
454
|
+
return this.http.request({
|
|
455
|
+
method: "POST",
|
|
456
|
+
path: `/mailings/${mailingId}/validate-addresses`
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
async estimateCost(params) {
|
|
460
|
+
return this.http.request({
|
|
461
|
+
method: "POST",
|
|
462
|
+
path: "/mailings/estimate-cost",
|
|
463
|
+
body: {
|
|
464
|
+
type: params.type,
|
|
465
|
+
contactListId: params.contactListId,
|
|
466
|
+
contactIds: params.contactIds,
|
|
467
|
+
documentId: params.documentId,
|
|
468
|
+
printColor: params.printColor ?? false
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
async calculateCost(mailingId) {
|
|
473
|
+
return this.http.request({
|
|
474
|
+
method: "POST",
|
|
475
|
+
path: `/mailings/${mailingId}/calculate-cost`
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
async send(mailingId) {
|
|
479
|
+
return this.http.request({
|
|
480
|
+
method: "POST",
|
|
481
|
+
path: `/mailings/${mailingId}/send`
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
async listItems(mailingId, options) {
|
|
485
|
+
return this.http.request({
|
|
486
|
+
path: `/mailings/${mailingId}/items`,
|
|
487
|
+
params: {
|
|
488
|
+
page: options?.page ?? 1,
|
|
489
|
+
page_size: options?.pageSize ?? 50,
|
|
490
|
+
trackingStatus: options?.trackingStatus
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
iterItems(mailingId, options) {
|
|
495
|
+
return new PageIterator(
|
|
496
|
+
(page, pageSize) => this.listItems(mailingId, {
|
|
497
|
+
page,
|
|
498
|
+
pageSize,
|
|
499
|
+
trackingStatus: options?.trackingStatus
|
|
500
|
+
}),
|
|
501
|
+
options?.pageSize ?? 100
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
async listItemScans(mailingId, itemId) {
|
|
505
|
+
return this.http.request({
|
|
506
|
+
path: `/mailings/${mailingId}/items/${itemId}/scans`
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
// src/resources/campaigns.ts
|
|
512
|
+
var CampaignsResource = class {
|
|
513
|
+
constructor(http) {
|
|
514
|
+
this.http = http;
|
|
515
|
+
}
|
|
516
|
+
async list(options) {
|
|
517
|
+
return this.http.request({
|
|
518
|
+
path: "/campaigns",
|
|
519
|
+
params: {
|
|
520
|
+
page: options?.page ?? 1,
|
|
521
|
+
page_size: options?.pageSize ?? 50
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
iterAll(options) {
|
|
526
|
+
return new PageIterator(
|
|
527
|
+
(page, pageSize) => this.list({ page, pageSize }),
|
|
528
|
+
options?.pageSize ?? 100
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
async get(campaignId) {
|
|
532
|
+
return this.http.request({ path: `/campaigns/${campaignId}` });
|
|
533
|
+
}
|
|
534
|
+
async create(name, options) {
|
|
535
|
+
return this.http.request({
|
|
536
|
+
method: "POST",
|
|
537
|
+
path: "/campaigns",
|
|
538
|
+
body: { name, mailingIds: options?.mailingIds ?? [] }
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
async update(campaignId, params) {
|
|
542
|
+
return this.http.request({
|
|
543
|
+
method: "PUT",
|
|
544
|
+
path: `/campaigns/${campaignId}`,
|
|
545
|
+
body: params
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
async delete(campaignId) {
|
|
549
|
+
await this.http.request({
|
|
550
|
+
method: "DELETE",
|
|
551
|
+
path: `/campaigns/${campaignId}`
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
async addMailing(campaignId, mailingId) {
|
|
555
|
+
return this.http.request({
|
|
556
|
+
method: "POST",
|
|
557
|
+
path: `/campaigns/${campaignId}/mailings`,
|
|
558
|
+
body: { mailing_id: mailingId }
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
async removeMailing(campaignId, mailingId) {
|
|
562
|
+
return this.http.request({
|
|
563
|
+
method: "DELETE",
|
|
564
|
+
path: `/campaigns/${campaignId}/mailings/${mailingId}`
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/resources/dashboard.ts
|
|
570
|
+
var DashboardResource = class {
|
|
571
|
+
constructor(http) {
|
|
572
|
+
this.http = http;
|
|
573
|
+
}
|
|
574
|
+
async stats() {
|
|
575
|
+
return this.http.request({ path: "/dashboard/stats" });
|
|
576
|
+
}
|
|
577
|
+
async reporting(options) {
|
|
578
|
+
return this.http.request({
|
|
579
|
+
path: "/dashboard/reporting",
|
|
580
|
+
params: {
|
|
581
|
+
start_date: options?.startDate,
|
|
582
|
+
end_date: options?.endDate
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
// src/resources/payments.ts
|
|
589
|
+
var PaymentsResource = class {
|
|
590
|
+
constructor(http) {
|
|
591
|
+
this.http = http;
|
|
592
|
+
}
|
|
593
|
+
async createPaymentIntent(mailingId, options) {
|
|
594
|
+
const body = { mailingId };
|
|
595
|
+
if (options?.paymentMethodId) {
|
|
596
|
+
body.paymentMethodId = options.paymentMethodId;
|
|
597
|
+
}
|
|
598
|
+
return this.http.request({
|
|
599
|
+
method: "POST",
|
|
600
|
+
path: "/payments/create-payment-intent",
|
|
601
|
+
body
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
async getStripeKey() {
|
|
605
|
+
const data = await this.http.request({
|
|
606
|
+
path: "/payments/config"
|
|
607
|
+
});
|
|
608
|
+
return data.publishable_key;
|
|
609
|
+
}
|
|
610
|
+
async listCards() {
|
|
611
|
+
return this.http.request({ path: "/payments/cards" });
|
|
612
|
+
}
|
|
613
|
+
async deleteCard(paymentMethodId) {
|
|
614
|
+
await this.http.request({
|
|
615
|
+
method: "DELETE",
|
|
616
|
+
path: `/payments/cards/${paymentMethodId}`
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
// src/resources/qr.ts
|
|
622
|
+
var QrResource = class {
|
|
623
|
+
constructor(http) {
|
|
624
|
+
this.http = http;
|
|
625
|
+
}
|
|
626
|
+
async list(options) {
|
|
627
|
+
return this.http.request({
|
|
628
|
+
path: "/qr/links",
|
|
629
|
+
params: {
|
|
630
|
+
page: options?.page ?? 1,
|
|
631
|
+
page_size: options?.pageSize ?? 50
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
iterAll(options) {
|
|
636
|
+
return new PageIterator(
|
|
637
|
+
(page, pageSize) => this.list({ page, pageSize }),
|
|
638
|
+
options?.pageSize ?? 100
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
async create(params) {
|
|
642
|
+
return this.http.request({
|
|
643
|
+
method: "POST",
|
|
644
|
+
path: "/qr/links",
|
|
645
|
+
body: params
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
async delete(linkId) {
|
|
649
|
+
await this.http.request({
|
|
650
|
+
method: "DELETE",
|
|
651
|
+
path: `/qr/links/${linkId}`
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
async analytics(options) {
|
|
655
|
+
return this.http.request({
|
|
656
|
+
path: "/qr/analytics",
|
|
657
|
+
params: { days: options?.days ?? 30 }
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
// src/resources/merge-tags.ts
|
|
663
|
+
var MergeTagsResource = class {
|
|
664
|
+
constructor(http) {
|
|
665
|
+
this.http = http;
|
|
666
|
+
}
|
|
667
|
+
async list() {
|
|
668
|
+
return this.http.request({ path: "/merge-tags" });
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
// src/resources/pricing.ts
|
|
673
|
+
var PricingResource = class {
|
|
674
|
+
constructor(http) {
|
|
675
|
+
this.http = http;
|
|
676
|
+
}
|
|
677
|
+
async catalog() {
|
|
678
|
+
return this.http.request({ path: "/pricing" });
|
|
679
|
+
}
|
|
680
|
+
async countries() {
|
|
681
|
+
return this.http.request({
|
|
682
|
+
path: "/pricing/countries"
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
async postageZones() {
|
|
686
|
+
return this.http.request({
|
|
687
|
+
path: "/pricing/postage-zones"
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
async postageRates(options) {
|
|
691
|
+
return this.http.request({
|
|
692
|
+
path: "/pricing/postage-rates",
|
|
693
|
+
params: {
|
|
694
|
+
product_type: options?.productType,
|
|
695
|
+
zone: options?.zone
|
|
696
|
+
}
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
// src/resources/coupons.ts
|
|
702
|
+
var CouponsResource = class {
|
|
703
|
+
constructor(http) {
|
|
704
|
+
this.http = http;
|
|
705
|
+
}
|
|
706
|
+
async list() {
|
|
707
|
+
return this.http.request({ path: "/coupons" });
|
|
708
|
+
}
|
|
709
|
+
async get(listId) {
|
|
710
|
+
return this.http.request({ path: `/coupons/${listId}` });
|
|
711
|
+
}
|
|
712
|
+
async create(name, options) {
|
|
713
|
+
return this.http.request({
|
|
714
|
+
method: "POST",
|
|
715
|
+
path: "/coupons",
|
|
716
|
+
body: { name, description: options?.description }
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
async update(listId, params) {
|
|
720
|
+
return this.http.request({
|
|
721
|
+
method: "PUT",
|
|
722
|
+
path: `/coupons/${listId}`,
|
|
723
|
+
body: params
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
async delete(listId) {
|
|
727
|
+
await this.http.request({
|
|
728
|
+
method: "DELETE",
|
|
729
|
+
path: `/coupons/${listId}`
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
async listCodes(listId, options) {
|
|
733
|
+
return this.http.request({
|
|
734
|
+
path: `/coupons/${listId}/codes`,
|
|
735
|
+
params: {
|
|
736
|
+
page: options?.page ?? 1,
|
|
737
|
+
page_size: options?.pageSize ?? 50,
|
|
738
|
+
used: options?.used
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
async importCodes(listId, codes) {
|
|
743
|
+
return this.http.request({
|
|
744
|
+
method: "POST",
|
|
745
|
+
path: `/coupons/${listId}/codes/import`,
|
|
746
|
+
body: { codes }
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
async deleteCode(listId, codeId) {
|
|
750
|
+
await this.http.request({
|
|
751
|
+
method: "DELETE",
|
|
752
|
+
path: `/coupons/${listId}/codes/${codeId}`
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
async checkAvailability(listId, count) {
|
|
756
|
+
return this.http.request({
|
|
757
|
+
path: `/coupons/${listId}/availability`,
|
|
758
|
+
params: { count }
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
// src/resources/assets.ts
|
|
764
|
+
var AssetsResource = class {
|
|
765
|
+
constructor(http) {
|
|
766
|
+
this.http = http;
|
|
767
|
+
}
|
|
768
|
+
async list(options) {
|
|
769
|
+
return this.http.request({
|
|
770
|
+
path: "/assets",
|
|
771
|
+
params: {
|
|
772
|
+
page: options?.page ?? 1,
|
|
773
|
+
page_size: options?.pageSize ?? 50
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
iterAll(options) {
|
|
778
|
+
return new PageIterator(
|
|
779
|
+
(page, pageSize) => this.list({ page, pageSize }),
|
|
780
|
+
options?.pageSize ?? 100
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
async upload(file, filename, mimeType) {
|
|
784
|
+
const formData = new FormData();
|
|
785
|
+
const blob = file instanceof Blob ? file : new Blob([file], { type: mimeType ?? "application/octet-stream" });
|
|
786
|
+
formData.append("file", blob, filename);
|
|
787
|
+
const response = await this.http.requestRaw({
|
|
788
|
+
method: "POST",
|
|
789
|
+
path: "/assets",
|
|
790
|
+
rawBody: formData,
|
|
791
|
+
rawHeaders: {}
|
|
792
|
+
// let fetch set Content-Type with boundary
|
|
793
|
+
});
|
|
794
|
+
return await response.json();
|
|
795
|
+
}
|
|
796
|
+
async delete(assetId) {
|
|
797
|
+
await this.http.request({
|
|
798
|
+
method: "DELETE",
|
|
799
|
+
path: `/assets/${assetId}`
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
// src/client.ts
|
|
805
|
+
var MailerBot = class {
|
|
806
|
+
contacts;
|
|
807
|
+
contactLists;
|
|
808
|
+
documents;
|
|
809
|
+
postcards;
|
|
810
|
+
mailings;
|
|
811
|
+
campaigns;
|
|
812
|
+
dashboard;
|
|
813
|
+
payments;
|
|
814
|
+
qr;
|
|
815
|
+
mergeTags;
|
|
816
|
+
pricing;
|
|
817
|
+
coupons;
|
|
818
|
+
assets;
|
|
819
|
+
constructor(apiKey, options) {
|
|
820
|
+
const baseUrl = (options?.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
821
|
+
const timeout = options?.timeout ?? DEFAULT_TIMEOUT;
|
|
822
|
+
const headers = buildHeaders(apiKey);
|
|
823
|
+
const http = new HttpClient(baseUrl, headers, timeout);
|
|
824
|
+
this.contacts = new ContactsResource(http);
|
|
825
|
+
this.contactLists = new ContactListsResource(http);
|
|
826
|
+
this.documents = new DocumentsResource(http);
|
|
827
|
+
this.postcards = new PostcardsResource(http);
|
|
828
|
+
this.mailings = new MailingsResource(http);
|
|
829
|
+
this.campaigns = new CampaignsResource(http);
|
|
830
|
+
this.dashboard = new DashboardResource(http);
|
|
831
|
+
this.payments = new PaymentsResource(http);
|
|
832
|
+
this.qr = new QrResource(http);
|
|
833
|
+
this.mergeTags = new MergeTagsResource(http);
|
|
834
|
+
this.pricing = new PricingResource(http);
|
|
835
|
+
this.coupons = new CouponsResource(http);
|
|
836
|
+
this.assets = new AssetsResource(http);
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
|
|
840
|
+
export { AuthenticationError, MailerBot, MailerBotError, NotFoundError, PageIterator, PermissionError, RateLimitError, ServerError, ValidationError };
|
|
841
|
+
//# sourceMappingURL=index.js.map
|
|
842
|
+
//# sourceMappingURL=index.js.map
|