import {z} from 'zod' export type GetApiV2PhonePhoneArgs = {phone: string}; export type GetApiV2PhonePhoneOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumberTypeEnum = z.enum(["mobile", "landline", "voip", "tollfree", "unknown"]); export const phoneOwnerKindEnum = z.enum(["person", "business", "unknown"]); export const phoneFactsDto = z.object({e164: z.string(), national_format: z.string(), line_type: phoneNumberTypeEnum, carrier: z.string().nullable(), primary_owner: phoneOwnerKindEnum, returned_people: z.number().int(), people_limit: z.number().int(), people_truncated: z.boolean(), returned_businesses: z.number().int()}); export const ownedProperty = z.object({id: z.string(), address: z.string()}); export const phoneNumber = z.object({number: z.string(), type: phoneNumberTypeEnum, score: z.number().int().nullable()}); export const relative = z.object({id: z.string().nullable(), name: z.string()}); export const addressDto = z.object({address_id: z.string(), full_address: z.string(), city: z.string(), state: z.string(), line1: z.string(), zip: z.string()}); export const emailDto = z.object({email: z.string(), score: z.number().int()}); export const listCountDto = z.object({displayed: z.number().int(), additional: z.number().int()}); export const resultMetadataDto = z.object({details_url: z.string().nullable(), phones: listCountDto, emails: listCountDto, historic_addresses: listCountDto, owned_properties: listCountDto}); export const personSearchResultDto = z.object({id: z.string().nullable(), name: z.string(), aliases: z.array(z.string()), is_dead: z.boolean(), owned_properties: z.array(ownedProperty), phones: z.array(phoneNumber), date_of_birth: z.string().nullable(), linkedin_url: z.string().nullable(), company_name: z.string().nullable(), job_title: z.string().nullable(), relatives: z.array(relative), age: z.number().int().nullable().optional(), current_addresses: z.array(addressDto), historic_addresses: z.array(addressDto), match_score: z.number().int().nullable().optional(), matched_by: z.array(z.string()).optional(), emails: z.array(emailDto), result_metadata: resultMetadataDto}); export const latLong = z.object({lat: z.number(), lng: z.number()}); export const businessAddressDto = z.object({full_address: z.string(), line1: z.string(), city: z.string(), state: z.string(), zip: z.string(), plus4: z.string().nullable(), latlong: latLong.nullable(), is_historical: z.boolean().nullable()}); export const locationPrecisionEnum = z.enum(["zip4", "zip", "city", "state", "country", "unknown"]); export const approximateLocationDto = z.object({city: z.string().nullable(), state: z.string().nullable(), zip: z.string().nullable(), plus4: z.string().nullable(), country: z.string().nullable(), latlong: latLong.nullable(), is_historical: z.boolean(), precision: locationPrecisionEnum}); export const businessDto = z.object({name: z.string(), addresses: z.array(businessAddressDto), approximate_locations: z.array(approximateLocationDto)}); export const phoneLookupV2ResponseDto = z.object({phone: phoneFactsDto, people: z.array(personSearchResultDto), businesses: z.array(businessDto)}); 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 }; /** * Look up what is known about a phone number * * GET /v2/phone/{phone} */ export const getApiV2PhonePhone = async (args: GetApiV2PhonePhoneArgs, options?: GetApiV2PhonePhoneOptions) => { const {baseUrl = "https://api.whitepages.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/phone/${encodeURIComponent(String(args.phone))}${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(phoneLookupV2ResponseDto, await res.json()) };