migrate away from fastify api endpoints

This commit is contained in:
MuslemRahimi 2024-09-17 23:30:05 +02:00
parent ee822b3b6d
commit 4d81f78334
5 changed files with 56 additions and 41 deletions

View File

@ -0,0 +1,15 @@
import type { RequestHandler } from "./$types";
export const GET = (async ({ locals }) => {
const { user, pb } = locals;
let output;
try {
output = await pb.collection("watchlist").getFullList({
filter: `user="${user?.id}"`,
});
} catch (e) {
output = [];
}
return new Response(JSON.stringify(output));
}) satisfies RequestHandler;

View File

@ -23,17 +23,16 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
return output;
};
const fetchWatchlist = async (fastifyURL, userId) => {
const postData = { userId: userId };
const response = await fetch(fastifyURL + "/all-watchlists", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(postData),
});
const output = (await response.json())?.items;
const fetchWatchlist = async (pb, userId) => {
let output;
try {
output = await pb.collection("watchlist").getFullList({
filter: `user="${userId}"`,
});
} catch (e) {
//console.log(e)
output = [];
}
return output;
};
@ -57,15 +56,13 @@ async function fetchPortfolio(fastifyURL, userId)
*/
export const load = async ({ params, locals, setHeaders }) => {
let apiURL = locals?.apiURL;
let fastifyURL = locals?.fastifyURL;
let apiKey = locals?.apiKey;
const { apiURL, apiKey, pb, user } = locals;
const promises = [
fetchData(apiURL, apiKey, "/crypto-profile", params.tickerID),
fetchData(apiURL, apiKey, "/stock-quote", params.tickerID),
fetchData(apiURL, apiKey, "/one-day-price", params.tickerID),
fetchWatchlist(fastifyURL, locals?.user?.id),
fetchWatchlist(pb, user?.id),
//fetchPortfolio(fastifyURL, locals?.user?.id)
];

View File

@ -30,18 +30,21 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
return response.json();
};
const fetchFromFastify = async (fastifyURL, endpoint, userId) => {
const response = await fetch(`${fastifyURL}${endpoint}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId }),
});
const { items } = await response.json();
return items;
const fetchWatchlist = async (pb, userId) => {
let output;
try {
output = await pb.collection("watchlist").getFullList({
filter: `user="${userId}"`,
});
} catch (e) {
//console.log(e)
output = [];
}
return output;
};
export const load = async ({ params, locals, setHeaders }) => {
const { apiURL, fastifyURL, apiKey, user } = locals;
const { apiURL, apiKey, pb, user } = locals;
const { tickerID } = params;
const endpoints = [
@ -59,7 +62,7 @@ export const load = async ({ params, locals, setHeaders }) => {
...endpoints.map((endpoint) =>
fetchData(apiURL, apiKey, endpoint, tickerID)
),
fetchFromFastify(fastifyURL, "/all-watchlists", user?.id),
fetchWatchlist(pb, user?.id),
//fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id)
];

View File

@ -30,14 +30,17 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
return response.json();
};
const fetchFromFastify = async (fastifyURL, endpoint, userId) => {
const response = await fetch(`${fastifyURL}${endpoint}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId }),
});
const { items } = await response.json();
return items;
const fetchWatchlist = async (pb, userId) => {
let output;
try {
output = await pb.collection("watchlist").getFullList({
filter: `user="${userId}"`,
});
} catch (e) {
//console.log(e)
output = [];
}
return output;
};
const fetchCommunitySentiment = async (pb, ticker, cookies) => {
@ -58,7 +61,7 @@ const fetchCommunitySentiment = async (pb, ticker, cookies) => {
};
export const load = async ({ params, locals, cookies, setHeaders }) => {
const { apiURL, fastifyURL, apiKey, pb, user } = locals;
const { apiURL, apiKey, pb, user } = locals;
const { tickerID } = params;
const endpoints = [
@ -78,7 +81,7 @@ export const load = async ({ params, locals, cookies, setHeaders }) => {
...endpoints.map((endpoint) =>
fetchData(apiURL, apiKey, endpoint, tickerID)
),
fetchFromFastify(fastifyURL, "/all-watchlists", user?.id),
fetchWatchlist(pb, user?.id),
//fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id),
fetchCommunitySentiment(pb, tickerID, cookies),
];

View File

@ -311,17 +311,14 @@ async function deleteWatchList(event) {
async function getAllListData()
{
const postData = {'userId': data?.user?.id}
const response = await fetch(data?.fastifyURL+'/all-watchlists', {
method: 'POST',
const response = await fetch('/api/all-watchlists', {
method: 'GET',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(postData)
});
allList = (await response.json())?.items;
console.log(allList)
allList = await response.json();
if(allList?.length !== 0)
{