import {z} from 'zod' export type StateCodeEnum = 'AL' | 'AK' | 'AZ' | 'AR' | 'AS' | 'CA' | 'CO' | 'CT' | 'DE' | 'DC' | 'FL' | 'GA' | 'GU' | 'HI' | 'ID' | 'IL' | 'IN' | 'IA' | 'KS' | 'KY' | 'LA' | 'ME' | 'MD' | 'MA' | 'MI' | 'MN' | 'MS' | 'MO' | 'MT' | 'NE' | 'NV' | 'NH' | 'NJ' | 'NM' | 'NY' | 'NC' | 'ND' | 'MP' | 'OH' | 'OK' | 'OR' | 'PA' | 'PR' | 'RI' | 'SC' | 'SD' | 'TN' | 'TX' | 'TT' | 'UT' | 'VT' | 'VA' | 'VI' | 'WA' | 'WV' | 'WI' | 'WY'; export type GetApiV2PropertyArgs = {street?: (string | null) | undefined, city?: (string | null) | undefined, state_code?: (StateCodeEnum | null) | undefined, zipcode?: (string | null) | undefined, strict_match?: (boolean | null) | undefined}; export type GetApiV2PropertyOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyAddressDto = z.object({full_address: z.string(), line1: z.string(), city: z.string(), state: z.string(), zip: z.string(), house: z.string(), street: z.string(), street_type: z.string().nullable().optional(), county: z.string().nullable().optional()}); export const ownerOccupancyEnum = z.enum(["OwnerOccupied", "AbsenteeOwner", "Unknown"]); export const geolocationDto = z.object({lat: z.number(), lng: z.number()}); export const businessOwnerDto = z.object({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 phoneNumberTypeEnum = z.enum(["mobile", "landline", "voip", "tollfree", "unknown"]); export const phoneNumber = z.object({number: z.string(), type: phoneNumberTypeEnum, score: z.number().int().nullable()}); export const personEmailDto = z.object({email: z.string(), score: z.number().int().nullable().optional()}); export const listCountDto = z.object({displayed: z.number().int(), additional: z.number().int()}); export const personDisplayMetadataDto = z.object({details_url: z.string(), phones: listCountDto, emails: listCountDto, owned_properties: listCountDto}); export const personOwnerDto = z.object({id: z.string(), name: z.string(), age: z.number().int().nullable().optional(), current_addresses: z.array(addressDto).optional(), phones: z.array(phoneNumber).optional(), emails: z.array(personEmailDto).optional(), company_name: z.string().nullable().optional(), job_title: z.string().nullable().optional(), person_owner_metadata: personDisplayMetadataDto.nullable().optional()}); export const ownershipInfoDto = z.object({owner_type: z.string().nullable().optional(), sale_date: z.string().nullable().optional(), business_owners: z.array(businessOwnerDto).optional(), person_owners: z.array(personOwnerDto).optional()}); export const residentDto = z.object({id: z.string(), name: z.string(), age: z.number().int().nullable().optional(), current_addresses: z.array(addressDto).optional(), phones: z.array(phoneNumber).optional(), emails: z.array(personEmailDto).optional(), company_name: z.string().nullable().optional(), job_title: z.string().nullable().optional(), resident_metadata: personDisplayMetadataDto.nullable().optional()}); export const matchTypeEnum = z.enum(["exact", "best match"]); export const propertyMetadataDto = z.object({confidence_score: z.number().int().nullable().optional(), match_type: matchTypeEnum.nullable().optional(), details_url: z.string()}); export const propertyV2Dto = z.object({property_id: z.string(), address_id: z.string().nullable().optional(), apn: z.string().nullable().optional(), property_address: propertyAddressDto, mailing_address: propertyAddressDto.nullable().optional(), property_purpose: z.string().nullable().optional(), owner_occupied: ownerOccupancyEnum.nullable().optional(), is_vacant: z.boolean().nullable().optional(), geolocation: geolocationDto.nullable().optional(), ownership_info: ownershipInfoDto.nullable().optional(), residents: z.array(residentDto).optional(), property_metadata: propertyMetadataDto.nullable().optional()}); export const detailResponseDtoPropertyV2Dto = z.object({result: propertyV2Dto}); 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 }; /** * Search for property by address * * GET /v2/property/ */ export const getApiV2Property = async (args: GetApiV2PropertyArgs, options?: GetApiV2PropertyOptions) => { 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 (args.street != null) query.append("street", String(args.street)) if (args.city != null) query.append("city", String(args.city)) if (args.state_code != null) query.append("state_code", String(args.state_code)) if (args.zipcode != null) query.append("zipcode", String(args.zipcode)) if (args.strict_match != null) query.append("strict_match", String(args.strict_match)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/property/${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(detailResponseDtoPropertyV2Dto, await res.json()) };