import {z} from 'zod' export type AreaOfInterest = {level?: ('city' | 'county' | 'state' | 'region' | 'national' | null) | undefined, names?: Array | undefined}; export type Callback = {email?: (string | null) | undefined, webhook_url?: (string | null) | undefined}; export type Constraints = {acceptable_substitutes?: Array | undefined, already_have?: Array | undefined, must_not_be?: Array | undefined}; export type ExampleLocation = {address?: (string | null) | undefined, claimed_value?: (string | null) | undefined, lat?: (number | null) | undefined, lng?: (number | null) | undefined, note?: (string | null) | undefined, polygon?: ({[key: string]: unknown} | null) | undefined}; export type ExpectedVolume = {cadence?: ('one_off' | 'recurring' | null) | undefined, geography?: (string | null) | undefined, locations?: (number | null) | undefined}; export type Freshness = {changes_how_often?: (string | null) | undefined, max_staleness?: (string | null) | undefined}; export type KnownSource = {name?: (string | null) | undefined, note?: (string | null) | undefined, url?: (string | null) | undefined, why?: (string | null) | undefined}; export type KnownSources = {excluded?: Array | undefined, suggested?: Array | undefined}; export type OutputPreference = {buckets_ok?: (boolean | null) | undefined, units?: (string | null) | undefined, value_kind?: ('boolean' | 'category' | 'number' | 'distance' | 'geometry' | null) | undefined}; export type RequestedField = {ask: string}; export type FieldRequestPayload = {area_of_interest?: (AreaOfInterest | null) | undefined, callback?: (Callback | null) | undefined, constraints?: (Constraints | null) | undefined, context_blob?: (string | null) | undefined, deadline?: (string | null) | undefined, decision_threshold?: (string | null) | undefined, description: string, example_locations: Array, expected_volume?: (ExpectedVolume | null) | undefined, freshness?: (Freshness | null) | undefined, idempotency_key?: (string | null) | undefined, known_sources?: (KnownSources | null) | undefined, output_preference?: (OutputPreference | null) | undefined, requested_fields?: Array | undefined, use_case?: (string | null) | undefined}; export type CreateApiV1FieldRequestsArgs = {body: FieldRequestPayload}; export type CreateApiV1FieldRequestsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1FieldRequestsResponse = z.record(z.string(), 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 }; /** * Create Field Request * * POST /v1/field-requests */ export const createApiV1FieldRequests = async (args: CreateApiV1FieldRequestsArgs, options?: CreateApiV1FieldRequestsOptions) => { 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/field-requests${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(createApiV1FieldRequestsResponse, await res.json()) };