import {z} from 'zod' export type GetApiV1PersonArgs = {phone?: (string | null) | undefined, name?: (string | null) | undefined, first_name?: (string | null) | undefined, middle_name?: (string | null) | undefined, last_name?: (string | null) | undefined, street?: (string | null) | undefined, 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, zipcode?: (string | null) | undefined, include_historical_locations?: (boolean | null) | undefined, min_age?: (number | null) | undefined, max_age?: (number | null) | undefined, radius?: (number | null) | undefined, strict_match?: (boolean | null) | undefined}; export type GetApiV1PersonOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ownedProperty = z.object({id: z.string(), 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 relative = z.object({id: z.string().nullable(), name: z.string()}); export const latLong = z.object({lat: z.number(), lng: z.number()}); export const personAddress = z.object({id: z.string().nullable(), address: z.string(), latlong: latLong.nullable().optional(), score: z.number().int().nullable().optional()}); export const personResponseDto = 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), current_addresses: z.array(personAddress), historic_addresses: z.array(personAddress), emails: z.array(z.string())}); export const getApiV1PersonResponse = z.array(personResponseDto); 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 a person by name, phone number, and address * * GET /v1/person/ */ export const getApiV1Person = async (args: GetApiV1PersonArgs, options?: GetApiV1PersonOptions) => { 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.phone != null) query.append("phone", String(args.phone)) if (args.name != null) query.append("name", String(args.name)) if (args.first_name != null) query.append("first_name", String(args.first_name)) if (args.middle_name != null) query.append("middle_name", String(args.middle_name)) if (args.last_name != null) query.append("last_name", String(args.last_name)) 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.include_historical_locations != null) query.append("include_historical_locations", String(args.include_historical_locations)) if (args.min_age != null) query.append("min_age", String(args.min_age)) if (args.max_age != null) query.append("max_age", String(args.max_age)) if (args.radius != null) query.append("radius", String(args.radius)) 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(/\/+$/, '')}/v1/person/${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(getApiV1PersonResponse, await res.json()) };