import {z} from 'zod' export type JsonValue = unknown; export type SearchFilter = {field: string, op: 'eq' | 'ne' | 'lt' | 'lte' | 'gt' | 'gte' | 'in' | 'between' | 'intersects' | 'within' | 'match', value: JsonValue}; export type SearchRequest = {cursor?: (string | null) | undefined, fields?: Array | undefined, filters: Array, limit?: number | undefined, max_cost_usd?: (string | null) | undefined}; export type CreateApiV1SearchArgs = {'Idempotency-Key'?: (string | null) | undefined, body: SearchRequest}; export type CreateApiV1SearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 searchPoint = z.object({lat: z.number(), lng: z.number(), method: z.literal("parcel_stored_lat_lon")}); export const jsonValue = z.unknown(); export const searchMatch = z.object({address: z.string().nullable(), analysis_point: searchPoint, county: z.string().nullable(), fields: z.record(z.string(), jsonValue), parcelnumb: z.string().nullable(), pvd_gisacre: z.number().nullable(), scity: z.string().nullable(), source: z.string(), state2: z.string().nullable(), szip5: z.string().nullable()}); export const searchRemark = z.object({candidates: z.number().int().gte(1).optional(), code: z.enum(["time_limit_reached", "source_unavailable", "candidates_not_checked", "field_unavailable"]), hint: z.string().optional(), matches: z.number().int().gte(1).optional(), message: z.string(), path: z.string().optional()}); export const searchResponse = z.object({billing: paidBilling.nullable().optional(), matches: z.array(searchMatch), next_cursor: z.string().nullable(), remarks: z.array(searchRemark), request_id: z.string()}); 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 }; /** * Find parcels matching all filters * * POST /v1/search */ export const createApiV1Search = async (args: CreateApiV1SearchArgs, options?: CreateApiV1SearchOptions) => { 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/search${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(searchResponse, await res.json()) };