import {z} from 'zod' export type RpaV1Request = {number: string}; export type CreateApiPhoneAppendV1Args = {'Reference-Id'?: string | undefined, 'Timeout-Seconds'?: number | undefined, 'Add-Metadata'?: boolean | undefined, body: RpaV1Request}; export type CreateApiPhoneAppendV1Options = {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 rpaV1PhoneConfidence = z.enum(["Verified", "Unverified"]); export const rpaV1AddressConfidence = z.enum(["Verified", "Unverified"]); export const rpaV1Address = z.object({address_line1: z.string().nullable().optional(), address_line2: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), zip5: z.string().nullable().optional(), zip4: z.string().nullable().optional(), confidence: rpaV1AddressConfidence.optional()}); export const rpaV1Result = z.object({number: z.string().nullable().optional(), confidence: rpaV1PhoneConfidence.optional(), first_name: z.string().nullable().optional(), middle_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), address: rpaV1Address.optional()}); export const rpaV1PhoneType = z.enum(["Landline", "Mobile", "Other"]); export const rpaV1PhoneDetail = z.object({input_phone_type: rpaV1PhoneType.optional()}); export const rpaV1Metadata = z.object({phone_detail: rpaV1PhoneDetail.optional()}); export const rpaV1Response = z.object({error: responseError.optional(), result: rpaV1Result.optional(), metadata: rpaV1Metadata.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 get the reverse phone append. * * POST /phone/append/v1 * * Also accepts auth: Auth-Token */ export const createApiPhoneAppendV1 = async (args: CreateApiPhoneAppendV1Args, options?: CreateApiPhoneAppendV1Options) => { 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 (args['Timeout-Seconds'] != null) headers.set("Timeout-Seconds", String(args['Timeout-Seconds'])) if (args['Add-Metadata'] != null) headers.set("Add-Metadata", String(args['Add-Metadata'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/phone/append/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(rpaV1Response, await res.json()) };