frontend/src/routes/stocks/[tickerID]/forecast/analyst/+page.ts
MuslemRahimi 6b45d50161 ui fix
2024-09-15 10:02:43 +02:00

40 lines
981 B
TypeScript

import { getCache, setCache } from "$lib/store";
export const load = async ({ parent, params }) => {
const getAnalystTickerHistory = async () => {
let output;
const cachedData = getCache(params.tickerID, "getAnalystTickerHistory");
if (cachedData) {
output = cachedData;
} else {
const { apiURL, apiKey } = await parent();
const postData = {
ticker: params.tickerID,
};
// make the POST request to the endpoint
const response = await fetch(apiURL + "/analyst-ticker-history", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
output = await response.json();
setCache(params.tickerID, output, "getAnalystTickerHistory");
}
return output;
};
// Make sure to return a promise
return {
getAnalystTickerHistory: await getAnalystTickerHistory(),
};
};