82 lines
2 KiB
TypeScript
82 lines
2 KiB
TypeScript
import { PUBLIC_BACKEND_API_HOST } from "$env/static/public";
|
|
|
|
export type Subdomain = {
|
|
id: string;
|
|
domain: string;
|
|
name: string;
|
|
};
|
|
|
|
export type BaseDomain = {
|
|
id: string;
|
|
domain: string;
|
|
};
|
|
|
|
interface Domains {
|
|
loadingBaseDomains: boolean;
|
|
loadingSubdomains: boolean;
|
|
subdomains: Subdomain[];
|
|
subdomainsFromId: Record<string, Subdomain>,
|
|
baseDomains: BaseDomain[];
|
|
loadingFailed: boolean;
|
|
}
|
|
|
|
export let domains = $state<Domains>({
|
|
loadingBaseDomains: true,
|
|
loadingSubdomains: true,
|
|
subdomains: [],
|
|
subdomainsFromId: {},
|
|
baseDomains: [],
|
|
loadingFailed: false,
|
|
});
|
|
|
|
export async function clearSubdomains() {
|
|
domains.subdomains = [];
|
|
domains.loadingFailed = false;
|
|
domains.loadingSubdomains = true;
|
|
}
|
|
|
|
|
|
export async function getSubdomains() {
|
|
try {
|
|
const res = await fetch(
|
|
`${PUBLIC_BACKEND_API_HOST}/api/v1/subdomain`
|
|
);
|
|
const data = await res.json();
|
|
if (!res.ok) {
|
|
throw new Error(data?.msg || "Failed to load subdomains");
|
|
}
|
|
domains.subdomains = data
|
|
.slice()
|
|
.sort((a: { id: string; }, b: { id: any; }) => a.id.localeCompare(b.id));
|
|
|
|
for (const sub of domains.subdomains) {
|
|
domains.subdomainsFromId[sub.id] = sub;
|
|
}
|
|
|
|
} catch (err) {
|
|
console.error(err);
|
|
domains.loadingFailed = true;
|
|
} finally {
|
|
domains.loadingSubdomains = false;
|
|
}
|
|
};
|
|
|
|
export async function getBaseDomains() {
|
|
try {
|
|
const res = await fetch(
|
|
`${PUBLIC_BACKEND_API_HOST}/api/v1/subdomain/domains`
|
|
);
|
|
const data = await res.json();
|
|
if (!res.ok) {
|
|
throw new Error(data?.msg || "Failed to load base domains");
|
|
}
|
|
domains.baseDomains = data
|
|
.slice()
|
|
.sort((a: { id: string; }, b: { id: any; }) => a.id.localeCompare(b.id));
|
|
} catch (err) {
|
|
console.error(err);
|
|
domains.loadingFailed = true;
|
|
} finally {
|
|
domains.loadingBaseDomains = false;
|
|
}
|
|
};
|