46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { z } from "zod/v4";
|
|
|
|
import {
|
|
type ApplicationConfigurationInput,
|
|
ApplicationConfigurationResponseSchema,
|
|
ApplicationConfigurationsResponseSchema,
|
|
} from "../../common/application-configurations";
|
|
export const applicationKeys = { company: (id: string) => ["application-configurations", id] };
|
|
export class ApplicationApiError extends Error {
|
|
constructor(public readonly code: string) {
|
|
super(code);
|
|
}
|
|
}
|
|
async function request(
|
|
companyId: string,
|
|
values?: ApplicationConfigurationInput,
|
|
token?: string,
|
|
id?: string
|
|
) {
|
|
const response = await fetch(
|
|
`/api/application-configurations/${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 body: unknown = await response.json();
|
|
if (!response.ok) {
|
|
const error = z.object({ code: z.string() }).safeParse(body);
|
|
throw new ApplicationApiError(error.success ? error.data.code : "INTERNAL_ERROR");
|
|
}
|
|
return body;
|
|
}
|
|
export const applicationApi = {
|
|
async get(companyId: string) {
|
|
return ApplicationConfigurationsResponseSchema.parse(await request(companyId));
|
|
},
|
|
async save(companyId: string, values: ApplicationConfigurationInput, token: string, id?: string) {
|
|
return ApplicationConfigurationResponseSchema.parse(
|
|
await request(companyId, values, token, id)
|
|
);
|
|
},
|
|
};
|