import {z} from 'zod' export type GetApiBouncesArgs = {'X-Postmark-Server-Token': string, count: number, offset: number, type?: ('HardBounce' | 'Transient' | 'Unsubscribe' | 'Subscribe' | 'AutoResponder' | 'AddressChange' | 'DnsError' | 'SpamNotification' | 'OpenRelayTest' | 'Unknown' | 'SoftBounce' | 'VirusNotification' | 'MailFrontier Matador.' | 'BadEmailAddress' | 'SpamComplaint' | 'ManuallyDeactivated' | 'Unconfirmed' | 'Blocked' | 'SMTPApiError' | 'InboundError' | 'DMARCPolicy' | 'TemplateRenderingFailed') | undefined, inactive?: boolean | undefined, emailFilter?: string | undefined, messageID?: string | undefined, tag?: string | undefined, todate?: string | undefined, fromdate?: string | undefined}; export type GetApiBouncesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const bounceInfoResponse = z.object({ID: z.string().optional(), Type: z.string().optional(), TypeCode: z.number().int().optional(), Name: z.string().optional(), Tag: z.string().optional(), MessageID: z.string().optional(), Description: z.string().optional(), Details: z.string().optional(), Email: z.string().optional(), BouncedAt: z.string().optional(), DumpAvailable: z.boolean().optional(), Inactive: z.boolean().optional(), CanActivate: z.boolean().optional(), Subject: z.string().optional(), Content: z.string().optional()}); export const bounceSearchResponse = z.object({TotalCount: z.number().int().optional(), Bounces: z.array(bounceInfoResponse).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 }; /** * Get bounces * * GET /bounces */ export const getApiBounces = async (args: GetApiBouncesArgs, options: GetApiBouncesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Postmark-Server-Token'] != null) headers.set("X-Postmark-Server-Token", String(args['X-Postmark-Server-Token'])) if (args.count != null) query.append("count", String(args.count)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.type != null) query.append("type", String(args.type)) if (args.inactive != null) query.append("inactive", String(args.inactive)) if (args.emailFilter != null) query.append("emailFilter", String(args.emailFilter)) if (args.messageID != null) query.append("messageID", String(args.messageID)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.todate != null) query.append("todate", String(args.todate)) if (args.fromdate != null) query.append("fromdate", String(args.fromdate)) const url = `${baseUrl.replace(/\/+$/, '')}/bounces${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(bounceSearchResponse, await res.json()) };