@vzaps/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/dist/index.js ADDED
@@ -0,0 +1,992 @@
1
+ // src/events.ts
2
+ import WebSocket from "ws";
3
+ var EventsResource = class {
4
+ constructor(http, webSocketFactory) {
5
+ this.http = http;
6
+ this.webSocketFactory = webSocketFactory;
7
+ }
8
+ http;
9
+ webSocketFactory;
10
+ async subscribe(request) {
11
+ const subscription = new EventSubscription(this.http, request, this.webSocketFactory);
12
+ await subscription.open();
13
+ return subscription;
14
+ }
15
+ };
16
+ var EventSubscription = class {
17
+ constructor(http, request, webSocketFactory) {
18
+ this.http = http;
19
+ this.request = request;
20
+ this.webSocketFactory = webSocketFactory;
21
+ }
22
+ http;
23
+ request;
24
+ webSocketFactory;
25
+ handlers = /* @__PURE__ */ new Map();
26
+ errorHandlers = /* @__PURE__ */ new Set();
27
+ lifecycleHandlers = /* @__PURE__ */ new Map();
28
+ socket;
29
+ closed = false;
30
+ retryCount = 0;
31
+ async open() {
32
+ const token = await this.http.getAccessToken(this.request.signal);
33
+ const url = this.http.buildRealtimeUrl("/events/ws", {
34
+ instance_id: this.request.instanceId,
35
+ events: this.request.events?.join(","),
36
+ access_token: token,
37
+ client_token: this.http.clientToken,
38
+ instance_token: this.request.instanceToken,
39
+ last_event_id: this.request.lastEventId
40
+ });
41
+ const headers = {
42
+ Authorization: `Bearer ${token}`,
43
+ "X-Client-Token": this.http.clientToken
44
+ };
45
+ headers["X-Instance-Token"] = this.request.instanceToken;
46
+ this.socket = this.createSocket(url, headers);
47
+ this.bindSocket(this.socket);
48
+ }
49
+ on(event, handler) {
50
+ if (event === "error") {
51
+ this.errorHandlers.add(handler);
52
+ return this;
53
+ }
54
+ if (event === "open" || event === "close") {
55
+ this.addLifecycleHandler(event, handler);
56
+ return this;
57
+ }
58
+ const handlers = this.handlers.get(event) ?? /* @__PURE__ */ new Set();
59
+ handlers.add(handler);
60
+ this.handlers.set(event, handlers);
61
+ return this;
62
+ }
63
+ off(event, handler) {
64
+ if (event === "error") {
65
+ this.errorHandlers.delete(handler);
66
+ return this;
67
+ }
68
+ if (event === "open" || event === "close") {
69
+ this.lifecycleHandlers.get(event)?.delete(handler);
70
+ return this;
71
+ }
72
+ this.handlers.get(event)?.delete(handler);
73
+ return this;
74
+ }
75
+ async close(code = 1e3, reason = "Client closed subscription") {
76
+ this.closed = true;
77
+ this.socket?.close(code, reason);
78
+ }
79
+ createSocket(url, headers) {
80
+ if (this.webSocketFactory) {
81
+ return this.webSocketFactory(url, headers);
82
+ }
83
+ if (typeof globalThis.WebSocket !== "undefined") {
84
+ return new globalThis.WebSocket(url);
85
+ }
86
+ return new WebSocket(url, { headers });
87
+ }
88
+ bindSocket(socket) {
89
+ listen(socket, "open", () => {
90
+ this.retryCount = 0;
91
+ this.emitLifecycle("open");
92
+ });
93
+ listen(socket, "message", (eventOrData) => {
94
+ const raw = readMessageData(eventOrData);
95
+ if (raw !== void 0) {
96
+ void this.dispatch(raw);
97
+ }
98
+ });
99
+ listen(socket, "error", (error) => {
100
+ this.emitError(error);
101
+ });
102
+ listen(socket, "close", () => {
103
+ this.emitLifecycle("close");
104
+ void this.reconnectIfNeeded();
105
+ });
106
+ this.request.signal?.addEventListener("abort", () => {
107
+ void this.close(1e3, "Subscription aborted");
108
+ }, { once: true });
109
+ }
110
+ async dispatch(raw) {
111
+ try {
112
+ const event = parseEvent(raw);
113
+ const handlers = /* @__PURE__ */ new Set([
114
+ ...this.handlers.get(event.type) ?? [],
115
+ ...this.handlers.get("All") ?? []
116
+ ]);
117
+ await Promise.all([...handlers].map((handler) => handler(event)));
118
+ this.ack(event.id);
119
+ } catch (error) {
120
+ this.emitError(error);
121
+ }
122
+ }
123
+ ack(eventId) {
124
+ if (!eventId || !this.socket) {
125
+ return;
126
+ }
127
+ this.socket.send(JSON.stringify({ type: "ack", event_id: eventId }));
128
+ }
129
+ async reconnectIfNeeded() {
130
+ if (this.closed || this.request.reconnect === false) {
131
+ return;
132
+ }
133
+ const maxRetries = this.request.maxRetries ?? Number.POSITIVE_INFINITY;
134
+ if (this.retryCount >= maxRetries) {
135
+ return;
136
+ }
137
+ this.retryCount += 1;
138
+ await delay(this.request.retryDelayMs ?? Math.min(3e4, 1e3 * this.retryCount));
139
+ await this.open();
140
+ }
141
+ addLifecycleHandler(event, handler) {
142
+ const handlers = this.lifecycleHandlers.get(event) ?? /* @__PURE__ */ new Set();
143
+ handlers.add(handler);
144
+ this.lifecycleHandlers.set(event, handlers);
145
+ }
146
+ emitLifecycle(event, ...args) {
147
+ for (const handler of this.lifecycleHandlers.get(event) ?? []) {
148
+ handler(...args);
149
+ }
150
+ }
151
+ emitError(error) {
152
+ if (this.errorHandlers.size === 0) {
153
+ return;
154
+ }
155
+ for (const handler of this.errorHandlers) {
156
+ handler(error);
157
+ }
158
+ }
159
+ };
160
+ function listen(socket, type, listener) {
161
+ if (socket.addEventListener) {
162
+ socket.addEventListener(type, listener);
163
+ return;
164
+ }
165
+ socket.on?.(type, listener);
166
+ }
167
+ function readMessageData(eventOrData) {
168
+ if (isRecord(eventOrData) && "data" in eventOrData) {
169
+ return eventOrData.data;
170
+ }
171
+ return eventOrData;
172
+ }
173
+ function parseEvent(raw) {
174
+ const text = typeof raw === "string" ? raw : Buffer.isBuffer(raw) ? raw.toString("utf8") : String(raw);
175
+ return JSON.parse(text);
176
+ }
177
+ function delay(ms) {
178
+ return new Promise((resolve) => setTimeout(resolve, ms));
179
+ }
180
+ function isRecord(value) {
181
+ return typeof value === "object" && value !== null;
182
+ }
183
+
184
+ // src/errors.ts
185
+ var VZapsError = class extends Error {
186
+ status;
187
+ code;
188
+ details;
189
+ constructor(message, options = {}) {
190
+ super(message);
191
+ this.name = "VZapsError";
192
+ this.status = options.status;
193
+ this.code = options.code;
194
+ this.details = options.details;
195
+ }
196
+ };
197
+ var VZapsAuthenticationError = class extends VZapsError {
198
+ constructor(message = "Invalid VZaps client credentials", details) {
199
+ super(message, { status: 401, code: "AUTHENTICATION_FAILED", details });
200
+ this.name = "VZapsAuthenticationError";
201
+ }
202
+ };
203
+ var VZapsTimeoutError = class extends VZapsError {
204
+ constructor(message = "VZaps request timed out") {
205
+ super(message, { code: "REQUEST_TIMEOUT" });
206
+ this.name = "VZapsTimeoutError";
207
+ }
208
+ };
209
+
210
+ // src/casing.ts
211
+ function isPlainObject(value) {
212
+ return typeof value === "object" && value !== null && !Array.isArray(value);
213
+ }
214
+ function toSnakeCaseKey(key) {
215
+ return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").toLowerCase();
216
+ }
217
+ function toCamelCaseKey(key) {
218
+ return key.replace(/_([a-z0-9])/g, (_, char) => char.toUpperCase());
219
+ }
220
+ function keysToSnakeCase(value) {
221
+ return transformKeys(value, toSnakeCaseKey);
222
+ }
223
+ function keysToCamelCase(value) {
224
+ return transformKeys(value, toCamelCaseKey);
225
+ }
226
+ function transformKeys(value, transformKey) {
227
+ if (Array.isArray(value)) {
228
+ return value.map((item) => transformKeys(item, transformKey));
229
+ }
230
+ if (!isPlainObject(value)) {
231
+ return value;
232
+ }
233
+ const result = {};
234
+ for (const [key, nested] of Object.entries(value)) {
235
+ result[transformKey(key)] = transformKeys(nested, transformKey);
236
+ }
237
+ return result;
238
+ }
239
+
240
+ // src/http.ts
241
+ var DEFAULT_BASE_URL = "https://api.vzaps.com";
242
+ var DEFAULT_REALTIME_URL = "wss://realtime.vzaps.com";
243
+ var DEFAULT_TIMEOUT_MS = 3e4;
244
+ var DEFAULT_TOKEN_SKEW_MS = 6e4;
245
+ var HttpClient = class {
246
+ baseUrl;
247
+ realtimeUrl;
248
+ clientToken;
249
+ clientSecret;
250
+ fetchImpl;
251
+ timeoutMs;
252
+ tokenSkewMs;
253
+ userAgent;
254
+ cachedToken;
255
+ pendingToken;
256
+ constructor(options) {
257
+ this.clientToken = requireNonEmpty(options.clientToken, "clientToken");
258
+ this.clientSecret = requireNonEmpty(options.clientSecret, "clientSecret");
259
+ this.baseUrl = normalizeBaseUrl(options.baseUrl ?? DEFAULT_BASE_URL);
260
+ this.realtimeUrl = normalizeBaseUrl(options.realtimeUrl ?? DEFAULT_REALTIME_URL);
261
+ this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
262
+ this.tokenSkewMs = options.tokenSkewMs ?? DEFAULT_TOKEN_SKEW_MS;
263
+ this.userAgent = options.userAgent;
264
+ this.fetchImpl = options.fetch ?? globalThis.fetch;
265
+ if (!this.fetchImpl) {
266
+ throw new VZapsError("No fetch implementation available. Use Node.js 18+ or pass options.fetch.");
267
+ }
268
+ }
269
+ async getAccessToken(signal) {
270
+ if (this.cachedToken && this.cachedToken.expiresAt > Date.now()) {
271
+ return this.cachedToken.accessToken;
272
+ }
273
+ this.pendingToken ??= this.requestToken(signal).finally(() => {
274
+ this.pendingToken = void 0;
275
+ });
276
+ return this.pendingToken;
277
+ }
278
+ async request(method, path, options = {}) {
279
+ const url = this.buildUrl(path, options.query);
280
+ const headers = new Headers();
281
+ headers.set("Accept", "application/json");
282
+ if (options.body !== void 0) {
283
+ headers.set("Content-Type", "application/json");
284
+ }
285
+ if (this.userAgent) {
286
+ headers.set("User-Agent", this.userAgent);
287
+ }
288
+ for (const [key, value] of Object.entries(options.headers ?? {})) {
289
+ if (value !== void 0) {
290
+ headers.set(key, value);
291
+ }
292
+ }
293
+ if (options.auth !== false) {
294
+ headers.set("Authorization", `Bearer ${await this.getAccessToken(options.signal)}`);
295
+ headers.set("X-Client-Token", this.clientToken);
296
+ }
297
+ if (options.instanceToken) {
298
+ headers.set("X-Instance-Token", options.instanceToken);
299
+ }
300
+ const body = options.body === void 0 ? void 0 : JSON.stringify(keysToSnakeCase(options.body));
301
+ const response = await this.fetchWithTimeout(url, {
302
+ method,
303
+ headers,
304
+ body,
305
+ signal: options.signal
306
+ });
307
+ return parseResponse(response);
308
+ }
309
+ buildRealtimeUrl(path, query) {
310
+ return buildUrl(this.realtimeUrl, path, query);
311
+ }
312
+ async requestToken(signal) {
313
+ const response = await this.request("POST", "/token", {
314
+ auth: false,
315
+ signal,
316
+ body: {
317
+ clientToken: this.clientToken,
318
+ clientSecret: this.clientSecret
319
+ }
320
+ });
321
+ if (!response.accessToken || !response.expiresIn) {
322
+ throw new VZapsAuthenticationError("VZaps token response is missing accessToken or expiresIn", response);
323
+ }
324
+ this.cachedToken = {
325
+ accessToken: response.accessToken,
326
+ expiresAt: Date.now() + response.expiresIn * 1e3 - this.tokenSkewMs
327
+ };
328
+ return response.accessToken;
329
+ }
330
+ buildUrl(path, query) {
331
+ return buildUrl(this.baseUrl, path, query);
332
+ }
333
+ async fetchWithTimeout(url, init) {
334
+ const controller = new AbortController();
335
+ const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
336
+ const signal = mergeSignals(controller.signal, init.signal ?? void 0);
337
+ try {
338
+ return await this.fetchImpl(url, { ...init, signal });
339
+ } catch (error) {
340
+ if (controller.signal.aborted) {
341
+ throw new VZapsTimeoutError();
342
+ }
343
+ throw error;
344
+ } finally {
345
+ clearTimeout(timeout);
346
+ }
347
+ }
348
+ };
349
+ function requireNonEmpty(value, name) {
350
+ if (!value || value.trim() === "") {
351
+ throw new VZapsError(`VZaps ${name} is required`);
352
+ }
353
+ return value;
354
+ }
355
+ function normalizeBaseUrl(value) {
356
+ return value.replace(/\/+$/, "");
357
+ }
358
+ function buildUrl(baseUrl, path, query) {
359
+ const cleanPath = path.startsWith("/") ? path : `/${path}`;
360
+ const url = new URL(`${normalizeBaseUrl(baseUrl)}${cleanPath}`);
361
+ for (const [key, value] of Object.entries(query ?? {})) {
362
+ if (value !== void 0) {
363
+ url.searchParams.set(toSnakeCaseKey(key), String(value));
364
+ }
365
+ }
366
+ return url.toString();
367
+ }
368
+ async function parseResponse(response) {
369
+ const text = await response.text();
370
+ const data = text ? safeJsonParse(text) : void 0;
371
+ if (!response.ok) {
372
+ if (response.status === 401) {
373
+ throw new VZapsAuthenticationError(readErrorMessage(data, response.statusText), data);
374
+ }
375
+ throw new VZapsError(readErrorMessage(data, response.statusText), {
376
+ status: response.status,
377
+ details: data
378
+ });
379
+ }
380
+ return keysToCamelCase(data);
381
+ }
382
+ function safeJsonParse(text) {
383
+ try {
384
+ return JSON.parse(text);
385
+ } catch {
386
+ return text;
387
+ }
388
+ }
389
+ function readErrorMessage(data, fallback) {
390
+ if (isRecord2(data)) {
391
+ if (typeof data.error === "string") return data.error;
392
+ if (typeof data.message === "string") return data.message;
393
+ }
394
+ return fallback || "VZaps request failed";
395
+ }
396
+ function isRecord2(value) {
397
+ return typeof value === "object" && value !== null;
398
+ }
399
+ function mergeSignals(primary, secondary) {
400
+ if (!secondary) {
401
+ return primary;
402
+ }
403
+ if (secondary.aborted) {
404
+ return secondary;
405
+ }
406
+ const controller = new AbortController();
407
+ const abort = () => controller.abort();
408
+ primary.addEventListener("abort", abort, { once: true });
409
+ secondary.addEventListener("abort", abort, { once: true });
410
+ return controller.signal;
411
+ }
412
+
413
+ // src/resources.ts
414
+ var AuthResource = class {
415
+ constructor(http) {
416
+ this.http = http;
417
+ }
418
+ http;
419
+ getAccessToken(signal) {
420
+ return this.http.getAccessToken(signal);
421
+ }
422
+ };
423
+ var InstancesResource = class {
424
+ constructor(http) {
425
+ this.http = http;
426
+ }
427
+ http;
428
+ create(data, options = {}) {
429
+ return this.http.request("PUT", "/instances/create", {
430
+ body: data,
431
+ signal: options.signal
432
+ });
433
+ }
434
+ list(data = {}, options = {}) {
435
+ return this.http.request("POST", "/instances/list", {
436
+ body: data,
437
+ signal: options.signal
438
+ });
439
+ }
440
+ get(instanceId, options = {}) {
441
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}`, {
442
+ signal: options.signal,
443
+ instanceToken: options.instanceToken
444
+ });
445
+ }
446
+ update(instanceId, data, options = {}) {
447
+ return this.http.request("PATCH", `/instances/${encodeURIComponent(instanceId)}`, {
448
+ body: data,
449
+ signal: options.signal,
450
+ instanceToken: options.instanceToken
451
+ });
452
+ }
453
+ restart(instanceId, options = {}) {
454
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/restart`, {
455
+ signal: options.signal,
456
+ instanceToken: options.instanceToken
457
+ });
458
+ }
459
+ delete(instanceId, options = {}) {
460
+ return this.http.request("DELETE", `/instances/${encodeURIComponent(instanceId)}`, {
461
+ signal: options.signal,
462
+ instanceToken: options.instanceToken
463
+ });
464
+ }
465
+ provision(data, options = {}) {
466
+ return this.http.request("PUT", "/instances/provision", {
467
+ body: data,
468
+ signal: options.signal
469
+ });
470
+ }
471
+ search(data, options = {}) {
472
+ return this.http.request("POST", "/instances/search", {
473
+ body: data,
474
+ signal: options.signal
475
+ });
476
+ }
477
+ subscribe(instanceId, data = {}, options = {}) {
478
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/subscribe`, {
479
+ body: data,
480
+ signal: options.signal,
481
+ instanceToken: options.instanceToken
482
+ });
483
+ }
484
+ resumeSubscription(instanceId, options = {}) {
485
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/resume-subscription`, {
486
+ signal: options.signal,
487
+ instanceToken: options.instanceToken
488
+ });
489
+ }
490
+ cancel(instanceId, options = {}) {
491
+ return this.http.request("PUT", `/instances/${encodeURIComponent(instanceId)}/cancel`, {
492
+ signal: options.signal,
493
+ instanceToken: options.instanceToken
494
+ });
495
+ }
496
+ };
497
+ var MessagesResource = class {
498
+ constructor(http) {
499
+ this.http = http;
500
+ }
501
+ http;
502
+ sendText(request, options = {}) {
503
+ return this.post("/chat/send/text", request, options);
504
+ }
505
+ sendImage(request, options = {}) {
506
+ return this.post("/chat/send/image", request, options);
507
+ }
508
+ sendAudio(request, options = {}) {
509
+ return this.post("/chat/send/audio", request, options);
510
+ }
511
+ sendDocument(request, options = {}) {
512
+ return this.post("/chat/send/document", request, options);
513
+ }
514
+ sendVideo(request, options = {}) {
515
+ return this.post("/chat/send/video", request, options);
516
+ }
517
+ sendSticker(request, options = {}) {
518
+ return this.post("/chat/send/sticker", request, options);
519
+ }
520
+ sendGif(request, options = {}) {
521
+ return this.post("/chat/send/gif", request, options);
522
+ }
523
+ sendLocation(request, options = {}) {
524
+ return this.post("/chat/send/location", request, options);
525
+ }
526
+ sendContact(request, options = {}) {
527
+ return this.post("/chat/send/contact", request, options);
528
+ }
529
+ sendButtons(request, options = {}) {
530
+ return this.post("/chat/send/buttons", request, options);
531
+ }
532
+ sendList(request, options = {}) {
533
+ return this.post("/chat/send/list", request, options);
534
+ }
535
+ sendLink(request, options = {}) {
536
+ return this.post("/chat/send/link", request, options);
537
+ }
538
+ sendPoll(request, options = {}) {
539
+ return this.post("/chat/send/poll", request, options);
540
+ }
541
+ pollVote(request, options = {}) {
542
+ return this.post("/chat/poll/vote", request, options);
543
+ }
544
+ react(request, options = {}) {
545
+ return this.post("/chat/react", request, options);
546
+ }
547
+ removeReaction(request, options = {}) {
548
+ return this.sendRequest("DELETE", "/chat/react", request, options);
549
+ }
550
+ presence(request, options = {}) {
551
+ return this.post("/chat/presence", request, options);
552
+ }
553
+ markRead(request, options = {}) {
554
+ return this.post("/chat/markread", request, options);
555
+ }
556
+ downloadImage(request, options = {}) {
557
+ return this.post("/chat/downloadimage", request, options);
558
+ }
559
+ downloadVideo(request, options = {}) {
560
+ return this.post("/chat/downloadvideo", request, options);
561
+ }
562
+ downloadAudio(request, options = {}) {
563
+ return this.post("/chat/downloadaudio", request, options);
564
+ }
565
+ downloadDocument(request, options = {}) {
566
+ return this.post("/chat/downloaddocument", request, options);
567
+ }
568
+ edit(request, options = {}) {
569
+ const { messageId, ...rest } = request;
570
+ return this.sendRequest("PATCH", `/chat/messages/${encodeURIComponent(messageId)}`, rest, options);
571
+ }
572
+ delete(request, options = {}) {
573
+ const { messageId, ...rest } = request;
574
+ return this.sendRequest("DELETE", `/chat/messages/${encodeURIComponent(messageId)}`, rest, options);
575
+ }
576
+ send(instanceId, path, body, options = {}) {
577
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/chat/${path.replace(/^\/+/, "")}`, {
578
+ body,
579
+ signal: options.signal,
580
+ instanceToken: options.instanceToken
581
+ });
582
+ }
583
+ post(path, request, options) {
584
+ return this.sendRequest("POST", path, request, options);
585
+ }
586
+ sendRequest(method, path, request, options) {
587
+ const { instanceId, instanceToken, ...body } = request;
588
+ return this.http.request(method, `/instances/${encodeURIComponent(instanceId)}${path}`, {
589
+ body,
590
+ signal: options.signal,
591
+ instanceToken
592
+ });
593
+ }
594
+ };
595
+ var WebhooksResource = class {
596
+ constructor(http) {
597
+ this.http = http;
598
+ }
599
+ http;
600
+ get(instanceId, options = {}) {
601
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/webhook`, {
602
+ signal: options.signal,
603
+ instanceToken: options.instanceToken
604
+ });
605
+ }
606
+ set(request, options = {}) {
607
+ const { instanceId, instanceToken, ...body } = request;
608
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/webhook`, {
609
+ body,
610
+ signal: options.signal,
611
+ instanceToken
612
+ });
613
+ }
614
+ searchLogs(request, options = {}) {
615
+ const { instanceId, instanceToken, ...body } = request;
616
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/webhook/logs/search`, {
617
+ body,
618
+ signal: options.signal,
619
+ instanceToken
620
+ });
621
+ }
622
+ getLog(request, options = {}) {
623
+ return this.http.request(
624
+ "GET",
625
+ `/instances/${encodeURIComponent(request.instanceId)}/webhook/logs/${encodeURIComponent(request.logId)}`,
626
+ {
627
+ signal: options.signal,
628
+ instanceToken: request.instanceToken
629
+ }
630
+ );
631
+ }
632
+ retryLog(request, options = {}) {
633
+ return this.http.request(
634
+ "POST",
635
+ `/instances/${encodeURIComponent(request.instanceId)}/webhook/logs/${encodeURIComponent(request.logId)}/retry`,
636
+ {
637
+ signal: options.signal,
638
+ instanceToken: request.instanceToken
639
+ }
640
+ );
641
+ }
642
+ };
643
+ var ContactsResource = class {
644
+ constructor(http) {
645
+ this.http = http;
646
+ }
647
+ http;
648
+ list(instanceId, options = {}) {
649
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/contact/list`, {
650
+ signal: options.signal,
651
+ instanceToken: options.instanceToken
652
+ });
653
+ }
654
+ add(request, options = {}) {
655
+ const { instanceId, instanceToken, ...body } = request;
656
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/contact/add`, {
657
+ body,
658
+ signal: options.signal,
659
+ instanceToken
660
+ });
661
+ }
662
+ };
663
+ var GroupsResource = class {
664
+ constructor(http) {
665
+ this.http = http;
666
+ }
667
+ http;
668
+ list(request, options = {}) {
669
+ const { instanceId, instanceToken, page, pageSize } = request;
670
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/group/list`, {
671
+ query: { page, pageSize },
672
+ signal: options.signal,
673
+ instanceToken
674
+ });
675
+ }
676
+ get(request, options = {}) {
677
+ return this.http.request("GET", `/instances/${encodeURIComponent(request.instanceId)}/group/info`, {
678
+ query: { groupId: request.groupId },
679
+ signal: options.signal,
680
+ instanceToken: request.instanceToken
681
+ });
682
+ }
683
+ inviteLink(request, options = {}) {
684
+ return this.http.request("GET", `/instances/${encodeURIComponent(request.instanceId)}/group/invitelink`, {
685
+ query: { groupId: request.groupId, reset: request.reset },
686
+ signal: options.signal,
687
+ instanceToken: request.instanceToken
688
+ });
689
+ }
690
+ setPhoto(request, options = {}) {
691
+ return postInstancePath(this.http, request, "/group/photo", options);
692
+ }
693
+ setName(request, options = {}) {
694
+ return postInstancePath(this.http, request, "/group/name", options);
695
+ }
696
+ setDescription(request, options = {}) {
697
+ return postInstancePath(this.http, request, "/group/description", options);
698
+ }
699
+ setSettings(request, options = {}) {
700
+ return postInstancePath(this.http, request, "/group/settings", options);
701
+ }
702
+ create(request, options = {}) {
703
+ return postInstancePath(this.http, request, "/group/create", options);
704
+ }
705
+ addAdmin(request, options = {}) {
706
+ return postInstancePath(this.http, request, "/group/add-admin", options);
707
+ }
708
+ removeAdmin(request, options = {}) {
709
+ return postInstancePath(this.http, request, "/group/remove-admin", options);
710
+ }
711
+ };
712
+ var SessionsResource = class {
713
+ constructor(http) {
714
+ this.http = http;
715
+ }
716
+ http;
717
+ status(instanceId, options = {}) {
718
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/session/status`, {
719
+ signal: options.signal,
720
+ instanceToken: options.instanceToken
721
+ });
722
+ }
723
+ qr(instanceId, options = {}) {
724
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/session/qr`, {
725
+ signal: options.signal,
726
+ instanceToken: options.instanceToken
727
+ });
728
+ }
729
+ pairCode(instanceId, phone, options = {}) {
730
+ return this.http.request(
731
+ "GET",
732
+ `/instances/${encodeURIComponent(instanceId)}/session/paircode/${encodeURIComponent(phone)}`,
733
+ {
734
+ signal: options.signal,
735
+ instanceToken: options.instanceToken
736
+ }
737
+ );
738
+ }
739
+ disconnect(instanceId, options = {}) {
740
+ return this.http.request("POST", `/instances/${encodeURIComponent(instanceId)}/session/disconnect`, {
741
+ signal: options.signal,
742
+ instanceToken: options.instanceToken
743
+ });
744
+ }
745
+ };
746
+ var UsersResource = class {
747
+ constructor(http) {
748
+ this.http = http;
749
+ }
750
+ http;
751
+ info(request, options = {}) {
752
+ return postInstancePath(this.http, request, "/user/info", options);
753
+ }
754
+ check(request, options = {}) {
755
+ return postInstancePath(this.http, request, "/user/check", options);
756
+ }
757
+ avatar(request, options = {}) {
758
+ return postInstancePath(this.http, request, "/user/avatar", options);
759
+ }
760
+ contacts(instanceId, options = {}) {
761
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/user/contacts`, {
762
+ signal: options.signal,
763
+ instanceToken: options.instanceToken
764
+ });
765
+ }
766
+ };
767
+ var QueuesResource = class {
768
+ constructor(http) {
769
+ this.http = http;
770
+ }
771
+ http;
772
+ listMessages(request, options = {}) {
773
+ return getInstancePath(this.http, request, "/queue/messages", options);
774
+ }
775
+ purgeMessages(request, options = {}) {
776
+ return deleteInstancePath(this.http, request, "/queue/messages", options);
777
+ }
778
+ removeMessage(request, options = {}) {
779
+ const { messageId, ...rest } = request;
780
+ return deleteInstancePath(this.http, rest, `/queue/messages/${encodeURIComponent(messageId)}`, options);
781
+ }
782
+ listOperations(request, options = {}) {
783
+ return getInstancePath(this.http, request, "/queue/operations", options);
784
+ }
785
+ purgeOperations(request, options = {}) {
786
+ return deleteInstancePath(this.http, request, "/queue/operations", options);
787
+ }
788
+ removeOperation(request, options = {}) {
789
+ const { messageId, ...rest } = request;
790
+ return deleteInstancePath(this.http, rest, `/queue/operations/${encodeURIComponent(messageId)}`, options);
791
+ }
792
+ };
793
+ var TypebotsResource = class {
794
+ constructor(http) {
795
+ this.http = http;
796
+ }
797
+ http;
798
+ list(instanceId, options = {}) {
799
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/typebots`, {
800
+ signal: options.signal,
801
+ instanceToken: options.instanceToken
802
+ });
803
+ }
804
+ create(request, options = {}) {
805
+ return postInstancePath(this.http, request, "/typebots", options);
806
+ }
807
+ update(request, options = {}) {
808
+ const { typebotId, ...rest } = request;
809
+ return requestInstancePath(this.http, "PATCH", rest, `/typebots/${encodeURIComponent(typebotId)}`, options);
810
+ }
811
+ delete(request, options = {}) {
812
+ const { typebotId, ...rest } = request;
813
+ return deleteInstancePath(this.http, rest, `/typebots/${encodeURIComponent(typebotId)}`, options);
814
+ }
815
+ listSessions(instanceId, options = {}) {
816
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/typebots/sessions`, {
817
+ signal: options.signal,
818
+ instanceToken: options.instanceToken
819
+ });
820
+ }
821
+ startSession(request, options = {}) {
822
+ const { typebotId, ...rest } = request;
823
+ const path = typebotId ? `/typebots/${encodeURIComponent(typebotId)}/sessions/start` : "/typebots/sessions/start";
824
+ return postInstancePath(this.http, rest, path, options);
825
+ }
826
+ closeSession(request, options = {}) {
827
+ const { session, ...rest } = request;
828
+ return postInstancePath(this.http, rest, `/typebots/sessions/${encodeURIComponent(session)}/close`, options);
829
+ }
830
+ pauseSession(request, options = {}) {
831
+ const { session, ...rest } = request;
832
+ return postInstancePath(this.http, rest, `/typebots/sessions/${encodeURIComponent(session)}/pause`, options);
833
+ }
834
+ };
835
+ var ChatwootResource = class {
836
+ constructor(http) {
837
+ this.http = http;
838
+ }
839
+ http;
840
+ get(instanceId, options = {}) {
841
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/chatwoot`, {
842
+ signal: options.signal,
843
+ instanceToken: options.instanceToken
844
+ });
845
+ }
846
+ set(request, options = {}) {
847
+ return postInstancePath(this.http, request, "/chatwoot", options);
848
+ }
849
+ delete(instanceId, options = {}) {
850
+ return this.http.request("DELETE", `/instances/${encodeURIComponent(instanceId)}/chatwoot`, {
851
+ signal: options.signal,
852
+ instanceToken: options.instanceToken
853
+ });
854
+ }
855
+ triggerImport(request, options = {}) {
856
+ const { what, ...rest } = request;
857
+ return postInstancePath(this.http, rest, `/chatwoot/import/${encodeURIComponent(what)}`, options);
858
+ }
859
+ };
860
+ var ChatsResource = class {
861
+ constructor(http) {
862
+ this.http = http;
863
+ }
864
+ http;
865
+ list(request, options = {}) {
866
+ const { instanceId, instanceToken, page, pageSize } = request;
867
+ return this.http.request("GET", `/instances/${encodeURIComponent(instanceId)}/chats`, {
868
+ query: { page, pageSize },
869
+ signal: options.signal,
870
+ instanceToken
871
+ });
872
+ }
873
+ get(request, options = {}) {
874
+ return this.chatAction("GET", request, "", options);
875
+ }
876
+ archive(request, options = {}) {
877
+ return this.chatAction("POST", request, "/archive", options);
878
+ }
879
+ unarchive(request, options = {}) {
880
+ return this.chatAction("POST", request, "/unarchive", options);
881
+ }
882
+ mute(request, options = {}) {
883
+ return this.chatAction("POST", request, "/mute", options);
884
+ }
885
+ unmute(request, options = {}) {
886
+ return this.chatAction("POST", request, "/unmute", options);
887
+ }
888
+ pin(request, options = {}) {
889
+ return this.chatAction("POST", request, "/pin", options);
890
+ }
891
+ unpin(request, options = {}) {
892
+ return this.chatAction("POST", request, "/unpin", options);
893
+ }
894
+ read(request, options = {}) {
895
+ return this.chatAction("POST", request, "/read", options);
896
+ }
897
+ unread(request, options = {}) {
898
+ return this.chatAction("POST", request, "/unread", options);
899
+ }
900
+ clear(request, options = {}) {
901
+ return this.chatAction("POST", request, "/clear", options);
902
+ }
903
+ delete(request, options = {}) {
904
+ return this.chatAction("DELETE", request, "", options);
905
+ }
906
+ setExpiration(request, options = {}) {
907
+ return this.chatAction("PUT", request, "/expiration", options);
908
+ }
909
+ chatAction(method, request, suffix, options) {
910
+ const { instanceId, instanceToken, phone, ...body } = request;
911
+ return this.http.request(method, `/instances/${encodeURIComponent(instanceId)}/chats/${encodeURIComponent(phone)}${suffix}`, {
912
+ body: Object.keys(body).length > 0 ? body : void 0,
913
+ signal: options.signal,
914
+ instanceToken
915
+ });
916
+ }
917
+ };
918
+ function getInstancePath(http, request, path, options) {
919
+ return requestInstancePath(http, "GET", request, path, options);
920
+ }
921
+ function postInstancePath(http, request, path, options) {
922
+ return requestInstancePath(http, "POST", request, path, options);
923
+ }
924
+ function deleteInstancePath(http, request, path, options) {
925
+ return requestInstancePath(http, "DELETE", request, path, options);
926
+ }
927
+ function requestInstancePath(http, method, request, path, options) {
928
+ const { instanceId, instanceToken, ...body } = request;
929
+ return http.request(method, `/instances/${encodeURIComponent(instanceId)}${path}`, {
930
+ body: Object.keys(body).length > 0 ? body : void 0,
931
+ signal: options.signal,
932
+ instanceToken
933
+ });
934
+ }
935
+
936
+ // src/client.ts
937
+ var VZapsClient = class {
938
+ auth;
939
+ instances;
940
+ sessions;
941
+ messages;
942
+ webhooks;
943
+ contacts;
944
+ groups;
945
+ users;
946
+ queues;
947
+ typebots;
948
+ chatwoot;
949
+ chats;
950
+ events;
951
+ http;
952
+ constructor(options) {
953
+ this.http = new HttpClient(options);
954
+ this.auth = new AuthResource(this.http);
955
+ this.instances = new InstancesResource(this.http);
956
+ this.sessions = new SessionsResource(this.http);
957
+ this.messages = new MessagesResource(this.http);
958
+ this.webhooks = new WebhooksResource(this.http);
959
+ this.contacts = new ContactsResource(this.http);
960
+ this.groups = new GroupsResource(this.http);
961
+ this.users = new UsersResource(this.http);
962
+ this.queues = new QueuesResource(this.http);
963
+ this.typebots = new TypebotsResource(this.http);
964
+ this.chatwoot = new ChatwootResource(this.http);
965
+ this.chats = new ChatsResource(this.http);
966
+ this.events = new EventsResource(this.http, options.webSocketFactory);
967
+ }
968
+ request(method, path, options = {}) {
969
+ return this.http.request(method, path, options);
970
+ }
971
+ };
972
+ export {
973
+ AuthResource,
974
+ ChatsResource,
975
+ ChatwootResource,
976
+ ContactsResource,
977
+ EventSubscription,
978
+ EventsResource,
979
+ GroupsResource,
980
+ InstancesResource,
981
+ MessagesResource,
982
+ QueuesResource,
983
+ SessionsResource,
984
+ TypebotsResource,
985
+ UsersResource,
986
+ VZapsAuthenticationError,
987
+ VZapsClient,
988
+ VZapsError,
989
+ VZapsTimeoutError,
990
+ WebhooksResource
991
+ };
992
+ //# sourceMappingURL=index.js.map