diff --git a/src/lib/scripts/apiFetch.ts b/src/lib/scripts/apiFetch.ts new file mode 100644 index 0000000..22a1e4e --- /dev/null +++ b/src/lib/scripts/apiFetch.ts @@ -0,0 +1,53 @@ +import { dev } from '$app/environment'; +import { uuid } from '$lib/stores/uuid'; +import type { Unsubscriber } from 'svelte/store'; + +const testUrl: string = 'http://localhost:8460'; +const prodUrl: string = 'https://owlboard.info'; + +function getUrlString(): string { + if (dev) { + console.info('DEVMODE active, using testing URL: ', testUrl); + return testUrl; + } + return prodUrl; +} + +export async function apiGet(path: string): Promise { + let uuidString: string = ''; + let unsubscribe: Unsubscriber; + try { + unsubscribe = uuid.subscribe((value) => { + uuidString = value; + }); + } catch (err) { + throw new Error('Unable to read UUID'); + } + + const options = { + method: 'GET', + headers: { + uuid: uuidString + } + }; + + try { + const res = await fetch(getUrlString() + path, options); + + if (!res.ok) { + throw new Error('Network response not ok'); + } + const contentType = res.headers.get('content-type'); + if (!contentType || !contentType.includes('application/json')) { + throw new Error('Invalid response. Require JSON.'); + } + return await res.json(); + + } catch (err) { + console.error('Error fetching data:', err); + throw err; + + } finally { + unsubscribe(); + } +} diff --git a/src/lib/train/trainIcons.svelte b/src/lib/train/trainIcons.svelte index 69a6234..436cb2b 100644 --- a/src/lib/train/trainIcons.svelte +++ b/src/lib/train/trainIcons.svelte @@ -32,7 +32,7 @@ {#if serviceDetails.firstClass || serviceDetails.catering || serviceDetails.sleeper || serviceDetails.vstp} -
+
{/if}