@stxapp/stx-typescript 0.7.0 → 0.9.0-alpha.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.
@@ -299,6 +299,17 @@ var OPERATIONS = {
299
299
  isList: true,
300
300
  paginated: true
301
301
  },
302
+ events__id_players_get: {
303
+ method: "GET",
304
+ path: "/api/v1/events/{event_id}/players",
305
+ query: [],
306
+ pathParams: ["event_id"],
307
+ hasBody: false,
308
+ responseKey: "",
309
+ model: "",
310
+ isList: false,
311
+ paginated: false
312
+ },
302
313
  fills_get: {
303
314
  method: "GET",
304
315
  path: "/api/v1/fills",
@@ -310,6 +321,28 @@ var OPERATIONS = {
310
321
  isList: true,
311
322
  paginated: true
312
323
  },
324
+ leaderboard_get: {
325
+ method: "GET",
326
+ path: "/api/v1/leaderboard",
327
+ query: ["period", "category", "metric", "limit"],
328
+ pathParams: [],
329
+ hasBody: false,
330
+ responseKey: "",
331
+ model: "",
332
+ isList: false,
333
+ paginated: false
334
+ },
335
+ leaderboard_me_get: {
336
+ method: "GET",
337
+ path: "/api/v1/leaderboard/me",
338
+ query: ["period", "category"],
339
+ pathParams: [],
340
+ hasBody: false,
341
+ responseKey: "",
342
+ model: "",
343
+ isList: false,
344
+ paginated: false
345
+ },
313
346
  markets_get: {
314
347
  method: "GET",
315
348
  path: "/api/v1/markets",
@@ -321,6 +354,17 @@ var OPERATIONS = {
321
354
  isList: true,
322
355
  paginated: true
323
356
  },
357
+ markets_post: {
358
+ method: "POST",
359
+ path: "/api/v1/markets",
360
+ query: [],
361
+ pathParams: [],
362
+ hasBody: true,
363
+ responseKey: "",
364
+ model: "",
365
+ isList: false,
366
+ paginated: false
367
+ },
324
368
  me_get: {
325
369
  method: "GET",
326
370
  path: "/api/v1/me",
@@ -332,6 +376,17 @@ var OPERATIONS = {
332
376
  isList: false,
333
377
  paginated: false
334
378
  },
379
+ me_profile_patch: {
380
+ method: "PATCH",
381
+ path: "/api/v1/me/profile",
382
+ query: [],
383
+ pathParams: [],
384
+ hasBody: true,
385
+ responseKey: "me",
386
+ model: "Me",
387
+ isList: false,
388
+ paginated: false
389
+ },
335
390
  orders_get: {
336
391
  method: "GET",
337
392
  path: "/api/v1/orders",
@@ -359,7 +414,7 @@ var OPERATIONS = {
359
414
  path: "/api/v1/orders/all",
360
415
  query: [],
361
416
  pathParams: [],
362
- hasBody: false,
417
+ hasBody: true,
363
418
  responseKey: "cancellations",
364
419
  model: "Cancellation",
365
420
  isList: true,
@@ -392,7 +447,7 @@ var OPERATIONS = {
392
447
  path: "/api/v1/orders/{order_id}",
393
448
  query: [],
394
449
  pathParams: ["order_id"],
395
- hasBody: false,
450
+ hasBody: true,
396
451
  responseKey: "",
397
452
  model: "",
398
453
  isList: false,
@@ -496,9 +551,114 @@ var OPERATIONS = {
496
551
  model: "",
497
552
  isList: false,
498
553
  paginated: false
554
+ },
555
+ types_get: {
556
+ method: "GET",
557
+ path: "/api/v1/types",
558
+ query: [],
559
+ pathParams: [],
560
+ hasBody: false,
561
+ responseKey: "",
562
+ model: "",
563
+ isList: false,
564
+ paginated: false
499
565
  }
500
566
  };
501
567
 
568
+ // src/markets.ts
569
+ var Stats = {
570
+ Baseball: {
571
+ BASES: "BASES",
572
+ BK: "BK",
573
+ DOUBLES: "DOUBLES",
574
+ HITS: "HITS",
575
+ HIT_RUN_RBI: "HIT_RUN_RBI",
576
+ HOMERUN: "HOMERUN",
577
+ PERA: "PERA",
578
+ PHITA: "PHITA",
579
+ PK: "PK",
580
+ POUTS: "POUTS",
581
+ PRUNSALOWED: "PRUNSALOWED",
582
+ PWALKSA: "PWALKSA",
583
+ RBI: "RBI",
584
+ RUNS: "RUNS",
585
+ SINGLES: "SINGLES",
586
+ STOLENBASES: "STOLENBASES",
587
+ TRIPLES: "TRIPLES",
588
+ WALKS: "WALKS"
589
+ },
590
+ Basketball: {
591
+ ASSISTS: "ASSISTS",
592
+ BLOCKS: "BLOCKS",
593
+ POINTS: "POINTS",
594
+ POINTS_ASSISTS: "POINTS_ASSISTS",
595
+ POINTS_REBOUNDS: "POINTS_REBOUNDS",
596
+ POINTS_REBOUNDS_ASSISTS: "POINTS_REBOUNDS_ASSISTS",
597
+ REBOUNDS: "REBOUNDS",
598
+ REBOUNDS_ASSISTS: "REBOUNDS_ASSISTS",
599
+ STEALS: "STEALS",
600
+ STEALS_BLOCKS: "STEALS_BLOCKS",
601
+ THREE_POINTS: "THREE_POINTS",
602
+ TURNOVERS: "TURNOVERS"
603
+ },
604
+ Football: {
605
+ DEF_INTERCEPT: "DEF_INTERCEPT",
606
+ DEF_TD: "DEF_TD",
607
+ FG_MADE: "FG_MADE",
608
+ INTERCEPT: "INTERCEPT",
609
+ PASS_ATT: "PASS_ATT",
610
+ PASS_CMP: "PASS_CMP",
611
+ PASS_RUSH_YDS: "PASS_RUSH_YDS",
612
+ PASS_SACKS: "PASS_SACKS",
613
+ PASS_TD: "PASS_TD",
614
+ PASS_YDS: "PASS_YDS",
615
+ REC: "REC",
616
+ RECV_LONG: "RECV_LONG",
617
+ RECV_TD: "RECV_TD",
618
+ RECV_YDS: "RECV_YDS",
619
+ RUSH_ATT: "RUSH_ATT",
620
+ RUSH_LONG: "RUSH_LONG",
621
+ RUSH_REC_YDS: "RUSH_REC_YDS",
622
+ RUSH_TD: "RUSH_TD",
623
+ RUSH_YDS: "RUSH_YDS",
624
+ SACKS: "SACKS",
625
+ TACKLES_ASSIST: "TACKLES_ASSIST",
626
+ TACKLES_SOLO: "TACKLES_SOLO",
627
+ TACKLES_TOTAL: "TACKLES_TOTAL",
628
+ TD_SCORE: "TD_SCORE"
629
+ },
630
+ Hockey: {
631
+ ASSISTS: "ASSISTS",
632
+ GOALS: "GOALS",
633
+ GOAL_AG: "GOAL_AG",
634
+ GOAL_SO: "GOAL_SO",
635
+ POINTS: "POINTS",
636
+ SAVES: "SAVES"
637
+ }
638
+ };
639
+ var MAX_MARKETS_PER_REQUEST = 25;
640
+ function isValidLine(line) {
641
+ return typeof line === "number" && Number.isFinite(line) && line > 0 && line * 2 % 2 === 1;
642
+ }
643
+ function marketEntry(m) {
644
+ return Object.fromEntries(Object.entries(m).map(([k, v]) => [k.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`), v]));
645
+ }
646
+ function toCreateMarketResult(raw, index) {
647
+ return { ...raw, index, market_id: raw.market_id ?? null, reason: raw.reason ?? null };
648
+ }
649
+ function rejected(index, reason) {
650
+ return { index, status: "rejected", market_id: null, reason };
651
+ }
652
+ function checkMarket(m, players) {
653
+ if (m.rule !== "player_stat_line") return { market: m };
654
+ if (!isValidLine(m.line)) return { reason: "invalid_line" };
655
+ const stat = players.stats.find((s) => String(s.stat).toUpperCase() === String(m.stat).toUpperCase());
656
+ if (!stat) return { reason: "unsupported_stat" };
657
+ const ids2 = new Set(players.teams.flatMap((t) => t.players.map((p) => p.player_id)));
658
+ if (!ids2.has(m.playerId)) return { reason: "unknown_player" };
659
+ return { market: { ...m, stat: stat.stat } };
660
+ }
661
+
502
662
  // src/paging.ts
503
663
  var Page = class {
504
664
  items;
@@ -809,7 +969,7 @@ function credentialsFromSettings(keyId, privateKey, signer) {
809
969
  }
810
970
 
811
971
  // src/version.ts
812
- var VERSION = "0.7.0";
972
+ var VERSION = "0.9.0-alpha.1";
813
973
  var USER_AGENT = `stx-typescript/${VERSION}`;
814
974
  var ASYNC_DISPOSE = Symbol.asyncDispose ?? /* @__PURE__ */ Symbol.for("Symbol.asyncDispose");
815
975
 
@@ -1140,7 +1300,8 @@ function buildRequest(operationId, args = {}) {
1140
1300
  if (unknown.length) throw new TypeError(`${operationId} does not take ${unknown.sort().join(", ")}`);
1141
1301
  const qs = encodeQuery(query);
1142
1302
  if (qs) path = `${path}?${qs}`;
1143
- const body = op.hasBody || args.body !== void 0 ? JSON.stringify(args.body ?? {}) : void 0;
1303
+ const declared = op.hasBody && op.method !== "DELETE";
1304
+ const body = declared || args.body !== void 0 ? JSON.stringify(args.body ?? {}) : void 0;
1144
1305
  return {
1145
1306
  operationId,
1146
1307
  method: op.method,
@@ -1533,6 +1694,81 @@ var STX = class _STX {
1533
1694
  return this.#iterate("events_get", _STX.#eventsQuery({ ...query, cursor: void 0 }));
1534
1695
  }
1535
1696
  // ------------------------------------------------------------------
1697
+ // Creating markets
1698
+ // ------------------------------------------------------------------
1699
+ /**
1700
+ * `GET /api/v1/events/{event_id}/players`: the event's team rosters and the
1701
+ * stats a market on it can use. `teams` is empty until the rosters are
1702
+ * published. Needs an OAuth app token with `events`, or an API key.
1703
+ */
1704
+ eventPlayers(eventId) {
1705
+ return this.#one("events__id_players_get", { pathParams: { event_id: eventId } });
1706
+ }
1707
+ /** The stats `event` accepts for new markets. */
1708
+ async statsFor(event) {
1709
+ const eventId = typeof event === "string" ? event : event.event_id;
1710
+ if (!eventId) throw new TypeError("statsFor needs an event id");
1711
+ return (await this.eventPlayers(eventId)).stats;
1712
+ }
1713
+ /**
1714
+ * `GET /api/v1/types`: the market types, keyed by rule. One with
1715
+ * `partner_create: true` can be created with `createMarkets()`, and lists
1716
+ * the `fields` an entry takes and, per event type, the `stats` on offer. It
1717
+ * changes only when STX adds a rule or a stat, so read it once and keep it.
1718
+ * Needs an OAuth app token with `events`, or an API key.
1719
+ */
1720
+ marketTypes() {
1721
+ return this.#one("types_get");
1722
+ }
1723
+ /**
1724
+ * `POST /api/v1/markets`: create markets on a scheduled event, up to
1725
+ * `MAX_MARKETS_PER_REQUEST` per call. Each market names its `rule` and the
1726
+ * fields that rule takes. Needs an OAuth app token with `markets.write`,
1727
+ * which STX grants, from an address on the client's IP allow-list (it
1728
+ * applies to every call that client makes).
1729
+ *
1730
+ * Returns one result per market, in the order given, each with its `index`:
1731
+ * `created` or `exists` (both with `market_id`), `valid` (a `dryRun` market
1732
+ * that would be created), or `rejected` with a `reason`. A rejected market
1733
+ * is a result, not an error, and resending is safe: a market that is
1734
+ * already listed comes back `exists`. A refused request throws (`403`
1735
+ * without `markets.write`, `404` unknown event, `422` event not open).
1736
+ *
1737
+ * `dryRun` runs every check and creates nothing. More than
1738
+ * `MAX_MARKETS_PER_REQUEST` markets throws `TypeError` before any request.
1739
+ * `validate` also needs the `events` scope.
1740
+ */
1741
+ async createMarkets(eventId, markets, opts = {}) {
1742
+ if (markets.length < 1 || markets.length > MAX_MARKETS_PER_REQUEST) {
1743
+ throw new TypeError(`createMarkets takes 1 to ${MAX_MARKETS_PER_REQUEST} markets; got ${markets.length}`);
1744
+ }
1745
+ const results = new Array(markets.length);
1746
+ let send = markets.map((m, i) => ({ m, i }));
1747
+ if (opts.validate) {
1748
+ const players = await this.eventPlayers(eventId);
1749
+ send = send.flatMap(({ m, i }) => {
1750
+ const checked = checkMarket(m, players);
1751
+ if ("market" in checked) return [{ m: checked.market, i }];
1752
+ results[i] = rejected(i, checked.reason);
1753
+ return [];
1754
+ });
1755
+ }
1756
+ if (send.length) {
1757
+ const body = {
1758
+ event_id: eventId,
1759
+ ...opts.dryRun ? { dry_run: true } : {},
1760
+ markets: send.map(({ m }) => marketEntry(m))
1761
+ };
1762
+ const raw = await this.#one("markets_post", { body });
1763
+ (raw.markets ?? []).forEach((r, k) => {
1764
+ const sent = typeof r.index === "number" ? r.index : k;
1765
+ const slot = send[sent];
1766
+ if (slot) results[slot.i] = toCreateMarketResult(r, slot.i);
1767
+ });
1768
+ }
1769
+ return Array.from(results, (r, i) => r ?? rejected(i, "not_created"));
1770
+ }
1771
+ // ------------------------------------------------------------------
1536
1772
  // Orders
1537
1773
  // ------------------------------------------------------------------
1538
1774
  static #ordersQuery(q) {
@@ -2739,6 +2975,9 @@ export {
2739
2975
  baseUrlFor,
2740
2976
  socketUrlFor,
2741
2977
  OPERATIONS,
2978
+ Stats,
2979
+ MAX_MARKETS_PER_REQUEST,
2980
+ isValidLine,
2742
2981
  Page,
2743
2982
  RetryPolicy,
2744
2983
  NO_RETRY,
@@ -2771,4 +3010,4 @@ export {
2771
3010
  orderBody,
2772
3011
  STX
2773
3012
  };
2774
- //# sourceMappingURL=chunk-CDXZOW53.js.map
3013
+ //# sourceMappingURL=chunk-4CSBA2YV.js.map