import {z} from 'zod' export type MessageHeader = {Name?: string | undefined, Value?: string | undefined}; export type HeaderCollection = Array; export type Attachment = {Name?: string | undefined, Content?: string | undefined, ContentType?: string | undefined, ContentID?: string | undefined}; export type AttachmentCollection = Array; export type EmailWithTemplateRequest = {TemplateId: number, TemplateAlias: string, TemplateModel: Record, InlineCss?: boolean | undefined, From: string, To: string, Cc?: string | undefined, Bcc?: string | undefined, Tag?: string | undefined, ReplyTo?: string | undefined, Headers?: HeaderCollection | undefined, TrackOpens?: boolean | undefined, TrackLinks?: ('None' | 'HtmlAndText' | 'HtmlOnly' | 'TextOnly') | undefined, Attachments?: AttachmentCollection | undefined}; export type SendEmailTemplatedBatchRequest = {Messages?: Array | undefined}; export type CreateApiEmailBatchWithTemplatesArgs = {'X-Postmark-Server-Token': string, body: SendEmailTemplatedBatchRequest}; export type CreateApiEmailBatchWithTemplatesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const sendEmailResponse = z.object({To: z.string().optional(), SubmittedAt: z.string().optional(), MessageID: z.string().optional(), ErrorCode: z.number().int().optional(), Message: z.string().optional()}); export const sendEmailBatchResponse = z.array(sendEmailResponse); 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 }; /** * Send a batch of email using templates. * * POST /email/batchWithTemplates */ export const createApiEmailBatchWithTemplates = async (args: CreateApiEmailBatchWithTemplatesArgs, options: CreateApiEmailBatchWithTemplatesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Postmark-Server-Token'] != null) headers.set("X-Postmark-Server-Token", String(args['X-Postmark-Server-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/email/batchWithTemplates${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(sendEmailBatchResponse, await res.json()) };