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 GetApiV3PropertyOwnersArgs = {property_id?: (string | null) | undefined, 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}; export type GetApiV3PropertyOwnersOptions = {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 matchTypeEnum = z.enum(["exact", "best match"]); export const propertyContextMetadataV3Dto = z.object({details_url: z.string()}); export const propertyContextV3Dto = z.object({property_id: z.string(), address_id: z.string().nullable().optional(), apn: z.string().nullable().optional(), property_address: propertyAddressV3Dto, property_purpose: z.string().nullable().optional(), owner_occupied: z.string().nullable().optional(), match_type: matchTypeEnum.nullable().optional(), confidence_score: z.number().int().nullable().optional(), result_metadata: propertyContextMetadataV3Dto}); 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 propertyOwnersV3Dto = z.object({property: propertyContextV3Dto, owner_type: z.string().nullable().optional(), person_owners: z.array(personOwnerV3Dto).optional(), business_owners: z.array(businessOwnerV3Dto).optional()}); export const ownerListCountV3Dto = z.object({displayed: z.number().int(), complete: z.boolean()}); export const parcelResolutionV3Dto = z.object({matched: z.number().int().nullable(), resolved: z.string(), ambiguous: z.boolean().nullable(), complete: z.boolean()}); export const propertyOwnersResultMetadataV3Dto = z.object({person_owners: ownerListCountV3Dto, business_owners: ownerListCountV3Dto, parcel_resolution: parcelResolutionV3Dto}); export const propertyOwnersV3ResponseDto = z.object({result: propertyOwnersV3Dto, result_metadata: propertyOwnersResultMetadataV3Dto}); 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 }; /** * Get the owners of a property * * GET /v3/property/owners */ export const getApiV3PropertyOwners = async (args: GetApiV3PropertyOwnersArgs, options?: GetApiV3PropertyOwnersOptions) => { 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.property_id != null) query.append("property_id", String(args.property_id)) 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 (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/property/owners${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(propertyOwnersV3ResponseDto, await res.json()) };