From 4bbf220296765a20c3ba03c5351b21618a835424 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 25 Jul 2021 22:34:22 +0200 Subject: Reformated the client code a bit --- packages/client/src/util/fetch.ts | 16 ++++++++-------- packages/client/src/util/hljs-languages.ts | 2 +- packages/client/src/util/util.ts | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) (limited to 'packages/client/src/util') diff --git a/packages/client/src/util/fetch.ts b/packages/client/src/util/fetch.ts index 54f5afd..a67014a 100644 --- a/packages/client/src/util/fetch.ts +++ b/packages/client/src/util/fetch.ts @@ -1,6 +1,6 @@ import { Ref } from "vue"; -export default async function(endpoint: string, fetch_failed: Ref, is_loading: Ref, data_name: string) { +export default async function(endpoint: string, fetch_failed: Ref, is_loading: Ref, data_name: string): Promise { const fetch_timeout = setTimeout(() => { if(!fetch_failed.value) { fetch_failed.value = `Failed to fetch ${data_name} data.`; @@ -8,7 +8,7 @@ export default async function(endpoint: string, fetch_failed: Ref } }, 5000); - const data_req = await fetch(`${window.location.protocol}//${window.location.host}/api/v1/${endpoint}`).catch(() => { + const res = await fetch(`${window.location.protocol}//${window.location.host}/api/v1/${endpoint}`).catch(() => { if(!fetch_failed.value) { fetch_failed.value = `Failed to fetch ${data_name} data.`; is_loading.value = false; @@ -17,21 +17,21 @@ export default async function(endpoint: string, fetch_failed: Ref return null; }); - if(data_req !== null) { - const data = await data_req.json().catch(() => { + if(res !== null && res.ok) { + const data: Record | null = await res.json().catch(() => { fetch_failed.value = "Failed to parse server response."; }); - if(data_req.ok) { + if(data) { clearTimeout(fetch_timeout); is_loading.value = false; return data.data; - } else { - fetch_failed.value = `Failed to fetch ${data_name} data.`; } } + fetch_failed.value = `Failed to fetch ${data_name} data.`; + clearTimeout(fetch_timeout); is_loading.value = false; return null; -}; +} \ No newline at end of file diff --git a/packages/client/src/util/hljs-languages.ts b/packages/client/src/util/hljs-languages.ts index 2f50461..1ba036a 100644 --- a/packages/client/src/util/hljs-languages.ts +++ b/packages/client/src/util/hljs-languages.ts @@ -42,4 +42,4 @@ const languages = [ { name: "yml", extensions: [ ".yml" ] } ]; -export default languages; +export default languages; \ No newline at end of file diff --git a/packages/client/src/util/util.ts b/packages/client/src/util/util.ts index 5f72c27..58ff064 100644 --- a/packages/client/src/util/util.ts +++ b/packages/client/src/util/util.ts @@ -4,4 +4,4 @@ type Params = { export function getParam(params: Params, param: string): string { return params[param].toString(); -} +} \ No newline at end of file -- cgit v1.2.3-18-g5258