aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/lib/api/internal-fetch.ts
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-10-25 11:51:37 +0200
committerivarlovlie <git@ivarlovlie.no>2022-10-25 11:51:37 +0200
commit0005595703b2f3f7083ce4ba19bf5770057c75bd (patch)
tree193a897f61a9a5e566961601de4cf42ae85984a0 /code/app/src/lib/api/internal-fetch.ts
parent585c5c8537eb21dfc9f16108548e63d9ced3d971 (diff)
downloadgreatoffice-0005595703b2f3f7083ce4ba19bf5770057c75bd.tar.xz
greatoffice-0005595703b2f3f7083ce4ba19bf5770057c75bd.zip
.
Diffstat (limited to 'code/app/src/lib/api/internal-fetch.ts')
-rw-r--r--code/app/src/lib/api/internal-fetch.ts170
1 files changed, 0 insertions, 170 deletions
diff --git a/code/app/src/lib/api/internal-fetch.ts b/code/app/src/lib/api/internal-fetch.ts
deleted file mode 100644
index b21d669..0000000
--- a/code/app/src/lib/api/internal-fetch.ts
+++ /dev/null
@@ -1,170 +0,0 @@
-import { Temporal } from "temporal-polyfill";
-import { clear_session_data } from "$lib/session";
-import { resolve_references } from "$lib/helpers";
-import type { IInternalFetchResponse } from "$lib/models/IInternalFetchResponse";
-import type { IInternalFetchRequest } from "$lib/models/IInternalFetchRequest";
-import { redirect } from "@sveltejs/kit";
-
-export async function http_post(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<IInternalFetchResponse> {
- const init = {
- method: "post",
- } as RequestInit;
-
- if (abort_signal) {
- init.signal = abort_signal;
- }
-
- if (body) {
- init.headers = {
- "Content-Type": "application/json;charset=UTF-8",
- };
- init.body = JSON.stringify(body);
- }
-
- const response = await internal_fetch({ url, init, timeout });
- const result = {} as IInternalFetchResponse;
-
- if (!skip_401_check && await is_401(response)) return result;
-
- result.ok = response.ok;
- result.status = response.status;
- result.http_response = response;
-
- if (response.status !== 204) {
- try {
- const ct = response.headers.get("Content-Type")?.toString() ?? "";
- if (ct.startsWith("application/json")) {
- const data = await response.json();
- result.data = resolve_references(data);
- } else if (ct.startsWith("text/plain")) {
- const text = await response.text();
- result.data = text as string;
- }
- } catch {
- // Ignored
- }
- }
-
- return result;
-}
-
-export async function http_get(url: string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<IInternalFetchResponse> {
- const init = {
- method: "get",
- } as RequestInit;
-
- if (abort_signal) {
- init.signal = abort_signal;
- }
-
- const response = await internal_fetch({ url, init, timeout });
- const result = {} as IInternalFetchResponse;
-
- if (!skip_401_check && await is_401(response)) return result;
-
- result.ok = response.ok;
- result.status = response.status;
- result.http_response = response;
-
- if (response.status !== 204) {
- try {
- const ct = response.headers.get("Content-Type")?.toString() ?? "";
- if (ct.startsWith("application/json")) {
- const data = await response.json();
- result.data = resolve_references(data);
- } else if (ct.startsWith("text/plain")) {
- const text = await response.text();
- result.data = text as string;
- }
- } catch {
- // Ignored
- }
- }
-
- return result;
-}
-
-export async function http_delete(url: string, body?: object | string, timeout = -1, skip_401_check = false, abort_signal?: AbortSignal): Promise<IInternalFetchResponse> {
- const init = {
- method: "delete",
- } as RequestInit;
-
- if (abort_signal) {
- init.signal = abort_signal;
- }
-
- if (body) {
- init.headers = {
- "Content-Type": "application/json;charset=UTF-8",
- };
- init.body = JSON.stringify(body);
- }
-
- const response = await internal_fetch({ url, init, timeout });
- const result = {} as IInternalFetchResponse;
-
- if (!skip_401_check && await is_401(response)) return result;
-
- result.ok = response.ok;
- result.status = response.status;
- result.http_response = response;
-
- if (response.status !== 204) {
- try {
- const ct = response.headers.get("Content-Type")?.toString() ?? "";
- if (ct.startsWith("application/json")) {
- const data = await response.json();
- result.data = resolve_references(data);
- } else if (ct.startsWith("text/plain")) {
- const text = await response.text();
- result.data = text as string;
- }
- } catch (error) {
- // ignored
- }
- }
-
- return result;
-}
-
-async function internal_fetch(request: IInternalFetchRequest): Promise<Response> {
- if (!request.init) request.init = {};
- request.init.credentials = "include";
- request.init.headers = {
- "X-TimeZone": Temporal.Now.timeZone().id,
- ...request.init.headers
- };
-
- 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) {
- console.log(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; // rethrow other unexpected errors
- }
- }
-
- return response;
-}
-
-async function is_401(response: Response): Promise<boolean> {
- if (response.status === 401) {
- clear_session_data();
- throw redirect(307, "/login");
- }
- return false;
-}