import {z} from 'zod' export type PhoneValidationV2SupplementaryLiveStatus = {mobile?: (Array | null) | undefined, landline?: (Array | null) | undefined}; export type PhoneValidationV2Request = {number: string, output_format?: (string | null) | undefined, cache_value_days?: (number | null) | undefined, country_iso?: (string | null) | undefined, get_ported_date?: (boolean | null) | undefined, get_disposable_number?: (boolean | null) | undefined, supplementary_live_status?: PhoneValidationV2SupplementaryLiveStatus | undefined}; export type CreateApiPhoneValidateV2Args = {'Add-Metadata'?: boolean | undefined, 'Reference-Id'?: string | undefined, 'Timeout-Seconds'?: number | undefined, body: PhoneValidationV2Request}; export type CreateApiPhoneValidateV2Options = {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 phoneValidationV2Confidence = z.enum(["Verified", "Absent", "Teleservice not provisioned", "Unverified", "No coverage", "Unknown", "Dead"]); export const phoneValidationV2Result = z.object({number: z.string().nullable().optional(), validated_phone_number: z.string().nullable().optional(), formatted_phone_number: z.string().nullable().optional(), phone_type: z.string().nullable().optional(), confidence: phoneValidationV2Confidence.optional(), ported_date: z.string().nullable().optional(), disposable_number: z.string().nullable().optional()}); export const phoneValidationV2PhoneDetail = z.object({original_operator_name: z.string().nullable().optional(), original_network_status: z.string().nullable().optional(), original_home_network_identity: z.string().nullable().optional(), original_country_prefix: z.string().nullable().optional(), original_country_name: z.string().nullable().optional(), original_country_iso: z.string().nullable().optional(), operator_name: z.string().nullable().optional(), network_status: z.string().nullable().optional(), home_network_identity: z.string().nullable().optional(), country_prefix: z.string().nullable().optional(), country_name: z.string().nullable().optional(), country_iso: z.string().nullable().optional(), is_ported: z.string().nullable().optional(), cache_value_days: z.number().int().nullable().optional(), date_cached: z.string().nullable().optional(), email_to_sms_address: z.string().nullable().optional(), email_to_mms_address: z.string().nullable().optional()}); export const phoneValidationV2Metadata = z.object({code: z.string().nullable().optional(), message: z.string().nullable().optional(), phone_detail: phoneValidationV2PhoneDetail.optional()}); export const phoneValidationV2Response = z.object({error: responseError.optional(), result: phoneValidationV2Result.optional(), metadata: phoneValidationV2Metadata.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 a phone number to the service to be validated and returns the result of the validation. * * POST /phone/validate/v2 * * Also accepts auth: Auth-Token */ export const createApiPhoneValidateV2 = async (args: CreateApiPhoneValidateV2Args, options?: CreateApiPhoneValidateV2Options) => { 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['Add-Metadata'] != null) headers.set("Add-Metadata", String(args['Add-Metadata'])) if (args['Reference-Id'] != null) headers.set("Reference-Id", String(args['Reference-Id'])) if (args['Timeout-Seconds'] != null) headers.set("Timeout-Seconds", String(args['Timeout-Seconds'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/phone/validate/v2${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(phoneValidationV2Response, await res.json()) };