import {z} from 'zod' export type GetApiV2PersonIdArgs = {id: string}; export type GetApiV2PersonIdOptions = {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 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 emailDto = z.object({email: z.string(), score: z.number().int()}); export const personV2ResponseDto = 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), age: z.number().int().nullable().optional(), current_addresses: z.array(addressDto), historic_addresses: z.array(addressDto), match_score: z.number().int().nullable().optional(), matched_by: z.array(z.string()).optional(), emails: z.array(emailDto)}); export const detailResponseDtoPersonV2ResponseDto = z.object({result: personV2ResponseDto}); 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 }; /** * Gets person details by id * * GET /v2/person/{id} */ export const getApiV2PersonId = async (args: GetApiV2PersonIdArgs, options?: GetApiV2PersonIdOptions) => { 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 (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/person/${encodeURIComponent(String(args.id))}${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(detailResponseDtoPersonV2ResponseDto, await res.json()) };