36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { z } from "zod/v4";
|
|
|
|
import {
|
|
type WorkProfileInput,
|
|
WorkProfileResponseSchema,
|
|
WorkProfilesResponseSchema,
|
|
} from "../../common/work-profiles";
|
|
export const profileKeys = { company: (id: string) => ["work-profiles", id] };
|
|
export class WorkProfileApiError extends Error {
|
|
constructor(public readonly code: string) {
|
|
super(code);
|
|
}
|
|
}
|
|
async function request(companyId: string, values?: WorkProfileInput, token?: string, id?: string) {
|
|
const response = await fetch(`/api/work-profiles/${companyId}${id ? `/${id}/update` : ""}`, {
|
|
method: values ? "POST" : "GET",
|
|
credentials: "same-origin",
|
|
headers: values ? { "content-type": "application/json", "x-csrf-token": token ?? "" } : {},
|
|
...(values ? { body: JSON.stringify({ values }) } : {}),
|
|
});
|
|
const json: unknown = await response.json();
|
|
if (!response.ok) {
|
|
const parsed = z.object({ code: z.string() }).safeParse(json);
|
|
throw new WorkProfileApiError(parsed.success ? parsed.data.code : "INTERNAL_ERROR");
|
|
}
|
|
return json;
|
|
}
|
|
export const profileApi = {
|
|
async get(companyId: string) {
|
|
return WorkProfilesResponseSchema.parse(await request(companyId));
|
|
},
|
|
async save(companyId: string, values: WorkProfileInput, token: string, id?: string) {
|
|
return WorkProfileResponseSchema.parse(await request(companyId, values, token, id));
|
|
},
|
|
};
|