You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.3 KiB
TypeScript

// frontend/lib/automation/api.ts
import type { AutomationPage, ParsePreview, RuleCreate, RuleOut } from "@/lib/types";
const J = { "Content-Type": "application/json" };
async function ok<T>(r: Response): Promise<T> {
if (!r.ok) throw new Error(`${r.status} ${r.statusText}`);
return r.json() as Promise<T>;
}
export const automationApi = {
page: () => fetch("/api/automation", { cache: "no-store" }).then((r) => ok<AutomationPage>(r)),
parse: (text: string) =>
fetch("/api/automation/parse", { method: "POST", headers: J, body: JSON.stringify({ text }) }).then(
(r) => ok<ParsePreview>(r),
),
createRule: (b: RuleCreate) =>
fetch("/api/automation/rules", { method: "POST", headers: J, body: JSON.stringify(b) }).then((r) =>
ok<RuleOut>(r),
),
toggle: (id: string) =>
fetch(`/api/automation/rules/${id}/toggle`, { method: "POST" }).then((r) => ok<RuleOut>(r)),
remove: (id: string) =>
fetch(`/api/automation/rules/${id}`, { method: "DELETE" }).then((r) => ok<{ deleted: string }>(r)),
accept: (id: string) =>
fetch(`/api/automation/suggestions/${id}/accept`, { method: "POST" }).then((r) => ok<RuleOut>(r)),
dismiss: (id: string) =>
fetch(`/api/automation/suggestions/${id}/dismiss`, { method: "POST" }).then((r) =>
ok<{ dismissed: string }>(r),
),
};