import {z} from 'zod' export type GetApiV1EventsDeedArgs = {region: string, property_id?: (string | null) | undefined, event_date_from?: (string | null) | undefined, event_date_to?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1EventsDeedOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jurisdictionDto = z.object({county: z.string().nullable().optional(), state: z.string(), fips_code: z.string().nullable().optional()}); export const eventPropertyAddressDto = z.object({line1: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), zip: z.string().nullable().optional(), county: z.string().nullable().optional()}); export const eventGeolocationDto = z.object({lat: z.number(), lng: z.number()}); export const legalDescriptionDto = z.object({text: z.string().nullable().optional()}); export const propertyDto = z.object({property_id: z.string().nullable().optional(), property_address: eventPropertyAddressDto.nullable().optional(), apn: z.string().nullable().optional(), parcel_id: z.string().nullable().optional(), geolocation: eventGeolocationDto.nullable().optional(), legal_description: legalDescriptionDto.nullable().optional(), bedrooms: z.number().int().nullable().optional(), bathrooms: z.number().nullable().optional(), living_sqft: z.number().int().nullable().optional(), lot_sqft: z.number().int().nullable().optional(), property_purpose: z.string().nullable().optional()}); export const partyAddressDto = z.object({full_address: z.string()}); export const partyDto = z.object({role: z.string(), name: z.string().nullable().optional(), type: z.string().nullable().optional(), person_id: z.string().nullable().optional(), addresses: z.array(partyAddressDto).optional()}); export const documentTypeDto = z.object({category: z.string().nullable().optional(), text: z.string().nullable().optional()}); export const transactionReasonDto = z.object({category: z.string().nullable().optional(), text: z.string().nullable().optional()}); export const deedDetailDto = z.object({document_type: documentTypeDto.nullable().optional(), recorded_date: z.string().nullable().optional(), instrument_date: z.string().nullable().optional(), consideration: z.number().nullable().optional(), transaction_reason: transactionReasonDto.nullable().optional()}); export const deedEventDataDto = z.object({jurisdiction: jurisdictionDto, property: propertyDto.nullable().optional(), parties: z.array(partyDto).optional(), detail: deedDetailDto.nullable().optional()}); export const eventResponseDto = z.object({id: z.string(), object: z.string().optional(), event_type: z.string(), data: deedEventDataDto}); export const paginationDto = z.object({next_cursor: z.string().nullable().optional()}); export const searchDeedEventsResponseDto = z.object({results: z.array(eventResponseDto).optional(), pagination: paginationDto}); 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 all deed events * * GET /v1/events/deed */ export const getApiV1EventsDeed = async (args: GetApiV1EventsDeedArgs, options?: GetApiV1EventsDeedOptions) => { 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.region != null) query.append("region", String(args.region)) if (args.property_id != null) query.append("property_id", String(args.property_id)) if (args.event_date_from != null) query.append("event_date_from", String(args.event_date_from)) if (args.event_date_to != null) query.append("event_date_to", String(args.event_date_to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events/deed${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(searchDeedEventsResponseDto, await res.json()) };