update endpoint
This commit is contained in:
parent
6812cef274
commit
1d86d0a62c
@ -1,57 +1,82 @@
|
|||||||
import PocketBase from "pocketbase";
|
import PocketBase from "pocketbase";
|
||||||
import { serializeNonPOJOs } from '$lib/utils';
|
import { serializeNonPOJOs } from "$lib/utils";
|
||||||
|
|
||||||
const usRegion = new Set(['cle1', 'iad1', 'pdx1', 'sfo1']);
|
const usRegion = new Set(["cle1", "iad1", "pdx1", "sfo1"]);
|
||||||
|
|
||||||
export const handle = async ({ event, resolve }) => {
|
export const handle = async ({ event, resolve }) => {
|
||||||
// Use optional chaining and nullish coalescing for safer property access
|
// Use optional chaining and nullish coalescing for safer property access
|
||||||
const regionHeader = event?.request?.headers?.get('x-vercel-id') ?? 'fra1::fra1::8t4xg-1700258428633-157d82fdfcc7';
|
const regionHeader =
|
||||||
|
event?.request?.headers?.get("x-vercel-id") ??
|
||||||
|
"fra1::fra1::8t4xg-1700258428633-157d82fdfcc7";
|
||||||
|
|
||||||
// Use a more compatible way to get the first element of the split array
|
const ip =
|
||||||
const userRegion = regionHeader.split('::')[0] || '';
|
event.request.headers.get("x-forwarded-for") ||
|
||||||
|
event.request.headers.get("remote-address");
|
||||||
|
|
||||||
const isUsRegion = usRegion.has(userRegion);
|
let isUS = false;
|
||||||
|
|
||||||
// Use a ternary operator instead of the logical OR for better compatibility
|
if (ip) {
|
||||||
const pbURL = isUsRegion ? import.meta.env.VITE_USEAST_POCKETBASE_URL : import.meta.env.VITE_EU_POCKETBASE_URL;
|
const geoResponse = await fetch(`https://ipinfo.io/${ip}/geo`);
|
||||||
const apiURL = isUsRegion ? import.meta.env.VITE_USEAST_API_URL : import.meta.env.VITE_EU_API_URL;
|
const geoData = await geoResponse.json();
|
||||||
const fastifyURL = isUsRegion ? import.meta.env.VITE_USEAST_FASTIFY_URL : import.meta.env.VITE_EU_FASTIFY_URL;
|
if (geoData.country === "US") {
|
||||||
const wsURL = isUsRegion ? import.meta.env.VITE_USEAST_WS_URL : import.meta.env.VITE_EU_WS_URL;
|
isUS = true;
|
||||||
|
console.log("yelllo", geoData);
|
||||||
event.locals = {
|
|
||||||
region: decodeURIComponent(regionHeader),
|
|
||||||
pb: new PocketBase(pbURL),
|
|
||||||
apiURL,
|
|
||||||
fastifyURL,
|
|
||||||
wsURL,
|
|
||||||
apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY
|
|
||||||
};
|
|
||||||
|
|
||||||
const authCookie = event?.request?.headers?.get('cookie') || '';
|
|
||||||
event.locals.pb.authStore.loadFromCookie(authCookie);
|
|
||||||
|
|
||||||
if (event?.locals?.pb?.authStore?.isValid) {
|
|
||||||
try {
|
|
||||||
await event.locals.pb.collection('users').authRefresh();
|
|
||||||
event.locals.user = serializeNonPOJOs(event.locals.pb.authStore.model);
|
|
||||||
} catch (_) {
|
|
||||||
event.locals.pb.authStore.clear();
|
|
||||||
event.locals.user = undefined;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const response = await resolve(event);
|
// Use a more compatible way to get the first element of the split array
|
||||||
|
const userRegion = regionHeader.split("::")[0] || "";
|
||||||
|
|
||||||
// Use a more compatible way to set the cookie
|
const isUsRegion = usRegion.has(userRegion);
|
||||||
const cookieString = event.locals.pb.authStore.exportToCookie({
|
|
||||||
httpOnly: true,
|
|
||||||
path: '/',
|
|
||||||
sameSite: 'lax',
|
|
||||||
secure: true,
|
|
||||||
maxAge: 60 * 60 * 24 * 365
|
|
||||||
});
|
|
||||||
|
|
||||||
response.headers.append('set-cookie', cookieString);
|
// Use a ternary operator instead of the logical OR for better compatibility
|
||||||
|
const pbURL = isUsRegion
|
||||||
|
? import.meta.env.VITE_USEAST_POCKETBASE_URL
|
||||||
|
: import.meta.env.VITE_EU_POCKETBASE_URL;
|
||||||
|
const apiURL = isUsRegion
|
||||||
|
? import.meta.env.VITE_USEAST_API_URL
|
||||||
|
: import.meta.env.VITE_EU_API_URL;
|
||||||
|
const fastifyURL = isUsRegion
|
||||||
|
? import.meta.env.VITE_USEAST_FASTIFY_URL
|
||||||
|
: import.meta.env.VITE_EU_FASTIFY_URL;
|
||||||
|
const wsURL = isUsRegion
|
||||||
|
? import.meta.env.VITE_USEAST_WS_URL
|
||||||
|
: import.meta.env.VITE_EU_WS_URL;
|
||||||
|
|
||||||
return response;
|
event.locals = {
|
||||||
|
region: decodeURIComponent(regionHeader),
|
||||||
|
pb: new PocketBase(pbURL),
|
||||||
|
apiURL,
|
||||||
|
fastifyURL,
|
||||||
|
wsURL,
|
||||||
|
apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY,
|
||||||
|
};
|
||||||
|
|
||||||
|
const authCookie = event?.request?.headers?.get("cookie") || "";
|
||||||
|
event.locals.pb.authStore.loadFromCookie(authCookie);
|
||||||
|
|
||||||
|
if (event?.locals?.pb?.authStore?.isValid) {
|
||||||
|
try {
|
||||||
|
await event.locals.pb.collection("users").authRefresh();
|
||||||
|
event.locals.user = serializeNonPOJOs(event.locals.pb.authStore.model);
|
||||||
|
} catch (_) {
|
||||||
|
event.locals.pb.authStore.clear();
|
||||||
|
event.locals.user = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await resolve(event);
|
||||||
|
|
||||||
|
// Use a more compatible way to set the cookie
|
||||||
|
const cookieString = event.locals.pb.authStore.exportToCookie({
|
||||||
|
httpOnly: true,
|
||||||
|
path: "/",
|
||||||
|
sameSite: "lax",
|
||||||
|
secure: true,
|
||||||
|
maxAge: 60 * 60 * 24 * 365,
|
||||||
|
});
|
||||||
|
|
||||||
|
response.headers.append("set-cookie", cookieString);
|
||||||
|
|
||||||
|
return response;
|
||||||
};
|
};
|
||||||
@ -3,23 +3,27 @@ import type { RequestHandler } from "./$types";
|
|||||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||||
const data = await request.json();
|
const data = await request.json();
|
||||||
const selectedDate = data?.selectedDate;
|
const selectedDate = data?.selectedDate;
|
||||||
const { apiURL, apiKey } = locals;
|
const { apiURL, apiKey, user } = locals;
|
||||||
let output;
|
let output;
|
||||||
|
|
||||||
try {
|
if (user?.tier === "Pro") {
|
||||||
const postData = { date: selectedDate };
|
try {
|
||||||
const response = await fetch(apiURL + "/options-historical-flow", {
|
const postData = { date: selectedDate };
|
||||||
method: "POST",
|
const response = await fetch(apiURL + "/options-historical-flow", {
|
||||||
headers: {
|
method: "POST",
|
||||||
"Content-Type": "application/json",
|
headers: {
|
||||||
"X-API-KEY": apiKey,
|
"Content-Type": "application/json",
|
||||||
},
|
"X-API-KEY": apiKey,
|
||||||
body: JSON.stringify(postData),
|
},
|
||||||
});
|
body: JSON.stringify(postData),
|
||||||
|
});
|
||||||
|
|
||||||
output = await response.json();
|
output = await response.json();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
output = [];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
output = [];
|
output = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -794,7 +794,7 @@ $: {
|
|||||||
|
|
||||||
{#if !$isOpen}
|
{#if !$isOpen}
|
||||||
<div class="text-white text-sm sm:text-[1rem] italic text-center sm:text-start w-full ml-2 mb-3">
|
<div class="text-white text-sm sm:text-[1rem] italic text-center sm:text-start w-full ml-2 mb-3">
|
||||||
Live flow of {nyseDate} (NYSE Time)
|
Live flow of {data?.user?.tier === 'Pro' && selectedDate ? df.format(selectedDate?.toDate()) : nyseDate} (NYSE Time)
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
|||||||
@ -1,35 +1,57 @@
|
|||||||
import { isOpen} from '$lib/store';
|
import { isOpen } from "$lib/store";
|
||||||
|
|
||||||
|
|
||||||
const checkMarketHour = async () => {
|
const checkMarketHour = async () => {
|
||||||
const holidays = ['2024-01-01', '2024-01-15','2024-02-19','2024-03-29','2024-05-27','2024-06-19','2024-07-04','2024-09-02','2024-11-28','2024-12-25'];
|
const holidays = [
|
||||||
const currentDate = new Date().toISOString().split('T')[0];
|
"2024-01-01",
|
||||||
|
"2024-01-15",
|
||||||
|
"2024-02-19",
|
||||||
|
"2024-03-29",
|
||||||
|
"2024-05-27",
|
||||||
|
"2024-06-19",
|
||||||
|
"2024-07-04",
|
||||||
|
"2024-09-02",
|
||||||
|
"2024-11-28",
|
||||||
|
"2024-12-25",
|
||||||
|
];
|
||||||
|
const currentDate = new Date().toISOString().split("T")[0];
|
||||||
|
|
||||||
// Get the current time in the ET time zone
|
// Get the current time in the ET time zone
|
||||||
const etTimeZone = 'America/New_York';
|
const etTimeZone = "America/New_York";
|
||||||
const currentTime = new Date().toLocaleString('en-US', { timeZone: etTimeZone });
|
const currentTime = new Date().toLocaleString("en-US", {
|
||||||
|
timeZone: etTimeZone,
|
||||||
|
});
|
||||||
|
|
||||||
// Determine if the NYSE is currently open or closed
|
// Determine if the NYSE is currently open or closed
|
||||||
const currentHour = new Date(currentTime).getHours();
|
const currentHour = new Date(currentTime).getHours();
|
||||||
const isWeekendValue = new Date(currentTime).getDay() === 6 || new Date(currentTime).getDay() === 0;
|
const isWeekendValue =
|
||||||
const isBeforeMarketOpenValue = currentHour < 9 || (currentHour === 9 && new Date(currentTime).getMinutes() < 30);
|
new Date(currentTime).getDay() === 6 ||
|
||||||
|
new Date(currentTime).getDay() === 0;
|
||||||
|
const isBeforeMarketOpenValue =
|
||||||
|
currentHour < 9 ||
|
||||||
|
(currentHour === 9 && new Date(currentTime).getMinutes() < 30);
|
||||||
const isAfterMarketCloseValue = currentHour >= 16;
|
const isAfterMarketCloseValue = currentHour >= 16;
|
||||||
|
|
||||||
isOpen.set(!(isWeekendValue || isBeforeMarketOpenValue || isAfterMarketCloseValue || holidays?.includes(currentDate)));
|
isOpen.set(
|
||||||
}
|
!(
|
||||||
|
isWeekendValue ||
|
||||||
|
isBeforeMarketOpenValue ||
|
||||||
|
isAfterMarketCloseValue ||
|
||||||
|
holidays?.includes(currentDate)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const load = async ({ parent }) => {
|
||||||
|
|
||||||
export const load = async ({parent}) => {
|
|
||||||
await checkMarketHour();
|
await checkMarketHour();
|
||||||
const { apiURL, apiKey} = await parent();
|
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
|
||||||
const response = await fetch(apiURL + '/options-flow-feed', {
|
const response = await fetch(apiURL + "/options-flow-feed", {
|
||||||
method: 'GET',
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json", "X-API-KEY": apiKey
|
"Content-Type": "application/json",
|
||||||
|
"X-API-KEY": apiKey,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const output = await response.json();
|
const output = await response.json();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user