import {z} from 'zod' export type EmailPriority = 1 | 2 | 5 | 10 | 20 | 100; export type Table = 0 | 1 | 2 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 50 | 51 | 52 | 53 | 55 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 72; export type EmailQueueModel = {id?: number | undefined, departmentId?: number | undefined, mailAccountId?: number | undefined, fromAddress?: (string | null) | undefined, fromName?: (string | null) | undefined, toAddress?: (string | null) | undefined, ccAddress?: (string | null) | undefined, bccAddress?: (string | null) | undefined, subject?: (string | null) | undefined, content?: (string | null) | undefined, priority?: EmailPriority | undefined, linkedType?: Table | undefined, linkedId?: (number | null) | undefined, replyMessageId?: (string | null) | undefined, replyReferences?: (string | null) | undefined, replyLogId?: (number | null) | undefined, isForward?: number | undefined, outboxAppend?: boolean | undefined, attachments?: (Array | null) | undefined}; export type CreateApiV1EmailSend2Args = {body: EmailQueueModel}; export type CreateApiV1EmailSend2Options = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const emailLogType = z.union([z.literal(0), z.literal(1)]); export const emailLogStatus = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const emailLog = z.object({id: z.number().int().optional(), accountId: z.number().int().optional(), departmentId: z.number().int().optional(), mailAccountId: z.number().int().optional(), messageId: z.string().nullable().optional(), logType: emailLogType.optional(), isRead: z.number().int().optional(), isSpam: z.number().int().optional(), toAddress: z.string().nullable().optional(), ccAddress: z.string().nullable().optional(), bccAddress: z.string().nullable().optional(), replyToAddress: z.string().nullable().optional(), fromAddress: z.string().nullable().optional(), fromName: z.string().nullable().optional(), subject: z.string().nullable().optional(), content: z.string().nullable().optional(), status: emailLogStatus.optional(), statusMessage: z.string().nullable().optional(), userId: z.number().int().optional(), ipAddress: z.string().nullable().optional(), logTime: z.string().optional(), msgLength: z.number().int().optional(), replyMessageId: z.string().nullable().optional(), replyReferences: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Adds a new email to the send mail queue. This endpoint does not contain the attached files * * POST /v1/email/send2 * * Also accepts auth: API Key */ export const createApiV1EmailSend2 = async (args: CreateApiV1EmailSend2Args, options: CreateApiV1EmailSend2Options) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/email/send2${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(emailLog, await res.json()) };