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 GetApiV3PropertyArgs = {street?: (string | null) | undefined, city?: (string | null) | undefined, state_code?: (StateCodeEnum | null) | undefined, zipcode?: (string | null) | undefined, apn?: (string | null) | undefined, fips?: (string | null) | undefined, lat?: (number | null) | undefined, lng?: (number | null) | undefined, radius?: (number | null) | undefined, strict_match?: (boolean | null) | undefined, page?: number | undefined, page_size?: number | undefined}; export type GetApiV3PropertyOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyAddressV3Dto = z.object({full_address: z.string().nullable().optional(), line1: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), zip: z.string().nullable().optional(), house: z.string().nullable().optional(), street: z.string().nullable().optional(), street_type: z.string().nullable().optional(), county: z.string().nullable().optional()}); export const geolocationDto = z.object({lat: z.number(), lng: z.number()}); 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 personResultMetadataV3Dto = z.object({details_url: z.string().nullable().optional(), phones: listCountDto, emails: listCountDto}); export const personOwnerV3Dto = 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(), result_metadata: personResultMetadataV3Dto.nullable().optional()}); export const businessOwnerV3Dto = z.object({name: z.string()}); export const mortgageDto = z.object({amount: z.number().int().nullable().optional(), date: z.string().nullable().optional(), due_date: z.string().nullable().optional(), term: z.number().int().nullable().optional(), rate: z.number().nullable().optional(), rate_type: z.string().nullable().optional()}); export const ownershipInfoV3Dto = z.object({owner_type: z.string().nullable().optional(), person_owners: z.array(personOwnerV3Dto).optional(), business_owners: z.array(businessOwnerV3Dto).optional(), sale_date: z.string().nullable().optional(), last_sale_amount: z.number().int().nullable().optional(), mortgages: z.array(mortgageDto).nullable().optional()}); export const residentV3Dto = 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(), result_metadata: personResultMetadataV3Dto.nullable().optional()}); export const assessedInfoDto = z.object({market_value: z.number().int().nullable().optional(), assessed_value: z.number().int().nullable().optional(), property_tax_amount: z.number().nullable().optional(), property_tax_percent: z.number().nullable().optional()}); export const matchTypeEnum = z.enum(["exact", "best match"]); export const propertyResultMetadataV3Dto = z.object({details_url: z.string(), residents: listCountDto, person_owners: listCountDto, business_owners: listCountDto}); export const propertySearchResultV3Dto = z.object({property_id: z.string(), address_id: z.string().nullable().optional(), apn: z.string().nullable().optional(), property_address: propertyAddressV3Dto, mailing_address: propertyAddressV3Dto.nullable().optional(), geolocation: geolocationDto.nullable().optional(), property_purpose: z.string().nullable().optional(), owner_occupied: z.string().nullable().optional(), is_vacant: z.boolean().nullable().optional(), year_built: z.number().int().nullable().optional(), effective_year_built: z.number().int().nullable().optional(), living_sqft: z.number().int().nullable().optional(), lot_sqft: z.number().int().nullable().optional(), ground_floor_sqft: z.number().int().nullable().optional(), basement_sqft: z.number().int().nullable().optional(), basement_type: z.string().nullable().optional(), stories: z.number().int().nullable().optional(), bedrooms: z.number().int().nullable().optional(), bathrooms: z.number().int().nullable().optional(), has_pool: z.boolean().nullable().optional(), garage_type: z.string().nullable().optional(), roof_covering: z.string().nullable().optional(), heating_fuel: z.string().nullable().optional(), condition: z.string().nullable().optional(), build_quality: z.string().nullable().optional(), ownership_info: ownershipInfoV3Dto.nullable().optional(), residents: z.array(residentV3Dto).optional(), assessed_info: assessedInfoDto.nullable().optional(), match_score: z.number().int().nullable().optional(), distance_miles: z.number().nullable().optional(), match_type: matchTypeEnum.nullable().optional(), confidence_score: z.number().int().nullable().optional(), result_metadata: propertyResultMetadataV3Dto}); export const paginationMetadataDto = z.object({result_count: z.number().int(), page: z.number().int(), page_size: z.number().int()}); export const propertySearchV3ResponseDto = z.object({results: z.array(propertySearchResultV3Dto), metadata: paginationMetadataDto}); 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 properties by address or APN * * GET /v3/property/ */ export const getApiV3Property = async (args: GetApiV3PropertyArgs, options?: GetApiV3PropertyOptions) => { 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.apn != null) query.append("apn", String(args.apn)) if (args.fips != null) query.append("fips", String(args.fips)) if (args.lat != null) query.append("lat", String(args.lat)) if (args.lng != null) query.append("lng", String(args.lng)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.strict_match != null) query.append("strict_match", String(args.strict_match)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/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(propertySearchV3ResponseDto, await res.json()) };