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,25 +1,22 @@
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')
{
if (endpoint === "/crypto-profile") {
companyName = output?.name;
}
@ -27,20 +24,23 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
};
const fetchWatchlist = async (fastifyURL, userId) => {
const postData = {'userId': userId}
const response = await fetch(fastifyURL+'/all-watchlists', {
method: 'POST',
try {
const postData = { userId: userId };
const response = await fetch(fastifyURL + "/all-watchlists", {
method: "POST",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
body: JSON.stringify(postData)
body: JSON.stringify(postData),
});
const output = (await response.json())?.items;
return output;
}
} catch (e) {
console.log(e);
return [];
}
};
/*
async function fetchPortfolio(fastifyURL, userId)
@ -61,34 +61,26 @@ 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),
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,
getStockQuote,
@ -96,6 +88,4 @@ export const load = async ({ params, locals, setHeaders}) => {
getUserWatchlist,
companyName,
};
};

View File

@ -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) => {
try {
const response = await fetch(`${fastifyURL}${endpoint}`, {
method: 'POST',
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId })
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,

View File

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

View File

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