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 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 wsURL = import.meta.env[isUsRegion ? 'VITE_USEAST_WS_URL' : 'VITE_EU_WS_URL'];
event.locals = {
region: decodeURIComponent(regionHeader),
pb: new PocketBase(pbUrl),
apiURL,
fastifyURL,
wsURL,
apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY
};

View File

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

View File

@ -1,5 +1,4 @@
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let companyName;
@ -63,20 +62,9 @@ async function fetchPortfolio(fastifyURL, userId)
export const load = async ({ params, locals, setHeaders}) => {
const userRegion = locals?.region?.split("::")[0];
let apiURL = locals?.apiURL;
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;
};
let apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL;
let apiKey = locals?.apiKey;
const promises = [
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'];
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 () => {
export const load = async ({parent}) => {
const getDividendCalendar = async () => {
let output;
@ -28,12 +11,12 @@ export const load = async () => {
output = cachedData;
} else {
const data = await parent();
// make the POST request to the endpoint
const response = await fetch(apiURL + '/dividends-calendar', {
const response = await fetch(data?.apiURL + '/dividends-calendar', {
method: 'GET',
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'];
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 () => {
export const load = async ({parent}) => {
const getEarningsCalendar = async () => {
let output;
@ -28,12 +11,12 @@ export const load = async () => {
output = cachedData;
} else {
const data = await parent();
// make the POST request to the endpoint
const response = await fetch(apiURL + '/earnings-calendar', {
const response = await fetch(data?.apiURL + '/earnings-calendar', {
method: 'GET',
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'];
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 () => {
export const load = async ({parent}) => {
const getEconomicCalendar = async () => {
let output;
@ -28,7 +11,7 @@ export const load = async () => {
output = cachedData;
} else {
const { apiURL, apiKey } = await parent();
// make the POST request to the endpoint
const response = await fetch(apiURL + '/economic-calendar', {
method: 'GET',

View File

@ -1,6 +1,3 @@
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let companyName;
@ -88,28 +85,10 @@ async function fetchPortfolio(fastifyURL, userId)
export const load = async ({ params, locals, setHeaders}) => {
const userRegion = locals?.region?.split("::")[0];
let apiURL = locals?.apiURL;
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;
};
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;
};
let apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL;
let apiKey = locals?.apiKey;
let wsURL = locals?.wsURL;
const promises = [

View File

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

View File

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

View File

@ -1,65 +1,31 @@
import { userRegion, 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 }) => {
import { displayCompanyName, getCache, setCache } from '$lib/store';
export const load = async ({ parent, params }) => {
const getHedgeFundsData = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache(params.slug, 'getHedgeFundsData');
if (cachedData) {
output = cachedData;
}
else {
if (cachedData) return cachedData;
const postData = {
cik: params.slug
};
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 })
});
// 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)
});
const output = await response.json();
output = await response.json();
if (output?.holdings) {
output.holdings = output?.holdings?.filter(item => item?.sharesNumber && item?.symbol);
}
// 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)
setCache(params.slug, output, 'getHedgeFundsData');
displayCompanyName.update(() => output?.name ?? params.slug);
return output;
};
// Make sure to return a promise
return {
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'];
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 getIPOCalendar = async () => {
let output;
@ -28,13 +11,14 @@ export const load = async ({params}) => {
output = cachedData;
} else {
const data = await parent();
// make the POST request to the endpoint
const postData = {'year': 'all'};
const response = await fetch(apiURL + '/ipo-calendar', {
const response = await fetch(data?.apiURL + '/ipo-calendar', {
method: 'POST',
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
"Content-Type": "application/json", "X-API-KEY": data?.apiKey
},
body: JSON.stringify(postData),
});

View File

@ -1,24 +1,6 @@
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 ({params}) => {
export const load = async ({parent, params}) => {
const getIPOCalendar = async () => {
let output;
@ -28,13 +10,14 @@ export const load = async ({params}) => {
output = cachedData;
} else {
const data = await parent();
// make the POST request to the endpoint
const postData = {'year': params.slug};
const response = await fetch(apiURL + '/ipo-calendar', {
const response = await fetch(data?.apiURL + '/ipo-calendar', {
method: 'POST',
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
"Content-Type": "application/json", "X-API-KEY": data?.apiKey
},
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'];
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 () => {
export const load = async ({parent}) => {
const getAmexStocks = async () => {
let output;
@ -28,6 +11,7 @@ export const load = async () => {
output = cachedData;
} else {
const { apiURL, apiKey} = await parent();
const postData = {'filterList': 'amex'}
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 () => {
@ -19,30 +19,10 @@ const checkMarketHour = async () => {
}
const usRegion = ['cle1','iad1','pdx1','sfo1'];
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 () => {
export const load = async ({parent}) => {
await checkMarketHour();
const { apiURL, apiKey} = await parent();
const getOptionsFlowFeed = async () => {
// make the POST request to the endpoint
@ -60,6 +40,5 @@ export const load = async () => {
// Make sure to return a promise
return {
getOptionsFlowFeed: await getOptionsFlowFeed(),
wsURL,
};
};

View File

@ -1,4 +1,4 @@
import { userRegion, isOpen} from '$lib/store';
import {isOpen} from '$lib/store';
const checkMarketHour = async () => {
@ -19,31 +19,10 @@ const checkMarketHour = async () => {
}
const usRegion = ['cle1','iad1','pdx1','sfo1'];
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 () => {
export const load = async ({parent}) => {
await checkMarketHour();
const { apiURL, apiKey } = await parent();
const getOptionsZeroDTE = async () => {
// make the POST request to the endpoint
const response = await fetch(apiURL + '/options-zero-dte', {

View File

@ -1,6 +1,4 @@
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let companyName;
function cleanString(input) {
@ -117,22 +115,12 @@ async function fetchCommunitySentiment(pb, ticker, cookies)
export const load = async ({ params, locals, cookies, setHeaders}) => {
const userRegion = locals?.region?.split("::")[0];
let apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL;
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 = [
fetchData(apiURL,apiKey, '/similar-stocks',params.tickerID),
fetchData(apiURL,apiKey, '/stockdeck',params.tickerID),