Use hooks to check env and rewrite API call url if localhost

This commit is contained in:
2022-12-05 18:25:37 +01:00
parent 5f0b357d88
commit 86920d254f
7 changed files with 26 additions and 55 deletions

12
src/hooks.server.ts Normal file
View 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);
};

View File

@@ -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 || []

View File

@@ -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(':(');

View File

@@ -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;
};

View File

@@ -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 {

View File

@@ -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')
)

View File

@@ -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 {