import {z} from 'zod' export type EnrichRequest = {address?: (string | null) | undefined, fields?: (Array | null) | undefined, lat?: (number | null) | undefined, lng?: (number | null) | undefined, max_cost_usd?: (string | null) | undefined, preset?: (Array<'parcel' | 'zoning' | 'buildings' | 'valuation' | 'legal' | 'power' | 'access' | 'water' | 'terrain' | 'environment' | 'climate' | 'hazards' | 'market' | 'constraints'> | null) | undefined}; export type CreateApiV1EnrichArgs = {'Idempotency-Key'?: (string | null) | undefined, body: EnrichRequest}; export type CreateApiV1EnrichOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const publicAnalysisPoint = z.object({lat: z.number().gte(-90).lte(90), lng: z.number().gte(-180).lte(180), method: z.enum(["stored_parcel_centroid", "input_fallback"]), source: z.enum(["parcel_centroid", "geocoded_address", "input_coordinate"])}); export const paidBilling = z.object({charged_usd: z.string(), currency: z.literal("USD"), operation_id: z.string(), pricing_version: z.string(), replayed: z.boolean()}); export const confidence = z.enum(["high", "medium", "low", "unknown"]); export const jsonValue = z.unknown(); export const enrichPublicField = z.object({confidence: confidence, flags: z.array(z.string().min(1)).nullable().optional(), interpretation_hints: z.string().min(1), source: z.string().min(1), unit: z.string().nullable().optional(), value: jsonValue}); export const geocodeDetails = z.object({accuracy: z.number().nullable(), accuracy_type: z.string().nullable(), match_type: z.string().nullable(), normalized_address: z.string().nullable(), parcel_grade: z.boolean(), precision_note: z.string().nullable(), provider: z.string().nullable(), source: z.string().nullable()}); export const publicParcelCandidate = z.object({address: z.string().nullable().optional(), match_distance_m: z.number().gte(0).nullable().optional(), match_type: z.string().nullable().optional(), parcelnumb: z.string().nullable().optional(), source: z.string().min(1)}); export const publicParcelMatch = z.object({candidates: z.array(publicParcelCandidate).optional(), parcel: publicParcelCandidate.nullable().optional(), reason: z.string().min(1).nullable().optional(), status: z.enum(["matched", "ambiguous", "no_match", "unavailable"])}); export const resolvedLocation = z.object({lat: z.number().gte(-90).lte(90), lng: z.number().gte(-180).lte(180), source: z.enum(["address", "coordinate"])}); export const enrichPublicResponse = z.object({analysis_point: publicAnalysisPoint.nullable().optional(), billing: paidBilling.nullable().optional(), fields: z.record(z.string(), enrichPublicField), geocode: geocodeDetails.nullable().optional(), lat: z.number().gte(-90).lte(90), lng: z.number().gte(-180).lte(180), parcel_match: publicParcelMatch, resolved_location: resolvedLocation}); 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 }; /** * Enrich a parcel * * POST /v1/enrich */ export const createApiV1Enrich = async (args: CreateApiV1EnrichArgs, options?: CreateApiV1EnrichOptions) => { const {baseUrl = "https://api.mireye.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/enrich${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(enrichPublicResponse, await res.json()) };