import {z} from 'zod' export type CreateApiV1EmailSendArgs = {body: BodyInit}; export type CreateApiV1EmailSendOptions = {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. * * POST /v1/email/send * * Also accepts auth: API Key */ export const createApiV1EmailSend = async (args: CreateApiV1EmailSendArgs, options: CreateApiV1EmailSendOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/email/send${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: 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()) };