import {z} from 'zod' export type GetApiV1PropertyArgs = {street: string, city?: (string | null) | undefined, state_code?: ('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' | null) | undefined, zip?: (string | null) | undefined}; export type GetApiV1PropertyOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fullAddress = z.object({id: z.string().nullable(), full_address: 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 propertyPersonResponseDto = z.object({id: z.string().nullable(), name: z.string(), current_addresses: z.array(fullAddress), phones: z.array(phoneNumber), emails: z.array(z.string())}); export const propertyResponseDto = z.object({id: z.string().nullable(), property_address: fullAddress, owners: z.array(propertyPersonResponseDto), residents: z.array(propertyPersonResponseDto)}); 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 property details by address * * GET /v1/property/ */ export const getApiV1Property = async (args: GetApiV1PropertyArgs, options?: GetApiV1PropertyOptions) => { 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.zip != null) query.append("zip", String(args.zip)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(propertyResponseDto, await res.json()) };