import {z} from 'zod' export type EmailValidationV1Request = {email: string, timeout?: number | undefined}; export type CreateApiEmailValidationV1Args = {'Reference-Id'?: string | undefined, body: EmailValidationV1Request}; export type CreateApiEmailValidationV1Options = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const responseError = z.object({type: z.string().nullable().optional(), title: z.string().nullable().optional(), detail: z.string().nullable().optional(), instance: z.string().nullable().optional()}); export const emailValidationV1Confidence = z.enum(["verified", "undeliverable", "unreachable", "illegitimate", "disposable", "unknown"]); export const emailValidationV1Result = z.object({email: z.string().nullable().optional(), confidence: emailValidationV1Confidence.optional(), verbose: z.string().nullable().optional(), corrections: z.array(z.string()).nullable().optional()}); export const emailValidationV1Response = z.object({reference_id: z.string().nullable().optional(), transaction_id: z.string().nullable().optional(), error: responseError.optional(), result: emailValidationV1Result.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 }; /** * Submits an email address to the service to be validated and returns the result of the validation. * * POST /email/validation/v1 * * Also accepts auth: Auth-Token */ export const createApiEmailValidationV1 = async (args: CreateApiEmailValidationV1Args, options?: CreateApiEmailValidationV1Options) => { const {baseUrl = "https://api.experianaperture.io", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Reference-Id'] != null) headers.set("Reference-Id", String(args['Reference-Id'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/email/validation/v1${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(emailValidationV1Response, await res.json()) };