pmxtjs 2.34.2 → 2.35.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.
Files changed (37) hide show
  1. package/dist/esm/generated/src/apis/DefaultApi.d.ts +35 -1
  2. package/dist/esm/generated/src/apis/DefaultApi.js +55 -1
  3. package/dist/esm/generated/src/models/FetchEventsPaginated200Response.d.ts +46 -0
  4. package/dist/esm/generated/src/models/FetchEventsPaginated200Response.js +47 -0
  5. package/dist/esm/generated/src/models/PaginatedEventsResult.d.ts +45 -0
  6. package/dist/esm/generated/src/models/PaginatedEventsResult.js +50 -0
  7. package/dist/esm/generated/src/models/index.d.ts +2 -0
  8. package/dist/esm/generated/src/models/index.js +2 -0
  9. package/dist/esm/pmxt/client.d.ts +24 -1
  10. package/dist/esm/pmxt/client.js +418 -88
  11. package/dist/esm/pmxt/models.d.ts +11 -0
  12. package/dist/esm/pmxt/server-manager.js +16 -2
  13. package/dist/generated/src/apis/DefaultApi.d.ts +35 -1
  14. package/dist/generated/src/apis/DefaultApi.js +56 -2
  15. package/dist/generated/src/models/FetchEventsPaginated200Response.d.ts +46 -0
  16. package/dist/generated/src/models/FetchEventsPaginated200Response.js +54 -0
  17. package/dist/generated/src/models/PaginatedEventsResult.d.ts +45 -0
  18. package/dist/generated/src/models/PaginatedEventsResult.js +57 -0
  19. package/dist/generated/src/models/index.d.ts +2 -0
  20. package/dist/generated/src/models/index.js +2 -0
  21. package/dist/pmxt/client.d.ts +24 -1
  22. package/dist/pmxt/client.js +418 -88
  23. package/dist/pmxt/models.d.ts +11 -0
  24. package/dist/pmxt/server-manager.js +16 -2
  25. package/generated/.openapi-generator/FILES +4 -0
  26. package/generated/docs/DefaultApi.md +77 -0
  27. package/generated/docs/FetchEventsPaginated200Response.md +38 -0
  28. package/generated/docs/PaginatedEventsResult.md +39 -0
  29. package/generated/package.json +1 -1
  30. package/generated/src/apis/DefaultApi.ts +79 -0
  31. package/generated/src/models/FetchEventsPaginated200Response.ts +96 -0
  32. package/generated/src/models/PaginatedEventsResult.ts +91 -0
  33. package/generated/src/models/index.ts +2 -0
  34. package/package.json +2 -2
  35. package/pmxt/client.ts +394 -88
  36. package/pmxt/models.ts +14 -0
  37. package/pmxt/server-manager.ts +15 -2
@@ -335,6 +335,51 @@ export class Exchange {
335
335
  }
336
336
  return headers;
337
337
  }
338
+ /**
339
+ * Resolve the current sidecar base URL.
340
+ *
341
+ * For hosted mode the configured basePath is returned as-is.
342
+ * For local mode the port is re-read from the lock file on every
343
+ * call so we pick up sidecar restarts that land on a different port.
344
+ */
345
+ resolveBaseUrl() {
346
+ if (this.isHosted)
347
+ return this.config.basePath;
348
+ const port = this.serverManager.getRunningPort();
349
+ return `http://localhost:${port}`;
350
+ }
351
+ /**
352
+ * Execute a fetch with retry on connection failures.
353
+ *
354
+ * Only retries on connection-level errors (ECONNREFUSED, ECONNRESET) —
355
+ * never on HTTP responses (4xx, 5xx). On first connection failure,
356
+ * attempts to restart the sidecar.
357
+ */
358
+ async fetchWithRetry(input, init) {
359
+ const delays = [200, 500, 1000];
360
+ let lastError;
361
+ for (let attempt = 0; attempt <= delays.length; attempt++) {
362
+ try {
363
+ return await fetch(input, init);
364
+ }
365
+ catch (error) {
366
+ lastError = error;
367
+ if (attempt >= delays.length)
368
+ break;
369
+ // Connection failed — try to restart the sidecar on first failure
370
+ if (attempt === 0 && !this.isHosted) {
371
+ try {
372
+ await this.serverManager.ensureServerRunning();
373
+ }
374
+ catch {
375
+ // Restart failed — continue retrying anyway
376
+ }
377
+ }
378
+ await new Promise(resolve => setTimeout(resolve, delays[attempt]));
379
+ }
380
+ }
381
+ throw lastError;
382
+ }
338
383
  // Low-Level API Access
339
384
  /**
340
385
  * Call an exchange-specific REST endpoint by its operationId.
@@ -354,12 +399,12 @@ export class Exchange {
354
399
  async callApi(operationId, params) {
355
400
  await this.initPromise;
356
401
  try {
357
- const url = `${this.config.basePath}/api/${this.exchangeName}/callApi`;
402
+ const url = `${this.resolveBaseUrl()}/api/${this.exchangeName}/callApi`;
358
403
  const requestBody = {
359
404
  args: [operationId, params],
360
405
  credentials: this.getCredentials()
361
406
  };
362
- const response = await fetch(url, {
407
+ const response = await this.fetchWithRetry(url, {
363
408
  method: 'POST',
364
409
  headers: {
365
410
  'Content-Type': 'application/json',
@@ -403,12 +448,13 @@ export class Exchange {
403
448
  * @internal — shared transport used by every generated read method.
404
449
  */
405
450
  async sidecarReadRequest(methodName, query, args) {
406
- const baseUrl = `${this.config.basePath}/api/${this.exchangeName}/${methodName}`;
451
+ const resolvedBase = this.resolveBaseUrl();
452
+ const baseUrl = `${resolvedBase}/api/${this.exchangeName}/${methodName}`;
407
453
  const hasCredentials = this.getCredentials() !== undefined;
408
454
  if (!hasCredentials && !this._getReadsUnsupported && !queryHasNestedObject(query)) {
409
455
  const qs = buildSidecarQueryString(query);
410
456
  const getUrl = qs ? `${baseUrl}?${qs}` : baseUrl;
411
- const response = await fetch(getUrl, {
457
+ const response = await this.fetchWithRetry(getUrl, {
412
458
  method: 'GET',
413
459
  headers: this.getAuthHeaders(),
414
460
  });
@@ -431,7 +477,7 @@ export class Exchange {
431
477
  }
432
478
  }
433
479
  // POST fallback — identical to the original per-method template.
434
- const response = await fetch(baseUrl, {
480
+ const response = await this.fetchWithRetry(baseUrl, {
435
481
  method: 'POST',
436
482
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
437
483
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
@@ -451,14 +497,17 @@ export class Exchange {
451
497
  try {
452
498
  const args = [];
453
499
  args.push(reload);
454
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/loadMarkets`, {
500
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/loadMarkets`, {
455
501
  method: 'POST',
456
502
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
457
503
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
458
504
  });
459
505
  if (!response.ok) {
460
- const error = await response.json().catch(() => ({}));
461
- throw new Error(error.error?.message || response.statusText);
506
+ const body = await response.json().catch(() => ({}));
507
+ if (body.error && typeof body.error === "object") {
508
+ throw fromServerError(body.error);
509
+ }
510
+ throw new PmxtError(body.error?.message || response.statusText);
462
511
  }
463
512
  const json = await response.json();
464
513
  const data = this.handleResponse(json);
@@ -469,7 +518,9 @@ export class Exchange {
469
518
  return result;
470
519
  }
471
520
  catch (error) {
472
- throw new Error(`Failed to loadMarkets: ${error}`);
521
+ if (error instanceof PmxtError)
522
+ throw error;
523
+ throw new PmxtError(`Failed to loadMarkets: ${error}`);
473
524
  }
474
525
  }
475
526
  async fetchMarkets(params) {
@@ -478,21 +529,26 @@ export class Exchange {
478
529
  const args = [];
479
530
  if (params !== undefined)
480
531
  args.push(params);
481
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMarkets`, {
532
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchMarkets`, {
482
533
  method: 'POST',
483
534
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
484
535
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
485
536
  });
486
537
  if (!response.ok) {
487
- const error = await response.json().catch(() => ({}));
488
- throw new Error(error.error?.message || response.statusText);
538
+ const body = await response.json().catch(() => ({}));
539
+ if (body.error && typeof body.error === "object") {
540
+ throw fromServerError(body.error);
541
+ }
542
+ throw new PmxtError(body.error?.message || response.statusText);
489
543
  }
490
544
  const json = await response.json();
491
545
  const data = this.handleResponse(json);
492
546
  return data.map(convertMarket);
493
547
  }
494
548
  catch (error) {
495
- throw new Error(`Failed to fetchMarkets: ${error}`);
549
+ if (error instanceof PmxtError)
550
+ throw error;
551
+ throw new PmxtError(`Failed to fetchMarkets: ${error}`);
496
552
  }
497
553
  }
498
554
  async fetchMarketsPaginated(params) {
@@ -501,14 +557,17 @@ export class Exchange {
501
557
  const args = [];
502
558
  if (params !== undefined)
503
559
  args.push(params);
504
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMarketsPaginated`, {
560
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchMarketsPaginated`, {
505
561
  method: 'POST',
506
562
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
507
563
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
508
564
  });
509
565
  if (!response.ok) {
510
- const error = await response.json().catch(() => ({}));
511
- throw new Error(error.error?.message || response.statusText);
566
+ const body = await response.json().catch(() => ({}));
567
+ if (body.error && typeof body.error === "object") {
568
+ throw fromServerError(body.error);
569
+ }
570
+ throw new PmxtError(body.error?.message || response.statusText);
512
571
  }
513
572
  const json = await response.json();
514
573
  const data = this.handleResponse(json);
@@ -519,7 +578,9 @@ export class Exchange {
519
578
  };
520
579
  }
521
580
  catch (error) {
522
- throw new Error(`Failed to fetchMarketsPaginated: ${error}`);
581
+ if (error instanceof PmxtError)
582
+ throw error;
583
+ throw new PmxtError(`Failed to fetchMarketsPaginated: ${error}`);
523
584
  }
524
585
  }
525
586
  async fetchEvents(params) {
@@ -528,21 +589,58 @@ export class Exchange {
528
589
  const args = [];
529
590
  if (params !== undefined)
530
591
  args.push(params);
531
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchEvents`, {
592
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchEvents`, {
532
593
  method: 'POST',
533
594
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
534
595
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
535
596
  });
536
597
  if (!response.ok) {
537
- const error = await response.json().catch(() => ({}));
538
- throw new Error(error.error?.message || response.statusText);
598
+ const body = await response.json().catch(() => ({}));
599
+ if (body.error && typeof body.error === "object") {
600
+ throw fromServerError(body.error);
601
+ }
602
+ throw new PmxtError(body.error?.message || response.statusText);
539
603
  }
540
604
  const json = await response.json();
541
605
  const data = this.handleResponse(json);
542
606
  return data.map(convertEvent);
543
607
  }
544
608
  catch (error) {
545
- throw new Error(`Failed to fetchEvents: ${error}`);
609
+ if (error instanceof PmxtError)
610
+ throw error;
611
+ throw new PmxtError(`Failed to fetchEvents: ${error}`);
612
+ }
613
+ }
614
+ async fetchEventsPaginated(params) {
615
+ await this.initPromise;
616
+ try {
617
+ const args = [];
618
+ if (params !== undefined)
619
+ args.push(params);
620
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchEventsPaginated`, {
621
+ method: 'POST',
622
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
623
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
624
+ });
625
+ if (!response.ok) {
626
+ const body = await response.json().catch(() => ({}));
627
+ if (body.error && typeof body.error === "object") {
628
+ throw fromServerError(body.error);
629
+ }
630
+ throw new PmxtError(body.error?.message || response.statusText);
631
+ }
632
+ const json = await response.json();
633
+ const data = this.handleResponse(json);
634
+ return {
635
+ data: (data.data || []).map(convertEvent),
636
+ total: data.total,
637
+ nextCursor: data.nextCursor,
638
+ };
639
+ }
640
+ catch (error) {
641
+ if (error instanceof PmxtError)
642
+ throw error;
643
+ throw new PmxtError(`Failed to fetchEventsPaginated: ${error}`);
546
644
  }
547
645
  }
548
646
  async fetchMarket(params) {
@@ -551,21 +649,26 @@ export class Exchange {
551
649
  const args = [];
552
650
  if (params !== undefined)
553
651
  args.push(params);
554
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMarket`, {
652
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchMarket`, {
555
653
  method: 'POST',
556
654
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
557
655
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
558
656
  });
559
657
  if (!response.ok) {
560
- const error = await response.json().catch(() => ({}));
561
- throw new Error(error.error?.message || response.statusText);
658
+ const body = await response.json().catch(() => ({}));
659
+ if (body.error && typeof body.error === "object") {
660
+ throw fromServerError(body.error);
661
+ }
662
+ throw new PmxtError(body.error?.message || response.statusText);
562
663
  }
563
664
  const json = await response.json();
564
665
  const data = this.handleResponse(json);
565
666
  return convertMarket(data);
566
667
  }
567
668
  catch (error) {
568
- throw new Error(`Failed to fetchMarket: ${error}`);
669
+ if (error instanceof PmxtError)
670
+ throw error;
671
+ throw new PmxtError(`Failed to fetchMarket: ${error}`);
569
672
  }
570
673
  }
571
674
  async fetchEvent(params) {
@@ -574,21 +677,26 @@ export class Exchange {
574
677
  const args = [];
575
678
  if (params !== undefined)
576
679
  args.push(params);
577
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchEvent`, {
680
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchEvent`, {
578
681
  method: 'POST',
579
682
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
580
683
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
581
684
  });
582
685
  if (!response.ok) {
583
- const error = await response.json().catch(() => ({}));
584
- throw new Error(error.error?.message || response.statusText);
686
+ const body = await response.json().catch(() => ({}));
687
+ if (body.error && typeof body.error === "object") {
688
+ throw fromServerError(body.error);
689
+ }
690
+ throw new PmxtError(body.error?.message || response.statusText);
585
691
  }
586
692
  const json = await response.json();
587
693
  const data = this.handleResponse(json);
588
694
  return convertEvent(data);
589
695
  }
590
696
  catch (error) {
591
- throw new Error(`Failed to fetchEvent: ${error}`);
697
+ if (error instanceof PmxtError)
698
+ throw error;
699
+ throw new PmxtError(`Failed to fetchEvent: ${error}`);
592
700
  }
593
701
  }
594
702
  async fetchOrderBook(id) {
@@ -596,21 +704,26 @@ export class Exchange {
596
704
  try {
597
705
  const args = [];
598
706
  args.push(id);
599
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchOrderBook`, {
707
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchOrderBook`, {
600
708
  method: 'POST',
601
709
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
602
710
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
603
711
  });
604
712
  if (!response.ok) {
605
- const error = await response.json().catch(() => ({}));
606
- throw new Error(error.error?.message || response.statusText);
713
+ const body = await response.json().catch(() => ({}));
714
+ if (body.error && typeof body.error === "object") {
715
+ throw fromServerError(body.error);
716
+ }
717
+ throw new PmxtError(body.error?.message || response.statusText);
607
718
  }
608
719
  const json = await response.json();
609
720
  const data = this.handleResponse(json);
610
721
  return convertOrderBook(data);
611
722
  }
612
723
  catch (error) {
613
- throw new Error(`Failed to fetchOrderBook: ${error}`);
724
+ if (error instanceof PmxtError)
725
+ throw error;
726
+ throw new PmxtError(`Failed to fetchOrderBook: ${error}`);
614
727
  }
615
728
  }
616
729
  async submitOrder(built) {
@@ -618,21 +731,26 @@ export class Exchange {
618
731
  try {
619
732
  const args = [];
620
733
  args.push(built);
621
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/submitOrder`, {
734
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/submitOrder`, {
622
735
  method: 'POST',
623
736
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
624
737
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
625
738
  });
626
739
  if (!response.ok) {
627
- const error = await response.json().catch(() => ({}));
628
- throw new Error(error.error?.message || response.statusText);
740
+ const body = await response.json().catch(() => ({}));
741
+ if (body.error && typeof body.error === "object") {
742
+ throw fromServerError(body.error);
743
+ }
744
+ throw new PmxtError(body.error?.message || response.statusText);
629
745
  }
630
746
  const json = await response.json();
631
747
  const data = this.handleResponse(json);
632
748
  return convertOrder(data);
633
749
  }
634
750
  catch (error) {
635
- throw new Error(`Failed to submitOrder: ${error}`);
751
+ if (error instanceof PmxtError)
752
+ throw error;
753
+ throw new PmxtError(`Failed to submitOrder: ${error}`);
636
754
  }
637
755
  }
638
756
  async cancelOrder(orderId) {
@@ -640,21 +758,26 @@ export class Exchange {
640
758
  try {
641
759
  const args = [];
642
760
  args.push(orderId);
643
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/cancelOrder`, {
761
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/cancelOrder`, {
644
762
  method: 'POST',
645
763
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
646
764
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
647
765
  });
648
766
  if (!response.ok) {
649
- const error = await response.json().catch(() => ({}));
650
- throw new Error(error.error?.message || response.statusText);
767
+ const body = await response.json().catch(() => ({}));
768
+ if (body.error && typeof body.error === "object") {
769
+ throw fromServerError(body.error);
770
+ }
771
+ throw new PmxtError(body.error?.message || response.statusText);
651
772
  }
652
773
  const json = await response.json();
653
774
  const data = this.handleResponse(json);
654
775
  return convertOrder(data);
655
776
  }
656
777
  catch (error) {
657
- throw new Error(`Failed to cancelOrder: ${error}`);
778
+ if (error instanceof PmxtError)
779
+ throw error;
780
+ throw new PmxtError(`Failed to cancelOrder: ${error}`);
658
781
  }
659
782
  }
660
783
  async fetchOrder(orderId) {
@@ -662,21 +785,26 @@ export class Exchange {
662
785
  try {
663
786
  const args = [];
664
787
  args.push(orderId);
665
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchOrder`, {
788
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchOrder`, {
666
789
  method: 'POST',
667
790
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
668
791
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
669
792
  });
670
793
  if (!response.ok) {
671
- const error = await response.json().catch(() => ({}));
672
- throw new Error(error.error?.message || response.statusText);
794
+ const body = await response.json().catch(() => ({}));
795
+ if (body.error && typeof body.error === "object") {
796
+ throw fromServerError(body.error);
797
+ }
798
+ throw new PmxtError(body.error?.message || response.statusText);
673
799
  }
674
800
  const json = await response.json();
675
801
  const data = this.handleResponse(json);
676
802
  return convertOrder(data);
677
803
  }
678
804
  catch (error) {
679
- throw new Error(`Failed to fetchOrder: ${error}`);
805
+ if (error instanceof PmxtError)
806
+ throw error;
807
+ throw new PmxtError(`Failed to fetchOrder: ${error}`);
680
808
  }
681
809
  }
682
810
  async fetchOpenOrders(marketId) {
@@ -685,21 +813,26 @@ export class Exchange {
685
813
  const args = [];
686
814
  if (marketId !== undefined)
687
815
  args.push(marketId);
688
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchOpenOrders`, {
816
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchOpenOrders`, {
689
817
  method: 'POST',
690
818
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
691
819
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
692
820
  });
693
821
  if (!response.ok) {
694
- const error = await response.json().catch(() => ({}));
695
- throw new Error(error.error?.message || response.statusText);
822
+ const body = await response.json().catch(() => ({}));
823
+ if (body.error && typeof body.error === "object") {
824
+ throw fromServerError(body.error);
825
+ }
826
+ throw new PmxtError(body.error?.message || response.statusText);
696
827
  }
697
828
  const json = await response.json();
698
829
  const data = this.handleResponse(json);
699
830
  return data.map(convertOrder);
700
831
  }
701
832
  catch (error) {
702
- throw new Error(`Failed to fetchOpenOrders: ${error}`);
833
+ if (error instanceof PmxtError)
834
+ throw error;
835
+ throw new PmxtError(`Failed to fetchOpenOrders: ${error}`);
703
836
  }
704
837
  }
705
838
  async fetchMyTrades(params) {
@@ -708,21 +841,26 @@ export class Exchange {
708
841
  const args = [];
709
842
  if (params !== undefined)
710
843
  args.push(params);
711
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMyTrades`, {
844
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchMyTrades`, {
712
845
  method: 'POST',
713
846
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
714
847
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
715
848
  });
716
849
  if (!response.ok) {
717
- const error = await response.json().catch(() => ({}));
718
- throw new Error(error.error?.message || response.statusText);
850
+ const body = await response.json().catch(() => ({}));
851
+ if (body.error && typeof body.error === "object") {
852
+ throw fromServerError(body.error);
853
+ }
854
+ throw new PmxtError(body.error?.message || response.statusText);
719
855
  }
720
856
  const json = await response.json();
721
857
  const data = this.handleResponse(json);
722
858
  return data.map(convertUserTrade);
723
859
  }
724
860
  catch (error) {
725
- throw new Error(`Failed to fetchMyTrades: ${error}`);
861
+ if (error instanceof PmxtError)
862
+ throw error;
863
+ throw new PmxtError(`Failed to fetchMyTrades: ${error}`);
726
864
  }
727
865
  }
728
866
  async fetchClosedOrders(params) {
@@ -731,21 +869,26 @@ export class Exchange {
731
869
  const args = [];
732
870
  if (params !== undefined)
733
871
  args.push(params);
734
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchClosedOrders`, {
872
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchClosedOrders`, {
735
873
  method: 'POST',
736
874
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
737
875
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
738
876
  });
739
877
  if (!response.ok) {
740
- const error = await response.json().catch(() => ({}));
741
- throw new Error(error.error?.message || response.statusText);
878
+ const body = await response.json().catch(() => ({}));
879
+ if (body.error && typeof body.error === "object") {
880
+ throw fromServerError(body.error);
881
+ }
882
+ throw new PmxtError(body.error?.message || response.statusText);
742
883
  }
743
884
  const json = await response.json();
744
885
  const data = this.handleResponse(json);
745
886
  return data.map(convertOrder);
746
887
  }
747
888
  catch (error) {
748
- throw new Error(`Failed to fetchClosedOrders: ${error}`);
889
+ if (error instanceof PmxtError)
890
+ throw error;
891
+ throw new PmxtError(`Failed to fetchClosedOrders: ${error}`);
749
892
  }
750
893
  }
751
894
  async fetchAllOrders(params) {
@@ -754,21 +897,26 @@ export class Exchange {
754
897
  const args = [];
755
898
  if (params !== undefined)
756
899
  args.push(params);
757
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchAllOrders`, {
900
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchAllOrders`, {
758
901
  method: 'POST',
759
902
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
760
903
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
761
904
  });
762
905
  if (!response.ok) {
763
- const error = await response.json().catch(() => ({}));
764
- throw new Error(error.error?.message || response.statusText);
906
+ const body = await response.json().catch(() => ({}));
907
+ if (body.error && typeof body.error === "object") {
908
+ throw fromServerError(body.error);
909
+ }
910
+ throw new PmxtError(body.error?.message || response.statusText);
765
911
  }
766
912
  const json = await response.json();
767
913
  const data = this.handleResponse(json);
768
914
  return data.map(convertOrder);
769
915
  }
770
916
  catch (error) {
771
- throw new Error(`Failed to fetchAllOrders: ${error}`);
917
+ if (error instanceof PmxtError)
918
+ throw error;
919
+ throw new PmxtError(`Failed to fetchAllOrders: ${error}`);
772
920
  }
773
921
  }
774
922
  async fetchPositions(address) {
@@ -777,21 +925,26 @@ export class Exchange {
777
925
  const args = [];
778
926
  if (address !== undefined)
779
927
  args.push(address);
780
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchPositions`, {
928
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchPositions`, {
781
929
  method: 'POST',
782
930
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
783
931
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
784
932
  });
785
933
  if (!response.ok) {
786
- const error = await response.json().catch(() => ({}));
787
- throw new Error(error.error?.message || response.statusText);
934
+ const body = await response.json().catch(() => ({}));
935
+ if (body.error && typeof body.error === "object") {
936
+ throw fromServerError(body.error);
937
+ }
938
+ throw new PmxtError(body.error?.message || response.statusText);
788
939
  }
789
940
  const json = await response.json();
790
941
  const data = this.handleResponse(json);
791
942
  return data.map(convertPosition);
792
943
  }
793
944
  catch (error) {
794
- throw new Error(`Failed to fetchPositions: ${error}`);
945
+ if (error instanceof PmxtError)
946
+ throw error;
947
+ throw new PmxtError(`Failed to fetchPositions: ${error}`);
795
948
  }
796
949
  }
797
950
  async fetchBalance(address) {
@@ -800,21 +953,26 @@ export class Exchange {
800
953
  const args = [];
801
954
  if (address !== undefined)
802
955
  args.push(address);
803
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchBalance`, {
956
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchBalance`, {
804
957
  method: 'POST',
805
958
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
806
959
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
807
960
  });
808
961
  if (!response.ok) {
809
- const error = await response.json().catch(() => ({}));
810
- throw new Error(error.error?.message || response.statusText);
962
+ const body = await response.json().catch(() => ({}));
963
+ if (body.error && typeof body.error === "object") {
964
+ throw fromServerError(body.error);
965
+ }
966
+ throw new PmxtError(body.error?.message || response.statusText);
811
967
  }
812
968
  const json = await response.json();
813
969
  const data = this.handleResponse(json);
814
970
  return data.map(convertBalance);
815
971
  }
816
972
  catch (error) {
817
- throw new Error(`Failed to fetchBalance: ${error}`);
973
+ if (error instanceof PmxtError)
974
+ throw error;
975
+ throw new PmxtError(`Failed to fetchBalance: ${error}`);
818
976
  }
819
977
  }
820
978
  async unwatchOrderBook(id) {
@@ -822,20 +980,25 @@ export class Exchange {
822
980
  try {
823
981
  const args = [];
824
982
  args.push(id);
825
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/unwatchOrderBook`, {
983
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/unwatchOrderBook`, {
826
984
  method: 'POST',
827
985
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
828
986
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
829
987
  });
830
988
  if (!response.ok) {
831
- const error = await response.json().catch(() => ({}));
832
- throw new Error(error.error?.message || response.statusText);
989
+ const body = await response.json().catch(() => ({}));
990
+ if (body.error && typeof body.error === "object") {
991
+ throw fromServerError(body.error);
992
+ }
993
+ throw new PmxtError(body.error?.message || response.statusText);
833
994
  }
834
995
  const json = await response.json();
835
996
  this.handleResponse(json);
836
997
  }
837
998
  catch (error) {
838
- throw new Error(`Failed to unwatchOrderBook: ${error}`);
999
+ if (error instanceof PmxtError)
1000
+ throw error;
1001
+ throw new PmxtError(`Failed to unwatchOrderBook: ${error}`);
839
1002
  }
840
1003
  }
841
1004
  async unwatchAddress(address) {
@@ -843,40 +1006,207 @@ export class Exchange {
843
1006
  try {
844
1007
  const args = [];
845
1008
  args.push(address);
846
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/unwatchAddress`, {
1009
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/unwatchAddress`, {
847
1010
  method: 'POST',
848
1011
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
849
1012
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
850
1013
  });
851
1014
  if (!response.ok) {
852
- const error = await response.json().catch(() => ({}));
853
- throw new Error(error.error?.message || response.statusText);
1015
+ const body = await response.json().catch(() => ({}));
1016
+ if (body.error && typeof body.error === "object") {
1017
+ throw fromServerError(body.error);
1018
+ }
1019
+ throw new PmxtError(body.error?.message || response.statusText);
854
1020
  }
855
1021
  const json = await response.json();
856
1022
  this.handleResponse(json);
857
1023
  }
858
1024
  catch (error) {
859
- throw new Error(`Failed to unwatchAddress: ${error}`);
1025
+ if (error instanceof PmxtError)
1026
+ throw error;
1027
+ throw new PmxtError(`Failed to unwatchAddress: ${error}`);
860
1028
  }
861
1029
  }
862
1030
  async close() {
863
1031
  await this.initPromise;
864
1032
  try {
865
1033
  const args = [];
866
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/close`, {
1034
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/close`, {
867
1035
  method: 'POST',
868
1036
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
869
1037
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
870
1038
  });
871
1039
  if (!response.ok) {
872
- const error = await response.json().catch(() => ({}));
873
- throw new Error(error.error?.message || response.statusText);
1040
+ const body = await response.json().catch(() => ({}));
1041
+ if (body.error && typeof body.error === "object") {
1042
+ throw fromServerError(body.error);
1043
+ }
1044
+ throw new PmxtError(body.error?.message || response.statusText);
874
1045
  }
875
1046
  const json = await response.json();
876
1047
  this.handleResponse(json);
877
1048
  }
878
1049
  catch (error) {
879
- throw new Error(`Failed to close: ${error}`);
1050
+ if (error instanceof PmxtError)
1051
+ throw error;
1052
+ throw new PmxtError(`Failed to close: ${error}`);
1053
+ }
1054
+ }
1055
+ async fetchMarketMatches(params) {
1056
+ await this.initPromise;
1057
+ try {
1058
+ const args = [];
1059
+ args.push(params);
1060
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchMarketMatches`, {
1061
+ method: 'POST',
1062
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1063
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
1064
+ });
1065
+ if (!response.ok) {
1066
+ const body = await response.json().catch(() => ({}));
1067
+ if (body.error && typeof body.error === "object") {
1068
+ throw fromServerError(body.error);
1069
+ }
1070
+ throw new PmxtError(body.error?.message || response.statusText);
1071
+ }
1072
+ const json = await response.json();
1073
+ return this.handleResponse(json);
1074
+ }
1075
+ catch (error) {
1076
+ if (error instanceof PmxtError)
1077
+ throw error;
1078
+ throw new PmxtError(`Failed to fetchMarketMatches: ${error}`);
1079
+ }
1080
+ }
1081
+ async fetchMatches(params) {
1082
+ await this.initPromise;
1083
+ try {
1084
+ const args = [];
1085
+ args.push(params);
1086
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchMatches`, {
1087
+ method: 'POST',
1088
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1089
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
1090
+ });
1091
+ if (!response.ok) {
1092
+ const body = await response.json().catch(() => ({}));
1093
+ if (body.error && typeof body.error === "object") {
1094
+ throw fromServerError(body.error);
1095
+ }
1096
+ throw new PmxtError(body.error?.message || response.statusText);
1097
+ }
1098
+ const json = await response.json();
1099
+ return this.handleResponse(json);
1100
+ }
1101
+ catch (error) {
1102
+ if (error instanceof PmxtError)
1103
+ throw error;
1104
+ throw new PmxtError(`Failed to fetchMatches: ${error}`);
1105
+ }
1106
+ }
1107
+ async fetchEventMatches(params) {
1108
+ await this.initPromise;
1109
+ try {
1110
+ const args = [];
1111
+ args.push(params);
1112
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchEventMatches`, {
1113
+ method: 'POST',
1114
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1115
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
1116
+ });
1117
+ if (!response.ok) {
1118
+ const body = await response.json().catch(() => ({}));
1119
+ if (body.error && typeof body.error === "object") {
1120
+ throw fromServerError(body.error);
1121
+ }
1122
+ throw new PmxtError(body.error?.message || response.statusText);
1123
+ }
1124
+ const json = await response.json();
1125
+ return this.handleResponse(json);
1126
+ }
1127
+ catch (error) {
1128
+ if (error instanceof PmxtError)
1129
+ throw error;
1130
+ throw new PmxtError(`Failed to fetchEventMatches: ${error}`);
1131
+ }
1132
+ }
1133
+ async compareMarketPrices(params) {
1134
+ await this.initPromise;
1135
+ try {
1136
+ const args = [];
1137
+ args.push(params);
1138
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/compareMarketPrices`, {
1139
+ method: 'POST',
1140
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1141
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
1142
+ });
1143
+ if (!response.ok) {
1144
+ const body = await response.json().catch(() => ({}));
1145
+ if (body.error && typeof body.error === "object") {
1146
+ throw fromServerError(body.error);
1147
+ }
1148
+ throw new PmxtError(body.error?.message || response.statusText);
1149
+ }
1150
+ const json = await response.json();
1151
+ return this.handleResponse(json);
1152
+ }
1153
+ catch (error) {
1154
+ if (error instanceof PmxtError)
1155
+ throw error;
1156
+ throw new PmxtError(`Failed to compareMarketPrices: ${error}`);
1157
+ }
1158
+ }
1159
+ async fetchHedges(params) {
1160
+ await this.initPromise;
1161
+ try {
1162
+ const args = [];
1163
+ args.push(params);
1164
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchHedges`, {
1165
+ method: 'POST',
1166
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1167
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
1168
+ });
1169
+ if (!response.ok) {
1170
+ const body = await response.json().catch(() => ({}));
1171
+ if (body.error && typeof body.error === "object") {
1172
+ throw fromServerError(body.error);
1173
+ }
1174
+ throw new PmxtError(body.error?.message || response.statusText);
1175
+ }
1176
+ const json = await response.json();
1177
+ return this.handleResponse(json);
1178
+ }
1179
+ catch (error) {
1180
+ if (error instanceof PmxtError)
1181
+ throw error;
1182
+ throw new PmxtError(`Failed to fetchHedges: ${error}`);
1183
+ }
1184
+ }
1185
+ async fetchArbitrage(params) {
1186
+ await this.initPromise;
1187
+ try {
1188
+ const args = [];
1189
+ if (params !== undefined)
1190
+ args.push(params);
1191
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchArbitrage`, {
1192
+ method: 'POST',
1193
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1194
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
1195
+ });
1196
+ if (!response.ok) {
1197
+ const body = await response.json().catch(() => ({}));
1198
+ if (body.error && typeof body.error === "object") {
1199
+ throw fromServerError(body.error);
1200
+ }
1201
+ throw new PmxtError(body.error?.message || response.statusText);
1202
+ }
1203
+ const json = await response.json();
1204
+ return this.handleResponse(json);
1205
+ }
1206
+ catch (error) {
1207
+ if (error instanceof PmxtError)
1208
+ throw error;
1209
+ throw new PmxtError(`Failed to fetchArbitrage: ${error}`);
880
1210
  }
881
1211
  }
882
1212
  // END GENERATED METHODS
@@ -981,7 +1311,7 @@ export class Exchange {
981
1311
  if (limit !== undefined) {
982
1312
  args.push(limit);
983
1313
  }
984
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/watchOrderBook`, {
1314
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/watchOrderBook`, {
985
1315
  method: 'POST',
986
1316
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
987
1317
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
@@ -1040,7 +1370,7 @@ export class Exchange {
1040
1370
  if (limit !== undefined) {
1041
1371
  args.push(limit);
1042
1372
  }
1043
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/watchTrades`, {
1373
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/watchTrades`, {
1044
1374
  method: 'POST',
1045
1375
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1046
1376
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
@@ -1090,7 +1420,7 @@ export class Exchange {
1090
1420
  if (types !== undefined) {
1091
1421
  args.push(types);
1092
1422
  }
1093
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/watchAddress`, {
1423
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/watchAddress`, {
1094
1424
  method: 'POST',
1095
1425
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1096
1426
  body: JSON.stringify({ args, credentials: this.getCredentials() }),
@@ -1178,7 +1508,7 @@ export class Exchange {
1178
1508
  if (params.fee !== undefined) {
1179
1509
  paramsDict.fee = params.fee;
1180
1510
  }
1181
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/buildOrder`, {
1511
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/buildOrder`, {
1182
1512
  method: 'POST',
1183
1513
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1184
1514
  body: JSON.stringify({ args: [paramsDict], credentials: this.getCredentials() }),
@@ -1248,7 +1578,7 @@ export class Exchange {
1248
1578
  if (params.fee !== undefined) {
1249
1579
  paramsDict.fee = params.fee;
1250
1580
  }
1251
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/createOrder`, {
1581
+ const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/createOrder`, {
1252
1582
  method: 'POST',
1253
1583
  headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
1254
1584
  body: JSON.stringify({ args: [paramsDict], credentials: this.getCredentials() }),
@@ -1302,8 +1632,8 @@ export class Exchange {
1302
1632
  if (credentials) {
1303
1633
  body.credentials = credentials;
1304
1634
  }
1305
- const url = `${this.config.basePath}/api/${this.exchangeName}/getExecutionPriceDetailed`;
1306
- const response = await fetch(url, {
1635
+ const url = `${this.resolveBaseUrl()}/api/${this.exchangeName}/getExecutionPriceDetailed`;
1636
+ const response = await this.fetchWithRetry(url, {
1307
1637
  method: 'POST',
1308
1638
  headers: {
1309
1639
  'Content-Type': 'application/json',