@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
38 lines • 1.67 kB
JavaScript
import network from "@ledgerhq/live-network";
import { fetchCurrencyToMock } from "./__mocks__/fetchCurrencyTo.mocks";
import { isIntegrationTestEnv } from "../../utils/isIntegrationTestEnv";
import { DEFAULT_SWAP_TIMEOUT_MS } from "../../const/timeout";
import axios from "axios";
import { LedgerAPI4xx } from "@ledgerhq/errors";
import { flattenV5CurrenciesToAndFrom } from "../../utils/flattenV5CurrenciesToAndFrom";
import { getSwapAPIBaseURL, getSwapUserIP } from "../..";
export async function fetchCurrencyTo({ baseUrl = getSwapAPIBaseURL(), currencyFromId, providers, additionalCoinsFlag = false, }) {
if (isIntegrationTestEnv())
return Promise.resolve(flattenV5CurrenciesToAndFrom(fetchCurrencyToMock));
const url = new URL(`${baseUrl}/currencies/to`);
url.searchParams.append("providers-whitelist", providers.join(","));
url.searchParams.append("additional-coins-flag", additionalCoinsFlag.toString());
url.searchParams.append("currency-from", currencyFromId);
const headers = getSwapUserIP();
try {
const { data } = await network({
method: "GET",
url: url.toString(),
timeout: DEFAULT_SWAP_TIMEOUT_MS,
...(headers !== undefined ? { headers } : {}),
});
return flattenV5CurrenciesToAndFrom(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=fetchCurrencyTo.js.map