clean code

This commit is contained in:
MuslemRahimi 2024-09-19 10:00:41 +02:00
parent 0f4dcc817d
commit a825f2a7a0
16 changed files with 191 additions and 449 deletions

View File

@ -9,25 +9,10 @@ export const handle = async ({ event, resolve }) => {
event?.request?.headers?.get("x-vercel-id") ?? event?.request?.headers?.get("x-vercel-id") ??
"fra1::fra1::8t4xg-1700258428633-157d82fdfcc7"; "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 // 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 // Use a ternary operator instead of the logical OR for better compatibility
const pbURL = isUsRegion const pbURL = isUsRegion

View File

@ -1,114 +1,95 @@
import { userRegion } from "$lib/store"; 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; let fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
userRegion.subscribe(value => { userRegion.subscribe((value) => {
if (usRegion.includes(value)) { if (usRegion.includes(value)) {
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL; fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
} else { } else {
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
} }
}); });
export const trackPageVisit = async (path, userAgent) => { export const trackPageVisit = async (path, userAgent) => {
const postData = { const postData = {
type: 'trackPageVisit', type: "trackPageVisit",
path: path, path: path,
userAgent: userAgent, userAgent: userAgent,
}; };
const response = await fetch(fastifyURL + "/mixpanel", {
const response = await fetch(fastifyURL+'/mixpanel', { method: "POST",
method: 'POST', headers: {
headers: { "Content-Type": "application/json",
"Content-Type": "application/json" },
}, body: JSON.stringify(postData),
body: JSON.stringify(postData)
}); });
};
}
export const trackPageDuration = async (path, userAgent, time) => { export const trackPageDuration = async (path, userAgent, time) => {
const postData = { const postData = {
type: 'trackPageDuration', type: "trackPageDuration",
path: path, path: path,
time: time, time: time,
userAgent: userAgent, userAgent: userAgent,
}; };
const response = await fetch(fastifyURL + "/mixpanel", {
const response = await fetch(fastifyURL+'/mixpanel', { method: "POST",
method: 'POST', headers: {
headers: { "Content-Type": "application/json",
"Content-Type": "application/json" },
}, body: JSON.stringify(postData),
body: JSON.stringify(postData)
}); });
};
}
export const trackPageError = async (path, userAgent, status, message) => { export const trackPageError = async (path, userAgent, status, message) => {
const postData = { const postData = {
type: 'trackPageError', type: "trackPageError",
path: path, path: path,
status: status, status: status,
message: message, message: message,
userAgent: userAgent, userAgent: userAgent,
}; };
const response = await fetch(fastifyURL + "/mixpanel", {
const response = await fetch(fastifyURL+'/mixpanel', { method: "POST",
method: 'POST', headers: {
headers: { "Content-Type": "application/json",
"Content-Type": "application/json" },
}, body: JSON.stringify(postData),
body: JSON.stringify(postData)
}); });
};
}
export const trackAsset = async (symbol, assetType) => { export const trackAsset = async (symbol, assetType) => {
const postData = { const postData = {
type: 'trackAsset', type: "trackAsset",
symbol: symbol, symbol: symbol,
assetType: assetType, assetType: assetType,
}; };
const response = await fetch(fastifyURL + "/mixpanel", {
const response = await fetch(fastifyURL+'/mixpanel', { method: "POST",
method: 'POST', headers: {
headers: { "Content-Type": "application/json",
"Content-Type": "application/json" },
}, body: JSON.stringify(postData),
body: JSON.stringify(postData)
}); });
};
}
export const trackButtonClick = async (name) => { export const trackButtonClick = async (name) => {
const postData = { const postData = {
type: 'trackButton', type: "trackButton",
name: name, name: name,
}; };
const response = await fetch(fastifyURL + "/mixpanel", {
const response = await fetch(fastifyURL+'/mixpanel', { method: "POST",
method: 'POST', headers: {
headers: { "Content-Type": "application/json",
"Content-Type": "application/json" },
}, body: JSON.stringify(postData),
body: JSON.stringify(postData)
}); });
};
}

View File

@ -1,44 +1,27 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getAllETFProviders = async () => { const getAllETFProviders = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getAllETFProviders'); const cachedData = getCache("", "getAllETFProviders");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
const response = await fetch(apiURL + '/all-etf-providers', { const response = await fetch(apiURL + "/all-etf-providers", {
method: 'GET', method: "GET",
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json",
"X-API-KEY": apiKey,
}, },
}); });
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getAllETFProviders' // Cache the data for this specific tickerID with a specific name 'getAllETFProviders'
setCache('', output, 'getAllETFProviders'); setCache("", output, "getAllETFProviders");
} }
return output; return output;
@ -46,6 +29,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getAllETFProviders: await getAllETFProviders() getAllETFProviders: await getAllETFProviders(),
}; };
}; };

View File

@ -1,43 +1,26 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ params, parent }) => {
const usRegion = ['cle1','iad1','pdx1','sfo1']; const getProviderName = async () => {
return params.slug;
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;
};
const getETFProviderData = async () => { const getETFProviderData = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache(params.slug, 'getETFProviderData'); const cachedData = getCache(params.slug, "getETFProviderData");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
const postData = {'etfProvider': params.slug} const postData = { etfProvider: params.slug };
const response = await fetch(apiURL + '/etf-provider', { const response = await fetch(apiURL + "/etf-provider", {
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),
}); });
@ -45,7 +28,7 @@ export const load = async ({params}) => {
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getETFProviderData' // Cache the data for this specific tickerID with a specific name 'getETFProviderData'
setCache(params.slug, output, 'getETFProviderData'); setCache(params.slug, output, "getETFProviderData");
} }
return output; return output;
@ -56,4 +39,4 @@ export const load = async ({params}) => {
getETFProviderData: await getETFProviderData(), getETFProviderData: await getETFProviderData(),
getProviderName: await getProviderName(), getProviderName: await getProviderName(),
}; };
}; };

View File

@ -1,46 +1,29 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getETFNewLaunches = async () => { const getETFNewLaunches = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getETFNewLaunches'); const cachedData = getCache("", "getETFNewLaunches");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/etf-new-launches', { const response = await fetch(apiURL + "/etf-new-launches", {
method: 'GET', method: "GET",
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json",
"X-API-KEY": apiKey,
}, },
}); });
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getETFNewLaunches' // Cache the data for this specific tickerID with a specific name 'getETFNewLaunches'
setCache('', output, 'getETFNewLaunches'); setCache("", output, "getETFNewLaunches");
} }
return output; return output;
@ -48,6 +31,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getETFNewLaunches: await getETFNewLaunches() getETFNewLaunches: await getETFNewLaunches(),
}; };
}; };

View File

@ -1,6 +1,6 @@
<script lang='ts'> <script lang='ts'>
import { goto} from '$app/navigation'; import { goto} from '$app/navigation';
import { userRegion, numberOfUnreadNotification } from '$lib/store'; import { numberOfUnreadNotification } from '$lib/store';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import {getImageURL} from '$lib/utils'; import {getImageURL} from '$lib/utils';
import TopInvestors from '$lib/components/TopInvestors.svelte'; import TopInvestors from '$lib/components/TopInvestors.svelte';
@ -13,19 +13,6 @@ let leaderboard = data?.getLeaderboard ?? [];
let isLoaded = true; let isLoaded = true;
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let fastifyURL;
userRegion.subscribe(value => {
if (usRegion.includes(value)) {
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
} else {
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
}
});
let currentDate = new Date(); let currentDate = new Date();
const monthNames = [ const monthNames = [
@ -166,7 +153,7 @@ async function getLeaderboard() {
'endDate': endDate, 'endDate': endDate,
}; };
const response = await fetch(fastifyURL+'/leaderboard', { const response = await fetch(data?.fastifyURL+'/leaderboard', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"

View File

@ -1,60 +1,44 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ params, parent }) => {
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let fastifyURL;
userRegion.subscribe(value => {
if (usRegion.includes(value)) {
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
} else {
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
}
});
export const load = async ({ params }) => {
const getLeaderboard = async () => { const getLeaderboard = async () => {
const currentDate = new Date(); const currentDate = new Date();
const year = currentDate.getFullYear(); const year = currentDate.getFullYear();
const currentMonthIndex = currentDate.getMonth(); const currentMonthIndex = currentDate.getMonth();
const nextMonthIndex = (currentMonthIndex + 1) % 12; const nextMonthIndex = (currentMonthIndex + 1) % 12;
const nextYear = year + Math.floor((currentMonthIndex + 1) / 12); const nextYear = year + Math.floor((currentMonthIndex + 1) / 12);
const nextMonth = String(nextMonthIndex + 1).padStart(2, '0'); const nextMonth = String(nextMonthIndex + 1).padStart(2, "0");
const startDate = `${year}-${String(currentMonthIndex + 1).padStart(2, '0')}-01`; const startDate = `${year}-${String(currentMonthIndex + 1).padStart(2, "0")}-01`;
const endDate = `${nextYear}-${nextMonth}-01`; const endDate = `${nextYear}-${nextMonth}-01`;
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getLeaderboard'); const cachedData = getCache("", "getLeaderboard");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { fastifyURL } = await parent();
const postData = { const postData = {
'startDate': startDate, startDate: startDate,
'endDate': endDate, endDate: endDate,
}; };
const response = await fetch(fastifyURL+'/leaderboard', { const response = await fetch(fastifyURL + "/leaderboard", {
method: 'POST', method: "POST",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json",
}, },
body: JSON.stringify(postData), body: JSON.stringify(postData),
}); });
output = (await response.json())?.items output = (await response.json())?.items
?.filter(item => item.rank !== 0) ?.filter((item) => item.rank !== 0)
?.sort((a, b) => a.rank - b.rank); ?.sort((a, b) => a.rank - b.rank);
// Cache the data for this specific tickerID with a specific name 'getLeaderboard' // Cache the data for this specific tickerID with a specific name 'getLeaderboard'
setCache('', output, 'getLeaderboard'); setCache("", output, "getLeaderboard");
} }
return output; return output;
@ -62,6 +46,6 @@ export const load = async ({ params }) => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getLeaderboard: await getLeaderboard() getLeaderboard: await getLeaderboard(),
}; };
}; };

View File

@ -1,39 +1,22 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getLargeCapStocks = async () => { const getLargeCapStocks = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getLargeCapStocks'); const cachedData = getCache("", "getLargeCapStocks");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
const postData = {'filterList': 'largeCap'} const postData = { filterList: "largeCap" };
const response = await fetch(apiURL + '/filter-stock-list', { const response = await fetch(apiURL + "/filter-stock-list", {
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),
}); });
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getLargeCapStocks' // Cache the data for this specific tickerID with a specific name 'getLargeCapStocks'
setCache('', output, 'getLargeCapStocks'); setCache("", output, "getLargeCapStocks");
} }
return output; return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getLargeCapStocks: await getLargeCapStocks() getLargeCapStocks: await getLargeCapStocks(),
}; };
}; };

View File

@ -1,39 +1,22 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getMegaCapStocks = async () => { const getMegaCapStocks = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getMegaCapStocks'); const cachedData = getCache("", "getMegaCapStocks");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
const postData = {'filterList': 'megaCap'} const postData = { filterList: "megaCap" };
const response = await fetch(apiURL + '/filter-stock-list', { const response = await fetch(apiURL + "/filter-stock-list", {
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),
}); });
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getMegaCapStocks' // Cache the data for this specific tickerID with a specific name 'getMegaCapStocks'
setCache('', output, 'getMegaCapStocks'); setCache("", output, "getMegaCapStocks");
} }
return output; return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getMegaCapStocks: await getMegaCapStocks() getMegaCapStocks: await getMegaCapStocks(),
}; };
}; };

View File

@ -1,39 +1,22 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getMicroCapStocks = async () => { const getMicroCapStocks = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getMicroCapStocks'); const cachedData = getCache("", "getMicroCapStocks");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
const postData = {'filterList': 'microCap'} const postData = { filterList: "microCap" };
const response = await fetch(apiURL + '/filter-stock-list', { const response = await fetch(apiURL + "/filter-stock-list", {
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),
}); });
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getMicroCapStocks' // Cache the data for this specific tickerID with a specific name 'getMicroCapStocks'
setCache('', output, 'getMicroCapStocks'); setCache("", output, "getMicroCapStocks");
} }
return output; return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getMicroCapStocks: await getMicroCapStocks() getMicroCapStocks: await getMicroCapStocks(),
}; };
}; };

View File

@ -1,39 +1,22 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getMidCapStocks = async () => { const getMidCapStocks = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getMidCapStocks'); const cachedData = getCache("", "getMidCapStocks");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
const postData = {'filterList': 'midCap'} const postData = { filterList: "midCap" };
const response = await fetch(apiURL + '/filter-stock-list', { const response = await fetch(apiURL + "/filter-stock-list", {
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),
}); });
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getMidCapStocks' // Cache the data for this specific tickerID with a specific name 'getMidCapStocks'
setCache('', output, 'getMidCapStocks'); setCache("", output, "getMidCapStocks");
} }
return output; return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getMidCapStocks: await getMidCapStocks() getMidCapStocks: await getMidCapStocks(),
}; };
}; };

View File

@ -1,39 +1,22 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getSmallCapStocks = async () => { const getSmallCapStocks = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getSmallCapStocks'); const cachedData = getCache("", "getSmallCapStocks");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
const postData = {'filterList': 'smallCap'} const postData = { filterList: "smallCap" };
const response = await fetch(apiURL + '/filter-stock-list', { const response = await fetch(apiURL + "/filter-stock-list", {
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),
}); });
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getSmallCapStocks' // Cache the data for this specific tickerID with a specific name 'getSmallCapStocks'
setCache('', output, 'getSmallCapStocks'); setCache("", output, "getSmallCapStocks");
} }
return output; return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getSmallCapStocks: await getSmallCapStocks() getSmallCapStocks: await getSmallCapStocks(),
}; };
}; };

View File

@ -4,7 +4,7 @@
import InfiniteLoading from '$lib/components/InfiniteLoading.svelte'; import InfiniteLoading from '$lib/components/InfiniteLoading.svelte';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import {userRegion, numberOfUnreadNotification, scrollToComment } from '$lib/store'; import {numberOfUnreadNotification, scrollToComment } from '$lib/store';
export let data; export let data;
export let form; export let form;
@ -14,19 +14,9 @@
let isLoaded = false; let isLoaded = false;
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let fastifyURL;
userRegion.subscribe(value => {
if (usRegion.includes(value)) {
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
} else {
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
}
});
async function updateNotifications() async function updateNotifications()
{ {
@ -46,7 +36,7 @@ if (notificationIdList.length !== 0)
{ {
const postData = {'unreadList': notificationIdList}; const postData = {'unreadList': notificationIdList};
await fetch(fastifyURL+'/update-notifications', { await fetch(data?.fastifyURL+'/update-notifications', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"

View File

@ -1,17 +1,3 @@
import { userRegion } 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 config = { export const config = {
runtime: "nodejs20.x", runtime: "nodejs20.x",
}; };
@ -107,6 +93,7 @@ const website = "https://stocknear.com";
/** @type {import('./$types').RequestHandler} */ /** @type {import('./$types').RequestHandler} */
export async function GET({ locals }) { export async function GET({ locals }) {
//get all posts; //get all posts;
const { apiKey, apiURL } = locals;
const outputPost = await locals.pb.collection("posts").getFullList(); const outputPost = await locals.pb.collection("posts").getFullList();

View File

@ -1,46 +1,28 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getStockSplitsCalendar = async () => { const getStockSplitsCalendar = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getStockSplitsCalendar'); const cachedData = getCache("", "getStockSplitsCalendar");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/stock-splits-calendar', { const response = await fetch(apiURL + "/stock-splits-calendar", {
method: 'GET', method: "GET",
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json",
"X-API-KEY": apiKey,
}, },
}); });
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getStockSplitsCalendar' // Cache the data for this specific tickerID with a specific name 'getStockSplitsCalendar'
setCache('', output, 'getStockSplitsCalendar'); setCache("", output, "getStockSplitsCalendar");
} }
return output; return output;
@ -48,6 +30,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getStockSplitsCalendar: await getStockSplitsCalendar() getStockSplitsCalendar: await getStockSplitsCalendar(),
}; };
}; };

View File

@ -1,46 +1,28 @@
import { userRegion, getCache, setCache } from '$lib/store'; import { getCache, setCache } from "$lib/store";
export const load = async ({ parent }) => {
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 () => {
const getTrendingStocks = async () => { const getTrendingStocks = async () => {
let output; let output;
// Get cached data for the specific tickerID // Get cached data for the specific tickerID
const cachedData = getCache('', 'getTrendingStocks'); const cachedData = getCache("", "getTrendingStocks");
if (cachedData) { if (cachedData) {
output = cachedData; output = cachedData;
} else { } else {
const { apiKey, apiURL } = await parent();
// make the POST request to the endpoint // make the POST request to the endpoint
const response = await fetch(apiURL + '/trending', { const response = await fetch(apiURL + "/trending", {
method: 'GET', method: "GET",
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json",
"X-API-KEY": apiKey,
}, },
}); });
output = await response.json(); output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getTrendingStocks' // Cache the data for this specific tickerID with a specific name 'getTrendingStocks'
setCache('', output, 'getTrendingStocks'); setCache("", output, "getTrendingStocks");
} }
return output; return output;
@ -48,6 +30,6 @@ export const load = async () => {
// Make sure to return a promise // Make sure to return a promise
return { return {
getTrendingStocks: await getTrendingStocks() getTrendingStocks: await getTrendingStocks(),
}; };
}; };