From a825f2a7a04d2358a5aa64a5865b5a0e73ac7e20 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 19 Sep 2024 10:00:41 +0200 Subject: [PATCH] clean code --- src/hooks.server.ts | 19 +--- src/lib/tracker.ts | 107 ++++++++----------- src/routes/etf/etf-providers/+page.ts | 39 ++----- src/routes/etf/etf-providers/[slug]/+page.ts | 45 +++----- src/routes/etf/new-launches/+page.ts | 39 ++----- src/routes/leaderboard/+page.svelte | 17 +-- src/routes/leaderboard/+page.ts | 58 ++++------ src/routes/list/large-cap-stocks/+page.ts | 41 +++---- src/routes/list/mega-cap-stocks/+page.ts | 41 +++---- src/routes/list/micro-cap-stocks/+page.ts | 41 +++---- src/routes/list/mid-cap-stocks/+page.ts | 41 +++---- src/routes/list/small-cap-stocks/+page.ts | 41 +++---- src/routes/notifications/+page.svelte | 16 +-- src/routes/sitemap.xml/+server.ts | 15 +-- src/routes/stock-splits-calendar/+page.ts | 40 ++----- src/routes/trending/+page.ts | 40 ++----- 16 files changed, 191 insertions(+), 449 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 3029c314..b4a1c7e5 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -9,25 +9,10 @@ export const handle = async ({ event, resolve }) => { event?.request?.headers?.get("x-vercel-id") ?? "fra1::fra1::8t4xg-1700258428633-157d82fdfcc7"; - const ip = - event.request.headers.get("x-forwarded-for") || - event.request.headers.get("remote-address"); - - let isUS = false; - - if (ip) { - const geoResponse = await fetch(`https://ipinfo.io/${ip}/geo`); - const geoData = await geoResponse.json(); - if (geoData.country === "US") { - isUS = true; - //console.log("yelllo", geoData); - } - } - // Use a more compatible way to get the first element of the split array - const userRegion = "fra1"; //regionHeader.split("::")[0] || ""; + const userRegion = regionHeader.split("::")[0] || ""; - const isUsRegion = false; //usRegion.has(userRegion); + const isUsRegion = usRegion.has(userRegion); // Use a ternary operator instead of the logical OR for better compatibility const pbURL = isUsRegion diff --git a/src/lib/tracker.ts b/src/lib/tracker.ts index fb4dd1f7..02858734 100644 --- a/src/lib/tracker.ts +++ b/src/lib/tracker.ts @@ -1,114 +1,95 @@ import { userRegion } from "$lib/store"; - -const usRegion = ['cle1','iad1','pdx1','sfo1']; +const usRegion = ["cle1", "iad1", "pdx1", "sfo1"]; let fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; -userRegion.subscribe(value => { +userRegion.subscribe((value) => { if (usRegion.includes(value)) { - fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL; + fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL; } else { - fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; + fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; } }); - export const trackPageVisit = async (path, userAgent) => { - const postData = { - type: 'trackPageVisit', + type: "trackPageVisit", path: path, userAgent: userAgent, }; - - const response = await fetch(fastifyURL+'/mixpanel', { - method: 'POST', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(postData) + const response = await fetch(fastifyURL + "/mixpanel", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), }); - -} - +}; export const trackPageDuration = async (path, userAgent, time) => { - const postData = { - type: 'trackPageDuration', + type: "trackPageDuration", path: path, time: time, userAgent: userAgent, }; - - const response = await fetch(fastifyURL+'/mixpanel', { - method: 'POST', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(postData) + const response = await fetch(fastifyURL + "/mixpanel", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), }); - -} - +}; export const trackPageError = async (path, userAgent, status, message) => { - const postData = { - type: 'trackPageError', + type: "trackPageError", path: path, status: status, message: message, userAgent: userAgent, }; - - const response = await fetch(fastifyURL+'/mixpanel', { - method: 'POST', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(postData) + const response = await fetch(fastifyURL + "/mixpanel", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), }); - -} +}; export const trackAsset = async (symbol, assetType) => { - const postData = { - type: 'trackAsset', + type: "trackAsset", symbol: symbol, assetType: assetType, }; - - const response = await fetch(fastifyURL+'/mixpanel', { - method: 'POST', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(postData) + const response = await fetch(fastifyURL + "/mixpanel", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), }); - -} +}; export const trackButtonClick = async (name) => { - const postData = { - type: 'trackButton', + type: "trackButton", name: name, }; - - const response = await fetch(fastifyURL+'/mixpanel', { - method: 'POST', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(postData) + const response = await fetch(fastifyURL + "/mixpanel", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), }); - -} \ No newline at end of file +}; diff --git a/src/routes/etf/etf-providers/+page.ts b/src/routes/etf/etf-providers/+page.ts index 1d5ff3b3..c3a82a76 100644 --- a/src/routes/etf/etf-providers/+page.ts +++ b/src/routes/etf/etf-providers/+page.ts @@ -1,44 +1,27 @@ -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 getAllETFProviders = async () => { let output; // Get cached data for the specific tickerID - const cachedData = getCache('', 'getAllETFProviders'); + const cachedData = getCache("", "getAllETFProviders"); if (cachedData) { output = cachedData; } else { - - const response = await fetch(apiURL + '/all-etf-providers', { - method: 'GET', + const { apiKey, apiURL } = await parent(); + const response = await fetch(apiURL + "/all-etf-providers", { + method: "GET", headers: { - "Content-Type": "application/json", "X-API-KEY": apiKey + "Content-Type": "application/json", + "X-API-KEY": apiKey, }, }); output = await response.json(); // Cache the data for this specific tickerID with a specific name 'getAllETFProviders' - setCache('', output, 'getAllETFProviders'); + setCache("", output, "getAllETFProviders"); } return output; @@ -46,6 +29,6 @@ export const load = async () => { // Make sure to return a promise return { - getAllETFProviders: await getAllETFProviders() + getAllETFProviders: await getAllETFProviders(), }; -}; \ No newline at end of file +}; diff --git a/src/routes/etf/etf-providers/[slug]/+page.ts b/src/routes/etf/etf-providers/[slug]/+page.ts index e88b7317..1bd92415 100644 --- a/src/routes/etf/etf-providers/[slug]/+page.ts +++ b/src/routes/etf/etf-providers/[slug]/+page.ts @@ -1,43 +1,26 @@ -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}) => { - - const getProviderName = async () => { - return params.slug; - }; +export const load = async ({ params, parent }) => { + const getProviderName = async () => { + return params.slug; + }; const getETFProviderData = async () => { let output; // Get cached data for the specific tickerID - const cachedData = getCache(params.slug, 'getETFProviderData'); + const cachedData = getCache(params.slug, "getETFProviderData"); if (cachedData) { output = cachedData; } else { - - const postData = {'etfProvider': params.slug} + const { apiKey, apiURL } = await parent(); + const postData = { etfProvider: params.slug }; - const response = await fetch(apiURL + '/etf-provider', { - method: 'POST', + const response = await fetch(apiURL + "/etf-provider", { + method: "POST", headers: { - "Content-Type": "application/json", "X-API-KEY": apiKey + "Content-Type": "application/json", + "X-API-KEY": apiKey, }, body: JSON.stringify(postData), }); @@ -45,7 +28,7 @@ export const load = async ({params}) => { output = await response.json(); // Cache the data for this specific tickerID with a specific name 'getETFProviderData' - setCache(params.slug, output, 'getETFProviderData'); + setCache(params.slug, output, "getETFProviderData"); } return output; @@ -56,4 +39,4 @@ export const load = async ({params}) => { getETFProviderData: await getETFProviderData(), getProviderName: await getProviderName(), }; -}; \ No newline at end of file +}; diff --git a/src/routes/etf/new-launches/+page.ts b/src/routes/etf/new-launches/+page.ts index 23b212ae..01a6794e 100644 --- a/src/routes/etf/new-launches/+page.ts +++ b/src/routes/etf/new-launches/+page.ts @@ -1,46 +1,29 @@ -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 getETFNewLaunches = async () => { let output; // Get cached data for the specific tickerID - const cachedData = getCache('', 'getETFNewLaunches'); + const cachedData = getCache("", "getETFNewLaunches"); if (cachedData) { output = cachedData; } else { - + const { apiKey, apiURL } = await parent(); // make the POST request to the endpoint - const response = await fetch(apiURL + '/etf-new-launches', { - method: 'GET', + const response = await fetch(apiURL + "/etf-new-launches", { + method: "GET", headers: { - "Content-Type": "application/json", "X-API-KEY": apiKey + "Content-Type": "application/json", + "X-API-KEY": apiKey, }, }); output = await response.json(); // Cache the data for this specific tickerID with a specific name 'getETFNewLaunches' - setCache('', output, 'getETFNewLaunches'); + setCache("", output, "getETFNewLaunches"); } return output; @@ -48,6 +31,6 @@ export const load = async () => { // Make sure to return a promise return { - getETFNewLaunches: await getETFNewLaunches() + getETFNewLaunches: await getETFNewLaunches(), }; -}; \ No newline at end of file +}; diff --git a/src/routes/leaderboard/+page.svelte b/src/routes/leaderboard/+page.svelte index b68dd384..85204dfd 100644 --- a/src/routes/leaderboard/+page.svelte +++ b/src/routes/leaderboard/+page.svelte @@ -1,6 +1,6 @@