pmxtjs 2.26.1 → 2.27.1

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 (138) hide show
  1. package/dist/esm/generated/src/apis/DefaultApi.d.ts +266 -106
  2. package/dist/esm/generated/src/apis/DefaultApi.js +346 -61
  3. package/dist/esm/generated/src/models/index.d.ts +0 -19
  4. package/dist/esm/generated/src/models/index.js +0 -19
  5. package/dist/esm/pmxt/client.d.ts +59 -4
  6. package/dist/esm/pmxt/client.js +181 -195
  7. package/dist/esm/pmxt/constants.d.ts +53 -0
  8. package/dist/esm/pmxt/constants.js +60 -0
  9. package/dist/generated/src/apis/DefaultApi.d.ts +266 -106
  10. package/dist/generated/src/apis/DefaultApi.js +346 -61
  11. package/dist/generated/src/models/index.d.ts +0 -19
  12. package/dist/generated/src/models/index.js +0 -19
  13. package/dist/pmxt/client.d.ts +59 -4
  14. package/dist/pmxt/client.js +181 -195
  15. package/dist/pmxt/constants.d.ts +53 -0
  16. package/dist/pmxt/constants.js +64 -0
  17. package/generated/.openapi-generator/FILES +0 -38
  18. package/generated/docs/DefaultApi.md +276 -120
  19. package/generated/package.json +1 -1
  20. package/generated/src/apis/DefaultApi.ts +590 -210
  21. package/generated/src/models/index.ts +0 -19
  22. package/package.json +2 -2
  23. package/pmxt/client.ts +223 -208
  24. package/pmxt/constants.ts +67 -0
  25. package/dist/esm/generated/src/models/FetchAllOrdersRequest.d.ts +0 -40
  26. package/dist/esm/generated/src/models/FetchAllOrdersRequest.js +0 -45
  27. package/dist/esm/generated/src/models/FetchBalanceRequest.d.ts +0 -39
  28. package/dist/esm/generated/src/models/FetchBalanceRequest.js +0 -44
  29. package/dist/esm/generated/src/models/FetchClosedOrdersRequest.d.ts +0 -40
  30. package/dist/esm/generated/src/models/FetchClosedOrdersRequest.js +0 -45
  31. package/dist/esm/generated/src/models/FetchEventRequest.d.ts +0 -40
  32. package/dist/esm/generated/src/models/FetchEventRequest.js +0 -45
  33. package/dist/esm/generated/src/models/FetchEventsRequest.d.ts +0 -40
  34. package/dist/esm/generated/src/models/FetchEventsRequest.js +0 -45
  35. package/dist/esm/generated/src/models/FetchMarketRequest.d.ts +0 -40
  36. package/dist/esm/generated/src/models/FetchMarketRequest.js +0 -45
  37. package/dist/esm/generated/src/models/FetchMarketsPaginatedRequest.d.ts +0 -40
  38. package/dist/esm/generated/src/models/FetchMarketsPaginatedRequest.js +0 -45
  39. package/dist/esm/generated/src/models/FetchMarketsPaginatedRequestArgsInner.d.ts +0 -38
  40. package/dist/esm/generated/src/models/FetchMarketsPaginatedRequestArgsInner.js +0 -43
  41. package/dist/esm/generated/src/models/FetchMarketsRequest.d.ts +0 -40
  42. package/dist/esm/generated/src/models/FetchMarketsRequest.js +0 -45
  43. package/dist/esm/generated/src/models/FetchMyTradesRequest.d.ts +0 -40
  44. package/dist/esm/generated/src/models/FetchMyTradesRequest.js +0 -45
  45. package/dist/esm/generated/src/models/FetchOHLCVRequest.d.ts +0 -40
  46. package/dist/esm/generated/src/models/FetchOHLCVRequest.js +0 -47
  47. package/dist/esm/generated/src/models/FetchOHLCVRequestArgsInner.d.ts +0 -22
  48. package/dist/esm/generated/src/models/FetchOHLCVRequestArgsInner.js +0 -50
  49. package/dist/esm/generated/src/models/FetchOpenOrdersRequest.d.ts +0 -39
  50. package/dist/esm/generated/src/models/FetchOpenOrdersRequest.js +0 -44
  51. package/dist/esm/generated/src/models/FetchOrderBookRequest.d.ts +0 -39
  52. package/dist/esm/generated/src/models/FetchOrderBookRequest.js +0 -46
  53. package/dist/esm/generated/src/models/FetchOrderRequest.d.ts +0 -39
  54. package/dist/esm/generated/src/models/FetchOrderRequest.js +0 -46
  55. package/dist/esm/generated/src/models/FetchPositionsRequest.d.ts +0 -39
  56. package/dist/esm/generated/src/models/FetchPositionsRequest.js +0 -44
  57. package/dist/esm/generated/src/models/FetchTradesRequest.d.ts +0 -40
  58. package/dist/esm/generated/src/models/FetchTradesRequest.js +0 -47
  59. package/dist/esm/generated/src/models/FetchTradesRequestArgsInner.d.ts +0 -22
  60. package/dist/esm/generated/src/models/FetchTradesRequestArgsInner.js +0 -50
  61. package/dist/esm/generated/src/models/FetchTradesRequestArgsInnerOneOf.d.ts +0 -24
  62. package/dist/esm/generated/src/models/FetchTradesRequestArgsInnerOneOf.js +0 -54
  63. package/dist/generated/src/models/FetchAllOrdersRequest.d.ts +0 -40
  64. package/dist/generated/src/models/FetchAllOrdersRequest.js +0 -52
  65. package/dist/generated/src/models/FetchBalanceRequest.d.ts +0 -39
  66. package/dist/generated/src/models/FetchBalanceRequest.js +0 -51
  67. package/dist/generated/src/models/FetchClosedOrdersRequest.d.ts +0 -40
  68. package/dist/generated/src/models/FetchClosedOrdersRequest.js +0 -52
  69. package/dist/generated/src/models/FetchEventRequest.d.ts +0 -40
  70. package/dist/generated/src/models/FetchEventRequest.js +0 -52
  71. package/dist/generated/src/models/FetchEventsRequest.d.ts +0 -40
  72. package/dist/generated/src/models/FetchEventsRequest.js +0 -52
  73. package/dist/generated/src/models/FetchMarketRequest.d.ts +0 -40
  74. package/dist/generated/src/models/FetchMarketRequest.js +0 -52
  75. package/dist/generated/src/models/FetchMarketsPaginatedRequest.d.ts +0 -40
  76. package/dist/generated/src/models/FetchMarketsPaginatedRequest.js +0 -52
  77. package/dist/generated/src/models/FetchMarketsPaginatedRequestArgsInner.d.ts +0 -38
  78. package/dist/generated/src/models/FetchMarketsPaginatedRequestArgsInner.js +0 -50
  79. package/dist/generated/src/models/FetchMarketsRequest.d.ts +0 -40
  80. package/dist/generated/src/models/FetchMarketsRequest.js +0 -52
  81. package/dist/generated/src/models/FetchMyTradesRequest.d.ts +0 -40
  82. package/dist/generated/src/models/FetchMyTradesRequest.js +0 -52
  83. package/dist/generated/src/models/FetchOHLCVRequest.d.ts +0 -40
  84. package/dist/generated/src/models/FetchOHLCVRequest.js +0 -54
  85. package/dist/generated/src/models/FetchOHLCVRequestArgsInner.d.ts +0 -22
  86. package/dist/generated/src/models/FetchOHLCVRequestArgsInner.js +0 -56
  87. package/dist/generated/src/models/FetchOpenOrdersRequest.d.ts +0 -39
  88. package/dist/generated/src/models/FetchOpenOrdersRequest.js +0 -51
  89. package/dist/generated/src/models/FetchOrderBookRequest.d.ts +0 -39
  90. package/dist/generated/src/models/FetchOrderBookRequest.js +0 -53
  91. package/dist/generated/src/models/FetchOrderRequest.d.ts +0 -39
  92. package/dist/generated/src/models/FetchOrderRequest.js +0 -53
  93. package/dist/generated/src/models/FetchPositionsRequest.d.ts +0 -39
  94. package/dist/generated/src/models/FetchPositionsRequest.js +0 -51
  95. package/dist/generated/src/models/FetchTradesRequest.d.ts +0 -40
  96. package/dist/generated/src/models/FetchTradesRequest.js +0 -54
  97. package/dist/generated/src/models/FetchTradesRequestArgsInner.d.ts +0 -22
  98. package/dist/generated/src/models/FetchTradesRequestArgsInner.js +0 -56
  99. package/dist/generated/src/models/FetchTradesRequestArgsInnerOneOf.d.ts +0 -24
  100. package/dist/generated/src/models/FetchTradesRequestArgsInnerOneOf.js +0 -61
  101. package/generated/docs/FetchAllOrdersRequest.md +0 -36
  102. package/generated/docs/FetchBalanceRequest.md +0 -36
  103. package/generated/docs/FetchClosedOrdersRequest.md +0 -36
  104. package/generated/docs/FetchEventRequest.md +0 -36
  105. package/generated/docs/FetchEventsRequest.md +0 -36
  106. package/generated/docs/FetchMarketRequest.md +0 -36
  107. package/generated/docs/FetchMarketsPaginatedRequest.md +0 -36
  108. package/generated/docs/FetchMarketsPaginatedRequestArgsInner.md +0 -36
  109. package/generated/docs/FetchMarketsRequest.md +0 -36
  110. package/generated/docs/FetchMyTradesRequest.md +0 -36
  111. package/generated/docs/FetchOHLCVRequest.md +0 -36
  112. package/generated/docs/FetchOHLCVRequestArgsInner.md +0 -40
  113. package/generated/docs/FetchOpenOrdersRequest.md +0 -36
  114. package/generated/docs/FetchOrderBookRequest.md +0 -36
  115. package/generated/docs/FetchOrderRequest.md +0 -36
  116. package/generated/docs/FetchPositionsRequest.md +0 -36
  117. package/generated/docs/FetchTradesRequest.md +0 -36
  118. package/generated/docs/FetchTradesRequestArgsInner.md +0 -40
  119. package/generated/docs/FetchTradesRequestArgsInnerOneOf.md +0 -40
  120. package/generated/src/models/FetchAllOrdersRequest.ts +0 -88
  121. package/generated/src/models/FetchBalanceRequest.ts +0 -81
  122. package/generated/src/models/FetchClosedOrdersRequest.ts +0 -88
  123. package/generated/src/models/FetchEventRequest.ts +0 -88
  124. package/generated/src/models/FetchEventsRequest.ts +0 -88
  125. package/generated/src/models/FetchMarketRequest.ts +0 -88
  126. package/generated/src/models/FetchMarketsPaginatedRequest.ts +0 -88
  127. package/generated/src/models/FetchMarketsPaginatedRequestArgsInner.ts +0 -73
  128. package/generated/src/models/FetchMarketsRequest.ts +0 -88
  129. package/generated/src/models/FetchMyTradesRequest.ts +0 -88
  130. package/generated/src/models/FetchOHLCVRequest.ts +0 -89
  131. package/generated/src/models/FetchOHLCVRequestArgsInner.ts +0 -69
  132. package/generated/src/models/FetchOpenOrdersRequest.ts +0 -81
  133. package/generated/src/models/FetchOrderBookRequest.ts +0 -82
  134. package/generated/src/models/FetchOrderRequest.ts +0 -82
  135. package/generated/src/models/FetchPositionsRequest.ts +0 -81
  136. package/generated/src/models/FetchTradesRequest.ts +0 -89
  137. package/generated/src/models/FetchTradesRequestArgsInner.ts +0 -69
  138. package/generated/src/models/FetchTradesRequestArgsInnerOneOf.ts +0 -80
@@ -9,6 +9,7 @@ import { MarketList, } from "./models.js";
9
9
  import { ServerManager } from "./server-manager.js";
10
10
  import { buildArgsWithOptionalOptions } from "./args.js";
11
11
  import { PmxtError, fromServerError } from "./errors.js";
12
+ import { resolvePmxtBaseUrl } from "./constants.js";
12
13
  /**
13
14
  * Resolve a MarketOutcome shorthand to a plain outcome ID string.
14
15
  * Accepts either a raw string ID or a MarketOutcome object.
@@ -18,6 +19,51 @@ function resolveOutcomeId(input) {
18
19
  return input;
19
20
  return input.outcomeId;
20
21
  }
22
+ /**
23
+ * Build a URL-encoded query string from a plain record.
24
+ *
25
+ * - `undefined` / `null` values are skipped (they shouldn't appear in the URL).
26
+ * - Arrays are serialised as repeated `key=v1&key=v2` pairs.
27
+ * - Nested objects are skipped here; callers should route such queries through
28
+ * POST instead (see `queryHasNestedObject`).
29
+ */
30
+ function buildSidecarQueryString(query) {
31
+ const parts = [];
32
+ for (const [key, value] of Object.entries(query)) {
33
+ if (value === undefined || value === null)
34
+ continue;
35
+ if (Array.isArray(value)) {
36
+ for (const v of value) {
37
+ if (v === undefined || v === null)
38
+ continue;
39
+ parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`);
40
+ }
41
+ }
42
+ else if (typeof value === 'object') {
43
+ // Nested objects don't round-trip through query strings. Caller
44
+ // should have detected this and POSTed instead.
45
+ continue;
46
+ }
47
+ else {
48
+ parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
49
+ }
50
+ }
51
+ return parts.join('&');
52
+ }
53
+ /**
54
+ * True if any top-level value in the query is a nested object (not an array).
55
+ * Such queries can't be safely expressed in a query string, so we fall back
56
+ * to POST to preserve the original argument shape.
57
+ */
58
+ function queryHasNestedObject(query) {
59
+ for (const value of Object.values(query)) {
60
+ if (value === undefined || value === null)
61
+ continue;
62
+ if (typeof value === 'object' && !Array.isArray(value))
63
+ return true;
64
+ }
65
+ return false;
66
+ }
21
67
  // Converter functions
22
68
  function convertMarket(raw) {
23
69
  const outcomes = (raw.outcomes || []).map((o) => ({
@@ -183,23 +229,47 @@ export class Exchange {
183
229
  exchangeName;
184
230
  apiKey;
185
231
  privateKey;
232
+ pmxtApiKey;
186
233
  proxyAddress;
187
234
  signatureType;
188
235
  api;
189
236
  config;
190
237
  serverManager;
191
238
  initPromise;
239
+ isHosted;
240
+ /**
241
+ * Sticky flag: set to `true` the first time a GET read is rejected by
242
+ * the sidecar with 404/405 (i.e. an older pmxt-core that only supports
243
+ * POST). While false, read methods try GET first; once flipped they
244
+ * POST directly and skip the GET probe for the lifetime of this client.
245
+ */
246
+ _getReadsUnsupported = false;
192
247
  constructor(exchangeName, options = {}) {
193
248
  this.exchangeName = exchangeName.toLowerCase();
194
249
  this.apiKey = options.apiKey;
195
250
  this.privateKey = options.privateKey;
196
251
  this.proxyAddress = options.proxyAddress;
197
252
  this.signatureType = options.signatureType;
198
- let baseUrl = options.baseUrl || "http://localhost:3847";
199
- const autoStartServer = options.autoStartServer !== false;
200
- // Initialize server manager
253
+ // Resolve base URL + hosted API key via the shared precedence
254
+ // rules. See constants.ts for the full resolution table.
255
+ const resolved = resolvePmxtBaseUrl({
256
+ baseUrl: options.baseUrl,
257
+ pmxtApiKey: options.pmxtApiKey,
258
+ });
259
+ const baseUrl = resolved.baseUrl;
260
+ this.pmxtApiKey = resolved.pmxtApiKey;
261
+ this.isHosted = resolved.isHosted;
262
+ // auto_start_server defaults: true for local, false for hosted.
263
+ // An explicit value in the options always wins.
264
+ const autoStartServer = options.autoStartServer !== undefined
265
+ ? options.autoStartServer
266
+ : !this.isHosted;
267
+ // Initialize server manager (no network calls happen here — the
268
+ // constructor just stores config).
201
269
  this.serverManager = new ServerManager({ baseUrl });
202
- // Configure the API client with the initial base URL (will be updated if port changes)
270
+ // Configure the API client with the initial base URL (will be
271
+ // updated to the actual listen port if the local sidecar gets
272
+ // bumped off the default).
203
273
  this.config = new Configuration({ basePath: baseUrl });
204
274
  this.api = new DefaultApi(this.config);
205
275
  // Initialize the server connection asynchronously
@@ -249,10 +319,19 @@ export class Exchange {
249
319
  }
250
320
  getAuthHeaders() {
251
321
  const headers = { ...this.config.headers };
322
+ // Local sidecar access token (read from the lock file). Only
323
+ // meaningful when talking to a local sidecar we spawned
324
+ // ourselves; harmless elsewhere.
252
325
  const accessToken = this.serverManager.getAccessToken();
253
326
  if (accessToken) {
254
327
  headers['x-pmxt-access-token'] = accessToken;
255
328
  }
329
+ // Hosted pmxt bearer token. The hosted service requires this;
330
+ // the local sidecar ignores it. Safe to attach unconditionally
331
+ // whenever a pmxtApiKey has been resolved.
332
+ if (this.pmxtApiKey) {
333
+ headers['Authorization'] = `Bearer ${this.pmxtApiKey}`;
334
+ }
256
335
  return headers;
257
336
  }
258
337
  // Low-Level API Access
@@ -303,6 +382,68 @@ export class Exchange {
303
382
  throw new PmxtError(`Failed to call API '${operationId}': ${error}`);
304
383
  }
305
384
  }
385
+ /**
386
+ * Dispatch a sidecar read method, preferring GET but transparently
387
+ * falling back to POST for full backward compatibility.
388
+ *
389
+ * GET is used when:
390
+ * - the client has no per-instance credentials (the sidecar's GET
391
+ * handler intentionally drops credentials to avoid leaking them
392
+ * through query strings and access logs), and
393
+ * - the sidecar hasn't already returned 404/405 for a previous GET
394
+ * in this client's lifetime (`_getReadsUnsupported`), and
395
+ * - the query has no nested objects (query strings can't round-trip
396
+ * arbitrary JSON).
397
+ *
398
+ * Otherwise (or if the GET attempt is rejected with 404/405) the call
399
+ * is sent as POST with the original `{args, credentials}` body so that
400
+ * SDK users talking to an older pmxt-core continue to work unchanged.
401
+ *
402
+ * @internal — shared transport used by every generated read method.
403
+ */
404
+ async sidecarReadRequest(methodName, query, args) {
405
+ const baseUrl = `${this.config.basePath}/api/${this.exchangeName}/${methodName}`;
406
+ const hasCredentials = this.getCredentials() !== undefined;
407
+ if (!hasCredentials && !this._getReadsUnsupported && !queryHasNestedObject(query)) {
408
+ const qs = buildSidecarQueryString(query);
409
+ const getUrl = qs ? `${baseUrl}?${qs}` : baseUrl;
410
+ const response = await fetch(getUrl, {
411
+ method: 'GET',
412
+ headers: this.getAuthHeaders(),
413
+ });
414
+ // 404 / 405 => older sidecar without GET dispatch. Remember
415
+ // the downgrade so future calls skip the probe, and fall
416
+ // through to POST below.
417
+ if (response.status === 404 || response.status === 405) {
418
+ await response.text().catch(() => undefined);
419
+ this._getReadsUnsupported = true;
420
+ }
421
+ else {
422
+ if (!response.ok) {
423
+ const body = await response.json().catch(() => ({}));
424
+ if (body.error && typeof body.error === "object") {
425
+ throw fromServerError(body.error);
426
+ }
427
+ throw new PmxtError(body.error?.message || response.statusText);
428
+ }
429
+ return response.json();
430
+ }
431
+ }
432
+ // POST fallback — identical to the original per-method template.
433
+ const response = await fetch(baseUrl, {
434
+ method: 'POST',
435
+ headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
436
+ body: JSON.stringify({ args, credentials: this.getCredentials() }),
437
+ });
438
+ if (!response.ok) {
439
+ const body = await response.json().catch(() => ({}));
440
+ if (body.error && typeof body.error === "object") {
441
+ throw fromServerError(body.error);
442
+ }
443
+ throw new PmxtError(body.error?.message || response.statusText);
444
+ }
445
+ return response.json();
446
+ }
306
447
  // BEGIN GENERATED METHODS
307
448
  async loadMarkets(reload = false) {
308
449
  await this.initPromise;
@@ -339,19 +480,8 @@ export class Exchange {
339
480
  await this.initPromise;
340
481
  try {
341
482
  const args = buildArgsWithOptionalOptions(params);
342
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMarkets`, {
343
- method: 'POST',
344
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
345
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
346
- });
347
- if (!response.ok) {
348
- const body = await response.json().catch(() => ({}));
349
- if (body.error && typeof body.error === "object") {
350
- throw fromServerError(body.error);
351
- }
352
- throw new PmxtError(body.error?.message || response.statusText);
353
- }
354
- const json = await response.json();
483
+ const query = { ...(params || {}) };
484
+ const json = await this.sidecarReadRequest('fetchMarkets', query, args);
355
485
  const data = this.handleResponse(json);
356
486
  return data.map(convertMarket);
357
487
  }
@@ -365,19 +495,8 @@ export class Exchange {
365
495
  await this.initPromise;
366
496
  try {
367
497
  const args = buildArgsWithOptionalOptions(params);
368
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMarketsPaginated`, {
369
- method: 'POST',
370
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
371
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
372
- });
373
- if (!response.ok) {
374
- const body = await response.json().catch(() => ({}));
375
- if (body.error && typeof body.error === "object") {
376
- throw fromServerError(body.error);
377
- }
378
- throw new PmxtError(body.error?.message || response.statusText);
379
- }
380
- const json = await response.json();
498
+ const query = { ...(params || {}) };
499
+ const json = await this.sidecarReadRequest('fetchMarketsPaginated', query, args);
381
500
  const data = this.handleResponse(json);
382
501
  return {
383
502
  data: (data.data || []).map(convertMarket),
@@ -395,19 +514,8 @@ export class Exchange {
395
514
  await this.initPromise;
396
515
  try {
397
516
  const args = buildArgsWithOptionalOptions(params);
398
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchEvents`, {
399
- method: 'POST',
400
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
401
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
402
- });
403
- if (!response.ok) {
404
- const body = await response.json().catch(() => ({}));
405
- if (body.error && typeof body.error === "object") {
406
- throw fromServerError(body.error);
407
- }
408
- throw new PmxtError(body.error?.message || response.statusText);
409
- }
410
- const json = await response.json();
517
+ const query = { ...(params || {}) };
518
+ const json = await this.sidecarReadRequest('fetchEvents', query, args);
411
519
  const data = this.handleResponse(json);
412
520
  return data.map(convertEvent);
413
521
  }
@@ -421,19 +529,8 @@ export class Exchange {
421
529
  await this.initPromise;
422
530
  try {
423
531
  const args = buildArgsWithOptionalOptions(params);
424
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMarket`, {
425
- method: 'POST',
426
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
427
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
428
- });
429
- if (!response.ok) {
430
- const body = await response.json().catch(() => ({}));
431
- if (body.error && typeof body.error === "object") {
432
- throw fromServerError(body.error);
433
- }
434
- throw new PmxtError(body.error?.message || response.statusText);
435
- }
436
- const json = await response.json();
532
+ const query = { ...(params || {}) };
533
+ const json = await this.sidecarReadRequest('fetchMarket', query, args);
437
534
  const data = this.handleResponse(json);
438
535
  return convertMarket(data);
439
536
  }
@@ -447,19 +544,8 @@ export class Exchange {
447
544
  await this.initPromise;
448
545
  try {
449
546
  const args = buildArgsWithOptionalOptions(params);
450
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchEvent`, {
451
- method: 'POST',
452
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
453
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
454
- });
455
- if (!response.ok) {
456
- const body = await response.json().catch(() => ({}));
457
- if (body.error && typeof body.error === "object") {
458
- throw fromServerError(body.error);
459
- }
460
- throw new PmxtError(body.error?.message || response.statusText);
461
- }
462
- const json = await response.json();
547
+ const query = { ...(params || {}) };
548
+ const json = await this.sidecarReadRequest('fetchEvent', query, args);
463
549
  const data = this.handleResponse(json);
464
550
  return convertEvent(data);
465
551
  }
@@ -473,21 +559,9 @@ export class Exchange {
473
559
  await this.initPromise;
474
560
  const resolvedId = resolveOutcomeId(id);
475
561
  try {
476
- const args = [];
477
- args.push(resolvedId);
478
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchOrderBook`, {
479
- method: 'POST',
480
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
481
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
482
- });
483
- if (!response.ok) {
484
- const body = await response.json().catch(() => ({}));
485
- if (body.error && typeof body.error === "object") {
486
- throw fromServerError(body.error);
487
- }
488
- throw new PmxtError(body.error?.message || response.statusText);
489
- }
490
- const json = await response.json();
562
+ const args = [resolvedId];
563
+ const query = { id: resolvedId };
564
+ const json = await this.sidecarReadRequest('fetchOrderBook', query, args);
491
565
  const data = this.handleResponse(json);
492
566
  return convertOrderBook(data);
493
567
  }
@@ -527,21 +601,9 @@ export class Exchange {
527
601
  async fetchOrder(orderId) {
528
602
  await this.initPromise;
529
603
  try {
530
- const args = [];
531
- args.push(orderId);
532
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchOrder`, {
533
- method: 'POST',
534
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
535
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
536
- });
537
- if (!response.ok) {
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);
543
- }
544
- const json = await response.json();
604
+ const args = [orderId];
605
+ const query = { orderId };
606
+ const json = await this.sidecarReadRequest('fetchOrder', query, args);
545
607
  const data = this.handleResponse(json);
546
608
  return convertOrder(data);
547
609
  }
@@ -555,19 +617,8 @@ export class Exchange {
555
617
  await this.initPromise;
556
618
  try {
557
619
  const args = buildArgsWithOptionalOptions(marketId);
558
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchOpenOrders`, {
559
- method: 'POST',
560
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
561
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
562
- });
563
- if (!response.ok) {
564
- const body = await response.json().catch(() => ({}));
565
- if (body.error && typeof body.error === "object") {
566
- throw fromServerError(body.error);
567
- }
568
- throw new PmxtError(body.error?.message || response.statusText);
569
- }
570
- const json = await response.json();
620
+ const query = { marketId };
621
+ const json = await this.sidecarReadRequest('fetchOpenOrders', query, args);
571
622
  const data = this.handleResponse(json);
572
623
  return data.map(convertOrder);
573
624
  }
@@ -581,19 +632,8 @@ export class Exchange {
581
632
  await this.initPromise;
582
633
  try {
583
634
  const args = buildArgsWithOptionalOptions(params);
584
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchMyTrades`, {
585
- method: 'POST',
586
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
587
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
588
- });
589
- if (!response.ok) {
590
- const body = await response.json().catch(() => ({}));
591
- if (body.error && typeof body.error === "object") {
592
- throw fromServerError(body.error);
593
- }
594
- throw new PmxtError(body.error?.message || response.statusText);
595
- }
596
- const json = await response.json();
635
+ const query = { ...(params || {}) };
636
+ const json = await this.sidecarReadRequest('fetchMyTrades', query, args);
597
637
  const data = this.handleResponse(json);
598
638
  return data.map(convertUserTrade);
599
639
  }
@@ -607,19 +647,8 @@ export class Exchange {
607
647
  await this.initPromise;
608
648
  try {
609
649
  const args = buildArgsWithOptionalOptions(params);
610
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchClosedOrders`, {
611
- method: 'POST',
612
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
613
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
614
- });
615
- if (!response.ok) {
616
- const body = await response.json().catch(() => ({}));
617
- if (body.error && typeof body.error === "object") {
618
- throw fromServerError(body.error);
619
- }
620
- throw new PmxtError(body.error?.message || response.statusText);
621
- }
622
- const json = await response.json();
650
+ const query = { ...(params || {}) };
651
+ const json = await this.sidecarReadRequest('fetchClosedOrders', query, args);
623
652
  const data = this.handleResponse(json);
624
653
  return data.map(convertOrder);
625
654
  }
@@ -633,19 +662,8 @@ export class Exchange {
633
662
  await this.initPromise;
634
663
  try {
635
664
  const args = buildArgsWithOptionalOptions(params);
636
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchAllOrders`, {
637
- method: 'POST',
638
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
639
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
640
- });
641
- if (!response.ok) {
642
- const body = await response.json().catch(() => ({}));
643
- if (body.error && typeof body.error === "object") {
644
- throw fromServerError(body.error);
645
- }
646
- throw new PmxtError(body.error?.message || response.statusText);
647
- }
648
- const json = await response.json();
665
+ const query = { ...(params || {}) };
666
+ const json = await this.sidecarReadRequest('fetchAllOrders', query, args);
649
667
  const data = this.handleResponse(json);
650
668
  return data.map(convertOrder);
651
669
  }
@@ -659,19 +677,8 @@ export class Exchange {
659
677
  await this.initPromise;
660
678
  try {
661
679
  const args = address ? [address] : [];
662
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchPositions`, {
663
- method: 'POST',
664
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
665
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
666
- });
667
- if (!response.ok) {
668
- const body = await response.json().catch(() => ({}));
669
- if (body.error && typeof body.error === "object") {
670
- throw fromServerError(body.error);
671
- }
672
- throw new PmxtError(body.error?.message || response.statusText);
673
- }
674
- const json = await response.json();
680
+ const query = { address };
681
+ const json = await this.sidecarReadRequest('fetchPositions', query, args);
675
682
  const data = this.handleResponse(json);
676
683
  return data.map(convertPosition);
677
684
  }
@@ -685,19 +692,8 @@ export class Exchange {
685
692
  await this.initPromise;
686
693
  try {
687
694
  const args = address ? [address] : [];
688
- const response = await fetch(`${this.config.basePath}/api/${this.exchangeName}/fetchBalance`, {
689
- method: 'POST',
690
- headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
691
- body: JSON.stringify({ args, credentials: this.getCredentials() }),
692
- });
693
- if (!response.ok) {
694
- const body = await response.json().catch(() => ({}));
695
- if (body.error && typeof body.error === "object") {
696
- throw fromServerError(body.error);
697
- }
698
- throw new PmxtError(body.error?.message || response.statusText);
699
- }
700
- const json = await response.json();
695
+ const query = { address };
696
+ const json = await this.sidecarReadRequest('fetchBalance', query, args);
701
697
  const data = this.handleResponse(json);
702
698
  return data.map(convertBalance);
703
699
  }
@@ -764,15 +760,10 @@ export class Exchange {
764
760
  if (params.limit) {
765
761
  paramsDict.limit = params.limit;
766
762
  }
767
- const requestBody = {
768
- args: [resolvedOutcomeId, paramsDict],
769
- credentials: this.getCredentials()
770
- };
771
- const response = await this.api.fetchOHLCV({
772
- exchange: this.exchangeName,
773
- fetchOHLCVRequest: requestBody,
774
- }, { headers: this.getAuthHeaders() });
775
- const data = this.handleResponse(response);
763
+ const args = [resolvedOutcomeId, paramsDict];
764
+ const query = { id: resolvedOutcomeId, ...paramsDict };
765
+ const json = await this.sidecarReadRequest('fetchOHLCV', query, args);
766
+ const data = this.handleResponse(json);
776
767
  return data.map(convertCandle);
777
768
  }
778
769
  catch (error) {
@@ -798,15 +789,10 @@ export class Exchange {
798
789
  if (params.limit) {
799
790
  paramsDict.limit = params.limit;
800
791
  }
801
- const requestBody = {
802
- args: [resolvedOutcomeId, paramsDict],
803
- credentials: this.getCredentials()
804
- };
805
- const response = await this.api.fetchTrades({
806
- exchange: this.exchangeName,
807
- fetchTradesRequest: requestBody,
808
- }, { headers: this.getAuthHeaders() });
809
- const data = this.handleResponse(response);
792
+ const args = [resolvedOutcomeId, paramsDict];
793
+ const query = { id: resolvedOutcomeId, ...paramsDict };
794
+ const json = await this.sidecarReadRequest('fetchTrades', query, args);
795
+ const data = this.handleResponse(json);
810
796
  return data.map(convertTrade);
811
797
  }
812
798
  catch (error) {
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Shared URL and environment-variable constants for the pmxt SDK.
3
+ *
4
+ * These are deliberately plain exports (no runtime logic) so that they
5
+ * can be imported from any module without creating import cycles.
6
+ */
7
+ /**
8
+ * The hosted pmxt production endpoint.
9
+ *
10
+ * Exchange classes and `Router` default to this URL whenever a hosted
11
+ * pmxt API key is supplied (via `pmxtApiKey` kwarg or the
12
+ * `PMXT_API_KEY` environment variable) AND no explicit `baseUrl` /
13
+ * `PMXT_BASE_URL` is configured.
14
+ */
15
+ export declare const HOSTED_URL = "https://api.pmxt.dev";
16
+ /**
17
+ * The local sidecar default.
18
+ *
19
+ * This is the URL the SDK uses when no hosted key and no explicit
20
+ * override are present. It matches the port that the pmxt-core
21
+ * sidecar listens on by default.
22
+ */
23
+ export declare const LOCAL_URL = "http://localhost:3847";
24
+ /**
25
+ * Environment variable names. Centralised so tests and docs can
26
+ * reference a single source of truth.
27
+ */
28
+ export declare const ENV: {
29
+ readonly BASE_URL: "PMXT_BASE_URL";
30
+ readonly API_KEY: "PMXT_API_KEY";
31
+ };
32
+ /**
33
+ * Resolve the effective base URL for an SDK client, following the
34
+ * documented precedence rules:
35
+ *
36
+ * 1. Explicit `baseUrl` argument wins.
37
+ * 2. `PMXT_BASE_URL` environment variable.
38
+ * 3. If a hosted API key is present (argument or `PMXT_API_KEY` env),
39
+ * default to {@link HOSTED_URL}.
40
+ * 4. Otherwise, default to {@link LOCAL_URL}.
41
+ *
42
+ * Returns both the resolved URL and an `isHosted` flag (true iff the
43
+ * resolved URL is anything other than the local sidecar default).
44
+ */
45
+ export declare function resolvePmxtBaseUrl(args: {
46
+ baseUrl?: string;
47
+ pmxtApiKey?: string;
48
+ env?: NodeJS.ProcessEnv;
49
+ }): {
50
+ baseUrl: string;
51
+ pmxtApiKey?: string;
52
+ isHosted: boolean;
53
+ };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Shared URL and environment-variable constants for the pmxt SDK.
3
+ *
4
+ * These are deliberately plain exports (no runtime logic) so that they
5
+ * can be imported from any module without creating import cycles.
6
+ */
7
+ /**
8
+ * The hosted pmxt production endpoint.
9
+ *
10
+ * Exchange classes and `Router` default to this URL whenever a hosted
11
+ * pmxt API key is supplied (via `pmxtApiKey` kwarg or the
12
+ * `PMXT_API_KEY` environment variable) AND no explicit `baseUrl` /
13
+ * `PMXT_BASE_URL` is configured.
14
+ */
15
+ export const HOSTED_URL = "https://api.pmxt.dev";
16
+ /**
17
+ * The local sidecar default.
18
+ *
19
+ * This is the URL the SDK uses when no hosted key and no explicit
20
+ * override are present. It matches the port that the pmxt-core
21
+ * sidecar listens on by default.
22
+ */
23
+ export const LOCAL_URL = "http://localhost:3847";
24
+ /**
25
+ * Environment variable names. Centralised so tests and docs can
26
+ * reference a single source of truth.
27
+ */
28
+ export const ENV = {
29
+ BASE_URL: "PMXT_BASE_URL",
30
+ API_KEY: "PMXT_API_KEY",
31
+ };
32
+ /**
33
+ * Resolve the effective base URL for an SDK client, following the
34
+ * documented precedence rules:
35
+ *
36
+ * 1. Explicit `baseUrl` argument wins.
37
+ * 2. `PMXT_BASE_URL` environment variable.
38
+ * 3. If a hosted API key is present (argument or `PMXT_API_KEY` env),
39
+ * default to {@link HOSTED_URL}.
40
+ * 4. Otherwise, default to {@link LOCAL_URL}.
41
+ *
42
+ * Returns both the resolved URL and an `isHosted` flag (true iff the
43
+ * resolved URL is anything other than the local sidecar default).
44
+ */
45
+ export function resolvePmxtBaseUrl(args) {
46
+ const env = args.env ?? (typeof process !== "undefined" ? process.env : {});
47
+ const pmxtApiKey = args.pmxtApiKey ?? env[ENV.API_KEY] ?? undefined;
48
+ const pick = (url) => ({
49
+ baseUrl: url,
50
+ pmxtApiKey,
51
+ isHosted: url !== LOCAL_URL,
52
+ });
53
+ if (args.baseUrl)
54
+ return pick(args.baseUrl);
55
+ if (env[ENV.BASE_URL])
56
+ return pick(env[ENV.BASE_URL]);
57
+ if (pmxtApiKey)
58
+ return pick(HOSTED_URL);
59
+ return pick(LOCAL_URL);
60
+ }