bugfixing

This commit is contained in:
MuslemRahimi 2025-02-22 22:17:32 +01:00
parent 1411cde332
commit f602c1a1d0

View File

@ -32,20 +32,14 @@ const cleanString = (() => {
const CACHE_DURATION = 5 * 60 * 1000;
const REQUEST_TIMEOUT = 5000;
const ENDPOINTS = Object.freeze([
"/index-profile",
"/etf-holdings",
"/etf-sector-weighting",
"/stockdeck",
"/analyst-summary-rating",
"/stock-quote",
"/pre-post-quote",
"/wiim",
"/one-day-price",
"/stock-news"
]);
const SPY_PROXY_ENDPOINTS = Object.freeze([
"/etf-holdings",
"/etf-sector-weighting",
"/wiim",
"/next-earnings",
"/earnings-surprise",
"/stock-news"
]);
@ -94,13 +88,9 @@ const fetchWithTimeout = async (url, options, timeout) => {
}
};
// Main data fetching function with SPX/SPY handling
// Main data fetching function
const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
const useSpyTicker = ticker?.toLowerCase() === "^spx" &&
SPY_PROXY_ENDPOINTS.some(proxyEndpoint => ENDPOINTS.includes(proxyEndpoint));
const effectiveTicker = useSpyTicker ? "SPY" : ticker;
const cacheKey = `${endpoint}-${effectiveTicker}`;
const cacheKey = `${endpoint}-${ticker}`;
const cachedData = dataCache.get(cacheKey);
if (cachedData) return cachedData;
@ -110,10 +100,7 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
"Content-Type": "application/json",
"X-API-KEY": apiKey
},
body: JSON.stringify({
ticker: effectiveTicker,
endpoints: ENDPOINTS
})
body: JSON.stringify({ ticker, endpoints: ENDPOINTS })
};
try {
@ -144,64 +131,50 @@ const fetchWatchlist = async (pb, userId) => {
}
};
// Helper function to generate default response
const getDefaultResponse = (tickerID) => ({
getIndexProfile: [],
getIndexHolding: [],
getIndexSectorWeighting: [],
getStockQuote: [],
getPrePostQuote: [],
getWhyPriceMoved: [],
getOneDayPrice: [],
getNews: [],
getUserWatchlist: [],
companyName: '',
getParams: tickerID
});
// Main load function with parallel fetching
export const load = async ({ params, locals }) => {
const { apiURL, apiKey, pb, user } = locals;
const { tickerID } = params;
if (!tickerID) {
return getDefaultResponse(tickerID);
return { error: 'Invalid ticker ID' };
}
try {
// Fetch data in parallel
const [indexData, userWatchlist] = await Promise.all([
const [stockData, userWatchlist] = await Promise.all([
fetchData(apiURL, apiKey, "/bulk-data", tickerID),
fetchWatchlist(pb, user?.id)
]);
// Destructure with default empty arrays to prevent undefined errors
// Destructure with default empty object to prevent undefined errors
const {
'/index-profile': getIndexProfile = [],
'/etf-holdings': getIndexHolding = [],
'/etf-sector-weighting': getIndexSectorWeighting = [],
'/stock-quote': getStockQuote = [],
'/pre-post-quote': getPrePostQuote = [],
'/wiim': getWhyPriceMoved = [],
'/one-day-price': getOneDayPrice = [],
'/stock-news': getNews = []
} = indexData;
'/stockdeck': getStockDeck = {},
'/analyst-summary-rating': getAnalystSummary = {},
'/stock-quote': getStockQuote = {},
'/pre-post-quote': getPrePostQuote = {},
'/wiim': getWhyPriceMoved = {},
'/one-day-price': getOneDayPrice = {},
'/next-earnings': getNextEarnings = {},
'/earnings-surprise': getEarningsSurprise = {},
'/stock-news': getNews = {}
} = stockData;
return {
getIndexProfile,
getIndexHolding,
getIndexSectorWeighting,
getStockDeck,
getAnalystSummary,
getStockQuote,
getPrePostQuote,
getWhyPriceMoved,
getOneDayPrice,
getNextEarnings,
getEarningsSurprise,
getNews,
getUserWatchlist: userWatchlist,
companyName: cleanString(getIndexProfile?.at(0)?.name),
companyName: cleanString(getStockDeck?.companyName),
getParams: tickerID
};
} catch (error) {
console.error('Error in load function:', error);
return getDefaultResponse(tickerID);
return { error: 'Failed to load stock data' };
}
};