@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
46 lines • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGetFearAndGreedLatestQuery = exports.cmcApi = exports.FIFTEEN_MINUTES_IN_MS = void 0;
const react_1 = require("@reduxjs/toolkit/query/react");
const live_env_1 = require("@ledgerhq/live-env");
const types_1 = require("./types");
const logs_1 = require("@ledgerhq/logs");
const ONE_MINUTE_IN_MS = 60 * 1000;
exports.FIFTEEN_MINUTES_IN_MS = 15 * ONE_MINUTE_IN_MS;
const ONE_MINUTE_IN_SECONDS = 60;
const FIFTEEN_MINUTES_IN_SECONDS = 15 * ONE_MINUTE_IN_SECONDS;
function transformFearAndGreedResponse(response) {
const result = types_1.FearAndGreedResponseSchema.safeParse(response);
if (!result.success) {
(0, logs_1.log)("cmc-client", "Invalid response schema:", {
errors: result.error.issues,
received: response,
});
throw new Error(`[CMC API] Schema validation failed: ${result.error.issues
.map(e => `${e.path.join(".")}: ${e.message}`)
.join(", ")}`);
}
return {
value: result.data.data.value,
classification: result.data.data.value_classification,
};
}
exports.cmcApi = (0, react_1.createApi)({
reducerPath: "cmcApi",
baseQuery: (0, react_1.fetchBaseQuery)({
baseUrl: (0, live_env_1.getEnv)("CMC_API_URL"),
}),
tagTypes: [types_1.FearAndGreedTags.Latest],
endpoints: build => ({
getFearAndGreedLatest: build.query({
query: () => ({
url: "/fear-and-greed/latest",
}),
providesTags: [types_1.FearAndGreedTags.Latest],
transformResponse: transformFearAndGreedResponse,
keepUnusedDataFor: FIFTEEN_MINUTES_IN_SECONDS,
}),
}),
});
exports.useGetFearAndGreedLatestQuery = exports.cmcApi.useGetFearAndGreedLatestQuery;
//# sourceMappingURL=api.js.map