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';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch }) => {
|
export const load: PageServerLoad = async ({ fetch }) => {
|
||||||
let url = '/api/orders';
|
const res = await fetch('/api/orders');
|
||||||
if (dev || env.API_HOST) {
|
|
||||||
url = (env.API_HOST || 'http://localhost:30010').concat(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(url);
|
|
||||||
const response = await res.json();
|
const response = await res.json();
|
||||||
console.log('orders length:', response?.orders);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
orders: response?.orders || []
|
orders: response?.orders || []
|
||||||
|
|||||||
@@ -1,18 +1,11 @@
|
|||||||
import { dev } from '$app/environment';
|
import type { IOrderDTO } from '$lib/interfaces/ApiResponse';
|
||||||
import { env } from '$env/dynamic/private';
|
|
||||||
import type { IOrderResponse } from '$lib/interfaces/ApiResponse';
|
|
||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch, params }) => {
|
export const load: PageServerLoad = async ({ fetch, params }) => {
|
||||||
const { id } = params;
|
const { id } = params;
|
||||||
|
|
||||||
let url = `/api/order/${id}`;
|
const res = await fetch(`/api/order/${id}`);
|
||||||
if (dev || env.API_HOST) {
|
const orderResponse: IOrderDTO = await res.json();
|
||||||
url = (env.API_HOST || 'http://localhost:30010').concat(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(url);
|
|
||||||
const orderResponse: IOrderResponse = await res.json();
|
|
||||||
|
|
||||||
if (orderResponse?.success == false || orderResponse?.order === undefined) {
|
if (orderResponse?.success == false || orderResponse?.order === undefined) {
|
||||||
throw Error(':(');
|
throw Error(':(');
|
||||||
|
|||||||
@@ -1,16 +1,9 @@
|
|||||||
import { dev } from '$app/environment';
|
import type { IProductsDTO } from '$lib/interfaces/ApiResponse';
|
||||||
import { env } from '$env/dynamic/private';
|
|
||||||
import type { IProductsResponse } from '$lib/interfaces/ApiResponse';
|
|
||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch }) => {
|
export const load: PageServerLoad = async ({ fetch }) => {
|
||||||
let url = '/api/products';
|
const res = await fetch('/api/products');
|
||||||
if (dev || env.API_HOST) {
|
const products: IProductsDTO = await res.json();
|
||||||
url = (env.API_HOST || 'http://localhost:30010').concat(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(url);
|
|
||||||
const products: IProductsResponse = await res.json();
|
|
||||||
|
|
||||||
return products;
|
return products;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +1,12 @@
|
|||||||
import { dev } from '$app/environment';
|
|
||||||
import { env } from '$env/dynamic/private';
|
|
||||||
import generateProductJsonLd from '$lib/jsonld/product';
|
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';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch, params }) => {
|
export const load: PageServerLoad = async ({ fetch, params }) => {
|
||||||
const { id } = params;
|
const { id } = params;
|
||||||
|
|
||||||
let url = `/api/product/${id}`;
|
const res = await fetch(`/api/product/${id}`);
|
||||||
if (dev || env.API_HOST) {
|
const productResponse: IProductDTO = await res.json();
|
||||||
url = (env.API_HOST || 'http://localhost:30010').concat(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(url);
|
|
||||||
const productResponse: IProductResponse = await res.json();
|
|
||||||
const jsonld = generateProductJsonLd(productResponse?.product);
|
const jsonld = generateProductJsonLd(productResponse?.product);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -52,15 +52,10 @@ function sitemapPages(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function sitemapShopPages(): Promise<string> {
|
async function sitemapShopPages(): Promise<string> {
|
||||||
let url = `/api/products`;
|
const res = await fetch('/api/products');
|
||||||
if (dev || env.API_HOST) {
|
const productResponse: IProductsDTO = await res.json();
|
||||||
url = (env.API_HOST || 'http://localhost:30010').concat(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(url);
|
return productResponse?.products
|
||||||
const products: IProductResponse = await res.json();
|
|
||||||
|
|
||||||
return products?.products
|
|
||||||
?.map((product) =>
|
?.map((product) =>
|
||||||
buildSitemapUrl(`/shop/${product.product_no}`, String(product.updated), 'daily')
|
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';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch }) => {
|
export const load: PageServerLoad = async ({ fetch }) => {
|
||||||
let url = '/api/warehouse';
|
const res = await fetch('/api/warehouse');
|
||||||
if (dev || env.API_HOST) {
|
|
||||||
url = (env.API_HOST || 'http://localhost:30010').concat(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(url);
|
|
||||||
const warehouse = await res.json();
|
const warehouse = await res.json();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user