🚿 Adds api function to ensure a host

This commit is contained in:
Rune Harlyk
2024-08-24 21:05:02 +02:00
committed by Rune Harlyk
parent 16e653afa8
commit 73c2038497
+8
View File
@@ -1,6 +1,7 @@
import { user } from '$lib/stores/user';
import { get } from 'svelte/store';
import { Err, Ok, type Result } from './utilities';
import { location } from './stores';
export namespace api {
export function get<TResponse>(endpoint: string, params?: RequestInit) {
@@ -26,6 +27,7 @@ async function sendRequest<TResponse>(
data?: unknown,
params?: RequestInit
): Promise<Result<TResponse, Error>> {
endpoint = resolveUrl(endpoint);
const user_token = get(user).bearer_token;
const body = data !== null && typeof data !== 'undefined' ? JSON.stringify(data) : undefined;
@@ -66,6 +68,12 @@ async function sendRequest<TResponse>(
}
}
function resolveUrl(url: string): string {
if (url.startsWith('http') || !get(location)) return url;
const protocol = window.location.protocol;
return `${protocol}//${get(location)}${url.startsWith('/') ? '' : '/'}${url}`;
}
export class ApiError extends Error {
constructor(public readonly response: Response) {
super(`${response.status}`);