@skinpricer/sdk 0.4.0 → 0.6.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 +47 -0
- package/README.md +230 -21
- package/dist/index.cjs +600 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +805 -105
- package/dist/index.d.ts +805 -105
- package/dist/index.js +599 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -81,14 +81,18 @@ var BaseResource = class {
|
|
|
81
81
|
http;
|
|
82
82
|
game;
|
|
83
83
|
/** Performs a request and returns the data with {@link ResponseMeta} attached. */
|
|
84
|
-
async call(input, options) {
|
|
84
|
+
async call(input, options, readBody) {
|
|
85
85
|
assertPathSegments(input.path);
|
|
86
86
|
const scoped = this.game === void 0 ? input : {
|
|
87
87
|
...input,
|
|
88
88
|
baseUrl: this.http.versionedBaseUrl("v2", input.baseUrl),
|
|
89
89
|
query: { ...input.query, game: this.game }
|
|
90
90
|
};
|
|
91
|
-
const { data, meta } = await this.http.request(
|
|
91
|
+
const { data, meta } = await this.http.request(
|
|
92
|
+
scoped,
|
|
93
|
+
options,
|
|
94
|
+
readBody
|
|
95
|
+
);
|
|
92
96
|
return attachMeta(data, meta);
|
|
93
97
|
}
|
|
94
98
|
};
|
|
@@ -101,7 +105,9 @@ function historyQuery(params) {
|
|
|
101
105
|
markets: params.markets,
|
|
102
106
|
listingType: params.listingType,
|
|
103
107
|
salesWindow: params.salesWindow,
|
|
104
|
-
interval: params.interval
|
|
108
|
+
interval: params.interval,
|
|
109
|
+
basis: params.basis,
|
|
110
|
+
currency: params.currency
|
|
105
111
|
};
|
|
106
112
|
}
|
|
107
113
|
var HistoryResource = class extends BaseResource {
|
|
@@ -128,6 +134,43 @@ var HistoryResource = class extends BaseResource {
|
|
|
128
134
|
);
|
|
129
135
|
}
|
|
130
136
|
};
|
|
137
|
+
var GameHistoryResource = class extends HistoryResource {
|
|
138
|
+
get(marketHashName, params = {}, options) {
|
|
139
|
+
return super.get(marketHashName, params, options);
|
|
140
|
+
}
|
|
141
|
+
byMarket(marketHashName, params = {}, options) {
|
|
142
|
+
return super.byMarket(marketHashName, params, options);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* `POST /v2/pricing/history/batch` — the aggregate series of {@link get} for
|
|
146
|
+
* up to 100 items. Entries that do not resolve are listed in `errors` and
|
|
147
|
+
* the rest are still served.
|
|
148
|
+
*
|
|
149
|
+
* Items x buckets may not exceed 10,000 points (twice the buckets for
|
|
150
|
+
* `SALE_HISTORY`), and a batch in 10m buckets takes at most 25 items; the
|
|
151
|
+
* API rejects a larger batch with a 400 error. Each distinct item costs 5
|
|
152
|
+
* rate-limit units.
|
|
153
|
+
*/
|
|
154
|
+
batch(params, options) {
|
|
155
|
+
return this.call(
|
|
156
|
+
{
|
|
157
|
+
method: "POST",
|
|
158
|
+
path: "/pricing/history/batch",
|
|
159
|
+
body: {
|
|
160
|
+
items: params.items,
|
|
161
|
+
from: params.from,
|
|
162
|
+
to: params.to,
|
|
163
|
+
interval: params.interval,
|
|
164
|
+
listingType: params.listingType,
|
|
165
|
+
salesWindow: params.salesWindow,
|
|
166
|
+
basis: params.basis,
|
|
167
|
+
currency: params.currency
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
options
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
131
174
|
|
|
132
175
|
// src/resources/items.ts
|
|
133
176
|
var ItemSearchResource = class extends BaseResource {
|
|
@@ -263,6 +306,14 @@ var MarketsResource = class extends BaseResource {
|
|
|
263
306
|
};
|
|
264
307
|
|
|
265
308
|
// src/resources/nbbo.ts
|
|
309
|
+
function nbboQuery(params) {
|
|
310
|
+
return {
|
|
311
|
+
id: params.id,
|
|
312
|
+
marketHashName: params.marketHashName,
|
|
313
|
+
markets: params.markets,
|
|
314
|
+
currency: params.currency
|
|
315
|
+
};
|
|
316
|
+
}
|
|
266
317
|
var NbboResource = class extends BaseResource {
|
|
267
318
|
/** `GET /v1/nbbo` — best bid/ask across markets. Pass `id` or `marketHashName`. */
|
|
268
319
|
get(params, options) {
|
|
@@ -270,11 +321,7 @@ var NbboResource = class extends BaseResource {
|
|
|
270
321
|
{
|
|
271
322
|
method: "GET",
|
|
272
323
|
path: "/nbbo",
|
|
273
|
-
query:
|
|
274
|
-
id: params.id,
|
|
275
|
-
market_hash_name: params.marketHashName,
|
|
276
|
-
markets: params.markets
|
|
277
|
-
}
|
|
324
|
+
query: nbboQuery(params)
|
|
278
325
|
},
|
|
279
326
|
options
|
|
280
327
|
);
|
|
@@ -287,7 +334,7 @@ var NbboResource = class extends BaseResource {
|
|
|
287
334
|
path: "/nbbo/depth",
|
|
288
335
|
query: {
|
|
289
336
|
id: params.id,
|
|
290
|
-
|
|
337
|
+
marketHashName: params.marketHashName,
|
|
291
338
|
markets: params.markets,
|
|
292
339
|
depth: params.depth
|
|
293
340
|
}
|
|
@@ -296,8 +343,16 @@ var NbboResource = class extends BaseResource {
|
|
|
296
343
|
);
|
|
297
344
|
}
|
|
298
345
|
};
|
|
346
|
+
var GameNbboResource = class extends NbboResource {
|
|
347
|
+
get(params, options) {
|
|
348
|
+
return super.get(params, options);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
299
351
|
|
|
300
352
|
// src/resources/pricing.ts
|
|
353
|
+
function currentPriceQuery(params) {
|
|
354
|
+
return { markets: params.markets, currency: params.currency };
|
|
355
|
+
}
|
|
301
356
|
var PricingResource = class extends BaseResource {
|
|
302
357
|
/** `GET /v1/pricing/:marketHashName` — full cross-market pricing snapshot. */
|
|
303
358
|
get(marketHashName, params = {}, options) {
|
|
@@ -305,7 +360,7 @@ var PricingResource = class extends BaseResource {
|
|
|
305
360
|
{
|
|
306
361
|
method: "GET",
|
|
307
362
|
path: `/pricing/${encodePathSegment(marketHashName)}`,
|
|
308
|
-
query:
|
|
363
|
+
query: currentPriceQuery(params)
|
|
309
364
|
},
|
|
310
365
|
options
|
|
311
366
|
);
|
|
@@ -322,6 +377,11 @@ var PricingResource = class extends BaseResource {
|
|
|
322
377
|
);
|
|
323
378
|
}
|
|
324
379
|
};
|
|
380
|
+
var GamePricingResource = class extends PricingResource {
|
|
381
|
+
get(marketHashName, params = {}, options) {
|
|
382
|
+
return super.get(marketHashName, params, options);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
325
385
|
|
|
326
386
|
// src/resources/schema.ts
|
|
327
387
|
var SchemaResource = class extends BaseResource {
|
|
@@ -374,10 +434,10 @@ var GameClient = class {
|
|
|
374
434
|
if (game !== "cs2" && game !== "rust") {
|
|
375
435
|
throw new ConfigurationError('The game must be "cs2" or "rust".');
|
|
376
436
|
}
|
|
377
|
-
this.pricing = new
|
|
378
|
-
this.history = new
|
|
437
|
+
this.pricing = new GamePricingResource(http, game);
|
|
438
|
+
this.history = new GameHistoryResource(http, game);
|
|
379
439
|
this.items = new ItemSearchResource(http, game);
|
|
380
|
-
this.nbbo = new
|
|
440
|
+
this.nbbo = new GameNbboResource(http, game);
|
|
381
441
|
this.liquidity = new ItemLiquidityResource(http, game);
|
|
382
442
|
this.schema = new SchemaResource(http, game);
|
|
383
443
|
this.markets = new MarketsResource(http, game);
|
|
@@ -393,7 +453,7 @@ var GameClient = class {
|
|
|
393
453
|
};
|
|
394
454
|
|
|
395
455
|
// src/version.ts
|
|
396
|
-
var VERSION = "0.
|
|
456
|
+
var VERSION = "0.6.0";
|
|
397
457
|
|
|
398
458
|
// src/config.ts
|
|
399
459
|
var DEFAULT_BASE_URL = "https://pricing.skinpricer.com/v1";
|
|
@@ -427,6 +487,15 @@ function resolveFetch(custom) {
|
|
|
427
487
|
"No global `fetch` is available. Provide a `fetch` implementation in the client config (required on Node < 18 or non-fetch runtimes)."
|
|
428
488
|
);
|
|
429
489
|
}
|
|
490
|
+
function globalWebSocket(url, headers) {
|
|
491
|
+
const WebSocket = globalThis.WebSocket;
|
|
492
|
+
if (typeof WebSocket !== "function") {
|
|
493
|
+
throw new ConfigurationError(
|
|
494
|
+
"No global `WebSocket` is available. Provide `webSocket` in the client config (required on Node < 22), for example with the ws package."
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
return new WebSocket(url, { headers });
|
|
498
|
+
}
|
|
430
499
|
function resolveRetry(retry) {
|
|
431
500
|
if (retry === false) return null;
|
|
432
501
|
if (retry === void 0) return DEFAULT_RETRY;
|
|
@@ -463,6 +532,7 @@ function resolveConfig(config) {
|
|
|
463
532
|
config.publicBaseUrl ?? derivePublicBaseUrl(baseUrl)
|
|
464
533
|
),
|
|
465
534
|
fetch: resolveFetch(config.fetch),
|
|
535
|
+
webSocket: config.webSocket ?? globalWebSocket,
|
|
466
536
|
timeoutMs: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
467
537
|
retry: resolveRetry(config.retry),
|
|
468
538
|
headers: { ...config.headers },
|
|
@@ -564,6 +634,16 @@ function extractErrorFields(body, status) {
|
|
|
564
634
|
const message = typeof err.message === "string" ? err.message : defaultMessage(status);
|
|
565
635
|
const result = { message };
|
|
566
636
|
if (typeof err.code === "string") result.code = err.code;
|
|
637
|
+
if (Array.isArray(err.details)) {
|
|
638
|
+
const messages = err.details.filter(
|
|
639
|
+
(entry) => typeof entry === "string"
|
|
640
|
+
);
|
|
641
|
+
if (messages.length > 0) {
|
|
642
|
+
result.message = `${message}: ${messages.join(", ")}`;
|
|
643
|
+
result.messages = messages;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
if (typeof body.requestId === "string") result.requestId = body.requestId;
|
|
567
647
|
return result;
|
|
568
648
|
}
|
|
569
649
|
const rawMessage = body.message;
|
|
@@ -593,11 +673,14 @@ function looksLikeFeatureError(message) {
|
|
|
593
673
|
}
|
|
594
674
|
function errorFromResponse(input) {
|
|
595
675
|
const { status, body, meta } = input;
|
|
596
|
-
const { code, message, messages } = extractErrorFields(
|
|
676
|
+
const { code, message, messages, requestId } = extractErrorFields(
|
|
677
|
+
body,
|
|
678
|
+
status
|
|
679
|
+
);
|
|
597
680
|
const base = {
|
|
598
681
|
status,
|
|
599
682
|
code,
|
|
600
|
-
requestId: meta.requestId,
|
|
683
|
+
requestId: meta.requestId ?? requestId ?? null,
|
|
601
684
|
responseBody: body,
|
|
602
685
|
rateLimit: meta.rateLimit
|
|
603
686
|
};
|
|
@@ -661,10 +744,12 @@ function tryJsonParse(text) {
|
|
|
661
744
|
return void 0;
|
|
662
745
|
}
|
|
663
746
|
}
|
|
664
|
-
async function parseResponse(response, allowNotModified = false) {
|
|
747
|
+
async function parseResponse(response, allowNotModified = false, readBody, signal = new AbortController().signal) {
|
|
665
748
|
const meta = buildMeta(response);
|
|
666
749
|
if (allowNotModified && response.status === 304)
|
|
667
750
|
return { data: null, meta };
|
|
751
|
+
if (readBody !== void 0 && response.ok)
|
|
752
|
+
return { data: await readBody(response, meta, signal), meta };
|
|
668
753
|
const text = await response.text();
|
|
669
754
|
if (response.ok) {
|
|
670
755
|
if (text.length === 0) {
|
|
@@ -822,7 +907,13 @@ var HttpClient = class {
|
|
|
822
907
|
url.pathname = /\/v[0-9]+$/.test(url.pathname) ? url.pathname.replace(/\/v[0-9]+$/, `/${version}`) : `${url.pathname.replace(/\/$/, "")}/${version}`;
|
|
823
908
|
return url.toString().replace(/\/$/, "");
|
|
824
909
|
}
|
|
825
|
-
|
|
910
|
+
/** Opens a WebSocket to a GET route, sending the request's headers. */
|
|
911
|
+
openWebSocket(input) {
|
|
912
|
+
const { url, headers } = buildRequest(this.config, input);
|
|
913
|
+
const open = this.config.webSocket ?? globalWebSocket;
|
|
914
|
+
return open(url.replace(/^http/, "ws"), headers);
|
|
915
|
+
}
|
|
916
|
+
async request(input, options = {}, readBody) {
|
|
826
917
|
const prepared = buildRequest(this.config, input);
|
|
827
918
|
const retry = resolveRequestRetry(this.config.retry, options.retry);
|
|
828
919
|
const timeoutMs = options.timeoutMs ?? this.config.timeoutMs;
|
|
@@ -846,7 +937,9 @@ var HttpClient = class {
|
|
|
846
937
|
});
|
|
847
938
|
const parsed = await parseResponse(
|
|
848
939
|
response,
|
|
849
|
-
input.ifNoneMatch !== void 0
|
|
940
|
+
input.ifNoneMatch !== void 0,
|
|
941
|
+
readBody,
|
|
942
|
+
handle.signal
|
|
850
943
|
);
|
|
851
944
|
handle.cleanup();
|
|
852
945
|
this.recordRateLimit(parsed.meta.rateLimit);
|
|
@@ -1147,7 +1240,410 @@ var MarketAnalyticsResource = class extends BaseResource {
|
|
|
1147
1240
|
}
|
|
1148
1241
|
};
|
|
1149
1242
|
|
|
1243
|
+
// src/http/ndjson.ts
|
|
1244
|
+
var isGzip = (bytes, at) => bytes[at] === 31 && bytes[at + 1] === 139 && bytes[at + 2] === 8;
|
|
1245
|
+
async function gunzip(bytes, signal) {
|
|
1246
|
+
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"), { signal });
|
|
1247
|
+
return new Uint8Array(await new Response(stream).arrayBuffer());
|
|
1248
|
+
}
|
|
1249
|
+
async function ndjsonText(response, signal) {
|
|
1250
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
1251
|
+
if (!isGzip(bytes, 0)) return new TextDecoder().decode(bytes);
|
|
1252
|
+
const decoder = new TextDecoder();
|
|
1253
|
+
let text = "";
|
|
1254
|
+
let start = 0;
|
|
1255
|
+
while (start < bytes.length) {
|
|
1256
|
+
let end = start;
|
|
1257
|
+
for (; ; ) {
|
|
1258
|
+
end += 1;
|
|
1259
|
+
while (end < bytes.length && !isGzip(bytes, end)) end += 1;
|
|
1260
|
+
try {
|
|
1261
|
+
text += decoder.decode(await gunzip(bytes.slice(start, end), signal), {
|
|
1262
|
+
stream: true
|
|
1263
|
+
});
|
|
1264
|
+
break;
|
|
1265
|
+
} catch (error) {
|
|
1266
|
+
if (end >= bytes.length || signal.aborted) throw error;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
start = end;
|
|
1270
|
+
}
|
|
1271
|
+
return text + decoder.decode();
|
|
1272
|
+
}
|
|
1273
|
+
async function readNdjsonRows(response, meta, signal, fileName) {
|
|
1274
|
+
const text = await ndjsonText(response, signal);
|
|
1275
|
+
const rows = [];
|
|
1276
|
+
for (const line of text.split("\n")) {
|
|
1277
|
+
if (line.length === 0) continue;
|
|
1278
|
+
try {
|
|
1279
|
+
rows.push(JSON.parse(line));
|
|
1280
|
+
} catch (cause) {
|
|
1281
|
+
throw new ParseError(`Failed to parse a ${fileName} line.`, {
|
|
1282
|
+
status: meta.status,
|
|
1283
|
+
responseBody: line,
|
|
1284
|
+
cause,
|
|
1285
|
+
rateLimit: meta.rateLimit,
|
|
1286
|
+
requestId: meta.requestId
|
|
1287
|
+
});
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
signal.throwIfAborted();
|
|
1291
|
+
return rows;
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
// src/latest-prices-mirror.ts
|
|
1295
|
+
var RECENT_LINKS = 1024;
|
|
1296
|
+
var MAX_FAILED_OPENS = 10;
|
|
1297
|
+
var MAX_RECONNECT_DELAY_MS = 3e4;
|
|
1298
|
+
var CLOSE_KEY_NOT_ADMITTED = 4003;
|
|
1299
|
+
function cursorVersion(cursor) {
|
|
1300
|
+
return cursor.trim().replace(/^W\//, "").replace(/^"(.*)"$/, "$1");
|
|
1301
|
+
}
|
|
1302
|
+
function isFeedReset(error) {
|
|
1303
|
+
return error instanceof APIError && error.status === 410;
|
|
1304
|
+
}
|
|
1305
|
+
function messageText(data) {
|
|
1306
|
+
if (typeof data === "string") return data;
|
|
1307
|
+
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
1308
|
+
return new TextDecoder().decode(data);
|
|
1309
|
+
}
|
|
1310
|
+
throw new ParseError("The price stream sent a message that is not text.");
|
|
1311
|
+
}
|
|
1312
|
+
function replacePoint(points, market, value) {
|
|
1313
|
+
const index = points.findIndex((point) => point.market === market);
|
|
1314
|
+
if (value === null) {
|
|
1315
|
+
if (index >= 0) points.splice(index, 1);
|
|
1316
|
+
} else if (index >= 0) {
|
|
1317
|
+
points[index] = value;
|
|
1318
|
+
} else {
|
|
1319
|
+
points.push(value);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
var LatestPricesMirror = class {
|
|
1323
|
+
constructor(prices, params) {
|
|
1324
|
+
this.prices = prices;
|
|
1325
|
+
const { game, ...given } = params;
|
|
1326
|
+
const list = (values, lower = false) => {
|
|
1327
|
+
const kept = (values ?? []).map((value) => lower ? value.trim().toLowerCase() : value.trim()).filter((value) => value !== "");
|
|
1328
|
+
return kept.length > 0 ? kept : void 0;
|
|
1329
|
+
};
|
|
1330
|
+
const markets = list(given.markets, true);
|
|
1331
|
+
const items = list(given.items);
|
|
1332
|
+
const sides = list(given.sides, true);
|
|
1333
|
+
this.game = game;
|
|
1334
|
+
this.filter = { markets, items, sides, best: given.best };
|
|
1335
|
+
if (markets) this.marketSet = new Set(markets);
|
|
1336
|
+
if (items) this.itemSet = new Set(items);
|
|
1337
|
+
this.asks = !sides || sides.includes("ask");
|
|
1338
|
+
this.bids = !sides || sides.includes("bid");
|
|
1339
|
+
this.best = given.best ?? !markets;
|
|
1340
|
+
}
|
|
1341
|
+
prices;
|
|
1342
|
+
game;
|
|
1343
|
+
filter;
|
|
1344
|
+
marketSet;
|
|
1345
|
+
itemSet;
|
|
1346
|
+
asks;
|
|
1347
|
+
bids;
|
|
1348
|
+
best;
|
|
1349
|
+
byId = /* @__PURE__ */ new Map();
|
|
1350
|
+
applied = /* @__PURE__ */ new Set();
|
|
1351
|
+
envelope = {};
|
|
1352
|
+
generatedAt = "";
|
|
1353
|
+
version = null;
|
|
1354
|
+
/** The version of the snapshot the copy equals, or null before a snapshot loads. */
|
|
1355
|
+
get cursor() {
|
|
1356
|
+
return this.version;
|
|
1357
|
+
}
|
|
1358
|
+
get size() {
|
|
1359
|
+
return this.byId.size;
|
|
1360
|
+
}
|
|
1361
|
+
item(canonicalItemId) {
|
|
1362
|
+
return this.byId.get(canonicalItemId);
|
|
1363
|
+
}
|
|
1364
|
+
items() {
|
|
1365
|
+
return this.byId.values();
|
|
1366
|
+
}
|
|
1367
|
+
/** The copy in snapshot form, items ordered by `canonicalItemId`. */
|
|
1368
|
+
toSnapshot() {
|
|
1369
|
+
const items = [...this.byId.values()].sort(
|
|
1370
|
+
(a, b) => a.canonicalItemId < b.canonicalItemId ? -1 : a.canonicalItemId > b.canonicalItemId ? 1 : 0
|
|
1371
|
+
);
|
|
1372
|
+
return {
|
|
1373
|
+
generatedAt: this.generatedAt,
|
|
1374
|
+
game: this.game,
|
|
1375
|
+
...this.envelope,
|
|
1376
|
+
items,
|
|
1377
|
+
itemCount: items.length
|
|
1378
|
+
};
|
|
1379
|
+
}
|
|
1380
|
+
/**
|
|
1381
|
+
* Replaces the copy with a `latestAll()` snapshot read without `currency`
|
|
1382
|
+
* or `include`. `cursor` is its
|
|
1383
|
+
* ETag, or the `to` of the last link applied to a copy saved earlier.
|
|
1384
|
+
*/
|
|
1385
|
+
load(snapshot, cursor) {
|
|
1386
|
+
const envelope = { ...snapshot };
|
|
1387
|
+
for (const key of ["generatedAt", "game", "items", "itemCount"]) {
|
|
1388
|
+
delete envelope[key];
|
|
1389
|
+
}
|
|
1390
|
+
this.envelope = envelope;
|
|
1391
|
+
this.generatedAt = snapshot.generatedAt;
|
|
1392
|
+
this.byId.clear();
|
|
1393
|
+
this.applied.clear();
|
|
1394
|
+
for (const item of snapshot.items) {
|
|
1395
|
+
if (this.itemSet && !this.itemSet.has(item.canonicalItemId)) continue;
|
|
1396
|
+
this.byId.set(item.canonicalItemId, {
|
|
1397
|
+
...item,
|
|
1398
|
+
bestAsk: this.best && this.asks ? item.bestAsk : null,
|
|
1399
|
+
bestBid: this.best && this.bids ? item.bestBid : null,
|
|
1400
|
+
markets: this.asks ? item.markets.filter(this.selected, this) : [],
|
|
1401
|
+
buyOrders: this.bids ? item.buyOrders.filter(this.selected, this) : []
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
this.version = cursorVersion(cursor);
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Applies one link and moves the cursor to its `to`. Returns false for a
|
|
1408
|
+
* link already applied. A link that does not start at the cursor throws,
|
|
1409
|
+
* and the copy then needs a new snapshot.
|
|
1410
|
+
*/
|
|
1411
|
+
apply(link) {
|
|
1412
|
+
const to = cursorVersion(link.to);
|
|
1413
|
+
if (this.applied.has(to)) return false;
|
|
1414
|
+
if (this.version === null || cursorVersion(link.from) !== this.version) {
|
|
1415
|
+
throw new SkinpricerError(
|
|
1416
|
+
`The link from ${link.from} does not continue the copy at ${this.version ?? "no snapshot"}.`
|
|
1417
|
+
);
|
|
1418
|
+
}
|
|
1419
|
+
this.version = null;
|
|
1420
|
+
for (const change of link.changes) this.applyChange(change);
|
|
1421
|
+
if (link.envelope) this.envelope = { ...link.envelope };
|
|
1422
|
+
this.generatedAt = link.generatedAt;
|
|
1423
|
+
this.version = to;
|
|
1424
|
+
this.applied.add(to);
|
|
1425
|
+
if (this.applied.size > RECENT_LINKS) {
|
|
1426
|
+
this.applied.delete(this.applied.values().next().value);
|
|
1427
|
+
}
|
|
1428
|
+
return true;
|
|
1429
|
+
}
|
|
1430
|
+
/** Loads a new default snapshot of the game. */
|
|
1431
|
+
async reset(options) {
|
|
1432
|
+
const snapshot = await this.prices.latestAll({ game: this.game }, options);
|
|
1433
|
+
const etag = getMeta(snapshot)?.headers.get("etag");
|
|
1434
|
+
if (!etag) {
|
|
1435
|
+
throw new ParseError("The price snapshot has no ETag to resume from.");
|
|
1436
|
+
}
|
|
1437
|
+
this.load(snapshot, etag);
|
|
1438
|
+
}
|
|
1439
|
+
/**
|
|
1440
|
+
* Brings the copy up to date over HTTP: loads a snapshot when there is
|
|
1441
|
+
* none or the feed answers with a reset, then applies every link after
|
|
1442
|
+
* the cursor.
|
|
1443
|
+
*/
|
|
1444
|
+
async sync(options) {
|
|
1445
|
+
const result = { snapshot: false, links: [] };
|
|
1446
|
+
if (this.version === null) {
|
|
1447
|
+
await this.reset(options);
|
|
1448
|
+
result.snapshot = true;
|
|
1449
|
+
}
|
|
1450
|
+
for (; ; ) {
|
|
1451
|
+
let page;
|
|
1452
|
+
try {
|
|
1453
|
+
page = await this.prices.changes(
|
|
1454
|
+
{ ...this.filter, game: this.game, since: this.cursorOrThrow() },
|
|
1455
|
+
options
|
|
1456
|
+
);
|
|
1457
|
+
} catch (error) {
|
|
1458
|
+
if (!isFeedReset(error) || result.snapshot) throw error;
|
|
1459
|
+
await this.reset(options);
|
|
1460
|
+
result.snapshot = true;
|
|
1461
|
+
result.links = [];
|
|
1462
|
+
continue;
|
|
1463
|
+
}
|
|
1464
|
+
for (const link of page.links) {
|
|
1465
|
+
if (this.apply(link)) result.links.push(link);
|
|
1466
|
+
}
|
|
1467
|
+
if (page.complete || page.links.length === 0) return result;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Keeps the copy current over the WebSocket until `signal` aborts. It
|
|
1472
|
+
* resumes from the cursor after a disconnect and loads a new snapshot
|
|
1473
|
+
* after a reset. It rejects when the key is no longer admitted, when the
|
|
1474
|
+
* stream cannot be opened 10 times in a row, or when a callback throws.
|
|
1475
|
+
*/
|
|
1476
|
+
async stream(options = {}) {
|
|
1477
|
+
const { signal } = options;
|
|
1478
|
+
let failures = 0;
|
|
1479
|
+
let reason = null;
|
|
1480
|
+
try {
|
|
1481
|
+
while (!signal?.aborted) {
|
|
1482
|
+
if (this.version === null) {
|
|
1483
|
+
await this.reset({ signal });
|
|
1484
|
+
options.onSnapshot?.(reason);
|
|
1485
|
+
reason = null;
|
|
1486
|
+
if (signal?.aborted) return;
|
|
1487
|
+
}
|
|
1488
|
+
const end = await this.connect(options, (next) => {
|
|
1489
|
+
reason = next;
|
|
1490
|
+
});
|
|
1491
|
+
if (signal?.aborted) return;
|
|
1492
|
+
if (end.kind === "failed") throw end.error;
|
|
1493
|
+
if (end.code === CLOSE_KEY_NOT_ADMITTED) {
|
|
1494
|
+
throw new PermissionError(
|
|
1495
|
+
"The API key is no longer admitted to the price stream."
|
|
1496
|
+
);
|
|
1497
|
+
}
|
|
1498
|
+
failures = end.opened ? 0 : failures + 1;
|
|
1499
|
+
if (failures >= MAX_FAILED_OPENS) {
|
|
1500
|
+
throw new NetworkError(
|
|
1501
|
+
`The price stream could not be opened ${failures} times in a row.`
|
|
1502
|
+
);
|
|
1503
|
+
}
|
|
1504
|
+
await sleep(
|
|
1505
|
+
Math.min(1e3 * 2 ** failures, MAX_RECONNECT_DELAY_MS),
|
|
1506
|
+
signal
|
|
1507
|
+
);
|
|
1508
|
+
}
|
|
1509
|
+
} catch (error) {
|
|
1510
|
+
if (signal?.aborted) return;
|
|
1511
|
+
throw error;
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
connect(options, onReset) {
|
|
1515
|
+
const { signal } = options;
|
|
1516
|
+
return new Promise((resolve) => {
|
|
1517
|
+
let opened = false;
|
|
1518
|
+
let failure = null;
|
|
1519
|
+
const socket = this.prices.stream({
|
|
1520
|
+
...this.filter,
|
|
1521
|
+
game: this.game,
|
|
1522
|
+
since: this.cursorOrThrow()
|
|
1523
|
+
});
|
|
1524
|
+
const onAbort = () => socket.close(1e3);
|
|
1525
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1526
|
+
if (signal?.aborted) onAbort();
|
|
1527
|
+
socket.addEventListener("error", () => {
|
|
1528
|
+
});
|
|
1529
|
+
socket.addEventListener("message", (event) => {
|
|
1530
|
+
if (failure) return;
|
|
1531
|
+
opened = true;
|
|
1532
|
+
let applied = null;
|
|
1533
|
+
try {
|
|
1534
|
+
const message = JSON.parse(messageText(event.data));
|
|
1535
|
+
if (message.type === "reset") {
|
|
1536
|
+
this.version = null;
|
|
1537
|
+
onReset(message.reason);
|
|
1538
|
+
} else if (message.type === "link") {
|
|
1539
|
+
const link = message;
|
|
1540
|
+
if (this.apply(link)) applied = link;
|
|
1541
|
+
}
|
|
1542
|
+
} catch {
|
|
1543
|
+
this.version = null;
|
|
1544
|
+
socket.close(1e3);
|
|
1545
|
+
return;
|
|
1546
|
+
}
|
|
1547
|
+
if (!applied) return;
|
|
1548
|
+
try {
|
|
1549
|
+
options.onLink?.(applied);
|
|
1550
|
+
} catch (error) {
|
|
1551
|
+
failure = { error };
|
|
1552
|
+
socket.close(1e3);
|
|
1553
|
+
}
|
|
1554
|
+
});
|
|
1555
|
+
socket.addEventListener("close", (event) => {
|
|
1556
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1557
|
+
if (event.code === 4009) this.version = null;
|
|
1558
|
+
resolve(
|
|
1559
|
+
failure ? { kind: "failed", error: failure.error } : { kind: "closed", code: event.code ?? 1006, opened }
|
|
1560
|
+
);
|
|
1561
|
+
});
|
|
1562
|
+
});
|
|
1563
|
+
}
|
|
1564
|
+
selected(point) {
|
|
1565
|
+
return !this.marketSet || this.marketSet.has(point.market.toLowerCase());
|
|
1566
|
+
}
|
|
1567
|
+
cursorOrThrow() {
|
|
1568
|
+
if (this.version === null) {
|
|
1569
|
+
throw new SkinpricerError("The copy has no snapshot loaded.");
|
|
1570
|
+
}
|
|
1571
|
+
return this.version;
|
|
1572
|
+
}
|
|
1573
|
+
itemOf(change) {
|
|
1574
|
+
const item = this.byId.get(change.item);
|
|
1575
|
+
if (!item) {
|
|
1576
|
+
throw new SkinpricerError(
|
|
1577
|
+
`Change ${change.id} is for item ${change.item}, which the copy does not hold.`
|
|
1578
|
+
);
|
|
1579
|
+
}
|
|
1580
|
+
return item;
|
|
1581
|
+
}
|
|
1582
|
+
applyChange(change) {
|
|
1583
|
+
switch (change.field) {
|
|
1584
|
+
case "name": {
|
|
1585
|
+
if (change.value === null) {
|
|
1586
|
+
this.byId.delete(change.item);
|
|
1587
|
+
return;
|
|
1588
|
+
}
|
|
1589
|
+
const item = this.byId.get(change.item);
|
|
1590
|
+
if (item) {
|
|
1591
|
+
item.name = change.value;
|
|
1592
|
+
} else {
|
|
1593
|
+
this.byId.set(change.item, {
|
|
1594
|
+
name: change.value,
|
|
1595
|
+
canonicalItemId: change.item,
|
|
1596
|
+
bestAsk: null,
|
|
1597
|
+
bestBid: null,
|
|
1598
|
+
markets: [],
|
|
1599
|
+
buyOrders: []
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1602
|
+
return;
|
|
1603
|
+
}
|
|
1604
|
+
case "bestAsk":
|
|
1605
|
+
case "bestBid":
|
|
1606
|
+
this.itemOf(change)[change.field] = change.value;
|
|
1607
|
+
return;
|
|
1608
|
+
case "markets":
|
|
1609
|
+
replacePoint(this.itemOf(change).markets, change.market, change.value);
|
|
1610
|
+
return;
|
|
1611
|
+
case "buyOrders":
|
|
1612
|
+
replacePoint(
|
|
1613
|
+
this.itemOf(change).buyOrders,
|
|
1614
|
+
change.market,
|
|
1615
|
+
change.value
|
|
1616
|
+
);
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
};
|
|
1621
|
+
|
|
1150
1622
|
// src/resources/prices.ts
|
|
1623
|
+
async function readPricesRollingFile(response, meta, signal) {
|
|
1624
|
+
const rows = await readNdjsonRows(
|
|
1625
|
+
response,
|
|
1626
|
+
meta,
|
|
1627
|
+
signal,
|
|
1628
|
+
"rolling statistics file"
|
|
1629
|
+
);
|
|
1630
|
+
return {
|
|
1631
|
+
etag: meta.headers.get("etag"),
|
|
1632
|
+
windowEnd: meta.headers.get("x-rolling-window-end"),
|
|
1633
|
+
receivedThrough: meta.headers.get("x-rolling-received-through"),
|
|
1634
|
+
rows
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
function feedQuery(params) {
|
|
1638
|
+
return {
|
|
1639
|
+
game: params.game,
|
|
1640
|
+
since: params.since,
|
|
1641
|
+
markets: params.markets,
|
|
1642
|
+
items: params.items,
|
|
1643
|
+
sides: params.sides,
|
|
1644
|
+
best: params.best
|
|
1645
|
+
};
|
|
1646
|
+
}
|
|
1151
1647
|
var PricesResource = class extends BaseResource {
|
|
1152
1648
|
latestAll(params, options) {
|
|
1153
1649
|
return this.call(
|
|
@@ -1155,12 +1651,62 @@ var PricesResource = class extends BaseResource {
|
|
|
1155
1651
|
method: "GET",
|
|
1156
1652
|
path: "/prices/latest",
|
|
1157
1653
|
baseUrl: this.http.versionedBaseUrl("v2"),
|
|
1654
|
+
query: {
|
|
1655
|
+
game: params.game,
|
|
1656
|
+
currency: params.currency,
|
|
1657
|
+
include: params.include
|
|
1658
|
+
},
|
|
1659
|
+
ifNoneMatch: params.ifNoneMatch
|
|
1660
|
+
},
|
|
1661
|
+
options
|
|
1662
|
+
);
|
|
1663
|
+
}
|
|
1664
|
+
rolling(params, options) {
|
|
1665
|
+
return this.call(
|
|
1666
|
+
{
|
|
1667
|
+
method: "GET",
|
|
1668
|
+
path: "/prices/rolling",
|
|
1669
|
+
baseUrl: this.http.versionedBaseUrl("v2"),
|
|
1158
1670
|
query: { game: params.game },
|
|
1159
1671
|
ifNoneMatch: params.ifNoneMatch
|
|
1160
1672
|
},
|
|
1673
|
+
options,
|
|
1674
|
+
readPricesRollingFile
|
|
1675
|
+
);
|
|
1676
|
+
}
|
|
1677
|
+
/**
|
|
1678
|
+
* `GET /v2/prices/latest/changes`: up to 20 links after `since`. A cursor
|
|
1679
|
+
* that can no longer be resumed is an `APIError` with status 410 and code
|
|
1680
|
+
* `FEED_RESET`; take a new snapshot then.
|
|
1681
|
+
*/
|
|
1682
|
+
changes(params, options) {
|
|
1683
|
+
return this.call(
|
|
1684
|
+
{
|
|
1685
|
+
method: "GET",
|
|
1686
|
+
path: "/prices/latest/changes",
|
|
1687
|
+
baseUrl: this.http.versionedBaseUrl("v2"),
|
|
1688
|
+
query: feedQuery(params)
|
|
1689
|
+
},
|
|
1161
1690
|
options
|
|
1162
1691
|
);
|
|
1163
1692
|
}
|
|
1693
|
+
/**
|
|
1694
|
+
* Opens the WebSocket at `/v2/prices/stream`. It sends one `hello`, then a
|
|
1695
|
+
* `link` message per published snapshot, or a `reset` before closing with
|
|
1696
|
+
* code 4009. {@link mirror} handles the protocol.
|
|
1697
|
+
*/
|
|
1698
|
+
stream(params) {
|
|
1699
|
+
return this.http.openWebSocket({
|
|
1700
|
+
method: "GET",
|
|
1701
|
+
path: "/prices/stream",
|
|
1702
|
+
baseUrl: this.http.versionedBaseUrl("v2"),
|
|
1703
|
+
query: feedQuery(params)
|
|
1704
|
+
});
|
|
1705
|
+
}
|
|
1706
|
+
/** A local copy of the snapshot that the change feed keeps current. */
|
|
1707
|
+
mirror(params) {
|
|
1708
|
+
return new LatestPricesMirror(this, params);
|
|
1709
|
+
}
|
|
1164
1710
|
};
|
|
1165
1711
|
|
|
1166
1712
|
// src/resources/recommendations.ts
|
|
@@ -1178,6 +1724,37 @@ var RecommendationsResource = class extends BaseResource {
|
|
|
1178
1724
|
}
|
|
1179
1725
|
};
|
|
1180
1726
|
|
|
1727
|
+
// src/resources/sales.ts
|
|
1728
|
+
async function readSalesDailyFile(response, meta, signal) {
|
|
1729
|
+
const rows = await readNdjsonRows(
|
|
1730
|
+
response,
|
|
1731
|
+
meta,
|
|
1732
|
+
signal,
|
|
1733
|
+
"daily sales file"
|
|
1734
|
+
);
|
|
1735
|
+
return {
|
|
1736
|
+
etag: meta.headers.get("etag"),
|
|
1737
|
+
final: meta.headers.get("x-sales-final") === "true",
|
|
1738
|
+
receivedThrough: meta.headers.get("x-sales-received-through"),
|
|
1739
|
+
rows
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
var SalesResource = class extends BaseResource {
|
|
1743
|
+
daily(params, options) {
|
|
1744
|
+
return this.call(
|
|
1745
|
+
{
|
|
1746
|
+
method: "GET",
|
|
1747
|
+
path: "/sales/daily",
|
|
1748
|
+
baseUrl: this.http.versionedBaseUrl("v2"),
|
|
1749
|
+
query: { game: params.game, date: params.date },
|
|
1750
|
+
ifNoneMatch: params.ifNoneMatch
|
|
1751
|
+
},
|
|
1752
|
+
options,
|
|
1753
|
+
readSalesDailyFile
|
|
1754
|
+
);
|
|
1755
|
+
}
|
|
1756
|
+
};
|
|
1757
|
+
|
|
1181
1758
|
// src/resources/status.ts
|
|
1182
1759
|
var StatusResource = class extends BaseResource {
|
|
1183
1760
|
/** `GET /v1/status/freshness` — per-market data freshness and overall health. */
|
|
@@ -1213,6 +1790,7 @@ var SkinpricerClient = class {
|
|
|
1213
1790
|
markets;
|
|
1214
1791
|
schema;
|
|
1215
1792
|
prices;
|
|
1793
|
+
sales;
|
|
1216
1794
|
/** Access the v2 endpoints supported by the selected game. */
|
|
1217
1795
|
forGame(game) {
|
|
1218
1796
|
return new GameClient(this.httpClient, game);
|
|
@@ -1235,6 +1813,7 @@ var SkinpricerClient = class {
|
|
|
1235
1813
|
this.markets = new MarketsResource(this.httpClient);
|
|
1236
1814
|
this.schema = new SchemaResource(this.httpClient);
|
|
1237
1815
|
this.prices = new PricesResource(this.httpClient);
|
|
1816
|
+
this.sales = new SalesResource(this.httpClient);
|
|
1238
1817
|
}
|
|
1239
1818
|
};
|
|
1240
1819
|
|
|
@@ -1323,6 +1902,6 @@ function parseIsoDate(value) {
|
|
|
1323
1902
|
return new Date(value);
|
|
1324
1903
|
}
|
|
1325
1904
|
|
|
1326
|
-
export { APIError, ATTRIBUTE_MARKETS, AggregationsResource, ArbitrageResource, AttributePricesResource, AuthenticationError, BaseResource, Buff163Resource, ConfigurationError, DEFAULT_BASE_URL, DEFAULT_PUBLIC_BASE_URL, DEFAULT_RETRY, DEFAULT_TIMEOUT_MS, FeatureNotAvailableError, GameClient, HistoryResource, ItemsResource, KNOWN_MARKETS, LIQUIDITY_BADGES, LIQUIDITY_CONFIDENCE, LIQUIDITY_LABELS, LiquidityResource, MarketAnalyticsResource, MarketsResource, NbboResource, NetworkError, NotFoundError, ParseError, PermissionError, PricesResource, PricingResource, RESPONSE_META, RateLimitError, RecommendationsResource, SUPPORTED_LIQUIDITY_MARKETS, SUPPORTED_RUST_LIQUIDITY_MARKETS, SchemaResource, ServerError, SkinpricerClient, SkinpricerError, StatusResource, TimeoutError, VERSION, ValidationError, centsToUsd, chunk, errorFromResponse, formatUsd, getMeta, mapWithConcurrency, parseIsoDate, resolveConfig };
|
|
1905
|
+
export { APIError, ATTRIBUTE_MARKETS, AggregationsResource, ArbitrageResource, AttributePricesResource, AuthenticationError, BaseResource, Buff163Resource, ConfigurationError, DEFAULT_BASE_URL, DEFAULT_PUBLIC_BASE_URL, DEFAULT_RETRY, DEFAULT_TIMEOUT_MS, FeatureNotAvailableError, GameClient, HistoryResource, ItemsResource, KNOWN_MARKETS, LIQUIDITY_BADGES, LIQUIDITY_CONFIDENCE, LIQUIDITY_LABELS, LatestPricesMirror, LiquidityResource, MarketAnalyticsResource, MarketsResource, NbboResource, NetworkError, NotFoundError, ParseError, PermissionError, PricesResource, PricingResource, RESPONSE_META, RateLimitError, RecommendationsResource, SUPPORTED_LIQUIDITY_MARKETS, SUPPORTED_RUST_LIQUIDITY_MARKETS, SalesResource, SchemaResource, ServerError, SkinpricerClient, SkinpricerError, StatusResource, TimeoutError, VERSION, ValidationError, centsToUsd, chunk, errorFromResponse, formatUsd, getMeta, mapWithConcurrency, parseIsoDate, resolveConfig };
|
|
1327
1906
|
//# sourceMappingURL=index.js.map
|
|
1328
1907
|
//# sourceMappingURL=index.js.map
|