web/api-url: replace getter with state
This commit is contained in:
parent
64ac458941
commit
6d4477a962
3 changed files with 22 additions and 23 deletions
|
@ -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;
|
|
||||||
}
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { get, writable } from "svelte/store";
|
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";
|
import type { CobaltServerInfoResponse, CobaltErrorResponse, CobaltServerInfo } from "$lib/types/api";
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ export type CobaltServerInfoCache = {
|
||||||
export const cachedInfo = writable<CobaltServerInfoCache | undefined>();
|
export const cachedInfo = writable<CobaltServerInfoCache | undefined>();
|
||||||
|
|
||||||
const request = async () => {
|
const request = async () => {
|
||||||
const apiEndpoint = `${currentApiURL()}/`;
|
const apiEndpoint = `${get(APIUrl)}/`;
|
||||||
|
|
||||||
const response: CobaltServerInfoResponse = await fetch(apiEndpoint, {
|
const response: CobaltServerInfoResponse = await fetch(apiEndpoint, {
|
||||||
redirect: "manual",
|
redirect: "manual",
|
||||||
|
@ -35,7 +35,7 @@ const request = async () => {
|
||||||
export const getServerInfo = async () => {
|
export const getServerInfo = async () => {
|
||||||
const cache = get(cachedInfo);
|
const cache = get(cachedInfo);
|
||||||
|
|
||||||
if (cache && cache.origin === currentApiURL()) {
|
if (cache && cache.origin === get(APIUrl)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ export const getServerInfo = async () => {
|
||||||
if (!("status" in freshInfo)) {
|
if (!("status" in freshInfo)) {
|
||||||
cachedInfo.set({
|
cachedInfo.set({
|
||||||
info: freshInfo,
|
info: freshInfo,
|
||||||
origin: currentApiURL(),
|
origin: get(APIUrl),
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
18
web/src/lib/state/api-url.ts
Normal file
18
web/src/lib/state/api-url.ts
Normal 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;
|
||||||
|
}
|
||||||
|
);
|
Loading…
Reference in a new issue