import {z} from 'zod' export type GetApiAvmValueArgs = {address?: string | undefined, latitude?: number | undefined, longitude?: number | undefined, propertyType?: ('Single Family' | 'Condo' | 'Townhouse' | 'Manufactured' | 'Multi-Family' | 'Apartment' | 'Land') | undefined, bedrooms?: number | undefined, bathrooms?: number | undefined, squareFootage?: number | undefined, maxRadius?: number | undefined, daysOld?: number | undefined, compCount?: number | undefined, lookupSubjectAttributes?: boolean | undefined}; export type GetApiAvmValueOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAvmValueResponse = z.object({price: z.number().optional(), priceRangeLow: z.number().optional(), priceRangeHigh: z.number().optional(), subjectProperty: 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(), lastSaleDate: z.string().optional(), lastSalePrice: z.number().optional()}).optional(), comparables: 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(), status: z.string().optional(), price: z.number().optional(), listingType: z.string().optional(), listedDate: z.string().optional(), removedDate: z.string().optional(), lastSeenDate: z.string().optional(), daysOnMarket: z.number().optional(), distance: z.number().optional(), daysOld: z.number().optional(), correlation: z.number().optional()})).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 }; /** * Value Estimate * * GET /avm/value */ export const getApiAvmValue = async (args: GetApiAvmValueArgs, options?: GetApiAvmValueOptions) => { 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.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) 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.maxRadius != null) query.append("maxRadius", String(args.maxRadius)) if (args.daysOld != null) query.append("daysOld", String(args.daysOld)) if (args.compCount != null) query.append("compCount", String(args.compCount)) if (args.lookupSubjectAttributes != null) query.append("lookupSubjectAttributes", String(args.lookupSubjectAttributes)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/avm/value${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(getApiAvmValueResponse, await res.json()) };