import {z} from 'zod' export type GetApiEmailsArgs = {status?: ('pending' | 'sent' | 'softfail' | 'hardfail' | 'bounced' | 'held') | undefined, to?: string | undefined, from?: string | undefined, tag?: string | undefined, after?: string | undefined, before?: string | undefined, page?: number | undefined, perPage?: number | undefined}; export type GetApiEmailsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const emailSummary = z.object({id: z.string(), to: z.string(), from: z.string(), subject: z.string(), status: z.enum(["pending", "sent", "softfail", "hardfail", "bounced", "held"]), tag: z.string().optional(), timestamp: z.number(), timestampIso: z.string()}); export const apiMeta = z.object({requestId: z.string()}); export const listEmailsResponse = z.object({data: z.array(emailSummary), page: z.number().int(), perPage: z.number().int(), total: z.number().int(), totalPages: z.number().int(), meta: apiMeta}); 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 }; /** * List sent emails * * GET /emails */ export const getApiEmails = async (args: GetApiEmailsArgs, options?: GetApiEmailsOptions) => { const {baseUrl = "https://api.arkhq.io/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.to != null) query.append("to", String(args.to)) if (args.from != null) query.append("from", String(args.from)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/emails${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(listEmailsResponse, await res.json()) };