init commit
This commit is contained in:
commit
dfe324cf8f
43 changed files with 4237 additions and 0 deletions
82
src/lib/domains.svelte.ts
Normal file
82
src/lib/domains.svelte.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
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;
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue