@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
37 lines • 1.57 kB
JavaScript
import network from "@ledgerhq/live-network/network";
import { DEFAULT_SWAP_TIMEOUT_MS } from "../../const/timeout";
import axios from "axios";
import { LedgerAPI4xx } from "@ledgerhq/errors";
import { fetchCurrencyAllMock } from "./__mocks__/fetchCurrencyAll.mocks";
import { flattenV5CurrenciesAll } from "../../utils/flattenV5CurrenciesAll";
import { getSwapAPIBaseURL, getSwapUserIP } from "../..";
import { getEnv } from "@ledgerhq/live-env";
export async function fetchCurrencyAll({ baseUrl = getSwapAPIBaseURL(), providers, additionalCoinsFlag = false, }) {
if (getEnv("MOCK") || getEnv("PLAYWRIGHT_RUN"))
return Promise.resolve(flattenV5CurrenciesAll(fetchCurrencyAllMock));
const url = new URL(`${baseUrl}/currencies/all`);
url.searchParams.append("providers-whitelist", providers.join(","));
url.searchParams.append("additional-coins-flag", additionalCoinsFlag.toString());
const headers = getSwapUserIP();
try {
const { data } = await network({
method: "GET",
url: url.toString(),
timeout: DEFAULT_SWAP_TIMEOUT_MS,
...(headers !== undefined ? { headers } : {}),
});
return flattenV5CurrenciesAll(data);
}
catch (e) {
if (axios.isAxiosError(e)) {
if (e.code === "ECONNABORTED") {
// TODO: LIVE-8901 (handle request timeout)
}
}
if (e instanceof LedgerAPI4xx) {
// TODO: LIVE-8901 (handle 4xx)
}
throw e;
}
}
//# sourceMappingURL=fetchCurrencyAll.js.map