69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { fetchJson } from "@/lib/client/fetch-json";
|
|
|
|
export type Bucket = {
|
|
id: number;
|
|
name: string;
|
|
description: string | null;
|
|
iconKey: string | null;
|
|
budgetLimitDollars: number | null;
|
|
position: number;
|
|
necessity: "NECESSARY" | "BOTH" | "UNNECESSARY";
|
|
windowDays: number;
|
|
tags: string[];
|
|
totalUsage: number;
|
|
matchedCount: number;
|
|
};
|
|
|
|
export async function bucketsList() {
|
|
return fetchJson<{ buckets: Bucket[] }>("/api/buckets", { method: "GET" });
|
|
}
|
|
|
|
export async function bucketsCreate(input: {
|
|
name: string;
|
|
description?: string;
|
|
iconKey?: string | null;
|
|
budgetLimitDollars?: number | null;
|
|
position?: number;
|
|
tags?: string[];
|
|
necessity?: "NECESSARY" | "BOTH" | "UNNECESSARY";
|
|
windowDays?: number;
|
|
}) {
|
|
return fetchJson<{ bucket: Bucket }>("/api/buckets", {
|
|
method: "POST",
|
|
body: JSON.stringify(input)
|
|
});
|
|
}
|
|
|
|
export async function bucketsUpdate(input: {
|
|
id: number;
|
|
name: string;
|
|
description?: string;
|
|
iconKey?: string | null;
|
|
budgetLimitDollars?: number | null;
|
|
position?: number;
|
|
tags?: string[];
|
|
necessity?: "NECESSARY" | "BOTH" | "UNNECESSARY";
|
|
windowDays?: number;
|
|
}) {
|
|
return fetchJson<{ bucket: Bucket }>(`/api/buckets/${input.id}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify({
|
|
name: input.name,
|
|
description: input.description,
|
|
iconKey: input.iconKey,
|
|
budgetLimitDollars: input.budgetLimitDollars,
|
|
position: input.position,
|
|
tags: input.tags,
|
|
necessity: input.necessity,
|
|
windowDays: input.windowDays
|
|
})
|
|
});
|
|
}
|
|
|
|
export async function bucketsDelete(input: { id: number | string }) {
|
|
const numericId = Number(input.id);
|
|
if (!Number.isFinite(numericId) || numericId <= 0)
|
|
return { error: { code: "INVALID_ID", message: "Invalid id" } } as const;
|
|
return fetchJson<{ ok: true }>(`/api/buckets/${numericId}`, { method: "DELETE" });
|
|
}
|