@stxapp/stx-typescript 0.8.3 → 0.9.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/CHANGELOG.md +36 -0
- package/README.md +42 -0
- package/dist/{chunk-SIYERAKK.js → chunk-46XK6VDD.js} +406 -27
- package/dist/chunk-46XK6VDD.js.map +1 -0
- package/dist/{chunk-SI3EILHN.cjs → chunk-R77AZ5UH.cjs} +470 -91
- package/dist/chunk-R77AZ5UH.cjs.map +1 -0
- package/dist/{client-BrHjO1__.d.ts → client-B-MMKiRt.d.cts} +249 -17
- package/dist/{client-BrHjO1__.d.cts → client-B-MMKiRt.d.ts} +249 -17
- package/dist/index.cjs +14 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +13 -1
- package/dist/oauth.cjs +566 -94
- package/dist/oauth.cjs.map +1 -1
- package/dist/oauth.d.cts +264 -19
- package/dist/oauth.d.ts +264 -19
- package/dist/oauth.js +537 -65
- package/dist/oauth.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-SI3EILHN.cjs.map +0 -1
- package/dist/chunk-SIYERAKK.js.map +0 -1
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
// src/enums.ts
|
|
2
|
-
var Region = {
|
|
3
|
-
US: "us",
|
|
4
|
-
ONTARIO: "ontario"
|
|
5
|
-
};
|
|
6
|
-
var Environment = {
|
|
7
|
-
PRODUCTION: "production",
|
|
8
|
-
DEMO: "demo"
|
|
9
|
-
};
|
|
10
|
-
|
|
11
1
|
// src/geo.ts
|
|
12
2
|
var IP_NOT_ALLOWED = /from this IP address (\S+?) is disallowed/i;
|
|
13
3
|
function geoRejection(message) {
|
|
@@ -68,6 +58,16 @@ var STXInvalidTokenException = class extends STXAuthenticationException {
|
|
|
68
58
|
};
|
|
69
59
|
var STXInsufficientScopeException = class extends STXForbiddenException {
|
|
70
60
|
};
|
|
61
|
+
var STXTermsRequiredException = class extends STXForbiddenException {
|
|
62
|
+
/** The STX page where the member accepts the terms, when the answer names it. */
|
|
63
|
+
termsUrl;
|
|
64
|
+
constructor(message, opts = {}) {
|
|
65
|
+
super(message, opts);
|
|
66
|
+
this.termsUrl = opts.termsUrl;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var STXIPNotAllowedException = class extends STXForbiddenException {
|
|
70
|
+
};
|
|
71
71
|
var STXNotFoundException = class extends STXAPIException {
|
|
72
72
|
};
|
|
73
73
|
var STXRejectedException = class extends STXAPIException {
|
|
@@ -114,8 +114,69 @@ var STXChannelException = class extends STXException {
|
|
|
114
114
|
};
|
|
115
115
|
var STXTimeoutException = class extends STXChannelException {
|
|
116
116
|
};
|
|
117
|
+
var NOT_AVAILABLE = /* @__PURE__ */ Symbol.for("@stxapp/stx-typescript.notAvailable");
|
|
118
|
+
var FEATURE_PHRASES = {
|
|
119
|
+
oauth: "OAuth is",
|
|
120
|
+
"oauth.member": "Member sign-in with OAuth is",
|
|
121
|
+
"oauth.app": "App tokens are",
|
|
122
|
+
connect: "Sign in with STX is",
|
|
123
|
+
"terms.stx_page": "Accepting terms on an STX page is",
|
|
124
|
+
"markets.create": "Creating markets is",
|
|
125
|
+
market_types: "Market types are",
|
|
126
|
+
event_players: "Event players are",
|
|
127
|
+
leaderboard: "The leaderboard is",
|
|
128
|
+
combos: "Combos are"
|
|
129
|
+
};
|
|
130
|
+
function notAvailableMessage(feature, host) {
|
|
131
|
+
const phrase = feature.startsWith("scope:") ? `The ${feature.slice("scope:".length)} scope is` : FEATURE_PHRASES[feature] ?? `${feature} is`;
|
|
132
|
+
return `${phrase} not available on ${host} yet.`;
|
|
133
|
+
}
|
|
134
|
+
var STXNotAvailableException = class _STXNotAvailableException extends STXAPIException {
|
|
135
|
+
/** The feature id, e.g. `"combos"`. */
|
|
136
|
+
feature;
|
|
137
|
+
/** The exchange host that lacks it, e.g. `"demo.stxapp.ca"`. */
|
|
138
|
+
host;
|
|
139
|
+
constructor(feature, host, opts = {}) {
|
|
140
|
+
super(opts.message ?? notAvailableMessage(feature, host), opts);
|
|
141
|
+
this.feature = feature;
|
|
142
|
+
this.host = host;
|
|
143
|
+
markNotAvailable(this);
|
|
144
|
+
}
|
|
145
|
+
// `this` is the class on the right of `instanceof`: a subclass keeps the
|
|
146
|
+
// ordinary check, only the base class also accepts marked exceptions.
|
|
147
|
+
static [Symbol.hasInstance](value) {
|
|
148
|
+
const cls = this;
|
|
149
|
+
if (Function.prototype[Symbol.hasInstance].call(cls, value)) return true;
|
|
150
|
+
return cls === _STXNotAvailableException && typeof value === "object" && value !== null && value[NOT_AVAILABLE] === true;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
function markNotAvailable(err) {
|
|
154
|
+
Object.defineProperty(err, NOT_AVAILABLE, { value: true, enumerable: false });
|
|
155
|
+
}
|
|
117
156
|
var STXCombosUnavailableException = class extends STXChannelException {
|
|
157
|
+
feature = "combos";
|
|
158
|
+
/** The exchange host that lacks combos. */
|
|
159
|
+
host;
|
|
160
|
+
constructor(message, opts = {}) {
|
|
161
|
+
super(message, opts);
|
|
162
|
+
this.host = opts.host ?? "";
|
|
163
|
+
markNotAvailable(this);
|
|
164
|
+
}
|
|
118
165
|
};
|
|
166
|
+
function isMissingRoute(err) {
|
|
167
|
+
if (!(err instanceof STXNotFoundException)) return false;
|
|
168
|
+
const body = err.body;
|
|
169
|
+
return !!body && typeof body === "object" && body.error === "not_found" && Object.keys(body).length === 1;
|
|
170
|
+
}
|
|
171
|
+
function isEdgeBlock(status, text) {
|
|
172
|
+
if (status !== 403) return false;
|
|
173
|
+
try {
|
|
174
|
+
JSON.parse(text);
|
|
175
|
+
return false;
|
|
176
|
+
} catch {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
119
180
|
var STXComboRefusedException = class extends STXChannelException {
|
|
120
181
|
command;
|
|
121
182
|
code;
|
|
@@ -150,6 +211,16 @@ function errorForStatus(statusCode, body, opts) {
|
|
|
150
211
|
case 401:
|
|
151
212
|
return bearerError(opts.wwwAuthenticate) === "invalid_token" ? new STXInvalidTokenException(message, base) : new STXAuthenticationException(message, base);
|
|
152
213
|
case 403: {
|
|
214
|
+
const fields = body && typeof body === "object" ? body : {};
|
|
215
|
+
if (fields.error === "ip_not_allowed") {
|
|
216
|
+
const description = typeof fields.error_description === "string" ? fields.error_description : message;
|
|
217
|
+
return new STXIPNotAllowedException(description, base);
|
|
218
|
+
}
|
|
219
|
+
if (fields.error === "terms_required") {
|
|
220
|
+
const description = typeof fields.error_description === "string" ? fields.error_description : message;
|
|
221
|
+
const termsUrl = typeof fields.terms_url === "string" ? fields.terms_url : void 0;
|
|
222
|
+
return new STXTermsRequiredException(description, { ...base, termsUrl });
|
|
223
|
+
}
|
|
153
224
|
if (bearerError(opts.wwwAuthenticate) === "insufficient_scope" || /insufficient scope/i.test(message)) {
|
|
154
225
|
return new STXInsufficientScopeException(message, {
|
|
155
226
|
...base,
|
|
@@ -188,6 +259,147 @@ function insufficientScopes(header, body) {
|
|
|
188
259
|
return raw ? raw.split(/\s+/).filter(Boolean) : [];
|
|
189
260
|
}
|
|
190
261
|
|
|
262
|
+
// src/capabilities.ts
|
|
263
|
+
var FEATURES = [
|
|
264
|
+
"oauth",
|
|
265
|
+
"oauth.member",
|
|
266
|
+
"oauth.app",
|
|
267
|
+
"connect",
|
|
268
|
+
"terms.stx_page",
|
|
269
|
+
"markets.create",
|
|
270
|
+
"market_types",
|
|
271
|
+
"event_players",
|
|
272
|
+
"leaderboard",
|
|
273
|
+
"combos"
|
|
274
|
+
];
|
|
275
|
+
var MEMBER_SCOPES = [
|
|
276
|
+
"profile.read",
|
|
277
|
+
"balance.read",
|
|
278
|
+
"portfolio.read",
|
|
279
|
+
"orders.read",
|
|
280
|
+
"transfers.read",
|
|
281
|
+
"orders.write"
|
|
282
|
+
];
|
|
283
|
+
var Capabilities = class {
|
|
284
|
+
/** The exchange host, e.g. `"demo.stxapp.io"`. */
|
|
285
|
+
host;
|
|
286
|
+
/** `GET /api_version`, e.g. `"3.3.196-rnd.9"`, or null when it did not answer. */
|
|
287
|
+
apiVersion;
|
|
288
|
+
/** When the checks ran. */
|
|
289
|
+
checkedAt;
|
|
290
|
+
/** The scopes the exchange's OAuth discovery document lists; empty without OAuth. */
|
|
291
|
+
scopesSupported;
|
|
292
|
+
/** Every feature and whether the exchange has it. */
|
|
293
|
+
features;
|
|
294
|
+
constructor(init) {
|
|
295
|
+
this.host = init.host;
|
|
296
|
+
this.apiVersion = init.apiVersion;
|
|
297
|
+
this.checkedAt = init.checkedAt;
|
|
298
|
+
this.scopesSupported = init.scopesSupported;
|
|
299
|
+
this.features = init.features;
|
|
300
|
+
}
|
|
301
|
+
/** True only when the exchange is known to have `feature`. */
|
|
302
|
+
has(feature) {
|
|
303
|
+
return this.features[feature] === true;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Throw `STXNotAvailableException` unless the exchange is known to have
|
|
307
|
+
* `feature`. A feature that could not be checked (`null`) does not throw:
|
|
308
|
+
* the call itself will say.
|
|
309
|
+
*/
|
|
310
|
+
require(feature) {
|
|
311
|
+
if (this.features[feature] === false) throw new STXNotAvailableException(feature, this.host);
|
|
312
|
+
}
|
|
313
|
+
toJSON() {
|
|
314
|
+
return {
|
|
315
|
+
host: this.host,
|
|
316
|
+
apiVersion: this.apiVersion,
|
|
317
|
+
checkedAt: this.checkedAt.toISOString(),
|
|
318
|
+
scopesSupported: this.scopesSupported,
|
|
319
|
+
features: this.features
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
var NOBODY = "00000000-0000-0000-0000-000000000000";
|
|
324
|
+
function json(answer) {
|
|
325
|
+
if (!answer) return null;
|
|
326
|
+
try {
|
|
327
|
+
const parsed = JSON.parse(answer.text);
|
|
328
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null;
|
|
329
|
+
} catch {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function strings(value) {
|
|
334
|
+
return Array.isArray(value) ? value.filter((v) => typeof v === "string") : [];
|
|
335
|
+
}
|
|
336
|
+
function missingRoute(answer) {
|
|
337
|
+
if (!answer || answer.status !== 404) return false;
|
|
338
|
+
const body = json(answer);
|
|
339
|
+
return !!body && body.error === "not_found" && Object.keys(body).length === 1;
|
|
340
|
+
}
|
|
341
|
+
function oauthMetadataFrom(answer) {
|
|
342
|
+
if (!answer) return null;
|
|
343
|
+
const body = json(answer);
|
|
344
|
+
if (answer.status === 404) return false;
|
|
345
|
+
if (answer.status === 403 && !body) return false;
|
|
346
|
+
if (answer.status !== 200 || !body) return null;
|
|
347
|
+
if (typeof body.issuer !== "string" || typeof body.authorization_endpoint !== "string" || typeof body.token_endpoint !== "string") {
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
return body;
|
|
351
|
+
}
|
|
352
|
+
async function discoverCapabilities(probes) {
|
|
353
|
+
const swallow = (p) => p ? p.catch(() => null) : Promise.resolve(null);
|
|
354
|
+
const [version, discovery, types, players, board, combos] = await Promise.all([
|
|
355
|
+
swallow(probes.get("/api_version")),
|
|
356
|
+
swallow(probes.get("/.well-known/oauth-authorization-server")),
|
|
357
|
+
swallow(probes.signedGet("/api/v1/types")),
|
|
358
|
+
swallow(probes.signedGet(`/api/v1/events/${NOBODY}/players`)),
|
|
359
|
+
swallow(probes.signedGet("/api/v1/leaderboard?limit=1")),
|
|
360
|
+
swallow(probes.joinReason(`rfq:${NOBODY}`))
|
|
361
|
+
]);
|
|
362
|
+
const apiVersion = json(version)?.api_version;
|
|
363
|
+
const meta = oauthMetadataFrom(discovery);
|
|
364
|
+
const scopes = meta ? strings(meta.scopes_supported) : [];
|
|
365
|
+
const grants = meta ? strings(meta.grant_types_supported) : [];
|
|
366
|
+
const fromMeta = (decide) => meta === null ? null : meta === false ? false : decide();
|
|
367
|
+
const oauth = fromMeta(() => true);
|
|
368
|
+
const member = fromMeta(() => grants.includes("authorization_code") && scopes.some((s) => MEMBER_SCOPES.includes(s)));
|
|
369
|
+
const app = fromMeta(() => grants.includes("client_credentials"));
|
|
370
|
+
const features = {
|
|
371
|
+
oauth,
|
|
372
|
+
"oauth.member": member,
|
|
373
|
+
"oauth.app": app,
|
|
374
|
+
connect: fromMeta(() => scopes.includes("openid")),
|
|
375
|
+
"terms.stx_page": fromMeta(() => member === true && !scopes.includes("terms.write")),
|
|
376
|
+
"markets.create": fromMeta(() => app === true && scopes.includes("markets.write")),
|
|
377
|
+
market_types: types === null ? null : types.status >= 200 && types.status < 300 ? true : missingRoute(types) ? false : null,
|
|
378
|
+
// A 404 about the event (not about the route) means the route is there.
|
|
379
|
+
event_players: players === null ? null : missingRoute(players) ? false : players.status === 404 || players.status < 300 ? true : null,
|
|
380
|
+
// Switched off answers 404 too ("Leaderboard is not enabled").
|
|
381
|
+
leaderboard: board === null ? null : board.status >= 200 && board.status < 300 ? true : board.status === 404 ? false : null,
|
|
382
|
+
combos: combos === null ? null : combos !== "unmatched topic"
|
|
383
|
+
};
|
|
384
|
+
return new Capabilities({
|
|
385
|
+
host: probes.host,
|
|
386
|
+
apiVersion: typeof apiVersion === "string" ? apiVersion : null,
|
|
387
|
+
checkedAt: /* @__PURE__ */ new Date(),
|
|
388
|
+
scopesSupported: scopes,
|
|
389
|
+
features
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/enums.ts
|
|
394
|
+
var Region = {
|
|
395
|
+
US: "us",
|
|
396
|
+
ONTARIO: "ontario"
|
|
397
|
+
};
|
|
398
|
+
var Environment = {
|
|
399
|
+
PRODUCTION: "production",
|
|
400
|
+
DEMO: "demo"
|
|
401
|
+
};
|
|
402
|
+
|
|
191
403
|
// src/config.ts
|
|
192
404
|
var Environments = {
|
|
193
405
|
/** United States demo. Shared by every integrator. No real money. */
|
|
@@ -571,6 +783,17 @@ var OPERATIONS = {
|
|
|
571
783
|
model: "",
|
|
572
784
|
isList: false,
|
|
573
785
|
paginated: false
|
|
786
|
+
},
|
|
787
|
+
types_get: {
|
|
788
|
+
method: "GET",
|
|
789
|
+
path: "/api/v1/types",
|
|
790
|
+
query: [],
|
|
791
|
+
pathParams: [],
|
|
792
|
+
hasBody: false,
|
|
793
|
+
responseKey: "",
|
|
794
|
+
model: "",
|
|
795
|
+
isList: false,
|
|
796
|
+
paginated: false
|
|
574
797
|
}
|
|
575
798
|
};
|
|
576
799
|
|
|
@@ -986,7 +1209,7 @@ function credentialsFromSettings(keyId, privateKey, signer) {
|
|
|
986
1209
|
}
|
|
987
1210
|
|
|
988
1211
|
// src/version.ts
|
|
989
|
-
var VERSION = "0.
|
|
1212
|
+
var VERSION = "0.9.0";
|
|
990
1213
|
var USER_AGENT = `stx-typescript/${VERSION}`;
|
|
991
1214
|
var ASYNC_DISPOSE = Symbol.asyncDispose ?? /* @__PURE__ */ Symbol.for("Symbol.asyncDispose");
|
|
992
1215
|
|
|
@@ -1187,13 +1410,14 @@ var MAKER_EVENTS = /* @__PURE__ */ new Set([
|
|
|
1187
1410
|
"placed",
|
|
1188
1411
|
"negotiation_closed"
|
|
1189
1412
|
]);
|
|
1190
|
-
function comboRefusal(command, topic, response) {
|
|
1413
|
+
function comboRefusal(command, topic, response, host = "") {
|
|
1191
1414
|
const reply = response && typeof response === "object" ? response : {};
|
|
1192
1415
|
const reason = typeof reply.reason === "string" ? reply.reason : void 0;
|
|
1193
1416
|
if (reason === UNAVAILABLE_REASON) {
|
|
1194
|
-
return new STXCombosUnavailableException("
|
|
1417
|
+
return new STXCombosUnavailableException(notAvailableMessage("combos", host || "this exchange"), {
|
|
1195
1418
|
topic,
|
|
1196
|
-
reply: response
|
|
1419
|
+
reply: response,
|
|
1420
|
+
host
|
|
1197
1421
|
});
|
|
1198
1422
|
}
|
|
1199
1423
|
if (reason === void 0) {
|
|
@@ -1226,10 +1450,10 @@ function comboRefusal(command, topic, response) {
|
|
|
1226
1450
|
}
|
|
1227
1451
|
return new STXComboRefusedException(message, { topic, reply: response, command, code, detail });
|
|
1228
1452
|
}
|
|
1229
|
-
function comboJoinError(err) {
|
|
1453
|
+
function comboJoinError(err, host = "") {
|
|
1230
1454
|
if (err instanceof STXChannelException && !(err instanceof STXCombosUnavailableException) && err.reply !== void 0) {
|
|
1231
1455
|
if (err.constructor !== STXChannelException) return err;
|
|
1232
|
-
return comboRefusal("phx_join", err.topic, err.reply);
|
|
1456
|
+
return comboRefusal("phx_join", err.topic, err.reply, host);
|
|
1233
1457
|
}
|
|
1234
1458
|
return err;
|
|
1235
1459
|
}
|
|
@@ -1340,7 +1564,16 @@ var ComboSession = class {
|
|
|
1340
1564
|
this.#waiters.push(waiter);
|
|
1341
1565
|
});
|
|
1342
1566
|
}
|
|
1343
|
-
/**
|
|
1567
|
+
/**
|
|
1568
|
+
* Leave the topic. Requests opened and quotes placed on this channel end
|
|
1569
|
+
* (`cancelled`), as on any close, and delivery on this channel stops when
|
|
1570
|
+
* `leave()` resolves: the exchange's `negotiation_closed` for those requests
|
|
1571
|
+
* arrives after the channel has closed and is not passed to `onEvent` or to
|
|
1572
|
+
* an iterator. Treat every request still open here as cancelled once
|
|
1573
|
+
* `leave()` resolves. To see each request's own close first, `cancel()` it
|
|
1574
|
+
* and wait for its `negotiation_closed` before leaving. Other connections of
|
|
1575
|
+
* the same member still receive the close.
|
|
1576
|
+
*/
|
|
1344
1577
|
leave() {
|
|
1345
1578
|
return this.channel.leave();
|
|
1346
1579
|
}
|
|
@@ -1713,6 +1946,15 @@ function ids(value) {
|
|
|
1713
1946
|
return typeof value === "string" ? [value] : [...value];
|
|
1714
1947
|
}
|
|
1715
1948
|
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1949
|
+
var GATED_OPERATIONS = {
|
|
1950
|
+
types_get: "market_types",
|
|
1951
|
+
events__id_players_get: "event_players",
|
|
1952
|
+
markets_post: "markets.create",
|
|
1953
|
+
leaderboard_get: "leaderboard",
|
|
1954
|
+
leaderboard_me_get: "leaderboard",
|
|
1955
|
+
me_profile_patch: "leaderboard"
|
|
1956
|
+
};
|
|
1957
|
+
var LEADERBOARD_OFF = "Leaderboard is not enabled";
|
|
1716
1958
|
var STX = class _STX {
|
|
1717
1959
|
baseUrl;
|
|
1718
1960
|
socketUrl;
|
|
@@ -1728,6 +1970,7 @@ var STX = class _STX {
|
|
|
1728
1970
|
onResponse;
|
|
1729
1971
|
#fetch;
|
|
1730
1972
|
#me;
|
|
1973
|
+
#capabilities;
|
|
1731
1974
|
/** Test hook: how retries wait. */
|
|
1732
1975
|
_sleep = sleep;
|
|
1733
1976
|
constructor(opts = {}) {
|
|
@@ -1781,6 +2024,74 @@ var STX = class _STX {
|
|
|
1781
2024
|
toString() {
|
|
1782
2025
|
return `STX(baseUrl=${JSON.stringify(this.baseUrl)}, keyId=${JSON.stringify(this.credentials?.keyId ?? null)})`;
|
|
1783
2026
|
}
|
|
2027
|
+
/** The exchange host, e.g. `"demo.stxapp.io"`: the name `STXNotAvailableException` and `capabilities()` report. */
|
|
2028
|
+
get host() {
|
|
2029
|
+
return new URL(this.baseUrl).host;
|
|
2030
|
+
}
|
|
2031
|
+
// ------------------------------------------------------------------
|
|
2032
|
+
// Capabilities
|
|
2033
|
+
// ------------------------------------------------------------------
|
|
2034
|
+
/**
|
|
2035
|
+
* Which features this exchange has, from what it publishes: its OAuth
|
|
2036
|
+
* discovery document, `/api_version`, a signed GET of the routes that not
|
|
2037
|
+
* every exchange has yet, and a join of a combos topic nobody owns. Nothing
|
|
2038
|
+
* is created or changed. Checked once per client and cached; pass
|
|
2039
|
+
* `{ refresh: true }` to check again.
|
|
2040
|
+
*
|
|
2041
|
+
* const caps = await stx.capabilities();
|
|
2042
|
+
* caps.features.combos; // false on demo.stxapp.ca
|
|
2043
|
+
* caps.require("combos"); // throws STXNotAvailableException there
|
|
2044
|
+
*
|
|
2045
|
+
* Each feature is `true`, `false`, or `null` when it could not be checked
|
|
2046
|
+
* (no credentials for a signed check, or no answer). A missing feature never
|
|
2047
|
+
* throws here; the call that needs it throws `STXNotAvailableException`.
|
|
2048
|
+
*/
|
|
2049
|
+
capabilities(opts = {}) {
|
|
2050
|
+
if (opts.refresh || !this.#capabilities) {
|
|
2051
|
+
const run = discoverCapabilities({
|
|
2052
|
+
host: this.host,
|
|
2053
|
+
get: (path) => this.#rawGet(path, false),
|
|
2054
|
+
signedGet: (path) => this.auth ? this.#rawGet(path, true) : null,
|
|
2055
|
+
joinReason: (topic) => this.#joinReason(topic)
|
|
2056
|
+
});
|
|
2057
|
+
this.#capabilities = run;
|
|
2058
|
+
run.catch(() => {
|
|
2059
|
+
if (this.#capabilities === run) this.#capabilities = void 0;
|
|
2060
|
+
});
|
|
2061
|
+
}
|
|
2062
|
+
return this.#capabilities;
|
|
2063
|
+
}
|
|
2064
|
+
async #rawGet(path, signed) {
|
|
2065
|
+
const headers = { "User-Agent": USER_AGENT, Accept: "application/json" };
|
|
2066
|
+
if (signed && this.auth) Object.assign(headers, await this.auth.authHeaders({ method: "GET", path }));
|
|
2067
|
+
const controller = new AbortController();
|
|
2068
|
+
const timer = setTimeout(() => controller.abort(), Math.min(this.timeoutMs, 15e3));
|
|
2069
|
+
try {
|
|
2070
|
+
const res = await this.#fetch(this.baseUrl + path, { method: "GET", headers, signal: controller.signal });
|
|
2071
|
+
return { status: res.status, text: await res.text() };
|
|
2072
|
+
} catch {
|
|
2073
|
+
return null;
|
|
2074
|
+
} finally {
|
|
2075
|
+
clearTimeout(timer);
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
async #joinReason(topic) {
|
|
2079
|
+
if (!this.auth) return null;
|
|
2080
|
+
const ws = this.websocket({ reconnect: false, joinTimeoutMs: 8e3 });
|
|
2081
|
+
try {
|
|
2082
|
+
const channel = await ws.join(topic);
|
|
2083
|
+
await channel.leave().catch(() => {
|
|
2084
|
+
});
|
|
2085
|
+
return "ok";
|
|
2086
|
+
} catch (err) {
|
|
2087
|
+
if (err instanceof STXTimeoutException) return null;
|
|
2088
|
+
if (err instanceof STXChannelException) return err.reason ?? null;
|
|
2089
|
+
return null;
|
|
2090
|
+
} finally {
|
|
2091
|
+
await ws.close().catch(() => {
|
|
2092
|
+
});
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
1784
2095
|
/** Nothing to release (fetch pools connections itself); present so `await using` and explicit cleanup work. */
|
|
1785
2096
|
async close() {
|
|
1786
2097
|
}
|
|
@@ -1879,7 +2190,22 @@ var STX = class _STX {
|
|
|
1879
2190
|
}
|
|
1880
2191
|
}
|
|
1881
2192
|
async #call(operationId, args = {}) {
|
|
1882
|
-
|
|
2193
|
+
try {
|
|
2194
|
+
return await this.#send(buildRequest(operationId, args));
|
|
2195
|
+
} catch (err) {
|
|
2196
|
+
const feature = GATED_OPERATIONS[operationId];
|
|
2197
|
+
const off = feature === "leaderboard" && err instanceof STXNotFoundException && err.body?.error === LEADERBOARD_OFF;
|
|
2198
|
+
if (feature && (isMissingRoute(err) || off)) {
|
|
2199
|
+
throw new STXNotAvailableException(feature, this.host, {
|
|
2200
|
+
statusCode: err.statusCode,
|
|
2201
|
+
body: err.body,
|
|
2202
|
+
method: err.method,
|
|
2203
|
+
path: err.path,
|
|
2204
|
+
cause: err
|
|
2205
|
+
});
|
|
2206
|
+
}
|
|
2207
|
+
throw err;
|
|
2208
|
+
}
|
|
1883
2209
|
}
|
|
1884
2210
|
async #one(operationId, args = {}) {
|
|
1885
2211
|
const body = await this.#call(operationId, args);
|
|
@@ -2015,9 +2341,21 @@ var STX = class _STX {
|
|
|
2015
2341
|
return (await this.eventPlayers(eventId)).stats;
|
|
2016
2342
|
}
|
|
2017
2343
|
/**
|
|
2018
|
-
* `
|
|
2019
|
-
*
|
|
2020
|
-
* `
|
|
2344
|
+
* `GET /api/v1/types`: the market types, keyed by rule. One with
|
|
2345
|
+
* `partner_create: true` can be created with `createMarkets()`, and lists
|
|
2346
|
+
* the `fields` an entry takes and, per event type, the `stats` on offer. It
|
|
2347
|
+
* changes only when STX adds a rule or a stat, so read it once and keep it.
|
|
2348
|
+
* Needs an OAuth app token with `events`, or an API key.
|
|
2349
|
+
*/
|
|
2350
|
+
marketTypes() {
|
|
2351
|
+
return this.#one("types_get");
|
|
2352
|
+
}
|
|
2353
|
+
/**
|
|
2354
|
+
* `POST /api/v1/markets`: create markets on a scheduled event, up to
|
|
2355
|
+
* `MAX_MARKETS_PER_REQUEST` per call. Each market names its `rule` and the
|
|
2356
|
+
* fields that rule takes. Needs an OAuth app token with `markets.write`,
|
|
2357
|
+
* which STX grants, from an address on the client's IP allow-list (it
|
|
2358
|
+
* applies to every call that client makes).
|
|
2021
2359
|
*
|
|
2022
2360
|
* Returns one result per market, in order: `created` or `exists` (both with
|
|
2023
2361
|
* `market_id`), `valid` with `dryRun`, or `rejected` with a `reason`. Each
|
|
@@ -2284,9 +2622,41 @@ var STX = class _STX {
|
|
|
2284
2622
|
return this.#iterate("portfolio_loyalty_get", { limit: query.limit });
|
|
2285
2623
|
}
|
|
2286
2624
|
// ------------------------------------------------------------------
|
|
2625
|
+
// Leaderboard and public profile
|
|
2626
|
+
// ------------------------------------------------------------------
|
|
2627
|
+
/**
|
|
2628
|
+
* `GET /api/v1/leaderboard`: ranked members for a period, sport and metric.
|
|
2629
|
+
* Rows carry a public handle and avatar, never an account id. Throws
|
|
2630
|
+
* `STXNotAvailableException` (feature `leaderboard`) where the exchange does
|
|
2631
|
+
* not have the leaderboard.
|
|
2632
|
+
*/
|
|
2633
|
+
leaderboard(query = {}) {
|
|
2634
|
+
const { period, category, metric, limit } = query;
|
|
2635
|
+
return this.#one("leaderboard_get", { query: { period, category, metric, limit } });
|
|
2636
|
+
}
|
|
2637
|
+
/** `GET /api/v1/leaderboard/me`: your own standing, including your rank outside the list. */
|
|
2638
|
+
leaderboardMe(query = {}) {
|
|
2639
|
+
return this.#one("leaderboard_me_get", { query: { period: query.period, category: query.category } });
|
|
2640
|
+
}
|
|
2641
|
+
/**
|
|
2642
|
+
* `PATCH /api/v1/me/profile`: change your public handle, avatar or
|
|
2643
|
+
* leaderboard opt-in. Resolves to the updated `me`.
|
|
2644
|
+
*/
|
|
2645
|
+
async updateProfile(update) {
|
|
2646
|
+
this.#me = await this.#one("me_profile_patch", { body: update });
|
|
2647
|
+
return this.#me;
|
|
2648
|
+
}
|
|
2649
|
+
// ------------------------------------------------------------------
|
|
2287
2650
|
// Terms
|
|
2288
2651
|
// ------------------------------------------------------------------
|
|
2289
|
-
/**
|
|
2652
|
+
/**
|
|
2653
|
+
* `POST /api/v1/tnc/accept`: accept the current terms for your own account,
|
|
2654
|
+
* with your own API key. Resolves to the server's message.
|
|
2655
|
+
*
|
|
2656
|
+
* An app cannot accept the terms for a member: called with a member's OAuth
|
|
2657
|
+
* access token, the exchange answers 403 `terms_not_delegable`
|
|
2658
|
+
* (`STXForbiddenException`). The member accepts on an STX page instead.
|
|
2659
|
+
*/
|
|
2290
2660
|
async acceptTerms(deviceId, opts = {}) {
|
|
2291
2661
|
const body = {
|
|
2292
2662
|
device_id: deviceId,
|
|
@@ -2400,6 +2770,7 @@ var MessageQueue = class {
|
|
|
2400
2770
|
});
|
|
2401
2771
|
}
|
|
2402
2772
|
};
|
|
2773
|
+
var UNPINGED_CHANNELS = /* @__PURE__ */ new Set(["market", "rfq", "combo_quote"]);
|
|
2403
2774
|
var Channel = class {
|
|
2404
2775
|
/** The wire topic, e.g. `orders:<user_id>`. */
|
|
2405
2776
|
topic;
|
|
@@ -2921,7 +3292,7 @@ var STXWebSocket = class {
|
|
|
2921
3292
|
try {
|
|
2922
3293
|
channel = await this.#joinAccount(name, await comboJoinPayload(opts.geoLocation), onMessage);
|
|
2923
3294
|
} catch (err) {
|
|
2924
|
-
throw comboJoinError(err);
|
|
3295
|
+
throw comboJoinError(err, new URL(this.rest.baseUrl).host);
|
|
2925
3296
|
}
|
|
2926
3297
|
if (typeof opts.geoLocation === "function") {
|
|
2927
3298
|
const geo = opts.geoLocation;
|
|
@@ -2943,8 +3314,9 @@ var STXWebSocket = class {
|
|
|
2943
3314
|
const existing = this.channels.get(topic);
|
|
2944
3315
|
if (existing && !existing.closed) throw new STXChannelException(`Already joined ${topic}`, { topic });
|
|
2945
3316
|
await this.connect();
|
|
3317
|
+
const name = topic.split(":", 1)[0];
|
|
2946
3318
|
const channel = new Channel(this, topic, payload, onMessage, {
|
|
2947
|
-
pingIntervalMs: opts.pingIntervalMs
|
|
3319
|
+
pingIntervalMs: opts.pingIntervalMs !== void 0 ? opts.pingIntervalMs : UNPINGED_CHANNELS.has(name) ? null : this.channelPingIntervalMs,
|
|
2948
3320
|
queueSize: this.queueSize
|
|
2949
3321
|
});
|
|
2950
3322
|
this.channels.set(topic, channel);
|
|
@@ -3298,8 +3670,6 @@ function dropUndefined(payload) {
|
|
|
3298
3670
|
}
|
|
3299
3671
|
|
|
3300
3672
|
export {
|
|
3301
|
-
Region,
|
|
3302
|
-
Environment,
|
|
3303
3673
|
geoRejection,
|
|
3304
3674
|
STXException,
|
|
3305
3675
|
STXConfigException,
|
|
@@ -3309,6 +3679,8 @@ export {
|
|
|
3309
3679
|
STXForbiddenException,
|
|
3310
3680
|
STXInvalidTokenException,
|
|
3311
3681
|
STXInsufficientScopeException,
|
|
3682
|
+
STXTermsRequiredException,
|
|
3683
|
+
STXIPNotAllowedException,
|
|
3312
3684
|
STXNotFoundException,
|
|
3313
3685
|
STXRejectedException,
|
|
3314
3686
|
STXGeoLocationException,
|
|
@@ -3317,10 +3689,17 @@ export {
|
|
|
3317
3689
|
STXTransportException,
|
|
3318
3690
|
STXChannelException,
|
|
3319
3691
|
STXTimeoutException,
|
|
3692
|
+
notAvailableMessage,
|
|
3693
|
+
STXNotAvailableException,
|
|
3320
3694
|
STXCombosUnavailableException,
|
|
3695
|
+
isEdgeBlock,
|
|
3321
3696
|
STXComboRefusedException,
|
|
3322
3697
|
STXComboPriceMovedException,
|
|
3323
3698
|
errorForStatus,
|
|
3699
|
+
FEATURES,
|
|
3700
|
+
Capabilities,
|
|
3701
|
+
Region,
|
|
3702
|
+
Environment,
|
|
3324
3703
|
Environments,
|
|
3325
3704
|
HOSTS,
|
|
3326
3705
|
environmentNamed,
|
|
@@ -3365,4 +3744,4 @@ export {
|
|
|
3365
3744
|
orderBody,
|
|
3366
3745
|
STX
|
|
3367
3746
|
};
|
|
3368
|
-
//# sourceMappingURL=chunk-
|
|
3747
|
+
//# sourceMappingURL=chunk-46XK6VDD.js.map
|