backend optimizationo
This commit is contained in:
parent
fa6cd84a78
commit
07986e2956
@ -3,17 +3,30 @@ import { validateData } from "$lib/utils";
|
|||||||
import { loginUserSchema, registerUserSchema } from "$lib/schemas";
|
import { loginUserSchema, registerUserSchema } from "$lib/schemas";
|
||||||
|
|
||||||
|
|
||||||
export const load = async ({ params, locals }) => {
|
|
||||||
|
|
||||||
|
export const load = async ({ locals, params }) => {
|
||||||
const { apiURL, apiKey, user } = locals;
|
const { apiURL, apiKey, user } = locals;
|
||||||
|
const ticker = params.tickerID;
|
||||||
|
|
||||||
|
if (!ticker) {
|
||||||
|
return { error: 'Invalid ticker ID' };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define the endpoints you want to fetch in bulk
|
||||||
|
const endpoints = [
|
||||||
|
"/dark-pool-level",
|
||||||
|
"/historical-dark-pool",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Prepare the payload for the bulk request
|
||||||
const postData = {
|
const postData = {
|
||||||
ticker: params.tickerID,
|
ticker,
|
||||||
|
endpoints
|
||||||
};
|
};
|
||||||
|
|
||||||
const getPriceLevel = async () => {
|
try {
|
||||||
|
const response = await fetch(`${apiURL}/bulk-data`, {
|
||||||
|
|
||||||
const response = await fetch(apiURL + "/dark-pool-level", {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@ -22,38 +35,27 @@ export const load = async ({ params, locals }) => {
|
|||||||
body: JSON.stringify(postData),
|
body: JSON.stringify(postData),
|
||||||
});
|
});
|
||||||
|
|
||||||
let output = await response.json();
|
if (!response.ok) {
|
||||||
output.hottestTrades = user?.tier !== "Pro" ? output?.hottestTrades?.slice(0, 3) : output.hottestTrades;
|
throw new Error("Failed to fetch bulk data");
|
||||||
|
}
|
||||||
|
|
||||||
|
const bulkData = await response.json();
|
||||||
|
|
||||||
return output;
|
// Process analyst ticker history: if user isn't Pro, limit to 6 items
|
||||||
};
|
let priceLevel = bulkData["/dark-pool-level"];
|
||||||
|
if (user?.tier !== "Pro") {
|
||||||
|
priceLevel.hottestTrades = priceLevel?.hottestTrades?.slice(0, 3);
|
||||||
|
}
|
||||||
|
|
||||||
const getHistoricalDarkPool = async () => {
|
|
||||||
|
|
||||||
|
|
||||||
// make the POST request to the endpoint
|
|
||||||
const response = await fetch(apiURL + "/historical-dark-pool", {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"X-API-KEY": apiKey,
|
|
||||||
},
|
|
||||||
body: JSON.stringify(postData),
|
|
||||||
});
|
|
||||||
|
|
||||||
const output = await response.json();
|
|
||||||
|
|
||||||
return output;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Make sure to return a promise
|
|
||||||
return {
|
return {
|
||||||
getPriceLevel: await getPriceLevel(),
|
getPriceLevel: priceLevel,
|
||||||
getHistoricalDarkPool: await getHistoricalDarkPool(),
|
getHistoricalDarkPool: bulkData["/historical-dark-pool"],
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return { error: "Failed to load data" };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user