2026-10-07 16:08:48 +00:00
|
|
|
import { z } from "zod/v4";
|
|
|
|
|
|
2026-10-08 14:43:34 +00:00
|
|
|
import type { BrandInput } from "../common/brands";
|
|
|
|
|
import { BrandResponseSchema, BrandsResponseSchema } from "../common/brands";
|
2026-10-07 16:08:48 +00:00
|
|
|
export const brandKeys = { all: ["asset-brands"] as const };
|
|
|
|
|
export class BrandApiError extends Error {
|
|
|
|
|
constructor(public readonly code: string) {
|
|
|
|
|
super(code);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async function request(path = "", values?: BrandInput, token?: string) {
|
|
|
|
|
const response = await fetch(`/api/asset-brands${path}`, {
|
|
|
|
|
method: values === undefined ? "GET" : "POST",
|
|
|
|
|
credentials: "same-origin",
|
|
|
|
|
headers:
|
|
|
|
|
values === undefined
|
|
|
|
|
? {}
|
|
|
|
|
: { "content-type": "application/json", "x-csrf-token": token ?? "" },
|
|
|
|
|
...(values === undefined ? {} : { body: JSON.stringify({ values }) }),
|
|
|
|
|
});
|
|
|
|
|
const data: unknown = await response.json();
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const error = z.object({ code: z.string() }).safeParse(data);
|
|
|
|
|
throw new BrandApiError(error.success ? error.data.code : "INTERNAL_ERROR");
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
export const brandApi = {
|
|
|
|
|
async list() {
|
|
|
|
|
return BrandsResponseSchema.parse(await request());
|
|
|
|
|
},
|
|
|
|
|
async save(values: BrandInput, token: string, id?: string) {
|
|
|
|
|
return BrandResponseSchema.parse(await request(id ? `/${id}/update` : "", values, token));
|
|
|
|
|
},
|
|
|
|
|
};
|