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") ??
"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

View File

@ -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),
});
}
};

View File

@ -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(),
};
};
};

View File

@ -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(),
};
};
};

View File

@ -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(),
};
};
};

View File

@ -1,6 +1,6 @@
<script lang='ts'>
import { goto} from '$app/navigation';
import { userRegion, numberOfUnreadNotification } from '$lib/store';
import { numberOfUnreadNotification } from '$lib/store';
import { onMount } from 'svelte';
import {getImageURL} from '$lib/utils';
import TopInvestors from '$lib/components/TopInvestors.svelte';
@ -13,19 +13,6 @@ let leaderboard = data?.getLeaderboard ?? [];
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();
const monthNames = [
@ -166,7 +153,7 @@ async function getLeaderboard() {
'endDate': endDate,
};
const response = await fetch(fastifyURL+'/leaderboard', {
const response = await fetch(data?.fastifyURL+'/leaderboard', {
method: 'POST',
headers: {
"Content-Type": "application/json"

View File

@ -1,60 +1,44 @@
import { userRegion, getCache, setCache } from '$lib/store';
import { getCache, setCache } from "$lib/store";
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 }) => {
export const load = async ({ params, parent }) => {
const getLeaderboard = async () => {
const currentDate = new Date();
const year = currentDate.getFullYear();
const currentMonthIndex = currentDate.getMonth();
const nextMonthIndex = (currentMonthIndex + 1) % 12;
const nextYear = year + Math.floor((currentMonthIndex + 1) / 12);
const nextMonth = String(nextMonthIndex + 1).padStart(2, '0');
const startDate = `${year}-${String(currentMonthIndex + 1).padStart(2, '0')}-01`;
const nextMonth = String(nextMonthIndex + 1).padStart(2, "0");
const startDate = `${year}-${String(currentMonthIndex + 1).padStart(2, "0")}-01`;
const endDate = `${nextYear}-${nextMonth}-01`;
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getLeaderboard');
const cachedData = getCache("", "getLeaderboard");
if (cachedData) {
output = cachedData;
} else {
const { fastifyURL } = await parent();
const postData = {
'startDate': startDate,
'endDate': endDate,
};
const response = await fetch(fastifyURL+'/leaderboard', {
method: 'POST',
const postData = {
startDate: startDate,
endDate: endDate,
};
const response = await fetch(fastifyURL + "/leaderboard", {
method: "POST",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
body: JSON.stringify(postData),
});
output = (await response.json())?.items
?.filter(item => item.rank !== 0)
});
output = (await response.json())?.items
?.filter((item) => item.rank !== 0)
?.sort((a, b) => a.rank - b.rank);
// Cache the data for this specific tickerID with a specific name 'getLeaderboard'
setCache('', output, 'getLeaderboard');
setCache("", output, "getLeaderboard");
}
return output;
@ -62,6 +46,6 @@ export const load = async ({ params }) => {
// Make sure to return a promise
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";
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 getLargeCapStocks = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getLargeCapStocks');
const cachedData = getCache("", "getLargeCapStocks");
if (cachedData) {
output = cachedData;
} else {
const postData = {'filterList': 'largeCap'}
const { apiKey, apiURL } = await parent();
const postData = { filterList: "largeCap" };
const response = await fetch(apiURL + '/filter-stock-list', {
method: 'POST',
const response = await fetch(apiURL + "/filter-stock-list", {
method: "POST",
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getLargeCapStocks'
setCache('', output, 'getLargeCapStocks');
setCache("", output, "getLargeCapStocks");
}
return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise
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";
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 getMegaCapStocks = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getMegaCapStocks');
const cachedData = getCache("", "getMegaCapStocks");
if (cachedData) {
output = cachedData;
} else {
const postData = {'filterList': 'megaCap'}
const { apiKey, apiURL } = await parent();
const postData = { filterList: "megaCap" };
const response = await fetch(apiURL + '/filter-stock-list', {
method: 'POST',
const response = await fetch(apiURL + "/filter-stock-list", {
method: "POST",
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getMegaCapStocks'
setCache('', output, 'getMegaCapStocks');
setCache("", output, "getMegaCapStocks");
}
return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise
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";
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 getMicroCapStocks = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getMicroCapStocks');
const cachedData = getCache("", "getMicroCapStocks");
if (cachedData) {
output = cachedData;
} else {
const postData = {'filterList': 'microCap'}
const { apiKey, apiURL } = await parent();
const postData = { filterList: "microCap" };
const response = await fetch(apiURL + '/filter-stock-list', {
method: 'POST',
const response = await fetch(apiURL + "/filter-stock-list", {
method: "POST",
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getMicroCapStocks'
setCache('', output, 'getMicroCapStocks');
setCache("", output, "getMicroCapStocks");
}
return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise
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";
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 getMidCapStocks = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getMidCapStocks');
const cachedData = getCache("", "getMidCapStocks");
if (cachedData) {
output = cachedData;
} else {
const postData = {'filterList': 'midCap'}
const { apiKey, apiURL } = await parent();
const postData = { filterList: "midCap" };
const response = await fetch(apiURL + '/filter-stock-list', {
method: 'POST',
const response = await fetch(apiURL + "/filter-stock-list", {
method: "POST",
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getMidCapStocks'
setCache('', output, 'getMidCapStocks');
setCache("", output, "getMidCapStocks");
}
return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise
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";
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 getSmallCapStocks = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getSmallCapStocks');
const cachedData = getCache("", "getSmallCapStocks");
if (cachedData) {
output = cachedData;
} else {
const postData = {'filterList': 'smallCap'}
const { apiKey, apiURL } = await parent();
const postData = { filterList: "smallCap" };
const response = await fetch(apiURL + '/filter-stock-list', {
method: 'POST',
const response = await fetch(apiURL + "/filter-stock-list", {
method: "POST",
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
"Content-Type": "application/json",
"X-API-KEY": apiKey,
},
body: JSON.stringify(postData),
});
@ -41,7 +24,7 @@ export const load = async () => {
output = await response.json();
// Cache the data for this specific tickerID with a specific name 'getSmallCapStocks'
setCache('', output, 'getSmallCapStocks');
setCache("", output, "getSmallCapStocks");
}
return output;
@ -49,6 +32,6 @@ export const load = async () => {
// Make sure to return a promise
return {
getSmallCapStocks: await getSmallCapStocks()
getSmallCapStocks: await getSmallCapStocks(),
};
};
};

View File

@ -4,7 +4,7 @@
import InfiniteLoading from '$lib/components/InfiniteLoading.svelte';
import { onMount } from 'svelte';
import {userRegion, numberOfUnreadNotification, scrollToComment } from '$lib/store';
import {numberOfUnreadNotification, scrollToComment } from '$lib/store';
export let data;
export let form;
@ -14,19 +14,9 @@
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()
{
@ -46,7 +36,7 @@ if (notificationIdList.length !== 0)
{
const postData = {'unreadList': notificationIdList};
await fetch(fastifyURL+'/update-notifications', {
await fetch(data?.fastifyURL+'/update-notifications', {
method: 'POST',
headers: {
"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 = {
runtime: "nodejs20.x",
};
@ -107,6 +93,7 @@ const website = "https://stocknear.com";
/** @type {import('./$types').RequestHandler} */
export async function GET({ locals }) {
//get all posts;
const { apiKey, apiURL } = locals;
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";
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 getStockSplitsCalendar = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getStockSplitsCalendar');
const cachedData = getCache("", "getStockSplitsCalendar");
if (cachedData) {
output = cachedData;
} else {
const { apiKey, apiURL } = await parent();
// make the POST request to the endpoint
const response = await fetch(apiURL + '/stock-splits-calendar', {
method: 'GET',
const response = await fetch(apiURL + "/stock-splits-calendar", {
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 'getStockSplitsCalendar'
setCache('', output, 'getStockSplitsCalendar');
setCache("", output, "getStockSplitsCalendar");
}
return output;
@ -48,6 +30,6 @@ export const load = async () => {
// Make sure to return a promise
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";
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 getTrendingStocks = async () => {
let output;
// Get cached data for the specific tickerID
const cachedData = getCache('', 'getTrendingStocks');
const cachedData = getCache("", "getTrendingStocks");
if (cachedData) {
output = cachedData;
} else {
const { apiKey, apiURL } = await parent();
// make the POST request to the endpoint
const response = await fetch(apiURL + '/trending', {
method: 'GET',
const response = await fetch(apiURL + "/trending", {
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 'getTrendingStocks'
setCache('', output, 'getTrendingStocks');
setCache("", output, "getTrendingStocks");
}
return output;
@ -48,6 +30,6 @@ export const load = async () => {
// Make sure to return a promise
return {
getTrendingStocks: await getTrendingStocks()
getTrendingStocks: await getTrendingStocks(),
};
};
};