@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
43 lines • 1.64 kB
JavaScript
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { getEnv } from "@ledgerhq/live-env";
import { FearAndGreedTags, FearAndGreedResponseSchema } from "./types";
import { log } from "@ledgerhq/logs";
const ONE_MINUTE_IN_MS = 60 * 1000;
export const 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 = FearAndGreedResponseSchema.safeParse(response);
if (!result.success) {
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,
};
}
export const cmcApi = createApi({
reducerPath: "cmcApi",
baseQuery: fetchBaseQuery({
baseUrl: getEnv("CMC_API_URL"),
}),
tagTypes: [FearAndGreedTags.Latest],
endpoints: build => ({
getFearAndGreedLatest: build.query({
query: () => ({
url: "/fear-and-greed/latest",
}),
providesTags: [FearAndGreedTags.Latest],
transformResponse: transformFearAndGreedResponse,
keepUnusedDataFor: FIFTEEN_MINUTES_IN_SECONDS,
}),
}),
});
export const { useGetFearAndGreedLatestQuery } = cmcApi;
//# sourceMappingURL=api.js.map