import {z} from 'zod' export type GetApiV11EmailVerifyBatchBatchIdDownloadArgs = {download?: string | undefined, batchId: string}; export type GetApiV11EmailVerifyBatchBatchIdDownloadOptions = {baseUrl?: string | undefined, apikeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emailRecord = z.object({email: z.string(), status: z.enum(["deliverable", "risky", "undeliverable", "unknown"]), reason: z.enum(["accepted_email", "low_deliverability", "low_quality", "invalid_email", "invalid_domain", "rejected_email", "dns_error", "unavailable_smtp", "unsupported", "timeout", "unknown"]), domain: z.object({name: z.string(), acceptAll: z.enum(["yes", "no", "unknown"]), disposable: z.enum(["yes", "no", "unknown"]), free: z.enum(["yes", "no", "unknown"])}).optional(), account: z.object({role: z.enum(["yes", "no", "unknown"]), disabled: z.enum(["yes", "no", "unknown"]), fullMailbox: z.enum(["yes", "no", "unknown"])}).optional(), dns: z.object({type: z.string(), record: z.string().optional()}).optional(), provider: z.string().optional(), score: z.number().int().gte(0).lte(100).optional(), toxic: z.string().optional(), toxicity: z.number().int().gte(0).lte(5).optional(), retryAfter: z.string().optional()}); export const getApiV11EmailVerifyBatchBatchIdDownloadResponse = z.array(emailRecord); 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 results * * GET /v1.1/email/verify/batch/{batchId}/download */ export const getApiV11EmailVerifyBatchBatchIdDownload = async (args: GetApiV11EmailVerifyBatchBatchIdDownloadArgs, options?: GetApiV11EmailVerifyBatchBatchIdDownloadOptions) => { const {baseUrl = "https://api.usebouncer.com", fetch: fetchFn = fetch, apikeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.download != null) query.append("download", String(args.download)) if (apikeyAuth !== undefined) headers.set("x-api-key", apikeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1.1/email/verify/batch/${encodeURIComponent(String(args.batchId))}/download${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(getApiV11EmailVerifyBatchBatchIdDownloadResponse, await res.json()) };