crossbar-client 1.0.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,796 @@
1
+ // src/errors.ts
2
+ var CrossbarError = class extends Error {
3
+ constructor(message, status, requestId, body) {
4
+ super(message);
5
+ this.message = message;
6
+ this.status = status;
7
+ this.requestId = requestId;
8
+ this.body = body;
9
+ this.name = "CrossbarError";
10
+ this.status = status;
11
+ this.requestId = requestId;
12
+ this.body = body;
13
+ }
14
+ };
15
+ var CrossbarAuthError = class extends CrossbarError {
16
+ constructor(message, requestId, body) {
17
+ super(message, 401, requestId, body);
18
+ this.name = "CrossbarAuthError";
19
+ }
20
+ };
21
+
22
+ // src/resources/accounts.ts
23
+ var AccountsResource = class {
24
+ constructor(client) {
25
+ this.client = client;
26
+ }
27
+ async get(accountId, options) {
28
+ return this.client.get(`/accounts/${accountId}`, options);
29
+ }
30
+ async create(parentAccountId, data, options) {
31
+ return this.client.put(`/accounts/${parentAccountId}`, data, options);
32
+ }
33
+ async update(accountId, data, options) {
34
+ return this.client.post(`/accounts/${accountId}`, data, options);
35
+ }
36
+ async patch(accountId, data, options) {
37
+ return this.client.patch(`/accounts/${accountId}`, data, options);
38
+ }
39
+ async remove(accountId, options) {
40
+ return this.client.delete(`/accounts/${accountId}`, options);
41
+ }
42
+ async children(accountId, options) {
43
+ return this.client.get(`/accounts/${accountId}/children`, options);
44
+ }
45
+ async descendants(accountId, options) {
46
+ return this.client.get(`/accounts/${accountId}/descendants`, options);
47
+ }
48
+ async siblings(accountId, options) {
49
+ return this.client.get(`/accounts/${accountId}/siblings`, options);
50
+ }
51
+ async parents(accountId, options) {
52
+ return this.client.get(`/accounts/${accountId}/parents`, options);
53
+ }
54
+ async tree(accountId, options) {
55
+ return this.client.get(`/accounts/${accountId}/tree`, options);
56
+ }
57
+ async getApiKey(accountId, options) {
58
+ return this.client.get(`/accounts/${accountId}/api_key`, options);
59
+ }
60
+ async regenerateApiKey(accountId, options) {
61
+ return this.client.put(
62
+ `/accounts/${accountId}/api_key`,
63
+ void 0,
64
+ options
65
+ );
66
+ }
67
+ async move(accountId, toAccountId, options) {
68
+ return this.client.post(`/accounts/${accountId}/move`, { to: toAccountId }, options);
69
+ }
70
+ async promoteReseller(accountId, options) {
71
+ return this.client.put(`/accounts/${accountId}/reseller`, void 0, options);
72
+ }
73
+ async demoteReseller(accountId, options) {
74
+ return this.client.delete(`/accounts/${accountId}/reseller`, options);
75
+ }
76
+ };
77
+
78
+ // src/types.ts
79
+ function mergePaginate(options) {
80
+ if (!options) return void 0;
81
+ const { paginate, ...rest } = options;
82
+ if (paginate === false) {
83
+ return { ...rest, query: { ...rest.query, paginate: "false" } };
84
+ }
85
+ return rest;
86
+ }
87
+
88
+ // src/resources/callflows.ts
89
+ var CallflowsResource = class {
90
+ constructor(client) {
91
+ this.client = client;
92
+ }
93
+ async list(accountId, options) {
94
+ return this.client.get(`/accounts/${accountId}/callflows`, mergePaginate(options));
95
+ }
96
+ async get(accountId, callflowId, options) {
97
+ return this.client.get(`/accounts/${accountId}/callflows/${callflowId}`, options);
98
+ }
99
+ async create(accountId, data, options) {
100
+ return this.client.put(`/accounts/${accountId}/callflows`, data, options);
101
+ }
102
+ async update(accountId, callflowId, data, options) {
103
+ return this.client.post(`/accounts/${accountId}/callflows/${callflowId}`, data, options);
104
+ }
105
+ async patch(accountId, callflowId, data, options) {
106
+ return this.client.patch(`/accounts/${accountId}/callflows/${callflowId}`, data, options);
107
+ }
108
+ async remove(accountId, callflowId, options) {
109
+ return this.client.delete(`/accounts/${accountId}/callflows/${callflowId}`, options);
110
+ }
111
+ };
112
+
113
+ // src/resources/cdrs.ts
114
+ function mergeCdrOptions(options) {
115
+ if (!options) return void 0;
116
+ const { created_from, created_to, ...rest } = options;
117
+ const merged = mergePaginate(rest);
118
+ if (created_from === void 0 && created_to === void 0) return merged;
119
+ return {
120
+ ...merged,
121
+ query: {
122
+ ...merged?.query,
123
+ ...created_from !== void 0 && { created_from },
124
+ ...created_to !== void 0 && { created_to }
125
+ }
126
+ };
127
+ }
128
+ var CdrsResource = class {
129
+ constructor(client) {
130
+ this.client = client;
131
+ }
132
+ async list(accountId, options) {
133
+ return this.client.get(`/accounts/${accountId}/cdrs`, mergeCdrOptions(options));
134
+ }
135
+ async get(accountId, cdrId, options) {
136
+ return this.client.get(`/accounts/${accountId}/cdrs/${cdrId}`, options);
137
+ }
138
+ async listByUser(accountId, userId, options) {
139
+ return this.client.get(
140
+ `/accounts/${accountId}/users/${userId}/cdrs`,
141
+ mergeCdrOptions(options)
142
+ );
143
+ }
144
+ };
145
+
146
+ // src/resources/channels.ts
147
+ var ChannelsResource = class {
148
+ constructor(client) {
149
+ this.client = client;
150
+ }
151
+ async list(accountId, options) {
152
+ return this.client.get(`/accounts/${accountId}/channels`, options);
153
+ }
154
+ async get(accountId, callId, options) {
155
+ return this.client.get(`/accounts/${accountId}/channels/${callId}`, options);
156
+ }
157
+ async hangup(accountId, callId, options) {
158
+ return this.client.delete(`/accounts/${accountId}/channels/${callId}`, options);
159
+ }
160
+ };
161
+
162
+ // src/resources/conferences.ts
163
+ var ConferencesResource = class {
164
+ constructor(client) {
165
+ this.client = client;
166
+ }
167
+ async list(accountId, options) {
168
+ return this.client.get(`/accounts/${accountId}/conferences`, mergePaginate(options));
169
+ }
170
+ async get(accountId, conferenceId, options) {
171
+ return this.client.get(`/accounts/${accountId}/conferences/${conferenceId}`, options);
172
+ }
173
+ async create(accountId, data, options) {
174
+ return this.client.put(`/accounts/${accountId}/conferences`, data, options);
175
+ }
176
+ async update(accountId, conferenceId, data, options) {
177
+ return this.client.post(`/accounts/${accountId}/conferences/${conferenceId}`, data, options);
178
+ }
179
+ async patch(accountId, conferenceId, data, options) {
180
+ return this.client.patch(
181
+ `/accounts/${accountId}/conferences/${conferenceId}`,
182
+ data,
183
+ options
184
+ );
185
+ }
186
+ async remove(accountId, conferenceId, options) {
187
+ return this.client.delete(`/accounts/${accountId}/conferences/${conferenceId}`, options);
188
+ }
189
+ async participants(accountId, conferenceId, options) {
190
+ return this.client.get(
191
+ `/accounts/${accountId}/conferences/${conferenceId}/participants`,
192
+ options
193
+ );
194
+ }
195
+ async muteParticipant(accountId, conferenceId, participantId, options) {
196
+ return this.client.post(
197
+ `/accounts/${accountId}/conferences/${conferenceId}/participants/${participantId}/mute`,
198
+ void 0,
199
+ options
200
+ );
201
+ }
202
+ async unmuteParticipant(accountId, conferenceId, participantId, options) {
203
+ return this.client.post(
204
+ `/accounts/${accountId}/conferences/${conferenceId}/participants/${participantId}/unmute`,
205
+ void 0,
206
+ options
207
+ );
208
+ }
209
+ async deafParticipant(accountId, conferenceId, participantId, options) {
210
+ return this.client.post(
211
+ `/accounts/${accountId}/conferences/${conferenceId}/participants/${participantId}/deaf`,
212
+ void 0,
213
+ options
214
+ );
215
+ }
216
+ async undeafParticipant(accountId, conferenceId, participantId, options) {
217
+ return this.client.post(
218
+ `/accounts/${accountId}/conferences/${conferenceId}/participants/${participantId}/undeaf`,
219
+ void 0,
220
+ options
221
+ );
222
+ }
223
+ async kickParticipant(accountId, conferenceId, participantId, options) {
224
+ return this.client.delete(
225
+ `/accounts/${accountId}/conferences/${conferenceId}/participants/${participantId}`,
226
+ options
227
+ );
228
+ }
229
+ };
230
+
231
+ // src/resources/devices.ts
232
+ var DevicesResource = class {
233
+ constructor(client) {
234
+ this.client = client;
235
+ }
236
+ async list(accountId, options) {
237
+ return this.client.get(`/accounts/${accountId}/devices`, mergePaginate(options));
238
+ }
239
+ async get(accountId, deviceId, options) {
240
+ return this.client.get(`/accounts/${accountId}/devices/${deviceId}`, options);
241
+ }
242
+ async create(accountId, data, options) {
243
+ return this.client.put(`/accounts/${accountId}/devices`, data, options);
244
+ }
245
+ async update(accountId, deviceId, data, options) {
246
+ return this.client.post(`/accounts/${accountId}/devices/${deviceId}`, data, options);
247
+ }
248
+ async patch(accountId, deviceId, data, options) {
249
+ return this.client.patch(`/accounts/${accountId}/devices/${deviceId}`, data, options);
250
+ }
251
+ async remove(accountId, deviceId, options) {
252
+ return this.client.delete(`/accounts/${accountId}/devices/${deviceId}`, options);
253
+ }
254
+ async status(accountId, options) {
255
+ return this.client.get(`/accounts/${accountId}/devices/status`, options);
256
+ }
257
+ };
258
+
259
+ // src/resources/faxes.ts
260
+ var FaxesResource = class {
261
+ constructor(client) {
262
+ this.client = client;
263
+ }
264
+ async listInbox(accountId, options) {
265
+ return this.client.get(`/accounts/${accountId}/faxes/inbox`, mergePaginate(options));
266
+ }
267
+ async listOutbox(accountId, options) {
268
+ return this.client.get(`/accounts/${accountId}/faxes/outbox`, mergePaginate(options));
269
+ }
270
+ async getInbox(accountId, faxId, options) {
271
+ return this.client.get(`/accounts/${accountId}/faxes/inbox/${faxId}`, options);
272
+ }
273
+ async getOutbox(accountId, faxId, options) {
274
+ return this.client.get(`/accounts/${accountId}/faxes/outbox/${faxId}`, options);
275
+ }
276
+ async send(accountId, data, options) {
277
+ return this.client.put(`/accounts/${accountId}/faxes`, data, options);
278
+ }
279
+ async removeInbox(accountId, faxId, options) {
280
+ return this.client.delete(`/accounts/${accountId}/faxes/inbox/${faxId}`, options);
281
+ }
282
+ async removeOutbox(accountId, faxId, options) {
283
+ return this.client.delete(`/accounts/${accountId}/faxes/outbox/${faxId}`, options);
284
+ }
285
+ };
286
+
287
+ // src/resources/media.ts
288
+ var MediaResource = class {
289
+ constructor(client) {
290
+ this.client = client;
291
+ }
292
+ async list(accountId, options) {
293
+ return this.client.get(`/accounts/${accountId}/media`, mergePaginate(options));
294
+ }
295
+ async get(accountId, mediaId, options) {
296
+ return this.client.get(`/accounts/${accountId}/media/${mediaId}`, options);
297
+ }
298
+ async create(accountId, data, options) {
299
+ return this.client.put(`/accounts/${accountId}/media`, data, options);
300
+ }
301
+ async update(accountId, mediaId, data, options) {
302
+ return this.client.post(`/accounts/${accountId}/media/${mediaId}`, data, options);
303
+ }
304
+ async patch(accountId, mediaId, data, options) {
305
+ return this.client.patch(`/accounts/${accountId}/media/${mediaId}`, data, options);
306
+ }
307
+ async remove(accountId, mediaId, options) {
308
+ return this.client.delete(`/accounts/${accountId}/media/${mediaId}`, options);
309
+ }
310
+ };
311
+
312
+ // src/resources/menus.ts
313
+ var MenusResource = class {
314
+ constructor(client) {
315
+ this.client = client;
316
+ }
317
+ async list(accountId, options) {
318
+ return this.client.get(`/accounts/${accountId}/menus`, mergePaginate(options));
319
+ }
320
+ async get(accountId, menuId, options) {
321
+ return this.client.get(`/accounts/${accountId}/menus/${menuId}`, options);
322
+ }
323
+ async create(accountId, data, options) {
324
+ return this.client.put(`/accounts/${accountId}/menus`, data, options);
325
+ }
326
+ async update(accountId, menuId, data, options) {
327
+ return this.client.post(`/accounts/${accountId}/menus/${menuId}`, data, options);
328
+ }
329
+ async patch(accountId, menuId, data, options) {
330
+ return this.client.patch(`/accounts/${accountId}/menus/${menuId}`, data, options);
331
+ }
332
+ async remove(accountId, menuId, options) {
333
+ return this.client.delete(`/accounts/${accountId}/menus/${menuId}`, options);
334
+ }
335
+ };
336
+
337
+ // src/resources/phone-numbers.ts
338
+ var PhoneNumbersResource = class {
339
+ constructor(client) {
340
+ this.client = client;
341
+ }
342
+ async list(accountId, options) {
343
+ return this.client.get(`/accounts/${accountId}/phone_numbers`, mergePaginate(options));
344
+ }
345
+ async get(accountId, number, options) {
346
+ return this.client.get(
347
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}`,
348
+ options
349
+ );
350
+ }
351
+ async create(accountId, number, data, options) {
352
+ return this.client.put(
353
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}`,
354
+ data,
355
+ options
356
+ );
357
+ }
358
+ async update(accountId, number, data, options) {
359
+ return this.client.post(
360
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}`,
361
+ data,
362
+ options
363
+ );
364
+ }
365
+ async patch(accountId, number, data, options) {
366
+ return this.client.patch(
367
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}`,
368
+ data,
369
+ options
370
+ );
371
+ }
372
+ async remove(accountId, number, options) {
373
+ return this.client.delete(
374
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}`,
375
+ options
376
+ );
377
+ }
378
+ async activate(accountId, number, data, options) {
379
+ return this.client.put(
380
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}/activate`,
381
+ data,
382
+ options
383
+ );
384
+ }
385
+ async reserve(accountId, number, data, options) {
386
+ return this.client.put(
387
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}/reserve`,
388
+ data,
389
+ options
390
+ );
391
+ }
392
+ async portIn(accountId, number, data, options) {
393
+ return this.client.put(
394
+ `/accounts/${accountId}/phone_numbers/${encodeURIComponent(number)}/port_in`,
395
+ data,
396
+ options
397
+ );
398
+ }
399
+ };
400
+
401
+ // src/resources/temporal-rules.ts
402
+ var TemporalRulesResource = class {
403
+ constructor(client) {
404
+ this.client = client;
405
+ }
406
+ async list(accountId, options) {
407
+ return this.client.get(`/accounts/${accountId}/temporal_rules`, mergePaginate(options));
408
+ }
409
+ async get(accountId, ruleId, options) {
410
+ return this.client.get(`/accounts/${accountId}/temporal_rules/${ruleId}`, options);
411
+ }
412
+ async create(accountId, data, options) {
413
+ return this.client.put(`/accounts/${accountId}/temporal_rules`, data, options);
414
+ }
415
+ async update(accountId, ruleId, data, options) {
416
+ return this.client.post(`/accounts/${accountId}/temporal_rules/${ruleId}`, data, options);
417
+ }
418
+ async patch(accountId, ruleId, data, options) {
419
+ return this.client.patch(`/accounts/${accountId}/temporal_rules/${ruleId}`, data, options);
420
+ }
421
+ async remove(accountId, ruleId, options) {
422
+ return this.client.delete(`/accounts/${accountId}/temporal_rules/${ruleId}`, options);
423
+ }
424
+ };
425
+
426
+ // src/resources/token.ts
427
+ var TokenResource = class {
428
+ constructor(client) {
429
+ this.client = client;
430
+ }
431
+ async info(options) {
432
+ return this.client.get("/token_auth", options);
433
+ }
434
+ async invalidate(options) {
435
+ return this.client.delete("/token_auth", options);
436
+ }
437
+ };
438
+
439
+ // src/resources/users.ts
440
+ var UsersResource = class {
441
+ constructor(client) {
442
+ this.client = client;
443
+ }
444
+ async list(accountId, options) {
445
+ return this.client.get(`/accounts/${accountId}/users`, mergePaginate(options));
446
+ }
447
+ async get(accountId, userId, options) {
448
+ return this.client.get(`/accounts/${accountId}/users/${userId}`, options);
449
+ }
450
+ async create(accountId, data, options) {
451
+ return this.client.put(`/accounts/${accountId}/users`, data, options);
452
+ }
453
+ async update(accountId, userId, data, options) {
454
+ return this.client.post(`/accounts/${accountId}/users/${userId}`, data, options);
455
+ }
456
+ async patch(accountId, userId, data, options) {
457
+ return this.client.patch(`/accounts/${accountId}/users/${userId}`, data, options);
458
+ }
459
+ async remove(accountId, userId, options) {
460
+ return this.client.delete(`/accounts/${accountId}/users/${userId}`, options);
461
+ }
462
+ };
463
+
464
+ // src/resources/voicemails.ts
465
+ var VoicemailsResource = class {
466
+ constructor(client) {
467
+ this.client = client;
468
+ }
469
+ async list(accountId, options) {
470
+ return this.client.get(`/accounts/${accountId}/vmboxes`, mergePaginate(options));
471
+ }
472
+ async get(accountId, vmboxId, options) {
473
+ return this.client.get(`/accounts/${accountId}/vmboxes/${vmboxId}`, options);
474
+ }
475
+ async create(accountId, data, options) {
476
+ return this.client.put(`/accounts/${accountId}/vmboxes`, data, options);
477
+ }
478
+ async update(accountId, vmboxId, data, options) {
479
+ return this.client.post(`/accounts/${accountId}/vmboxes/${vmboxId}`, data, options);
480
+ }
481
+ async patch(accountId, vmboxId, data, options) {
482
+ return this.client.patch(`/accounts/${accountId}/vmboxes/${vmboxId}`, data, options);
483
+ }
484
+ async remove(accountId, vmboxId, options) {
485
+ return this.client.delete(`/accounts/${accountId}/vmboxes/${vmboxId}`, options);
486
+ }
487
+ async messages(accountId, vmboxId, options) {
488
+ return this.client.get(
489
+ `/accounts/${accountId}/vmboxes/${vmboxId}/messages`,
490
+ options
491
+ );
492
+ }
493
+ async getMessage(accountId, vmboxId, messageId, options) {
494
+ return this.client.get(
495
+ `/accounts/${accountId}/vmboxes/${vmboxId}/messages/${messageId}`,
496
+ options
497
+ );
498
+ }
499
+ async updateMessage(accountId, vmboxId, messageId, data, options) {
500
+ return this.client.post(
501
+ `/accounts/${accountId}/vmboxes/${vmboxId}/messages/${messageId}`,
502
+ data,
503
+ options
504
+ );
505
+ }
506
+ async deleteMessage(accountId, vmboxId, messageId, options) {
507
+ return this.client.delete(
508
+ `/accounts/${accountId}/vmboxes/${vmboxId}/messages/${messageId}`,
509
+ options
510
+ );
511
+ }
512
+ };
513
+
514
+ // src/resources/webhooks.ts
515
+ var WebhooksResource = class {
516
+ constructor(client) {
517
+ this.client = client;
518
+ }
519
+ async list(accountId, options) {
520
+ return this.client.get(`/accounts/${accountId}/webhooks`, mergePaginate(options));
521
+ }
522
+ async get(accountId, webhookId, options) {
523
+ return this.client.get(`/accounts/${accountId}/webhooks/${webhookId}`, options);
524
+ }
525
+ async create(accountId, data, options) {
526
+ return this.client.put(`/accounts/${accountId}/webhooks`, data, options);
527
+ }
528
+ async update(accountId, webhookId, data, options) {
529
+ return this.client.post(`/accounts/${accountId}/webhooks/${webhookId}`, data, options);
530
+ }
531
+ async patch(accountId, webhookId, data, options) {
532
+ return this.client.patch(`/accounts/${accountId}/webhooks/${webhookId}`, data, options);
533
+ }
534
+ async remove(accountId, webhookId, options) {
535
+ return this.client.delete(`/accounts/${accountId}/webhooks/${webhookId}`, options);
536
+ }
537
+ };
538
+
539
+ // src/client.ts
540
+ var DEFAULT_VERSION = "v2";
541
+ var CrossbarClient = class {
542
+ baseUrl;
543
+ version;
544
+ defaultHeaders;
545
+ defaultTimeout;
546
+ retryConfig;
547
+ authConfig;
548
+ authToken;
549
+ _http;
550
+ _accounts;
551
+ _tokenAuth;
552
+ _users;
553
+ _devices;
554
+ _callflows;
555
+ _voicemails;
556
+ _phoneNumbers;
557
+ _cdrs;
558
+ _channels;
559
+ _conferences;
560
+ _faxes;
561
+ _media;
562
+ _menus;
563
+ _temporalRules;
564
+ _webhooks;
565
+ constructor(config) {
566
+ this.baseUrl = config.url.replace(/\/+$/, "");
567
+ this.version = config.version ?? DEFAULT_VERSION;
568
+ this.defaultHeaders = { ...config.headers };
569
+ this.defaultTimeout = config.timeout;
570
+ this.retryConfig = config.retry ?? {};
571
+ this.authConfig = config.auth;
572
+ if (config.auth?.type === "token") {
573
+ this.authToken = config.auth.token;
574
+ }
575
+ this._http = {
576
+ get: (path, options) => this.request("GET", path, void 0, options),
577
+ put: (path, data, options) => this.request("PUT", path, data, options),
578
+ post: (path, data, options) => this.request("POST", path, data, options),
579
+ patch: (path, data, options) => this.request("PATCH", path, data, options),
580
+ delete: (path, options) => this.request("DELETE", path, void 0, options)
581
+ };
582
+ }
583
+ get http() {
584
+ return this._http;
585
+ }
586
+ get accounts() {
587
+ this._accounts ??= new AccountsResource(this._http);
588
+ return this._accounts;
589
+ }
590
+ get tokenAuth() {
591
+ this._tokenAuth ??= new TokenResource(this._http);
592
+ return this._tokenAuth;
593
+ }
594
+ get users() {
595
+ this._users ??= new UsersResource(this._http);
596
+ return this._users;
597
+ }
598
+ get devices() {
599
+ this._devices ??= new DevicesResource(this._http);
600
+ return this._devices;
601
+ }
602
+ get callflows() {
603
+ this._callflows ??= new CallflowsResource(this._http);
604
+ return this._callflows;
605
+ }
606
+ get voicemails() {
607
+ this._voicemails ??= new VoicemailsResource(this._http);
608
+ return this._voicemails;
609
+ }
610
+ get phoneNumbers() {
611
+ this._phoneNumbers ??= new PhoneNumbersResource(this._http);
612
+ return this._phoneNumbers;
613
+ }
614
+ get cdrs() {
615
+ this._cdrs ??= new CdrsResource(this._http);
616
+ return this._cdrs;
617
+ }
618
+ get channels() {
619
+ this._channels ??= new ChannelsResource(this._http);
620
+ return this._channels;
621
+ }
622
+ get conferences() {
623
+ this._conferences ??= new ConferencesResource(this._http);
624
+ return this._conferences;
625
+ }
626
+ get faxes() {
627
+ this._faxes ??= new FaxesResource(this._http);
628
+ return this._faxes;
629
+ }
630
+ get media() {
631
+ this._media ??= new MediaResource(this._http);
632
+ return this._media;
633
+ }
634
+ get menus() {
635
+ this._menus ??= new MenusResource(this._http);
636
+ return this._menus;
637
+ }
638
+ get temporalRules() {
639
+ this._temporalRules ??= new TemporalRulesResource(this._http);
640
+ return this._temporalRules;
641
+ }
642
+ get webhooks() {
643
+ this._webhooks ??= new WebhooksResource(this._http);
644
+ return this._webhooks;
645
+ }
646
+ get token() {
647
+ return this.authToken;
648
+ }
649
+ set token(value) {
650
+ this.authToken = value;
651
+ }
652
+ async authenticate() {
653
+ if (!this.authConfig) {
654
+ throw new CrossbarError("No auth configuration provided", 0);
655
+ }
656
+ if (this.authConfig.type === "token") {
657
+ this.authToken = this.authConfig.token;
658
+ return {
659
+ data: {},
660
+ status: "success",
661
+ request_id: "",
662
+ auth_token: this.authToken
663
+ };
664
+ }
665
+ if (this.authConfig.type === "credentials") {
666
+ const body = {
667
+ credentials: this.authConfig.credentials
668
+ };
669
+ if (this.authConfig.accountName) {
670
+ body.account_name = this.authConfig.accountName;
671
+ }
672
+ if (this.authConfig.accountId) {
673
+ body.account_id = this.authConfig.accountId;
674
+ }
675
+ return this.executeRequest("PUT", "/user_auth", body);
676
+ }
677
+ return this.executeRequest("PUT", "/api_auth", {
678
+ api_key: this.authConfig.apiKey
679
+ });
680
+ }
681
+ async request(method, path, data, options) {
682
+ const { maxRetries = 1, reAuthOn401 = true, retryDelay = 0 } = this.retryConfig;
683
+ let serverErrorAttempts = 0;
684
+ let reAuthed = false;
685
+ for (; ; ) {
686
+ try {
687
+ return await this.executeRequest(method, path, data, options);
688
+ } catch (err) {
689
+ if (err instanceof CrossbarAuthError && reAuthOn401 && !reAuthed && this.canReAuth()) {
690
+ reAuthed = true;
691
+ await this.authenticate();
692
+ continue;
693
+ }
694
+ if (err instanceof CrossbarError && err.status >= 500 && serverErrorAttempts < maxRetries) {
695
+ serverErrorAttempts++;
696
+ if (retryDelay > 0) {
697
+ await new Promise((res) => setTimeout(res, retryDelay));
698
+ }
699
+ continue;
700
+ }
701
+ throw err;
702
+ }
703
+ }
704
+ }
705
+ canReAuth() {
706
+ return this.authConfig?.type === "credentials" || this.authConfig?.type === "api_key";
707
+ }
708
+ async executeRequest(method, path, data, options) {
709
+ const url = this.buildUrl(path, options?.query);
710
+ const headers = this.buildHeaders(options);
711
+ const fetchOptions = { method, headers };
712
+ if (data !== void 0) {
713
+ fetchOptions.body = JSON.stringify({ data });
714
+ }
715
+ const timeout = options?.timeout ?? this.defaultTimeout;
716
+ if (timeout || options?.signal) {
717
+ const signals = [];
718
+ if (timeout) signals.push(AbortSignal.timeout(timeout));
719
+ if (options?.signal) signals.push(options.signal);
720
+ fetchOptions.signal = signals.length === 1 ? signals[0] : AbortSignal.any(signals);
721
+ }
722
+ const response = await fetch(url, fetchOptions);
723
+ const body = await response.json();
724
+ if (body.auth_token) {
725
+ this.authToken = body.auth_token;
726
+ }
727
+ if (!response.ok) {
728
+ const message = body.message ?? body.error ?? `HTTP ${response.status}`;
729
+ if (response.status === 401) {
730
+ throw new CrossbarAuthError(message, body.request_id, body);
731
+ }
732
+ throw new CrossbarError(
733
+ message,
734
+ response.status,
735
+ body.request_id,
736
+ body
737
+ );
738
+ }
739
+ return {
740
+ data: body.data,
741
+ status: body.status,
742
+ request_id: body.request_id ?? "",
743
+ auth_token: body.auth_token,
744
+ page_size: body.page_size,
745
+ next_start_key: body.next_start_key,
746
+ start_key: body.start_key
747
+ };
748
+ }
749
+ buildUrl(path, query) {
750
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
751
+ let url = `${this.baseUrl}/${this.version}${normalizedPath}`;
752
+ if (query) {
753
+ const params = new URLSearchParams();
754
+ for (const [key, value] of Object.entries(query)) {
755
+ if (value !== void 0) params.set(key, String(value));
756
+ }
757
+ const qs = params.toString();
758
+ if (qs) url += `?${qs}`;
759
+ }
760
+ return url;
761
+ }
762
+ buildHeaders(options) {
763
+ const headers = {
764
+ "Content-Type": "application/json",
765
+ Accept: "application/json",
766
+ ...this.defaultHeaders,
767
+ ...options?.headers
768
+ };
769
+ if (this.authToken) {
770
+ headers["X-Auth-Token"] = this.authToken;
771
+ }
772
+ return headers;
773
+ }
774
+ };
775
+ export {
776
+ AccountsResource,
777
+ CallflowsResource,
778
+ CdrsResource,
779
+ ChannelsResource,
780
+ ConferencesResource,
781
+ CrossbarAuthError,
782
+ CrossbarClient,
783
+ CrossbarError,
784
+ DevicesResource,
785
+ FaxesResource,
786
+ MediaResource,
787
+ MenusResource,
788
+ PhoneNumbersResource,
789
+ TemporalRulesResource,
790
+ TokenResource,
791
+ UsersResource,
792
+ VoicemailsResource,
793
+ WebhooksResource,
794
+ mergePaginate
795
+ };
796
+ //# sourceMappingURL=index.js.map