Skip to content
Permalink
36bb82cb49
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
57 lines (45 sloc) 1.35 KB
import { get, writable } from "svelte/store";
import { currentApiURL } from "$lib/api/api-url";
import type { CobaltServerInfoResponse, CobaltErrorResponse, CobaltServerInfo } from "$lib/types/api";
export type CobaltServerInfoCache = {
info: CobaltServerInfo,
origin: string,
}
export const cachedInfo = writable<CobaltServerInfoCache | undefined>();
const request = async () => {
const apiEndpoint = `${currentApiURL()}/`;
const response: CobaltServerInfoResponse = await fetch(apiEndpoint, {
redirect: "manual",
signal: AbortSignal.timeout(10000),
})
.then(r => r.json())
.catch((e) => {
if (e?.message?.includes("timed out")) {
return {
status: "error",
error: {
code: "error.api.timed_out"
}
} as CobaltErrorResponse
}
});
return response;
}
export const getServerInfo = async () => {
const cache = get(cachedInfo);
if (cache && cache.origin === currentApiURL()) {
return true
}
const freshInfo = await request();
if (!freshInfo || !("cobalt" in freshInfo)) {
return false;
}
if (!("status" in freshInfo)) {
cachedInfo.set({
info: freshInfo,
origin: currentApiURL(),
});
return true;
}
return false;
}