38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import type { Color } from "$lib/models/color";
|
|
import type CommandInput from "$lib/models/command";
|
|
import type UIState from "$lib/models/uistate";
|
|
|
|
export default async function fetchLucifer<T>(path: string, init?: RequestInit): Promise<T> {
|
|
const url = (import.meta.env.VITE_LUCIFER4_BACKEND_URL||"") + "/" + path;
|
|
|
|
const res = await fetch(url, init);
|
|
if (res.status !== 200) {
|
|
if (res.headers.get("Content-Type")?.includes("application/json")) {
|
|
throw await res.json();
|
|
} else {
|
|
throw await res.text();
|
|
}
|
|
}
|
|
|
|
const json = await res.json();
|
|
|
|
return json as T;
|
|
}
|
|
|
|
export async function fetchUIState(): Promise<UIState> {
|
|
return fetchLucifer("state");
|
|
}
|
|
|
|
export async function fetchColor(color: string): Promise<Color> {
|
|
return fetchLucifer("color/"+color);
|
|
}
|
|
|
|
export async function runCommand(command: CommandInput): Promise<CommandInput> {
|
|
return fetchLucifer("command", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(command),
|
|
})
|
|
}
|
|
|