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

View File

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

View File

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