From 655618877809dd3cd7b08c342de26b32567ce610 Mon Sep 17 00:00:00 2001 From: Alessio Davoli Date: Fri, 22 Dec 2023 15:19:42 +0100 Subject: [PATCH] Fresh no fetch --- frontend/src/hooks.server.ts | 1 + frontend/src/lib/repo.ts | 16 +++++++++------- frontend/src/routes/+page.ts | 4 ++-- frontend/src/routes/paths/[slug]/+page.ts | 6 +++--- frontend/src/routes/routes/[slug]/+page.ts | 4 ++-- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/frontend/src/hooks.server.ts b/frontend/src/hooks.server.ts index 6588983..791d9c4 100644 --- a/frontend/src/hooks.server.ts +++ b/frontend/src/hooks.server.ts @@ -8,6 +8,7 @@ export async function handleFetch({ request, fetch }) { } export async function handleError({ error, event, status, message }) { + console.error(error) return { status, message diff --git a/frontend/src/lib/repo.ts b/frontend/src/lib/repo.ts index b382219..217f9b8 100644 --- a/frontend/src/lib/repo.ts +++ b/frontend/src/lib/repo.ts @@ -1,6 +1,8 @@ -const API_URL = "https://ale-dev.teck-developer.com/api"; +import {PUBLIC_BACKEND_URL} from '$env/static/public' -const getAllRoutes = async (fetch) => { +const API_URL = `${PUBLIC_BACKEND_URL}/api` + +const getAllRoutes = async () => { let data = []; const response = await fetch(`${API_URL}/all-routes`); @@ -10,7 +12,7 @@ const getAllRoutes = async (fetch) => { return data; } -const getRouteCategories = async (fetch) => { +const getRouteCategories = async () => { let data = []; const response = await fetch(`${API_URL}/route-categories`); @@ -20,7 +22,7 @@ const getRouteCategories = async (fetch) => { return data; } -const getRouteByCategory = async (fetch,categoryId: number) => { +const getRouteByCategory = async (categoryId: number) => { let data = []; const response = await fetch(`${API_URL}/route-by-category/${categoryId}`); const json = await response.json(); @@ -29,7 +31,7 @@ const getRouteByCategory = async (fetch,categoryId: number) => { return data; } -const getRoute = async (fetch,routeId: number) => { +const getRoute = async (routeId: number) => { let data = {}; const response = await fetch(`${API_URL}/route/${routeId}`); const json = await response.json(); @@ -39,7 +41,7 @@ const getRoute = async (fetch,routeId: number) => { } -const getSport = async (fetch,routeId: number, sportId: number) => { +const getSport = async (routeId: number, sportId: number) => { let data = {}; const response = await fetch(`${API_URL}/route/${routeId}/${sportId}`); const json = await response.json(); @@ -48,7 +50,7 @@ const getSport = async (fetch,routeId: number, sportId: number) => { return data; } -const getPlacemarks = async (fetch,routeId: number, sportId: number) => { +const getPlacemarks = async (routeId: number, sportId: number) => { let data = {}; const response = await fetch(`${API_URL}/getPlacemarks/${routeId}/${sportId}`); const json = await response.json(); diff --git a/frontend/src/routes/+page.ts b/frontend/src/routes/+page.ts index eb0d1ef..a2271f8 100644 --- a/frontend/src/routes/+page.ts +++ b/frontend/src/routes/+page.ts @@ -2,10 +2,10 @@ import { getRouteCategories } from '$lib/repo.js'; import { error } from '@sveltejs/kit'; -export async function load({fetch}) { +export async function load() { let categories = []; try { - categories = await getRouteCategories(fetch); + categories = await getRouteCategories(); } catch (ex) { error(500); } diff --git a/frontend/src/routes/paths/[slug]/+page.ts b/frontend/src/routes/paths/[slug]/+page.ts index 7a2f01e..acb0966 100644 --- a/frontend/src/routes/paths/[slug]/+page.ts +++ b/frontend/src/routes/paths/[slug]/+page.ts @@ -1,8 +1,8 @@ import { getRouteByCategory, getRouteCategories } from '$lib/repo.js'; import { error } from '@sveltejs/kit'; -export async function load({ fetch, params }) { - const categories: App.Category[] = await getRouteCategories(fetch); +export async function load({ params }) { + const categories: App.Category[] = await getRouteCategories(); const categoryId = Number(params.slug); const category: App.Category = categories.find(c => c.id === categoryId) as App.Category; @@ -13,7 +13,7 @@ export async function load({ fetch, params }) { const toReturn = { category: category.name_it, - routes: await getRouteByCategory(fetch, categoryId), + routes: await getRouteByCategory(categoryId), } return toReturn; diff --git a/frontend/src/routes/routes/[slug]/+page.ts b/frontend/src/routes/routes/[slug]/+page.ts index db0c3da..bc240e2 100644 --- a/frontend/src/routes/routes/[slug]/+page.ts +++ b/frontend/src/routes/routes/[slug]/+page.ts @@ -1,9 +1,9 @@ import { getRoute } from '$lib/repo.js'; import { error } from '@sveltejs/kit'; -export async function load({ fetch, params }) { +export async function load({ params }) { const routeId = Number(params.slug); - const route = await getRoute(fetch, routeId); + const route = await getRoute(routeId); if (!route) { error(404);