web/api-url: replace getter with state

This commit is contained in:
dumbmoron 2024-09-14 19:10:31 +00:00
parent 64ac458941
commit 6d4477a962
No known key found for this signature in database
3 changed files with 22 additions and 23 deletions

View file

@ -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;
}

View file

@ -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<CobaltServerInfoCache | undefined>();
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;
}

View file

@ -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;
}
);