From 6d4477a962bde927f01cd166287871e229c8afb1 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sat, 14 Sep 2024 19:10:31 +0000 Subject: [PATCH] web/api-url: replace getter with state --- web/src/lib/api/api-url.ts | 19 ------------------- web/src/lib/api/server-info.ts | 8 ++++---- web/src/lib/state/api-url.ts | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 23 deletions(-) delete mode 100644 web/src/lib/api/api-url.ts create mode 100644 web/src/lib/state/api-url.ts diff --git a/web/src/lib/api/api-url.ts b/web/src/lib/api/api-url.ts deleted file mode 100644 index d23af3ec..00000000 --- a/web/src/lib/api/api-url.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { get } from "svelte/store"; - -import env, { apiURL } from "$lib/env"; -import settings from "$lib/state/settings"; - -export const currentApiURL = () => { - const processingSettings = get(settings).processing; - const customInstanceURL = processingSettings.customInstanceURL; - - if (processingSettings.enableCustomInstances && customInstanceURL.length > 0) { - return new URL(customInstanceURL).origin; - } - - if (env.DEFAULT_API && processingSettings.allowDefaultOverride) { - return new URL(env.DEFAULT_API).origin; - } - - return new URL(apiURL).origin; -} diff --git a/web/src/lib/api/server-info.ts b/web/src/lib/api/server-info.ts index 51a3a90a..b8bb4a3c 100644 --- a/web/src/lib/api/server-info.ts +++ b/web/src/lib/api/server-info.ts @@ -1,5 +1,5 @@ import { get, writable } from "svelte/store"; -import { currentApiURL } from "$lib/api/api-url"; +import APIUrl from "$lib/state/api-url"; import type { CobaltServerInfoResponse, CobaltErrorResponse, CobaltServerInfo } from "$lib/types/api"; @@ -11,7 +11,7 @@ export type CobaltServerInfoCache = { export const cachedInfo = writable(); const request = async () => { - const apiEndpoint = `${currentApiURL()}/`; + const apiEndpoint = `${get(APIUrl)}/`; const response: CobaltServerInfoResponse = await fetch(apiEndpoint, { redirect: "manual", @@ -35,7 +35,7 @@ const request = async () => { export const getServerInfo = async () => { const cache = get(cachedInfo); - if (cache && cache.origin === currentApiURL()) { + if (cache && cache.origin === get(APIUrl)) { return true } @@ -48,7 +48,7 @@ export const getServerInfo = async () => { if (!("status" in freshInfo)) { cachedInfo.set({ info: freshInfo, - origin: currentApiURL(), + origin: get(APIUrl), }); return true; } diff --git a/web/src/lib/state/api-url.ts b/web/src/lib/state/api-url.ts new file mode 100644 index 00000000..1363e938 --- /dev/null +++ b/web/src/lib/state/api-url.ts @@ -0,0 +1,18 @@ +import { derived } from "svelte/store"; + +import env, { apiURL } from "$lib/env"; +import settings from "$lib/state/settings"; + +export default derived( + settings, + $settings => { + const { processing } = $settings; + + if (processing.enableCustomInstances && processing.customInstanceURL) + return new URL(processing.customInstanceURL).origin; + else if (env.DEFAULT_API && processing.allowDefaultOverride) + return new URL(env.DEFAULT_API).origin; + else + return new URL(apiURL).origin; + } +);