diff options
Diffstat (limited to 'code/app/src/lib/api/_fetch.ts')
| -rw-r--r-- | code/app/src/lib/api/_fetch.ts | 73 |
1 files changed, 23 insertions, 50 deletions
diff --git a/code/app/src/lib/api/_fetch.ts b/code/app/src/lib/api/_fetch.ts index b28e398..62cdf84 100644 --- a/code/app/src/lib/api/_fetch.ts +++ b/code/app/src/lib/api/_fetch.ts @@ -1,32 +1,30 @@ -import { Temporal } from "temporal-polyfill"; -import { clear_session_data } from "$lib/session"; -import type { Result } from "rustic"; -import { Err, Ok } from "rustic"; -import { redirect } from "@sveltejs/kit"; -import { browser } from "$app/environment"; -import { goto } from "$app/navigation"; -import { SignInPageMessage, signInPageMessageQueryKey } from "$routes/(main)/(public)/sign-in"; -import { log_error } from "$lib/logger"; +import {Temporal} from "temporal-polyfill"; +import {clear_session_data} from "$lib/session"; +import {redirect} from "@sveltejs/kit"; +import {browser} from "$app/environment"; +import {goto} from "$app/navigation"; +import {SignInPageMessage, signInPageMessageQueryKey} from "$routes/(main)/(public)/sign-in"; +import {log_error} from "$lib/logger"; -export async function http_post_async<T>(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<InternalFetchResponse<T>> { +export async function http_post_async(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Response> { const init = make_request_init("post", body, abort_signal); - const response = await internal_fetch_async({ url, init, timeout }); - if (!skip_401_check && await redirect_if_401_async(response)) return Err("Server returned 401"); - return make_response_async(response); + const response = await internal_fetch_async({url, init, timeout}); + if (!skip_401_check && await redirect_if_401_async(response)) throw new Error("Server returned 401"); + return response; } -export async function http_get_async<T>(url: string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Result<InternalFetchResponse<T>, string>> { +export async function http_get_async(url: string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Response> { const init = make_request_init("get", undefined, abort_signal); - const response = await internal_fetch_async({ url, init, timeout }); - if (!skip_401_check && await redirect_if_401_async(response)) return Err("Server returned 401"); - return make_response_async(response); + const response = await internal_fetch_async({url, init, timeout}); + if (!skip_401_check && await redirect_if_401_async(response)) throw new Error("Server returned 401"); + return response; } -export async function http_delete_async<T>(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Result<InternalFetchResponse<T>, string>> { +export async function http_delete_async(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<Response> { const init = make_request_init("delete", body, abort_signal); - const response = await internal_fetch_async({ url, init, timeout }); - if (!skip_401_check && await redirect_if_401_async(response)) return Err("Server returned 401"); - return make_response_async(response); + const response = await internal_fetch_async({url, init, timeout}); + if (!skip_401_check && await redirect_if_401_async(response)) throw new Error("Server returned 401"); + return response; } async function internal_fetch_async(request: InternalFetchRequest): Promise<Response> { @@ -38,7 +36,7 @@ async function internal_fetch_async(request: InternalFetchRequest): Promise<Resp if (request.timeout && request.timeout > 500) { response = await Promise.race([ fetch(fetch_request), - new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), request.timeout)) + new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), request.timeout)), ]); } else { response = await fetch(fetch_request); @@ -62,7 +60,7 @@ async function redirect_if_401_async(response: Response): Promise<boolean> { const redirectUrl = `/sign-in?${signInPageMessageQueryKey}=${SignInPageMessage.LOGGED_OUT}`; clear_session_data(); if (browser) { - await goto(redirectUrl) + await goto(redirectUrl); } else { throw redirect(307, redirectUrl); } @@ -70,36 +68,18 @@ async function redirect_if_401_async(response: Response): Promise<boolean> { return false; } -async function make_response_async<T>(response: Response): Promise<Result<InternalFetchResponse<T>, string>> { - const result = { - ok: response.ok, - status: response.status, - http_response: response, - } as InternalFetchResponse<T>; - - if (response.status !== 204) { - try { - result.data = await response.json() as T; - } catch (error) { - log_error("", { error, result }) - return Err("Deserialisation threw"); - } - } - return Ok(result); -} - function make_request_init(method: string, body?: any, signal?: AbortSignal): RequestInit { const init = { method, signal, headers: { "X-TimeZone": Temporal.Now.timeZone().id, - } + }, } as RequestInit; if (body) { init.body = JSON.stringify(body); - init.headers["Content-Type"] = "application/json;charset=UTF-8" + init.headers["Content-Type"] = "application/json;charset=UTF-8"; } return init; @@ -111,11 +91,4 @@ export type InternalFetchRequest = { init: RequestInit, timeout?: number retry_count?: number, -} - -export type InternalFetchResponse<T> = { - ok: boolean, - status: number, - data: T | undefined, - http_response: Response }
\ No newline at end of file |
