import {z} from 'zod' export type CheckEmailInputProxy = {host: string, port: number, username?: string | undefined, password?: string | undefined}; export type GmailVerifMethod = 'Smtp'; export type HotmailB2BVerifMethod = 'Smtp'; export type HotmailB2CVerifMethod = 'Smtp' | 'Headless'; export type YahooVerifMethod = 'Api' | 'Headless' | 'Smtp'; export type CheckEmailRequest = {from_email?: string | undefined, to_email: string, hello_name?: string | undefined, proxy?: CheckEmailInputProxy | undefined, smtp_port?: number | undefined, gmail_verif_method?: GmailVerifMethod | undefined, hotmailb2b_verif_method?: HotmailB2BVerifMethod | undefined, hotmailb2c_verif_method?: HotmailB2CVerifMethod | undefined, yahoo_verif_method?: YahooVerifMethod | undefined, check_gravatar?: boolean | undefined}; export type CreateApiV0CheckEmailArgs = {Authorization: string, body: CheckEmailRequest}; export type CreateApiV0CheckEmailOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const reachable = z.enum(["invalid", "unknown", "safe", "risky"]); export const miscDetails = z.object({is_disposable: z.boolean(), is_role_account: z.boolean(), gravatar_url: z.string().optional(), is_b2c: z.boolean()}); export const coreError = z.object({type: z.string(), message: z.string()}); export const mxDetails = z.object({accepts_mail: z.boolean(), records: z.array(z.string())}); export const smtpDetails = z.object({can_connect_smtp: z.boolean(), has_full_inbox: z.boolean(), is_catch_all: z.boolean(), is_deliverable: z.boolean(), is_disabled: z.boolean()}); export const syntaxDetails = z.object({domain: z.string(), is_valid_syntax: z.boolean(), username: z.string()}); export const duration = z.object({secs: z.number(), nanos: z.number()}); export const verifMethod = z.object({type: z.enum(["Smtp", "Headless", "Api", "Skipped"])}); export const debugDetailsSmtp = z.object({verif_method: verifMethod.optional()}); export const debugDetails = z.object({start_time: z.string(), end_time: z.string(), duration: duration, server_name: z.string(), reacher_version: z.string().optional(), smtp: debugDetailsSmtp}); export const checkEmailOutput = z.object({input: z.string(), is_reachable: reachable, misc: z.union([miscDetails, coreError]), mx: z.union([mxDetails, coreError]), smtp: z.union([smtpDetails, coreError]), syntax: syntaxDetails, debug: debugDetails.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 }; /** * /v0/check_email * * POST /v0/check_email */ export const createApiV0CheckEmail = async (args: CreateApiV0CheckEmailArgs, options?: CreateApiV0CheckEmailOptions) => { const {baseUrl = "https://api.reacher.email", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/v0/check_email${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(checkEmailOutput, await res.json()) };