import {z} from 'zod' export type GetApiPropertiesArgs = {address?: string | undefined, city?: string | undefined, state?: string | undefined, zipCode?: string | undefined, latitude?: number | undefined, longitude?: number | undefined, radius?: number | undefined, propertyType?: ('Single Family' | 'Condo' | 'Townhouse' | 'Manufactured' | 'Multi-Family' | 'Apartment' | 'Land') | undefined, bedrooms?: string | undefined, bathrooms?: string | undefined, squareFootage?: string | undefined, lotSize?: string | undefined, yearBuilt?: string | undefined, saleDateRange?: string | undefined, limit?: number | undefined, offset?: number | undefined, includeTotalCount?: boolean | undefined}; export type GetApiPropertiesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPropertiesResponse = z.array(z.object({id: z.string().optional(), formattedAddress: z.string().optional(), addressLine1: z.string().optional(), addressLine2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), stateFips: z.string().optional(), zipCode: z.string().optional(), county: z.string().optional(), countyFips: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), propertyType: z.string().optional(), bedrooms: z.number().optional(), bathrooms: z.number().optional(), squareFootage: z.number().optional(), lotSize: z.number().optional(), yearBuilt: z.number().optional(), assessorID: z.string().optional(), legalDescription: z.string().optional(), subdivision: z.string().optional(), zoning: z.string().optional(), lastSaleDate: z.string().optional(), lastSalePrice: z.number().optional(), hoa: z.object({fee: z.number().optional()}).optional(), features: z.object({architectureType: z.string().optional(), cooling: z.boolean().optional(), coolingType: z.string().optional(), exteriorType: z.string().optional(), fireplace: z.boolean().optional(), fireplaceType: z.string().optional(), floorCount: z.number().optional(), foundationType: z.string().optional(), garage: z.boolean().optional(), garageSpaces: z.number().optional(), garageType: z.string().optional(), heating: z.boolean().optional(), heatingType: z.string().optional(), pool: z.boolean().optional(), poolType: z.string().optional(), roofType: z.string().optional(), roomCount: z.number().optional(), unitCount: z.number().optional(), viewType: z.string().optional()}).optional(), taxAssessments: z.object({"2020": z.object({year: z.number().optional(), value: z.number().optional(), land: z.number().optional(), improvements: z.number().optional()}).optional(), "2021": z.object({year: z.number().optional(), value: z.number().optional(), land: z.number().optional(), improvements: z.number().optional()}).optional(), "2022": z.object({year: z.number().optional(), value: z.number().optional(), land: z.number().optional(), improvements: z.number().optional()}).optional(), "2023": z.object({year: z.number().optional(), value: z.number().optional(), land: z.number().optional(), improvements: z.number().optional()}).optional(), "2024": z.object({year: z.number().optional(), value: z.number().optional(), land: z.number().optional(), improvements: z.number().optional()}).optional()}).optional(), propertyTaxes: z.object({"2020": z.object({year: z.number().optional(), total: z.number().optional()}).optional(), "2021": z.object({year: z.number().optional(), total: z.number().optional()}).optional(), "2022": z.object({year: z.number().optional(), total: z.number().optional()}).optional(), "2023": z.object({year: z.number().optional(), total: z.number().optional()}).optional(), "2024": z.object({year: z.number().optional(), total: z.number().optional()}).optional()}).optional(), history: z.object({"2017-10-19": z.object({event: z.string().optional(), date: z.string().optional(), price: z.number().optional()}).optional(), "2024-11-18": z.object({event: z.string().optional(), date: z.string().optional(), price: z.number().optional()}).optional()}).optional(), owner: z.object({names: z.array(z.string()).optional(), type: z.string().optional(), mailingAddress: z.object({id: z.string().optional(), formattedAddress: z.string().optional(), addressLine1: z.string().optional(), addressLine2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), stateFips: z.string().optional(), zipCode: z.string().optional()}).optional()}).optional(), ownerOccupied: z.boolean().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 }; /** * Property Records * * GET /properties */ export const getApiProperties = async (args: GetApiPropertiesArgs, options?: GetApiPropertiesOptions) => { const {baseUrl = "https://api.rentcast.io/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.address != null) query.append("address", String(args.address)) if (args.city != null) query.append("city", String(args.city)) if (args.state != null) query.append("state", String(args.state)) if (args.zipCode != null) query.append("zipCode", String(args.zipCode)) if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.propertyType != null) query.append("propertyType", String(args.propertyType)) if (args.bedrooms != null) query.append("bedrooms", String(args.bedrooms)) if (args.bathrooms != null) query.append("bathrooms", String(args.bathrooms)) if (args.squareFootage != null) query.append("squareFootage", String(args.squareFootage)) if (args.lotSize != null) query.append("lotSize", String(args.lotSize)) if (args.yearBuilt != null) query.append("yearBuilt", String(args.yearBuilt)) if (args.saleDateRange != null) query.append("saleDateRange", String(args.saleDateRange)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.includeTotalCount != null) query.append("includeTotalCount", String(args.includeTotalCount)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/properties${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(getApiPropertiesResponse, await res.json()) };