import {z} from 'zod' export type GetApiV1TransactionsTransactionIdEmailNotificationsArgs = {transaction_id: unknown}; export type GetApiV1TransactionsTransactionIdEmailNotificationsOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const edubankTransactionEmailNotification = z.object({transaction_id: z.string(), email_subject: z.string(), email_from: z.string(), email_to: z.string(), email_reply_to: z.string(), category: z.enum(["overdue", "billing", "payment", "unsubscribe"]), created_at: z.string(), sent_at: z.string().optional(), delivered: z.boolean(), delivered_at: z.string().optional(), open: z.boolean(), open_at: z.string().optional(), rejected: z.boolean(), rejected_reason: z.string().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 }; /** * Returns all email notifications sent for the respective transaction * * GET /v1/transactions/{transaction_id}/email_notifications * * Also accepts auth: authorization */ export const getApiV1TransactionsTransactionIdEmailNotifications = async (args: GetApiV1TransactionsTransactionIdEmailNotificationsArgs, options: GetApiV1TransactionsTransactionIdEmailNotificationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transactions/${encodeURIComponent(String(args.transaction_id))}/email_notifications${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(edubankTransactionEmailNotification, await res.json()) };