clean code

This commit is contained in:
MuslemRahimi 2024-07-27 01:23:58 +02:00
parent 265c83c1fd
commit f54cc2f355
18 changed files with 93 additions and 388 deletions

View File

@ -11,12 +11,14 @@ export const handle = async ({ event, resolve }) => {
const pbUrl = import.meta.env[isUsRegion ? 'VITE_USEAST_POCKETBASE_URL' : 'VITE_EU_POCKETBASE_URL']; const pbUrl = import.meta.env[isUsRegion ? 'VITE_USEAST_POCKETBASE_URL' : 'VITE_EU_POCKETBASE_URL'];
const apiURL = import.meta.env[isUsRegion ? 'VITE_USEAST_API_URL' : 'VITE_EU_API_URL']; const apiURL = import.meta.env[isUsRegion ? 'VITE_USEAST_API_URL' : 'VITE_EU_API_URL'];
const fastifyURL = import.meta.env[isUsRegion ? 'VITE_USEAST_FASTIFY_URL' : 'VITE_EU_FASTIFY_URL']; const fastifyURL = import.meta.env[isUsRegion ? 'VITE_USEAST_FASTIFY_URL' : 'VITE_EU_FASTIFY_URL'];
const wsURL = import.meta.env[isUsRegion ? 'VITE_USEAST_WS_URL' : 'VITE_EU_WS_URL'];
event.locals = { event.locals = {
region: decodeURIComponent(regionHeader), region: decodeURIComponent(regionHeader),
pb: new PocketBase(pbUrl), pb: new PocketBase(pbUrl),
apiURL, apiURL,
fastifyURL, fastifyURL,
wsURL,
apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY
}; };

View File

@ -1,28 +1,13 @@
export const load = ({ locals, cookies }) => { export const load = ({ locals, cookies }) => {
const { user, region, apiURL, fastifyURL, wsURL, apiKey } = locals;
const consent = cookies?.get('cookie-consent') return {
user: user || undefined,
if (locals.user) { region,
return { cookieConsent: cookies?.get('cookie-consent'),
user: locals.user, apiURL,
region: locals.region, fastifyURL,
cookieConsent: consent, wsURL,
apiURL: locals.apiURL, apiKey,
fastifyURL: locals.fastifyURL, };
apiKey: locals.apiKey,
};
}
return {
user: undefined,
region: locals.region,
cookieConsent: consent,
apiURL: locals.apiURL,
fastifyURL: locals.fastifyURL,
apiKey: locals.apiKey,
}; };
};

View File

@ -1,5 +1,4 @@
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let companyName; let companyName;
@ -63,20 +62,9 @@ async function fetchPortfolio(fastifyURL, userId)
export const load = async ({ params, locals, setHeaders}) => { export const load = async ({ params, locals, setHeaders}) => {
const userRegion = locals?.region?.split("::")[0]; let apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL;
let apiURL = locals?.apiURL; let apiKey = locals?.apiKey;
let fastifyURL = locals?.fastifyURL;
let apiKey = locals?.apiKey;
let wsURL;
if (usRegion?.includes(userRegion)) {
wsURL = import.meta.env.VITE_USEAST_WS_URL;
} else {
wsURL = import.meta.env.VITE_EU_WS_URL;
};
const promises = [ const promises = [
fetchData(apiURL,apiKey,'/crypto-profile',params.tickerID), fetchData(apiURL,apiKey,'/crypto-profile',params.tickerID),

View File

@ -1,24 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({parent}) => {
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 () => {
const getDividendCalendar = async () => { const getDividendCalendar = async () => {
let output; let output;
@ -28,12 +11,12 @@ export const load = async () => {
output = cachedData; output = cachedData;
} else { } else {
const data = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/dividends-calendar', { const response = await fetch(data?.apiURL + '/dividends-calendar', {
method: 'GET', method: 'GET',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
}); });

View File

@ -1,24 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({parent}) => {
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 () => {
const getEarningsCalendar = async () => { const getEarningsCalendar = async () => {
let output; let output;
@ -28,12 +11,12 @@ export const load = async () => {
output = cachedData; output = cachedData;
} else { } else {
const data = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/earnings-calendar', { const response = await fetch(data?.apiURL + '/earnings-calendar', {
method: 'GET', method: 'GET',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
}); });

View File

@ -1,24 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({parent}) => {
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 () => {
const getEconomicCalendar = async () => { const getEconomicCalendar = async () => {
let output; let output;
@ -28,7 +11,7 @@ export const load = async () => {
output = cachedData; output = cachedData;
} else { } else {
const { apiURL, apiKey } = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/economic-calendar', { const response = await fetch(apiURL + '/economic-calendar', {
method: 'GET', method: 'GET',

View File

@ -1,6 +1,3 @@
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let companyName; let companyName;
@ -88,28 +85,10 @@ async function fetchPortfolio(fastifyURL, userId)
export const load = async ({ params, locals, setHeaders}) => { export const load = async ({ params, locals, setHeaders}) => {
const userRegion = locals?.region?.split("::")[0]; let apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL;
let apiURL = locals?.apiURL; let apiKey = locals?.apiKey;
let fastifyURL = locals?.fastifyURL; let wsURL = locals?.wsURL;
let apiKey = locals?.apiKey;
let wsURL;
if (usRegion?.includes(userRegion)) {
wsURL = import.meta.env.VITE_USEAST_WS_URL;
} else {
wsURL = import.meta.env.VITE_EU_WS_URL;
};
if (usRegion?.includes(userRegion)) {
apiURL = import.meta.env.VITE_USEAST_API_URL;
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
} else {
apiURL = import.meta.env.VITE_EU_API_URL;
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
};
const promises = [ const promises = [

View File

@ -1,24 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
userRegion.subscribe(value => { export const load = async ({ parent, params }) => {
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 getSenateTrading = async () => { const getSenateTrading = async () => {
let output; let output;
@ -27,15 +10,16 @@ export const load = async ({ params }) => {
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const data = await parent();
const postData = { const postData = {
ticker: params.tickerID ticker: params.tickerID
}; };
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/congress-trading-ticker', { const response = await fetch(data?.apiURL + '/congress-trading-ticker', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
body: JSON.stringify(postData) body: JSON.stringify(postData)
}); });

View File

@ -1,22 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({ parent, params }) => {
let apiURL = import.meta.env.VITE_EU_API_URL; // Set a default API URL
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 getStockDividend = async () => { const getStockDividend = async () => {
let newsList; let newsList;
@ -25,15 +10,16 @@ export const load = async ({ params }) => {
if (cachedData) { if (cachedData) {
newsList = cachedData; newsList = cachedData;
} else { } else {
const data = await parent();
const postData = { const postData = {
ticker: params.tickerID ticker: params.tickerID
}; };
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/stock-dividend', { const response = await fetch(data?.apiURL + '/stock-dividend', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
body: JSON.stringify(postData) body: JSON.stringify(postData)
}); });

View File

@ -1,24 +1,8 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 ({ parent, params }) => {
export const load = async ({ params }) => {
const getETFHoldings = async () => { const getETFHoldings = async () => {
let output; let output;
@ -27,15 +11,16 @@ export const load = async ({ params }) => {
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const data = await parent();
const postData = { const postData = {
ticker: params.tickerID ticker: params.tickerID
}; };
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/etf-holdings', { const response = await fetch(data?.apiURL + '/etf-holdings', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
body: JSON.stringify(postData) body: JSON.stringify(postData)
}); });

View File

@ -1,24 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({ parent, params }) => {
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 getStockNews = async () => { const getStockNews = async () => {
let output; let output;
@ -27,15 +10,16 @@ export const load = async ({ params }) => {
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const data = await parent();
const postData = { const postData = {
ticker: params.tickerID ticker: params.tickerID
}; };
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/stock-news', { const response = await fetch(data?.apiURL + '/stock-news', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
body: JSON.stringify(postData) body: JSON.stringify(postData)
}); });

View File

@ -1,65 +1,31 @@
import { userRegion, displayCompanyName, getCache, setCache } from '$lib/store'; import { displayCompanyName, getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 }) => {
export const load = async ({ parent, params }) => {
const getHedgeFundsData = async () => { const getHedgeFundsData = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache(params.slug, 'getHedgeFundsData'); const cachedData = getCache(params.slug, 'getHedgeFundsData');
if (cachedData) { if (cachedData) return cachedData;
output = cachedData;
const { apiURL, apiKey } = await parent();
const response = await fetch(apiURL+'/cik-data', {
method: 'POST',
headers: {
"Content-Type": "application/json",
"X-API-KEY": apiKey
},
body: JSON.stringify({ cik: params.slug })
});
const output = await response.json();
if (output?.holdings) {
output.holdings = output?.holdings?.filter(item => item?.sharesNumber && item?.symbol);
} }
else {
setCache(params.slug, output, 'getHedgeFundsData');
const postData = { displayCompanyName.update(() => output?.name ?? params.slug);
cik: params.slug
};
// make the POST request to the endpoint
const response = await fetch(apiURL + '/cik-data', {
method: 'POST',
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
},
body: JSON.stringify(postData)
});
output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getHedgeFundsData'
try {
output.holdings = output?.holdings?.filter(item => item?.sharesNumber !== 0 && item?.symbol !== null); //item?.symbol !== null
} catch(e) {
console.log(e)
}
setCache(params.slug, output, 'getHedgeFundsData');
}
displayCompanyName.update(value => output?.name ?? params.slug)
return output; return output;
}; };
// Make sure to return a promise
return { return {
getHedgeFundsData: await getHedgeFundsData() getHedgeFundsData: await getHedgeFundsData()
}; };

View File

@ -1,24 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({parent, params}) => {
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 getIPOCalendar = async () => { const getIPOCalendar = async () => {
let output; let output;
@ -28,13 +11,14 @@ export const load = async ({params}) => {
output = cachedData; output = cachedData;
} else { } else {
const data = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const postData = {'year': 'all'}; const postData = {'year': 'all'};
const response = await fetch(apiURL + '/ipo-calendar', { const response = await fetch(data?.apiURL + '/ipo-calendar', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
body: JSON.stringify(postData), body: JSON.stringify(postData),
}); });

View File

@ -1,24 +1,6 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
export const load = async ({parent, params}) => {
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 getIPOCalendar = async () => { const getIPOCalendar = async () => {
let output; let output;
@ -28,13 +10,14 @@ export const load = async ({params}) => {
output = cachedData; output = cachedData;
} else { } else {
const data = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const postData = {'year': params.slug}; const postData = {'year': params.slug};
const response = await fetch(apiURL + '/ipo-calendar', { const response = await fetch(data?.apiURL + '/ipo-calendar', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json", "X-API-KEY": data?.apiKey
}, },
body: JSON.stringify(postData), body: JSON.stringify(postData),
}); });

View File

@ -1,24 +1,7 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({parent}) => {
let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
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 () => {
const getAmexStocks = async () => { const getAmexStocks = async () => {
let output; let output;
@ -28,6 +11,7 @@ export const load = async () => {
output = cachedData; output = cachedData;
} else { } else {
const { apiURL, apiKey} = await parent();
const postData = {'filterList': 'amex'} const postData = {'filterList': 'amex'}
const response = await fetch(apiURL + '/filter-stock-list', { const response = await fetch(apiURL + '/filter-stock-list', {

View File

@ -1,4 +1,4 @@
import { userRegion, isOpen} from '$lib/store'; import { isOpen} from '$lib/store';
const checkMarketHour = async () => { const checkMarketHour = async () => {
@ -19,30 +19,10 @@ const checkMarketHour = async () => {
} }
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let apiURL; export const load = async ({parent}) => {
let wsURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
userRegion.subscribe(value => {
if (usRegion?.includes(value)) {
apiURL = import.meta.env.VITE_USEAST_API_URL;
wsURL = import.meta.env.VITE_USEAST_WS_URL;
} else {
apiURL = import.meta.env.VITE_EU_API_URL;
wsURL = import.meta.env.VITE_EU_WS_URL;
}
});
export const load = async () => {
await checkMarketHour(); await checkMarketHour();
const { apiURL, apiKey} = await parent();
const getOptionsFlowFeed = async () => { const getOptionsFlowFeed = async () => {
// make the POST request to the endpoint // make the POST request to the endpoint
@ -60,6 +40,5 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getOptionsFlowFeed: await getOptionsFlowFeed(), getOptionsFlowFeed: await getOptionsFlowFeed(),
wsURL,
}; };
}; };

View File

@ -1,4 +1,4 @@
import { userRegion, isOpen} from '$lib/store'; import {isOpen} from '$lib/store';
const checkMarketHour = async () => { const checkMarketHour = async () => {
@ -19,30 +19,9 @@ const checkMarketHour = async () => {
} }
const usRegion = ['cle1','iad1','pdx1','sfo1']; export const load = async ({parent}) => {
let apiURL;
let wsURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
userRegion.subscribe(value => {
if (usRegion?.includes(value)) {
apiURL = import.meta.env.VITE_USEAST_API_URL;
wsURL = import.meta.env.VITE_USEAST_WS_URL;
} else {
apiURL = import.meta.env.VITE_EU_API_URL;
wsURL = import.meta.env.VITE_EU_WS_URL;
}
});
export const load = async () => {
await checkMarketHour(); await checkMarketHour();
const { apiURL, apiKey } = await parent();
const getOptionsZeroDTE = async () => { const getOptionsZeroDTE = async () => {
// make the POST request to the endpoint // make the POST request to the endpoint

View File

@ -1,6 +1,4 @@
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let companyName; let companyName;
function cleanString(input) { function cleanString(input) {
@ -117,20 +115,10 @@ async function fetchCommunitySentiment(pb, ticker, cookies)
export const load = async ({ params, locals, cookies, setHeaders}) => { export const load = async ({ params, locals, cookies, setHeaders}) => {
const userRegion = locals?.region?.split("::")[0];
let apiURL = locals?.apiURL; let apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL; let fastifyURL = locals?.fastifyURL;
let apiKey = locals?.apiKey; let apiKey = locals?.apiKey;
let wsURL; let wsURL = locals?.wsURL;
if (usRegion?.includes(userRegion)) {
wsURL = import.meta.env.VITE_USEAST_WS_URL;
} else {
wsURL = import.meta.env.VITE_EU_WS_URL;
};
const promises = [ const promises = [