import {z} from 'zod' export type CreateApiZillowGetPropertyArgs = {body: {property_id: string}}; export type CreateApiZillowGetPropertyOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const zillowPriceHistoryEvent = z.object({date_posted: z.string().optional(), price: z.number().nullable().optional(), time_epoch: z.number().int().optional(), price_per_square_foot: z.number().nullable().optional(), price_change_rate: z.number().optional(), event: z.string().optional(), source: z.string().optional(), is_rental: z.boolean().optional()}); export const zillowProperty = z.object({property_id: z.number().int().optional(), address: z.object({streetAddress: z.string().optional(), zipcode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), buildingId: z.number().int().optional()}).optional(), photos: z.array(z.string()).optional(), bathrooms: z.number().nullable().optional(), bedrooms: z.number().int().nullable().optional(), living_area: z.number().nullable().optional(), year_built: z.number().int().nullable().optional(), property_type: z.string().optional(), listing_status: z.string().optional(), days_on_zillow: z.number().int().nullable().optional(), price: z.number().optional(), zestimate: z.number().nullable().optional(), rent_zestimate: z.number().nullable().optional(), price_history: z.array(zillowPriceHistoryEvent).optional()}); export const createApiZillowGetPropertyResponse = z.object({property: zillowProperty.optional()}); 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 from Zillow * * POST /zillow/get-property */ export const createApiZillowGetProperty = async (args: CreateApiZillowGetPropertyArgs, options?: CreateApiZillowGetPropertyOptions) => { const {baseUrl = "https://api.exfunc.dev", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/zillow/get-property${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiZillowGetPropertyResponse, await res.json()) };