From 4dbef3fcd7a14437d55c555cf10d50de8e50d7d1 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Fri, 9 Dec 2022 11:57:12 +0900 Subject: feat: Move everything out of $lib --- code/app/src/lib/api/_fetch.ts | 95 ------------------------------------------ 1 file changed, 95 deletions(-) delete mode 100644 code/app/src/lib/api/_fetch.ts (limited to 'code/app/src/lib/api/_fetch.ts') diff --git a/code/app/src/lib/api/_fetch.ts b/code/app/src/lib/api/_fetch.ts deleted file mode 100644 index c29d262..0000000 --- a/code/app/src/lib/api/_fetch.ts +++ /dev/null @@ -1,95 +0,0 @@ -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(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise { - 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)) throw new Error("Server returned 401"); - return response; -} - -export async function http_get_async(url: string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise { - 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)) throw new Error("Server returned 401"); - return response; -} - -export async function http_delete_async(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise { - 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)) throw new Error("Server returned 401"); - return response; -} - -async function internal_fetch_async(request: InternalFetchRequest): Promise { - if (!request.init) throw new Error("request.init is required"); - const fetch_request = new Request(request.url, request.init); - let response: any; - - try { - if (request.timeout && request.timeout > 500) { - response = await Promise.race([ - fetch(fetch_request), - new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), request.timeout)), - ]); - } else { - response = await fetch(fetch_request); - } - } catch (error: any) { - log_error(error); - if (error.message === "Timeout") { - console.error("Request timed out"); - } else if (error.message === "Network request failed") { - console.error("No internet connection"); - } else { - throw error; - } - } - - return response; -} - -async function redirect_if_401_async(response: Response): Promise { - if (response.status === 401) { - const redirectUrl = `/sign-in?${signInPageMessageQueryKey}=${SignInPageMessage.LOGGED_OUT}`; - clear_session_data(); - if (browser) { - await goto(redirectUrl); - } else { - throw redirect(307, redirectUrl); - } - } - return false; -} - -function make_request_init(method: string, body?: any, signal?: AbortSignal): RequestInit { - const init = { - method, - credentials: "include", - 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"; - } - - return init; -} - - -export type InternalFetchRequest = { - url: string, - init: RequestInit, - timeout?: number - retry_count?: number, -} \ No newline at end of file -- cgit v1.3