update backend

This commit is contained in:
MuslemRahimi 2025-02-23 11:33:48 +01:00
parent f602c1a1d0
commit fa6cd84a78

View File

@ -1,12 +1,27 @@
export const load = async ({ locals, params }) => { export const load = async ({ locals, params }) => {
const { apiURL, apiKey, user } = locals; const { apiURL, apiKey, user } = locals;
const ticker = params.tickerID;
if (!ticker) {
return { error: 'Invalid ticker ID' };
}
// Define the endpoints you want to fetch in bulk
const endpoints = [
"/analyst-ticker-history",
"/analyst-estimate",
"/analyst-insight",
"/top-analyst-summary-rating"
];
// Prepare the payload for the bulk request
const postData = { const postData = {
ticker: params.tickerID, ticker,
endpoints
}; };
const getAnalystTickerHistory = async () => { try {
// make the POST request to the endpoint const response = await fetch(`${apiURL}/bulk-data`, {
const response = await fetch(apiURL + "/analyst-ticker-history", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -15,61 +30,25 @@ export const load = async ({ locals, params }) => {
body: JSON.stringify(postData), body: JSON.stringify(postData),
}); });
let output = await response.json(); if (!response.ok) {
output = user?.tier !== "Pro" ? output?.slice(0, 6) : output; throw new Error("Failed to fetch bulk data");
}
return output; const bulkData = await response.json();
};
const getAnalystEstimate = async () => { // Process analyst ticker history: if user isn't Pro, limit to 6 items
// make the POST request to the endpoint let analystTickerHistory = bulkData["/analyst-ticker-history"];
const response = await fetch(apiURL + "/analyst-estimate", { if (user?.tier !== "Pro" && Array.isArray(analystTickerHistory)) {
method: "POST", analystTickerHistory = analystTickerHistory.slice(0, 6);
headers: { }
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
const output = await response.json(); return {
return output; getAnalystTickerHistory: analystTickerHistory,
}; getAnalystEstimate: bulkData["/analyst-estimate"],
getAnalystInsight: bulkData["/analyst-insight"],
const getAnalystInsight = async () => { getTopAnalystSummary: bulkData["/top-analyst-summary-rating"],
const response = await fetch(apiURL + "/analyst-insight", { };
method: "POST", } catch (error) {
headers: { return { error: "Failed to load data" };
"Content-Type": "application/json", }
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
const output = await response.json();
return output;
};
const getTopAnalystSummary = async () => {
const response = await fetch(apiURL + "/top-analyst-summary-rating", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
const output = await response.json();
return output;
};
// Make sure to return a promise
return {
getAnalystEstimate: await getAnalystEstimate(),
getAnalystInsight: await getAnalystInsight(),
getAnalystTickerHistory: await getAnalystTickerHistory(),
getTopAnalystSummary: await getTopAnalystSummary(),
};
}; };