import {z} from 'zod' export type GetApiPropertiesRandomArgs = {limit?: number | undefined}; export type GetApiPropertiesRandomOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPropertiesRandomResponse = 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 }; /** * Random Property Records * * GET /properties/random */ export const getApiPropertiesRandom = async (args: GetApiPropertiesRandomArgs, options?: GetApiPropertiesRandomOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/properties/random${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(getApiPropertiesRandomResponse, await res.json()) };