import {z} from 'zod' export type DistanceOp = {destinations: Array, max_cost_usd?: (string | null) | undefined, mode?: ('driving' | 'straightline') | undefined, op: 'distance', origins: Array, units?: ('miles' | 'km') | undefined}; export type NearestOp = {filters?: ({[key: string]: number | string | Array} | null) | undefined, max_cost_usd?: (string | null) | undefined, mode?: ('driving' | 'straightline') | undefined, n?: number | undefined, op: 'nearest', origin: string, radius_m?: (number | null) | undefined, return?: ('candidates' | 'count') | undefined, set: string}; export type ScreenOp = {anchors: Array, max_cost_usd?: (string | null) | undefined, max_minutes: number, min_minutes?: (number | null) | undefined, op: 'screen', origins: Array}; export type LaborShedOp = {estimate?: boolean | undefined, include?: Array<'demographics'> | undefined, max_cost_usd?: (string | null) | undefined, minutes: number, op: 'labor_shed', origin: string}; export type CreateApiV1ProximityArgs = {body: DistanceOp | NearestOp | ScreenOp | LaborShedOp}; export type CreateApiV1ProximityOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1ProximityResponse = z.unknown(); 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 }; /** * Compute Endpoint * * POST /v1/proximity */ export const createApiV1Proximity = async (args: CreateApiV1ProximityArgs, options?: CreateApiV1ProximityOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/v1/proximity${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(createApiV1ProximityResponse, await res.json()) };