tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
57 lines • 2.07 kB
JavaScript
import { getOptions } from "./options.js";
import { getJSON } from "./handy.js";
export async function getInstrumentInfo(exchange, filterOrSymbol) {
if (Array.isArray(exchange)) {
const exchanges = exchange;
const results = await Promise.all(exchanges.map((e) => getInstrumentInfoForExchange(e, filterOrSymbol)));
return results.flat();
}
else {
return getInstrumentInfoForExchange(exchange, filterOrSymbol);
}
}
async function getInstrumentInfoForExchange(exchange, filterOrSymbol) {
const options = getOptions();
let url = `${options.endpoint}/instruments/${exchange}`;
if (typeof filterOrSymbol === 'string') {
url += `/${encodeURIComponent(filterOrSymbol)}`;
}
else if (typeof filterOrSymbol === 'object') {
url += `?filter=${encodeURIComponent(JSON.stringify(filterOrSymbol))}`;
}
try {
const { data } = await getJSON(url, {
headers: { Authorization: `Bearer ${options.apiKey}` }
});
return data;
}
catch (e) {
// expose 400 error message from server
if (e.response?.statusCode === 400) {
let err;
try {
err = JSON.parse(e.response.body);
}
catch {
throw e;
}
throw err ? new Error(`${err.message} (${err.code})`) : e;
}
else {
throw e;
}
}
}
export async function findInstrumentSymbols(exchanges, filter, selector = 'id') {
if (selector !== 'id' && selector !== 'datasetId') {
throw new Error("Invalid selector. Supported values are 'id' and 'datasetId'.");
}
return await Promise.all(exchanges.map(async (exchange) => {
const instruments = (await getInstrumentInfoForExchange(exchange, filter));
return {
exchange,
symbols: instruments.map((instrument) => (selector === 'datasetId' ? (instrument.datasetId ?? instrument.id) : instrument.id))
};
}));
}
//# sourceMappingURL=instrumentinfo.js.map