mirror of
				https://github.com/KevinMidboe/planetposen-frontend.git
				synced 2025-10-29 13:10:12 +00:00 
			
		
		
		
	Use hooks to check env and rewrite API call url if localhost
This commit is contained in:
		
							
								
								
									
										12
									
								
								src/hooks.server.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/hooks.server.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| import type { HandleFetch } from '@sveltejs/kit'; | ||||
|  | ||||
| export const handleFetch: HandleFetch = async ({ request, fetch }) => { | ||||
|   const { origin } = new URL(request.url); | ||||
|  | ||||
|   if (request.url.startsWith(`${origin}/api`)) { | ||||
|     // clone the original request, but change the URL | ||||
|     request = new Request(request.url.replace(origin, 'http://localhost:30010'), request); | ||||
|   } | ||||
|  | ||||
|   return fetch(request); | ||||
| }; | ||||
| @@ -1,16 +1,8 @@ | ||||
| import { dev } from '$app/environment'; | ||||
| import { env } from '$env/dynamic/private'; | ||||
| import type { PageServerLoad } from './$types'; | ||||
|  | ||||
| export const load: PageServerLoad = async ({ fetch }) => { | ||||
|   let url = '/api/orders'; | ||||
|   if (dev || env.API_HOST) { | ||||
|     url = (env.API_HOST || 'http://localhost:30010').concat(url); | ||||
|   } | ||||
|  | ||||
|   const res = await fetch(url); | ||||
|   const res = await fetch('/api/orders'); | ||||
|   const response = await res.json(); | ||||
|   console.log('orders length:', response?.orders); | ||||
|  | ||||
|   return { | ||||
|     orders: response?.orders || [] | ||||
|   | ||||
| @@ -1,18 +1,11 @@ | ||||
| import { dev } from '$app/environment'; | ||||
| import { env } from '$env/dynamic/private'; | ||||
| import type { IOrderResponse } from '$lib/interfaces/ApiResponse'; | ||||
| import type { IOrderDTO } from '$lib/interfaces/ApiResponse'; | ||||
| import type { PageServerLoad } from './$types'; | ||||
|  | ||||
| export const load: PageServerLoad = async ({ fetch, params }) => { | ||||
|   const { id } = params; | ||||
|  | ||||
|   let url = `/api/order/${id}`; | ||||
|   if (dev || env.API_HOST) { | ||||
|     url = (env.API_HOST || 'http://localhost:30010').concat(url); | ||||
|   } | ||||
|  | ||||
|   const res = await fetch(url); | ||||
|   const orderResponse: IOrderResponse = await res.json(); | ||||
|   const res = await fetch(`/api/order/${id}`); | ||||
|   const orderResponse: IOrderDTO = await res.json(); | ||||
|  | ||||
|   if (orderResponse?.success == false || orderResponse?.order === undefined) { | ||||
|     throw Error(':('); | ||||
|   | ||||
| @@ -1,16 +1,9 @@ | ||||
| import { dev } from '$app/environment'; | ||||
| import { env } from '$env/dynamic/private'; | ||||
| import type { IProductsResponse } from '$lib/interfaces/ApiResponse'; | ||||
| import type { IProductsDTO } from '$lib/interfaces/ApiResponse'; | ||||
| import type { PageServerLoad } from './$types'; | ||||
|  | ||||
| export const load: PageServerLoad = async ({ fetch }) => { | ||||
|   let url = '/api/products'; | ||||
|   if (dev || env.API_HOST) { | ||||
|     url = (env.API_HOST || 'http://localhost:30010').concat(url); | ||||
|   } | ||||
|  | ||||
|   const res = await fetch(url); | ||||
|   const products: IProductsResponse = await res.json(); | ||||
|   const res = await fetch('/api/products'); | ||||
|   const products: IProductsDTO = await res.json(); | ||||
|  | ||||
|   return products; | ||||
| }; | ||||
|   | ||||
| @@ -1,19 +1,12 @@ | ||||
| import { dev } from '$app/environment'; | ||||
| import { env } from '$env/dynamic/private'; | ||||
| import generateProductJsonLd from '$lib/jsonld/product'; | ||||
| import type { IProductResponse } from '$lib/interfaces/ApiResponse'; | ||||
| import type { IProductDTO } from '$lib/interfaces/ApiResponse'; | ||||
| import type { PageServerLoad } from './$types'; | ||||
|  | ||||
| export const load: PageServerLoad = async ({ fetch, params }) => { | ||||
|   const { id } = params; | ||||
|  | ||||
|   let url = `/api/product/${id}`; | ||||
|   if (dev || env.API_HOST) { | ||||
|     url = (env.API_HOST || 'http://localhost:30010').concat(url); | ||||
|   } | ||||
|  | ||||
|   const res = await fetch(url); | ||||
|   const productResponse: IProductResponse = await res.json(); | ||||
|   const res = await fetch(`/api/product/${id}`); | ||||
|   const productResponse: IProductDTO = await res.json(); | ||||
|   const jsonld = generateProductJsonLd(productResponse?.product); | ||||
|  | ||||
|   return { | ||||
|   | ||||
| @@ -52,15 +52,10 @@ function sitemapPages(): string { | ||||
| } | ||||
|  | ||||
| async function sitemapShopPages(): Promise<string> { | ||||
|   let url = `/api/products`; | ||||
|   if (dev || env.API_HOST) { | ||||
|     url = (env.API_HOST || 'http://localhost:30010').concat(url); | ||||
|   } | ||||
|   const res = await fetch('/api/products'); | ||||
|   const productResponse: IProductsDTO = await res.json(); | ||||
|  | ||||
|   const res = await fetch(url); | ||||
|   const products: IProductResponse = await res.json(); | ||||
|  | ||||
|   return products?.products | ||||
|   return productResponse?.products | ||||
|     ?.map((product) => | ||||
|       buildSitemapUrl(`/shop/${product.product_no}`, String(product.updated), 'daily') | ||||
|     ) | ||||
|   | ||||
| @@ -1,14 +1,7 @@ | ||||
| import { dev } from '$app/environment'; | ||||
| import { env } from '$env/dynamic/private'; | ||||
| import type { PageServerLoad } from './$types'; | ||||
|  | ||||
| export const load: PageServerLoad = async ({ fetch }) => { | ||||
|   let url = '/api/warehouse'; | ||||
|   if (dev || env.API_HOST) { | ||||
|     url = (env.API_HOST || 'http://localhost:30010').concat(url); | ||||
|   } | ||||
|  | ||||
|   const res = await fetch(url); | ||||
|   const res = await fetch('/api/warehouse'); | ||||
|   const warehouse = await res.json(); | ||||
|  | ||||
|   return { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user