diff --git a/src/routes/stocks/[tickerID]/analyst/+page.server.ts b/src/routes/stocks/[tickerID]/analyst/+page.server.ts deleted file mode 100644 index 3fe2d9dc..00000000 --- a/src/routes/stocks/[tickerID]/analyst/+page.server.ts +++ /dev/null @@ -1,44 +0,0 @@ -const usRegion = ['cle1','iad1','pdx1','sfo1']; - - - -export const load = async ({ params, locals }) => { - - const userRegion = locals.region?.split("::")[0]; - - let apiURL; - - if (usRegion?.includes(userRegion)) { - apiURL = import.meta.env.VITE_USEAST_API_URL; - } else { - apiURL = import.meta.env.VITE_EU_API_URL; - }; - - - const getAnalystTickerHistory = async () => { - - 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', - }, - body: JSON.stringify(postData) - }); - - const output = await response.json(); - - - - return output; - }; - - // Make sure to return a promise - return { - getAnalystTickerHistory: await getAnalystTickerHistory() - }; -}; diff --git a/src/routes/stocks/[tickerID]/analyst/+page.ts b/src/routes/stocks/[tickerID]/analyst/+page.ts new file mode 100644 index 00000000..3b5103e7 --- /dev/null +++ b/src/routes/stocks/[tickerID]/analyst/+page.ts @@ -0,0 +1,56 @@ +import { userRegion, getCache, setCache } from '$lib/store'; + +const usRegion = ['cle1','iad1','pdx1','sfo1']; + +let apiURL = import.meta.env.VITE_EU_API_URL; // Set a default API URL + +userRegion.subscribe(value => { + + if (usRegion.includes(value)) { + apiURL = import.meta.env.VITE_USEAST_API_URL; + } else { + apiURL = import.meta.env.VITE_EU_API_URL; + } +}); + + + + + +export const load = async ({ params }) => { + + const getAnalystTickerHistory = async () => { + + let output; + + const cachedData = getCache(params.tickerID, 'getAnalystTickerHistory'); + if (cachedData) { + output = cachedData; + } else { + 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', + }, + 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() + }; +}; diff --git a/src/routes/stocks/[tickerID]/insider/+page.server.ts b/src/routes/stocks/[tickerID]/insider/+page.ts similarity index 64% rename from src/routes/stocks/[tickerID]/insider/+page.server.ts rename to src/routes/stocks/[tickerID]/insider/+page.ts index a70674e6..b5a35ded 100644 --- a/src/routes/stocks/[tickerID]/insider/+page.server.ts +++ b/src/routes/stocks/[tickerID]/insider/+page.ts @@ -1,5 +1,18 @@ +import { userRegion, getCache, setCache } from '$lib/store'; + const usRegion = ['cle1','iad1','pdx1','sfo1']; +let apiURL = import.meta.env.VITE_EU_API_URL; // Set a default API URL + +userRegion.subscribe(value => { + + if (usRegion.includes(value)) { + apiURL = import.meta.env.VITE_USEAST_API_URL; + } else { + apiURL = import.meta.env.VITE_EU_API_URL; + } +}); + @@ -25,26 +38,19 @@ const transactionTypeMap = { '': 'n/a' }; -export const load = async ({ params, locals }) => { - - const userRegion = locals.region?.split("::")[0]; - - let apiURL; - - if (usRegion?.includes(userRegion)) { - apiURL = import.meta.env.VITE_USEAST_API_URL; - } else { - apiURL = import.meta.env.VITE_EU_API_URL; - }; - +export const load = async ({ params }) => { const getInsiderTrading = async () => { let output; - // Get cached data for the specific tickerID - const postData = { - ticker: params.tickerID - }; + + const cachedData = getCache(params.tickerID, 'getInsiderTrading'); + if (cachedData) { + output = cachedData; + } else { + const postData = { + ticker: params.tickerID + }; // make the POST request to the endpoint const response = await fetch(apiURL + '/insider-trading', { @@ -63,7 +69,10 @@ export const load = async ({ params, locals }) => { ? transactionTypeMap[item?.transactionType](item) : transactionTypeMap[item?.transactionType] || 'n/a' })); - + + setCache(params.tickerID, output, 'getInsiderTrading'); + + } return output; }; @@ -71,10 +80,20 @@ export const load = async ({ params, locals }) => { const getInsiderTradingStatistics = async () => { - // Get cached data for the specific tickerID - const postData = { - ticker: params.tickerID - }; + let sums = { + totalBought: 0, + totalSold: 0, + purchases: 0, + sales: 0 + }; + + const cachedData = getCache(params.tickerID, 'getInsiderTradingStatistics'); + if (cachedData) { + sums = cachedData; + } else { + const postData = { + ticker: params.tickerID + }; // make the POST request to the endpoint const response = await fetch(apiURL + '/insider-trading-statistics', { @@ -85,15 +104,7 @@ export const load = async ({ params, locals }) => { body: JSON.stringify(postData) }); - const output = await response.json(); - - // Initialize a new object to store the sums - let sums = { - totalBought: 0, - totalSold: 0, - purchases: 0, - sales: 0 - }; + const output = await response?.json(); // Iterate through the list and accumulate the values output?.forEach(item => { @@ -102,9 +113,10 @@ export const load = async ({ params, locals }) => { sums.purchases += item.purchases; sums.sales += item.sales; }); + + setCache(params.tickerID, sums, 'getInsiderTradingStatistics'); - console.log(sums) - + } return sums; };