metis-desk/modules/companies/src/web/mail/mailbox-api.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
import {
type MailboxInput,
type MailboxUseInput,
MailboxesResponseSchema,
} from "../../common/mailboxes";
import { MailApiError } from "./mail-api";
export const mailboxKeys = { company: (id: string) => ["mailboxes", id] };
export const mailboxApi = {
async request(
companyId: string,
values?: MailboxInput | MailboxUseInput,
token?: string,
id?: string
) {
const suffix = values && "mailboxId" in values ? "/uses" : "";
const response = await fetch(
`/api/mailboxes/${companyId}${suffix}${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 MailApiError(parsed.success ? parsed.data.code : "INTERNAL_ERROR");
}
return MailboxesResponseSchema.parse(json);
},
};