bugfixing

This commit is contained in:
MuslemRahimi 2024-09-16 21:57:16 +02:00
parent ce45e12d2f
commit 9dc12f34ed
4 changed files with 88 additions and 68 deletions

View File

@ -1,46 +1,46 @@
let companyName; let companyName;
const fetchData = async (apiURL, apiKey, endpoint, ticker) => { const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
const postData = { const postData = {
ticker: ticker ticker: ticker,
}; };
const response = await fetch(apiURL + endpoint, { const response = await fetch(apiURL + endpoint, {
method: 'POST', method: "POST",
headers: { 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(); const output = await response.json();
if(endpoint === '/crypto-profile') if (endpoint === "/crypto-profile") {
{ companyName = output?.name;
companyName = output?.name; }
}
return output; return output;
}; };
const fetchWatchlist = async (fastifyURL, userId) => { const fetchWatchlist = async (fastifyURL, userId) => {
try {
const postData = {'userId': userId} const postData = { userId: userId };
const response = await fetch(fastifyURL+'/all-watchlists', { const response = await fetch(fastifyURL + "/all-watchlists", {
method: 'POST', method: "POST",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json",
}, },
body: JSON.stringify(postData) body: JSON.stringify(postData),
}); });
const output = (await response.json())?.items; const output = (await response.json())?.items;
return output; return output;
} } catch (e) {
console.log(e);
return [];
}
};
/* /*
async function fetchPortfolio(fastifyURL, userId) 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 apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL; let fastifyURL = locals?.fastifyURL;
let apiKey = locals?.apiKey; let apiKey = locals?.apiKey;
const promises = [ const promises = [
fetchData(apiURL,apiKey,'/crypto-profile',params.tickerID), fetchData(apiURL, apiKey, "/crypto-profile", params.tickerID),
fetchData(apiURL,apiKey,'/stock-quote',params.tickerID), fetchData(apiURL, apiKey, "/stock-quote", params.tickerID),
fetchData(apiURL,apiKey,'/one-day-price',params.tickerID), fetchData(apiURL, apiKey, "/one-day-price", params.tickerID),
fetchWatchlist(fastifyURL, locals?.user?.id), fetchWatchlist(fastifyURL, locals?.user?.id),
//fetchPortfolio(fastifyURL, locals?.user?.id) //fetchPortfolio(fastifyURL, locals?.user?.id)
]; ];
const [ const [getCryptoProfile, getStockQuote, getOneDayPrice, getUserWatchlist] =
getCryptoProfile, await Promise.all(promises);
getStockQuote,
getOneDayPrice,
getUserWatchlist,
] = await Promise.all(promises);
setHeaders({ setHeaders({
'cache-control': 'public, max-age=300' "cache-control": "public, max-age=300",
}); });
return { return {
getCryptoProfile, getCryptoProfile,
@ -96,6 +88,4 @@ export const load = async ({ params, locals, setHeaders}) => {
getUserWatchlist, getUserWatchlist,
companyName, companyName,
}; };
}; };

View File

@ -1,33 +1,48 @@
const cleanString = (input) => { const cleanString = (input) => {
const substringsToRemove = [ const substringsToRemove = [
'Depositary', 'Inc.', 'Incorporated', 'Holdings', 'Corporation', 'Corporations', "Depositary",
'LLC', 'Holdings plc American Depositary Shares', 'Holding Corporation', 'Oyj', "Inc.",
'Company', 'The', 'plc', "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'); const pattern = new RegExp(`\\b(${substringsToRemove.join("|")})\\b|,`, "gi");
return input?.replace(pattern, '').trim(); return input?.replace(pattern, "").trim();
}; };
const fetchData = async (apiURL, apiKey, endpoint, ticker) => { const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
const response = await fetch(`${apiURL}${endpoint}`, { const response = await fetch(`${apiURL}${endpoint}`, {
method: 'POST', method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"X-API-KEY": apiKey "X-API-KEY": apiKey,
}, },
body: JSON.stringify({ ticker }) body: JSON.stringify({ ticker }),
}); });
return response.json(); return response.json();
}; };
const fetchFromFastify = async (fastifyURL, endpoint, userId) => { const fetchFromFastify = async (fastifyURL, endpoint, userId) => {
const response = await fetch(`${fastifyURL}${endpoint}`, { try {
method: 'POST', const response = await fetch(`${fastifyURL}${endpoint}`, {
headers: { "Content-Type": "application/json" }, method: "POST",
body: JSON.stringify({ userId }) headers: { "Content-Type": "application/json" },
}); body: JSON.stringify({ userId }),
const { items } = await response.json(); });
return items; const { items } = await response.json();
return items;
} catch (e) {
console.log(e);
return [];
}
}; };
export const load = async ({ params, locals, setHeaders }) => { export const load = async ({ params, locals, setHeaders }) => {
@ -35,13 +50,21 @@ export const load = async ({ params, locals, setHeaders }) => {
const { tickerID } = params; const { tickerID } = params;
const endpoints = [ const endpoints = [
'/etf-profile', '/similar-etfs', '/etf-country-weighting', '/etf-holdings', "/etf-profile",
'/stock-dividend', '/stock-quote', '/wiim', '/one-day-price' "/similar-etfs",
"/etf-country-weighting",
"/etf-holdings",
"/stock-dividend",
"/stock-quote",
"/wiim",
"/one-day-price",
]; ];
const promises = [ const promises = [
...endpoints.map(endpoint => fetchData(apiURL, apiKey, endpoint, tickerID)), ...endpoints.map((endpoint) =>
fetchFromFastify(fastifyURL, '/all-watchlists', user?.id), fetchData(apiURL, apiKey, endpoint, tickerID)
),
fetchFromFastify(fastifyURL, "/all-watchlists", user?.id),
//fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id) //fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id)
]; ];
@ -57,7 +80,7 @@ export const load = async ({ params, locals, setHeaders }) => {
getUserWatchlist, getUserWatchlist,
] = await Promise.all(promises); ] = await Promise.all(promises);
setHeaders({ 'cache-control': 'public, max-age=300' }); setHeaders({ "cache-control": "public, max-age=300" });
return { return {
getETFProfile, getETFProfile,
@ -71,4 +94,4 @@ export const load = async ({ params, locals, setHeaders }) => {
getUserWatchlist, getUserWatchlist,
companyName: cleanString(getETFProfile?.at(0)?.name), companyName: cleanString(getETFProfile?.at(0)?.name),
}; };
}; };

View File

@ -31,13 +31,18 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
}; };
const fetchFromFastify = async (fastifyURL, endpoint, userId) => { const fetchFromFastify = async (fastifyURL, endpoint, userId) => {
const response = await fetch(`${fastifyURL}${endpoint}`, { try {
method: "POST", const response = await fetch(`${fastifyURL}${endpoint}`, {
headers: { "Content-Type": "application/json" }, method: "POST",
body: JSON.stringify({ userId }), headers: { "Content-Type": "application/json" },
}); body: JSON.stringify({ userId }),
const { items } = await response.json(); });
return items; const { items } = await response.json();
return items;
} catch (e) {
console.log(e);
return [];
}
}; };
const fetchCommunitySentiment = async (pb, ticker, cookies) => { const fetchCommunitySentiment = async (pb, ticker, cookies) => {

View File

@ -50,6 +50,8 @@ export const load = async ({ parent, params }) => {
output = await response.json(); output = await response.json();
console.log(output);
setCache(params.tickerID, output, "getOptionsHistoricalData"); setCache(params.tickerID, output, "getOptionsHistoricalData");
} }