From 9dc12f34ed2b55be4e8bd63fc22b9da67ffae29c Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 16 Sep 2024 21:57:16 +0200 Subject: [PATCH] bugfixing --- .../crypto/[tickerID]/+layout.server.ts | 70 ++++++++----------- src/routes/etf/[tickerID]/+layout.server.ts | 65 +++++++++++------ .../stocks/[tickerID]/+layout.server.ts | 19 +++-- src/routes/stocks/[tickerID]/options/+page.ts | 2 + 4 files changed, 88 insertions(+), 68 deletions(-) diff --git a/src/routes/crypto/[tickerID]/+layout.server.ts b/src/routes/crypto/[tickerID]/+layout.server.ts index d83545a0..6b0639ca 100644 --- a/src/routes/crypto/[tickerID]/+layout.server.ts +++ b/src/routes/crypto/[tickerID]/+layout.server.ts @@ -1,46 +1,46 @@ - - let companyName; const fetchData = async (apiURL, apiKey, endpoint, ticker) => { - const postData = { - ticker: ticker + ticker: ticker, }; const response = await fetch(apiURL + endpoint, { - method: 'POST', + method: "POST", headers: { - "Content-Type": "application/json", "X-API-KEY": apiKey + "Content-Type": "application/json", + "X-API-KEY": apiKey, }, - body: JSON.stringify(postData) + body: JSON.stringify(postData), }); const output = await response.json(); - if(endpoint === '/crypto-profile') - { - companyName = output?.name; - } + if (endpoint === "/crypto-profile") { + companyName = output?.name; + } return output; }; const fetchWatchlist = async (fastifyURL, userId) => { - - const postData = {'userId': userId} - const response = await fetch(fastifyURL+'/all-watchlists', { - method: 'POST', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(postData) + try { + const postData = { userId: userId }; + const response = await fetch(fastifyURL + "/all-watchlists", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), }); - const output = (await response.json())?.items; return output; -} + } catch (e) { + console.log(e); + return []; + } +}; /* async function fetchPortfolio(fastifyURL, userId) @@ -61,33 +61,25 @@ async function fetchPortfolio(fastifyURL, userId) } */ -export const load = async ({ params, locals, setHeaders}) => { - - +export const load = async ({ params, locals, setHeaders }) => { let apiURL = locals?.apiURL; let fastifyURL = locals?.fastifyURL; let apiKey = locals?.apiKey; - const promises = [ - fetchData(apiURL,apiKey,'/crypto-profile',params.tickerID), - fetchData(apiURL,apiKey,'/stock-quote',params.tickerID), - fetchData(apiURL,apiKey,'/one-day-price',params.tickerID), + const promises = [ + fetchData(apiURL, apiKey, "/crypto-profile", params.tickerID), + fetchData(apiURL, apiKey, "/stock-quote", params.tickerID), + fetchData(apiURL, apiKey, "/one-day-price", params.tickerID), fetchWatchlist(fastifyURL, locals?.user?.id), //fetchPortfolio(fastifyURL, locals?.user?.id) ]; - const [ - getCryptoProfile, - getStockQuote, - getOneDayPrice, - getUserWatchlist, - ] = await Promise.all(promises); - + const [getCryptoProfile, getStockQuote, getOneDayPrice, getUserWatchlist] = + await Promise.all(promises); setHeaders({ - 'cache-control': 'public, max-age=300' - }); - + "cache-control": "public, max-age=300", + }); return { getCryptoProfile, @@ -96,6 +88,4 @@ export const load = async ({ params, locals, setHeaders}) => { getUserWatchlist, companyName, }; - - }; diff --git a/src/routes/etf/[tickerID]/+layout.server.ts b/src/routes/etf/[tickerID]/+layout.server.ts index 7c79b97f..c3af3c27 100644 --- a/src/routes/etf/[tickerID]/+layout.server.ts +++ b/src/routes/etf/[tickerID]/+layout.server.ts @@ -1,33 +1,48 @@ const cleanString = (input) => { const substringsToRemove = [ - 'Depositary', 'Inc.', 'Incorporated', 'Holdings', 'Corporation', 'Corporations', - 'LLC', 'Holdings plc American Depositary Shares', 'Holding Corporation', 'Oyj', - 'Company', 'The', 'plc', + "Depositary", + "Inc.", + "Incorporated", + "Holdings", + "Corporation", + "Corporations", + "LLC", + "Holdings plc American Depositary Shares", + "Holding Corporation", + "Oyj", + "Company", + "The", + "plc", ]; - const pattern = new RegExp(`\\b(${substringsToRemove.join('|')})\\b|,`, 'gi'); - return input?.replace(pattern, '').trim(); + const pattern = new RegExp(`\\b(${substringsToRemove.join("|")})\\b|,`, "gi"); + return input?.replace(pattern, "").trim(); }; const fetchData = async (apiURL, apiKey, endpoint, ticker) => { const response = await fetch(`${apiURL}${endpoint}`, { - method: 'POST', + method: "POST", headers: { "Content-Type": "application/json", - "X-API-KEY": apiKey + "X-API-KEY": apiKey, }, - body: JSON.stringify({ ticker }) + body: JSON.stringify({ ticker }), }); return response.json(); }; const fetchFromFastify = async (fastifyURL, endpoint, userId) => { - const response = await fetch(`${fastifyURL}${endpoint}`, { - method: 'POST', - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ userId }) - }); - const { items } = await response.json(); - return items; + try { + const response = await fetch(`${fastifyURL}${endpoint}`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ userId }), + }); + const { items } = await response.json(); + return items; + } catch (e) { + console.log(e); + return []; + } }; export const load = async ({ params, locals, setHeaders }) => { @@ -35,13 +50,21 @@ export const load = async ({ params, locals, setHeaders }) => { const { tickerID } = params; const endpoints = [ - '/etf-profile', '/similar-etfs', '/etf-country-weighting', '/etf-holdings', - '/stock-dividend', '/stock-quote', '/wiim', '/one-day-price' + "/etf-profile", + "/similar-etfs", + "/etf-country-weighting", + "/etf-holdings", + "/stock-dividend", + "/stock-quote", + "/wiim", + "/one-day-price", ]; const promises = [ - ...endpoints.map(endpoint => fetchData(apiURL, apiKey, endpoint, tickerID)), - fetchFromFastify(fastifyURL, '/all-watchlists', user?.id), + ...endpoints.map((endpoint) => + fetchData(apiURL, apiKey, endpoint, tickerID) + ), + fetchFromFastify(fastifyURL, "/all-watchlists", user?.id), //fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id) ]; @@ -57,7 +80,7 @@ export const load = async ({ params, locals, setHeaders }) => { getUserWatchlist, ] = await Promise.all(promises); - setHeaders({ 'cache-control': 'public, max-age=300' }); + setHeaders({ "cache-control": "public, max-age=300" }); return { getETFProfile, @@ -71,4 +94,4 @@ export const load = async ({ params, locals, setHeaders }) => { getUserWatchlist, companyName: cleanString(getETFProfile?.at(0)?.name), }; -}; \ No newline at end of file +}; diff --git a/src/routes/stocks/[tickerID]/+layout.server.ts b/src/routes/stocks/[tickerID]/+layout.server.ts index 2cb97f2c..50c30d25 100644 --- a/src/routes/stocks/[tickerID]/+layout.server.ts +++ b/src/routes/stocks/[tickerID]/+layout.server.ts @@ -31,13 +31,18 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => { }; const fetchFromFastify = async (fastifyURL, endpoint, userId) => { - const response = await fetch(`${fastifyURL}${endpoint}`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ userId }), - }); - const { items } = await response.json(); - return items; + try { + const response = await fetch(`${fastifyURL}${endpoint}`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ userId }), + }); + const { items } = await response.json(); + return items; + } catch (e) { + console.log(e); + return []; + } }; const fetchCommunitySentiment = async (pb, ticker, cookies) => { diff --git a/src/routes/stocks/[tickerID]/options/+page.ts b/src/routes/stocks/[tickerID]/options/+page.ts index 10ff6ef1..0a5f0f71 100644 --- a/src/routes/stocks/[tickerID]/options/+page.ts +++ b/src/routes/stocks/[tickerID]/options/+page.ts @@ -50,6 +50,8 @@ export const load = async ({ parent, params }) => { output = await response.json(); + console.log(output); + setCache(params.tickerID, output, "getOptionsHistoricalData"); }