From 86920d254fc6bd38a1b860aab9a0a7e563c15930 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Mon, 5 Dec 2022 18:25:37 +0100 Subject: [PATCH] Use hooks to check env and rewrite API call url if localhost --- src/hooks.server.ts | 12 ++++++++++++ src/routes/orders/+page.server.ts | 10 +--------- src/routes/orders/[id]/+page.server.ts | 13 +++---------- src/routes/shop/+page.server.ts | 13 +++---------- src/routes/shop/[id]/+page.server.ts | 13 +++---------- src/routes/sitemap.xml/+server.ts | 11 +++-------- src/routes/warehouse/+page.server.ts | 9 +-------- 7 files changed, 26 insertions(+), 55 deletions(-) create mode 100644 src/hooks.server.ts diff --git a/src/hooks.server.ts b/src/hooks.server.ts new file mode 100644 index 0000000..366931d --- /dev/null +++ b/src/hooks.server.ts @@ -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); +}; diff --git a/src/routes/orders/+page.server.ts b/src/routes/orders/+page.server.ts index 33f3b62..7ba128c 100644 --- a/src/routes/orders/+page.server.ts +++ b/src/routes/orders/+page.server.ts @@ -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 || [] diff --git a/src/routes/orders/[id]/+page.server.ts b/src/routes/orders/[id]/+page.server.ts index ba13079..e96d356 100644 --- a/src/routes/orders/[id]/+page.server.ts +++ b/src/routes/orders/[id]/+page.server.ts @@ -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(':('); diff --git a/src/routes/shop/+page.server.ts b/src/routes/shop/+page.server.ts index e2602c7..8b06193 100644 --- a/src/routes/shop/+page.server.ts +++ b/src/routes/shop/+page.server.ts @@ -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; }; diff --git a/src/routes/shop/[id]/+page.server.ts b/src/routes/shop/[id]/+page.server.ts index faf78f6..cd56bdd 100644 --- a/src/routes/shop/[id]/+page.server.ts +++ b/src/routes/shop/[id]/+page.server.ts @@ -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 { diff --git a/src/routes/sitemap.xml/+server.ts b/src/routes/sitemap.xml/+server.ts index 71957d1..83ab058 100644 --- a/src/routes/sitemap.xml/+server.ts +++ b/src/routes/sitemap.xml/+server.ts @@ -52,15 +52,10 @@ function sitemapPages(): string { } async function sitemapShopPages(): Promise { - 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') ) diff --git a/src/routes/warehouse/+page.server.ts b/src/routes/warehouse/+page.server.ts index e548827..212859f 100644 --- a/src/routes/warehouse/+page.server.ts +++ b/src/routes/warehouse/+page.server.ts @@ -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 {