@botozap/sdk 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 +229 -0
- package/dist/index.cjs +793 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +675 -0
- package/dist/index.d.ts +675 -0
- package/dist/index.js +765 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,793 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
BotoZap: () => BotoZap,
|
|
24
|
+
BotoZapError: () => BotoZapError
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/errors.ts
|
|
29
|
+
var BotoZapError = class extends Error {
|
|
30
|
+
code;
|
|
31
|
+
status;
|
|
32
|
+
/** Headers da resposta HTTP. Ausente em erros sem resposta (status 0). */
|
|
33
|
+
headers;
|
|
34
|
+
constructor(code, message, status, headers) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "BotoZapError";
|
|
37
|
+
this.code = code;
|
|
38
|
+
this.status = status;
|
|
39
|
+
this.headers = headers;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/resources/messages.ts
|
|
44
|
+
var Messages = class {
|
|
45
|
+
constructor(client) {
|
|
46
|
+
this.client = client;
|
|
47
|
+
}
|
|
48
|
+
client;
|
|
49
|
+
/** Envia uma mensagem de texto. */
|
|
50
|
+
async send(params) {
|
|
51
|
+
const result = await this.client.requestObject("POST", "/messages", {
|
|
52
|
+
body: {
|
|
53
|
+
to: params.to,
|
|
54
|
+
type: "text",
|
|
55
|
+
text: { body: params.text },
|
|
56
|
+
from: params.from
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return assertSendResult(result);
|
|
60
|
+
}
|
|
61
|
+
/** Envia uma mensagem usando um template aprovado. */
|
|
62
|
+
async sendTemplate(params) {
|
|
63
|
+
const result = await this.client.requestObject("POST", "/messages", {
|
|
64
|
+
body: {
|
|
65
|
+
to: params.to,
|
|
66
|
+
type: "template",
|
|
67
|
+
template: params.template,
|
|
68
|
+
from: params.from
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return assertSendResult(result);
|
|
72
|
+
}
|
|
73
|
+
/** Lista as mensagens da conta (paginação por cursor). */
|
|
74
|
+
list(params = {}) {
|
|
75
|
+
return this.client.requestCursorList("GET", "/messages", {
|
|
76
|
+
query: {
|
|
77
|
+
limit: params.limit,
|
|
78
|
+
after: params.after,
|
|
79
|
+
before: params.before,
|
|
80
|
+
phone_number_id: params.phone_number_id,
|
|
81
|
+
conversation_id: params.conversation_id,
|
|
82
|
+
direction: params.direction,
|
|
83
|
+
status: params.status,
|
|
84
|
+
message_type: params.message_type,
|
|
85
|
+
has_media: params.has_media === void 0 ? void 0 : String(params.has_media)
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/** Busca uma mensagem pelo id interno. */
|
|
90
|
+
get(id) {
|
|
91
|
+
return this.client.requestItem(
|
|
92
|
+
"GET",
|
|
93
|
+
`/messages/${encodeURIComponent(id)}`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
function assertSendResult(value) {
|
|
98
|
+
if (typeof value.wamid !== "string" || typeof value.to !== "string" || typeof value.status !== "string" || value.id !== null && typeof value.id !== "string") {
|
|
99
|
+
throw new BotoZapError(
|
|
100
|
+
"malformed_response",
|
|
101
|
+
"resposta de envio sem id/wamid/to/status v\xE1lidos",
|
|
102
|
+
0
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/resources/customers.ts
|
|
109
|
+
function enc(id) {
|
|
110
|
+
return encodeURIComponent(id);
|
|
111
|
+
}
|
|
112
|
+
var Customers = class {
|
|
113
|
+
constructor(client) {
|
|
114
|
+
this.client = client;
|
|
115
|
+
}
|
|
116
|
+
client;
|
|
117
|
+
/** Lista os clientes da conta (paginação por offset/página). */
|
|
118
|
+
list(params = {}) {
|
|
119
|
+
return this.client.requestOffsetList("GET", "/customers", {
|
|
120
|
+
query: { page: params.page, per_page: params.per_page }
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/** Busca um cliente pelo id. */
|
|
124
|
+
get(id) {
|
|
125
|
+
return this.client.requestItem("GET", `/customers/${enc(id)}`);
|
|
126
|
+
}
|
|
127
|
+
/** Cria um cliente. */
|
|
128
|
+
create(params) {
|
|
129
|
+
return this.client.requestItem("POST", "/customers", {
|
|
130
|
+
body: params
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/** Atualiza `name` / `external_customer_id` de um cliente. */
|
|
134
|
+
update(id, params) {
|
|
135
|
+
return this.client.requestItem("PATCH", `/customers/${enc(id)}`, {
|
|
136
|
+
body: params
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/** Exclui um cliente. Responde 204 (sem corpo). */
|
|
140
|
+
delete(id) {
|
|
141
|
+
return this.client.request("DELETE", `/customers/${enc(id)}`);
|
|
142
|
+
}
|
|
143
|
+
/** Lista os links de setup de um cliente (paginação por offset/página). */
|
|
144
|
+
listSetupLinks(customerId, params = {}) {
|
|
145
|
+
return this.client.requestOffsetList(
|
|
146
|
+
"GET",
|
|
147
|
+
`/customers/${enc(customerId)}/setup_links`,
|
|
148
|
+
{ query: { page: params.page, per_page: params.per_page } }
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
/** Cria um link de setup sob um cliente. */
|
|
152
|
+
createSetupLink(customerId, params = {}) {
|
|
153
|
+
return this.client.requestItem(
|
|
154
|
+
"POST",
|
|
155
|
+
`/customers/${enc(customerId)}/setup_links`,
|
|
156
|
+
{ body: params }
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
/** Atualiza status/expiração de um link de setup. */
|
|
160
|
+
updateSetupLink(customerId, linkId, params) {
|
|
161
|
+
return this.client.requestItem(
|
|
162
|
+
"PATCH",
|
|
163
|
+
`/customers/${enc(customerId)}/setup_links/${enc(linkId)}`,
|
|
164
|
+
{ body: params }
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/resources/templates.ts
|
|
170
|
+
var Templates = class {
|
|
171
|
+
constructor(client) {
|
|
172
|
+
this.client = client;
|
|
173
|
+
}
|
|
174
|
+
client;
|
|
175
|
+
/** Lista os templates da conta (paginação por offset/página). */
|
|
176
|
+
list(params = {}) {
|
|
177
|
+
return this.client.requestOffsetList("GET", "/templates", {
|
|
178
|
+
query: {
|
|
179
|
+
page: params.page,
|
|
180
|
+
per_page: params.per_page,
|
|
181
|
+
status: params.status,
|
|
182
|
+
category: params.category,
|
|
183
|
+
waba_connection_id: params.waba_connection_id,
|
|
184
|
+
phone_number_id: params.phone_number_id
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/** Busca um template pelo id. */
|
|
189
|
+
get(id) {
|
|
190
|
+
return this.client.requestItem(
|
|
191
|
+
"GET",
|
|
192
|
+
`/templates/${encodeURIComponent(id)}`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
/** Cria um template e o submete à Meta para análise. */
|
|
196
|
+
create(params) {
|
|
197
|
+
return this.client.requestItem("POST", "/templates", {
|
|
198
|
+
body: params
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// src/resources/broadcasts.ts
|
|
204
|
+
var Broadcasts = class {
|
|
205
|
+
constructor(client) {
|
|
206
|
+
this.client = client;
|
|
207
|
+
}
|
|
208
|
+
client;
|
|
209
|
+
/** Lista as transmissões da conta (paginação por offset/página). */
|
|
210
|
+
list(params = {}) {
|
|
211
|
+
return this.client.requestOffsetList("GET", "/broadcasts", {
|
|
212
|
+
query: {
|
|
213
|
+
page: params.page,
|
|
214
|
+
per_page: params.per_page,
|
|
215
|
+
status: params.status,
|
|
216
|
+
customer_id: params.customer_id,
|
|
217
|
+
phone_number_id: params.phone_number_id
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
get(id) {
|
|
222
|
+
return this.client.requestItem("GET", `/broadcasts/${enc2(id)}`);
|
|
223
|
+
}
|
|
224
|
+
create(params) {
|
|
225
|
+
return this.client.requestItem("POST", "/broadcasts", {
|
|
226
|
+
body: params
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
update(id, params) {
|
|
230
|
+
return this.client.requestItem(
|
|
231
|
+
"PATCH",
|
|
232
|
+
`/broadcasts/${enc2(id)}`,
|
|
233
|
+
{ body: params }
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
/** Lista os destinatários de uma transmissão (paginação por cursor). */
|
|
237
|
+
listRecipients(id, params = {}) {
|
|
238
|
+
return this.client.requestCursorList(
|
|
239
|
+
"GET",
|
|
240
|
+
`/broadcasts/${enc2(id)}/recipients`,
|
|
241
|
+
{
|
|
242
|
+
query: {
|
|
243
|
+
limit: params.limit,
|
|
244
|
+
after: params.after,
|
|
245
|
+
before: params.before,
|
|
246
|
+
status: params.status
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
/** Adiciona destinatários (lista de números ou contatos). */
|
|
252
|
+
addRecipients(id, recipients) {
|
|
253
|
+
return this.client.requestItem(
|
|
254
|
+
"POST",
|
|
255
|
+
`/broadcasts/${enc2(id)}/recipients`,
|
|
256
|
+
{ body: { recipients } }
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
/** Remove TODOS os destinatários. Responde 204 (sem corpo). */
|
|
260
|
+
removeRecipients(id) {
|
|
261
|
+
return this.client.request(
|
|
262
|
+
"DELETE",
|
|
263
|
+
`/broadcasts/${enc2(id)}/recipients`
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
schedule(id, params) {
|
|
267
|
+
return this.client.requestItem(
|
|
268
|
+
"POST",
|
|
269
|
+
`/broadcasts/${enc2(id)}/schedule`,
|
|
270
|
+
{ body: params }
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
send(id) {
|
|
274
|
+
return this.client.requestItem(
|
|
275
|
+
"POST",
|
|
276
|
+
`/broadcasts/${enc2(id)}/send`
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
cancel(id) {
|
|
280
|
+
return this.client.requestItem(
|
|
281
|
+
"POST",
|
|
282
|
+
`/broadcasts/${enc2(id)}/cancel`
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
function enc2(id) {
|
|
287
|
+
return encodeURIComponent(id);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// src/resources/contacts.ts
|
|
291
|
+
var Contacts = class {
|
|
292
|
+
constructor(client) {
|
|
293
|
+
this.client = client;
|
|
294
|
+
}
|
|
295
|
+
client;
|
|
296
|
+
/** Lista os contatos da conta (paginação por cursor). */
|
|
297
|
+
list(params = {}) {
|
|
298
|
+
return this.client.requestCursorList("GET", "/contacts", {
|
|
299
|
+
query: {
|
|
300
|
+
limit: params.limit,
|
|
301
|
+
after: params.after,
|
|
302
|
+
before: params.before,
|
|
303
|
+
customer_id: params.customer_id,
|
|
304
|
+
has_customer: params.has_customer === void 0 ? void 0 : String(params.has_customer),
|
|
305
|
+
profile_name_contains: params.profile_name_contains,
|
|
306
|
+
wa_id_contains: params.wa_id_contains,
|
|
307
|
+
created_after: params.created_after,
|
|
308
|
+
created_before: params.created_before
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
get(id) {
|
|
313
|
+
return this.client.requestItem("GET", `/contacts/${enc3(id)}`);
|
|
314
|
+
}
|
|
315
|
+
create(params) {
|
|
316
|
+
return this.client.requestItem("POST", "/contacts", {
|
|
317
|
+
body: params
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
update(id, params) {
|
|
321
|
+
return this.client.requestItem("PATCH", `/contacts/${enc3(id)}`, {
|
|
322
|
+
body: params
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
/** Remove um contato. Responde 204 (sem corpo). */
|
|
326
|
+
delete(id) {
|
|
327
|
+
return this.client.request("DELETE", `/contacts/${enc3(id)}`);
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
function enc3(id) {
|
|
331
|
+
return encodeURIComponent(id);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// src/resources/conversations.ts
|
|
335
|
+
var Conversations = class {
|
|
336
|
+
constructor(client) {
|
|
337
|
+
this.client = client;
|
|
338
|
+
}
|
|
339
|
+
client;
|
|
340
|
+
/** Lista as conversas da conta (paginação por cursor). */
|
|
341
|
+
list(params = {}) {
|
|
342
|
+
return this.client.requestCursorList(
|
|
343
|
+
"GET",
|
|
344
|
+
"/conversations",
|
|
345
|
+
{
|
|
346
|
+
query: {
|
|
347
|
+
limit: params.limit,
|
|
348
|
+
after: params.after,
|
|
349
|
+
before: params.before,
|
|
350
|
+
phone_number_id: params.phone_number_id,
|
|
351
|
+
status: params.status,
|
|
352
|
+
contact: params.contact,
|
|
353
|
+
phone_number: params.phone_number
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
get(id) {
|
|
359
|
+
return this.client.requestItem(
|
|
360
|
+
"GET",
|
|
361
|
+
`/conversations/${enc4(id)}`
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
update(id, params) {
|
|
365
|
+
return this.client.requestItem(
|
|
366
|
+
"PATCH",
|
|
367
|
+
`/conversations/${enc4(id)}`,
|
|
368
|
+
{ body: params }
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
/** Lista as atribuições da conversa (paginação por offset/página). */
|
|
372
|
+
listAssignments(id, params = {}) {
|
|
373
|
+
return this.client.requestOffsetList(
|
|
374
|
+
"GET",
|
|
375
|
+
`/conversations/${enc4(id)}/assignments`,
|
|
376
|
+
{ query: { page: params.page, per_page: params.per_page } }
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
createAssignment(id, params) {
|
|
380
|
+
return this.client.requestItem(
|
|
381
|
+
"POST",
|
|
382
|
+
`/conversations/${enc4(id)}/assignments`,
|
|
383
|
+
{ body: params }
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
getAssignment(id, assignmentId) {
|
|
387
|
+
return this.client.requestItem(
|
|
388
|
+
"GET",
|
|
389
|
+
`/conversations/${enc4(id)}/assignments/${enc4(assignmentId)}`
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
updateAssignment(id, assignmentId, params) {
|
|
393
|
+
return this.client.requestItem(
|
|
394
|
+
"PATCH",
|
|
395
|
+
`/conversations/${enc4(id)}/assignments/${enc4(assignmentId)}`,
|
|
396
|
+
{ body: params }
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
function enc4(id) {
|
|
401
|
+
return encodeURIComponent(id);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// src/resources/webhooks.ts
|
|
405
|
+
var Webhooks = class {
|
|
406
|
+
constructor(client) {
|
|
407
|
+
this.client = client;
|
|
408
|
+
}
|
|
409
|
+
client;
|
|
410
|
+
/** Lista os endpoints de webhook da conta (paginação por cursor). */
|
|
411
|
+
list(params = {}) {
|
|
412
|
+
return this.client.requestCursorList("GET", "/webhooks", {
|
|
413
|
+
query: {
|
|
414
|
+
limit: params.limit,
|
|
415
|
+
after: params.after,
|
|
416
|
+
before: params.before
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
get(id) {
|
|
421
|
+
return this.client.requestItem("GET", `/webhooks/${enc5(id)}`);
|
|
422
|
+
}
|
|
423
|
+
create(params) {
|
|
424
|
+
return this.client.requestItem("POST", "/webhooks", {
|
|
425
|
+
body: params
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
update(id, params) {
|
|
429
|
+
return this.client.requestItem("PATCH", `/webhooks/${enc5(id)}`, {
|
|
430
|
+
body: params
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
/** Remove um endpoint. Responde 204 (sem corpo). */
|
|
434
|
+
delete(id) {
|
|
435
|
+
return this.client.request("DELETE", `/webhooks/${enc5(id)}`);
|
|
436
|
+
}
|
|
437
|
+
/** Dispara um evento de teste para o endpoint. */
|
|
438
|
+
test(id) {
|
|
439
|
+
return this.client.requestItem(
|
|
440
|
+
"POST",
|
|
441
|
+
`/webhooks/${enc5(id)}/test`
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
function enc5(id) {
|
|
446
|
+
return encodeURIComponent(id);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/resources/phone-numbers.ts
|
|
450
|
+
var PhoneNumbers = class {
|
|
451
|
+
constructor(client) {
|
|
452
|
+
this.client = client;
|
|
453
|
+
}
|
|
454
|
+
client;
|
|
455
|
+
/** Lista os números da conta (paginação por offset/página). */
|
|
456
|
+
list(params = {}) {
|
|
457
|
+
return this.client.requestOffsetList(
|
|
458
|
+
"GET",
|
|
459
|
+
"/phone_numbers",
|
|
460
|
+
{
|
|
461
|
+
query: {
|
|
462
|
+
page: params.page,
|
|
463
|
+
per_page: params.per_page,
|
|
464
|
+
customer_id: params.customer_id
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
get(id) {
|
|
470
|
+
return this.client.requestItem(
|
|
471
|
+
"GET",
|
|
472
|
+
`/phone_numbers/${enc6(id)}`
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
// NOTA: `update` foi removido de propósito. A rota PATCH /v1/phone_numbers/:id
|
|
476
|
+
// responde SEMPRE 422 `no_updatable_fields` — o schema atual só tem campos de
|
|
477
|
+
// identidade e campos sincronizados da Meta, nenhum editável por aqui. Expor o
|
|
478
|
+
// método só entregaria uma chamada garantidamente quebrada.
|
|
479
|
+
/** Remove um número. Responde 204 (sem corpo). */
|
|
480
|
+
delete(id) {
|
|
481
|
+
return this.client.request("DELETE", `/phone_numbers/${enc6(id)}`);
|
|
482
|
+
}
|
|
483
|
+
/** Saúde do número (qualidade, limite, status na Meta). */
|
|
484
|
+
health(id) {
|
|
485
|
+
return this.client.requestItem(
|
|
486
|
+
"GET",
|
|
487
|
+
`/phone_numbers/${enc6(id)}/health`
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
function enc6(id) {
|
|
492
|
+
return encodeURIComponent(id);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// src/resources/flows.ts
|
|
496
|
+
var Flows = class {
|
|
497
|
+
constructor(client) {
|
|
498
|
+
this.client = client;
|
|
499
|
+
}
|
|
500
|
+
client;
|
|
501
|
+
/**
|
|
502
|
+
* Lista os flows de uma WABA (paginação por offset/página). Informe
|
|
503
|
+
* `phone_number_id` OU `waba_connection_id` — a rota exige um deles.
|
|
504
|
+
*/
|
|
505
|
+
list(params = {}) {
|
|
506
|
+
return this.client.requestOffsetList("GET", "/flows", {
|
|
507
|
+
query: {
|
|
508
|
+
page: params.page,
|
|
509
|
+
per_page: params.per_page,
|
|
510
|
+
phone_number_id: params.phone_number_id,
|
|
511
|
+
waba_connection_id: params.waba_connection_id
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
get(id, params) {
|
|
516
|
+
return this.client.requestItem("GET", `/flows/${enc7(id)}`, {
|
|
517
|
+
query: { phone_number_id: params.phone_number_id }
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
create(params) {
|
|
521
|
+
return this.client.requestItem("POST", "/flows", { body: params });
|
|
522
|
+
}
|
|
523
|
+
/** Remove um flow (só DRAFT, regra da Meta). Responde 204 (sem corpo). */
|
|
524
|
+
delete(id, params) {
|
|
525
|
+
return this.client.request("DELETE", `/flows/${enc7(id)}`, {
|
|
526
|
+
query: { phone_number_id: params.phone_number_id }
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
publish(id, params) {
|
|
530
|
+
return this.client.requestItem("POST", `/flows/${enc7(id)}/publish`, {
|
|
531
|
+
body: { phone_number_id: params.phone_number_id }
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
/** Lista as versões de um flow (paginação por offset/página). */
|
|
535
|
+
listVersions(id, params = {}) {
|
|
536
|
+
return this.client.requestOffsetList(
|
|
537
|
+
"GET",
|
|
538
|
+
`/flows/${enc7(id)}/versions`,
|
|
539
|
+
{ query: { page: params.page, per_page: params.per_page } }
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
createVersion(id, params) {
|
|
543
|
+
return this.client.requestItem(
|
|
544
|
+
"POST",
|
|
545
|
+
`/flows/${enc7(id)}/versions`,
|
|
546
|
+
{ body: params }
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
setupEncryption(id, params) {
|
|
550
|
+
return this.client.requestItem(
|
|
551
|
+
"POST",
|
|
552
|
+
`/flows/${enc7(id)}/setup_encryption`,
|
|
553
|
+
{ body: { phone_number_id: params.phone_number_id } }
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
getDataEndpoint(id) {
|
|
557
|
+
return this.client.requestItem(
|
|
558
|
+
"GET",
|
|
559
|
+
`/flows/${enc7(id)}/data_endpoint`
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
setDataEndpoint(id, params) {
|
|
563
|
+
return this.client.requestItem(
|
|
564
|
+
"POST",
|
|
565
|
+
`/flows/${enc7(id)}/data_endpoint`,
|
|
566
|
+
{ body: params }
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
function enc7(id) {
|
|
571
|
+
return encodeURIComponent(id);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// src/resources/media.ts
|
|
575
|
+
var Media = class {
|
|
576
|
+
constructor(client) {
|
|
577
|
+
this.client = client;
|
|
578
|
+
}
|
|
579
|
+
client;
|
|
580
|
+
upload(params) {
|
|
581
|
+
return this.client.requestItem("POST", "/media", {
|
|
582
|
+
body: params
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
// src/resources/read-only.ts
|
|
588
|
+
var Users = class {
|
|
589
|
+
constructor(client) {
|
|
590
|
+
this.client = client;
|
|
591
|
+
}
|
|
592
|
+
client;
|
|
593
|
+
/** Lista os membros da conta (paginação por offset/página). */
|
|
594
|
+
list(params = {}) {
|
|
595
|
+
return this.client.requestOffsetList("GET", "/users", {
|
|
596
|
+
query: { page: params.page, per_page: params.per_page }
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
var ApiLogs = class {
|
|
601
|
+
constructor(client) {
|
|
602
|
+
this.client = client;
|
|
603
|
+
}
|
|
604
|
+
client;
|
|
605
|
+
list(params = {}) {
|
|
606
|
+
return this.client.requestCursorList("GET", "/api_logs", {
|
|
607
|
+
query: {
|
|
608
|
+
limit: params.limit,
|
|
609
|
+
after: params.after,
|
|
610
|
+
before: params.before,
|
|
611
|
+
source: params.source,
|
|
612
|
+
method: params.method,
|
|
613
|
+
status_code: params.status_code
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
var WebhookDeliveries = class {
|
|
619
|
+
constructor(client) {
|
|
620
|
+
this.client = client;
|
|
621
|
+
}
|
|
622
|
+
client;
|
|
623
|
+
list(params = {}) {
|
|
624
|
+
return this.client.requestCursorList(
|
|
625
|
+
"GET",
|
|
626
|
+
"/webhook_deliveries",
|
|
627
|
+
{
|
|
628
|
+
query: {
|
|
629
|
+
limit: params.limit,
|
|
630
|
+
after: params.after,
|
|
631
|
+
before: params.before,
|
|
632
|
+
webhook_id: params.webhook_id,
|
|
633
|
+
status: params.status,
|
|
634
|
+
event_type: params.event_type
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// src/client.ts
|
|
642
|
+
var DEFAULT_BASE_URL = "https://botozap.com.br/api/v1";
|
|
643
|
+
var BotoZap = class {
|
|
644
|
+
messages;
|
|
645
|
+
customers;
|
|
646
|
+
templates;
|
|
647
|
+
broadcasts;
|
|
648
|
+
contacts;
|
|
649
|
+
conversations;
|
|
650
|
+
webhooks;
|
|
651
|
+
phoneNumbers;
|
|
652
|
+
flows;
|
|
653
|
+
media;
|
|
654
|
+
users;
|
|
655
|
+
apiLogs;
|
|
656
|
+
webhookDeliveries;
|
|
657
|
+
apiKey;
|
|
658
|
+
baseUrl;
|
|
659
|
+
fetchImpl;
|
|
660
|
+
constructor(options) {
|
|
661
|
+
if (!options || !options.apiKey) {
|
|
662
|
+
throw new Error("BotoZap: apiKey \xE9 obrigat\xF3rio.");
|
|
663
|
+
}
|
|
664
|
+
this.apiKey = options.apiKey;
|
|
665
|
+
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
666
|
+
const resolvedFetch = options.fetch ?? globalThis.fetch;
|
|
667
|
+
if (typeof resolvedFetch !== "function") {
|
|
668
|
+
throw new Error(
|
|
669
|
+
"BotoZap: fetch global n\xE3o encontrado. Use Node 20.19+ ou passe a op\xE7\xE3o `fetch`."
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
this.fetchImpl = resolvedFetch;
|
|
673
|
+
this.messages = new Messages(this);
|
|
674
|
+
this.customers = new Customers(this);
|
|
675
|
+
this.templates = new Templates(this);
|
|
676
|
+
this.broadcasts = new Broadcasts(this);
|
|
677
|
+
this.contacts = new Contacts(this);
|
|
678
|
+
this.conversations = new Conversations(this);
|
|
679
|
+
this.webhooks = new Webhooks(this);
|
|
680
|
+
this.phoneNumbers = new PhoneNumbers(this);
|
|
681
|
+
this.flows = new Flows(this);
|
|
682
|
+
this.media = new Media(this);
|
|
683
|
+
this.users = new Users(this);
|
|
684
|
+
this.apiLogs = new ApiLogs(this);
|
|
685
|
+
this.webhookDeliveries = new WebhookDeliveries(this);
|
|
686
|
+
}
|
|
687
|
+
/** Faz uma requisição autenticada e devolve o corpo já parseado. */
|
|
688
|
+
async request(method, path, opts = {}) {
|
|
689
|
+
const url = new URL(this.baseUrl + path);
|
|
690
|
+
if (opts.query) {
|
|
691
|
+
for (const [key, value] of Object.entries(opts.query)) {
|
|
692
|
+
if (value !== void 0) url.searchParams.set(key, String(value));
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
let res;
|
|
696
|
+
try {
|
|
697
|
+
res = await this.fetchImpl(url.toString(), {
|
|
698
|
+
method,
|
|
699
|
+
headers: {
|
|
700
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
701
|
+
"Content-Type": "application/json",
|
|
702
|
+
Accept: "application/json"
|
|
703
|
+
},
|
|
704
|
+
body: opts.body === void 0 ? void 0 : JSON.stringify(opts.body)
|
|
705
|
+
});
|
|
706
|
+
} catch (cause) {
|
|
707
|
+
const detail = cause instanceof Error ? cause.message : String(cause);
|
|
708
|
+
throw new BotoZapError(
|
|
709
|
+
"network_error",
|
|
710
|
+
`falha de rede ao chamar a API: ${detail}`,
|
|
711
|
+
0
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
const raw = await res.text();
|
|
715
|
+
const data = raw ? safeJson(raw) : void 0;
|
|
716
|
+
if (!res.ok) {
|
|
717
|
+
const envelope = data;
|
|
718
|
+
const headers = {};
|
|
719
|
+
res.headers.forEach((value, key) => {
|
|
720
|
+
headers[key] = value;
|
|
721
|
+
});
|
|
722
|
+
throw new BotoZapError(
|
|
723
|
+
envelope?.error?.code ?? "http_error",
|
|
724
|
+
envelope?.error?.message ?? `HTTP ${res.status}`,
|
|
725
|
+
res.status,
|
|
726
|
+
headers
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
return data;
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Requisição a um endpoint de ITEM (singular). A API embrulha essas respostas
|
|
733
|
+
* em `{ data: <obj> }` (helper `ok`); este método desempacota e devolve a
|
|
734
|
+
* entidade. NÃO é uma heurística "se tem .data desempacota" — o desempacote é
|
|
735
|
+
* o contrato conhecido do endpoint, declarado por quem chama este método (as
|
|
736
|
+
* rotas de LISTA usam `request` e devolvem `{ data, paging|meta }` inteiro; a
|
|
737
|
+
* rota POST /messages responde direto e também usa `request`).
|
|
738
|
+
*/
|
|
739
|
+
async requestItem(method, path, opts = {}) {
|
|
740
|
+
const env = await this.request(method, path, opts);
|
|
741
|
+
if (env == null || typeof env !== "object" || !("data" in env)) {
|
|
742
|
+
throw new BotoZapError(
|
|
743
|
+
"malformed_response",
|
|
744
|
+
"resposta sem envelope data \u2014 contrato violado",
|
|
745
|
+
0
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
return env.data;
|
|
749
|
+
}
|
|
750
|
+
/** Resposta JSON direta que precisa ser um objeto (ex.: POST /messages). */
|
|
751
|
+
async requestObject(method, path, opts = {}) {
|
|
752
|
+
const value = await this.request(method, path, opts);
|
|
753
|
+
if (!isObject(value)) {
|
|
754
|
+
throw malformed("resposta deveria ser um objeto JSON");
|
|
755
|
+
}
|
|
756
|
+
return value;
|
|
757
|
+
}
|
|
758
|
+
/** Lista paginada por cursor: exige `{ data: [], paging: {} }`. */
|
|
759
|
+
async requestCursorList(method, path, opts = {}) {
|
|
760
|
+
const value = await this.request(method, path, opts);
|
|
761
|
+
if (!isObject(value) || !Array.isArray(value.data) || !isObject(value.paging)) {
|
|
762
|
+
throw malformed("resposta sem data[]/paging \u2014 contrato de cursor violado");
|
|
763
|
+
}
|
|
764
|
+
return value;
|
|
765
|
+
}
|
|
766
|
+
/** Lista paginada por offset: exige `{ data: [], meta: {} }`. */
|
|
767
|
+
async requestOffsetList(method, path, opts = {}) {
|
|
768
|
+
const value = await this.request(method, path, opts);
|
|
769
|
+
if (!isObject(value) || !Array.isArray(value.data) || !isObject(value.meta)) {
|
|
770
|
+
throw malformed("resposta sem data[]/meta \u2014 contrato de offset violado");
|
|
771
|
+
}
|
|
772
|
+
return value;
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
function isObject(value) {
|
|
776
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
777
|
+
}
|
|
778
|
+
function malformed(message) {
|
|
779
|
+
return new BotoZapError("malformed_response", message, 0);
|
|
780
|
+
}
|
|
781
|
+
function safeJson(text) {
|
|
782
|
+
try {
|
|
783
|
+
return JSON.parse(text);
|
|
784
|
+
} catch {
|
|
785
|
+
return text;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
789
|
+
0 && (module.exports = {
|
|
790
|
+
BotoZap,
|
|
791
|
+
BotoZapError
|
|
792
|
+
});
|
|
793
|
+
//# sourceMappingURL=index.cjs.map
|