import {z} from 'zod' export type MissionStatus = 'scheduled' | 'canceled' | 'in-progress' | 'recurring' | 'complete'; export type DetailLevel = 'low' | 'medium' | 'high'; export type SensorRequirement = 'hi-def' | 'any' | 'no'; export type LidarRequirement = 'yes' | 'no' | 'any'; export type ThermalRequirement = '640x480' | 'any' | 'no'; export type SensorRequirements = {camera: SensorRequirement, lidar: LidarRequirement, thermal: ThermalRequirement}; export type Recurrence = 'daily' | 'weekly' | 'monthly' | 'yearly'; export type CreateMissionRequest = {name: string, description: string, status: MissionStatus, detailLevel: DetailLevel, sensors: SensorRequirements, scheduledFor: number, estimatedCompletionTime: number, dueDate?: number | undefined, recurrence?: Recurrence | undefined, estimatedCost: number, siteGeometries?: (Array<'point' | 'line' | 'area'>) | undefined, siteIds?: Array | undefined, clusterIds?: Array | undefined}; export type CreateApiMissionsArgs = {'Idempotency-Key'?: string | undefined, body: CreateMissionRequest}; export type CreateApiMissionsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const missionStatus = z.enum(["scheduled", "canceled", "in-progress", "recurring", "complete"]); export const detailLevel = z.enum(["low", "medium", "high"]); export const sensorRequirement = z.enum(["hi-def", "any", "no"]); export const lidarRequirement = z.enum(["yes", "no", "any"]); export const thermalRequirement = z.enum(["640x480", "any", "no"]); export const sensorRequirements = z.object({camera: sensorRequirement, lidar: lidarRequirement, thermal: thermalRequirement}); export const recurrence = z.enum(["daily", "weekly", "monthly", "yearly"]); export const mission = z.object({id: z.string(), organizationId: z.string(), name: z.string(), description: z.string(), status: missionStatus, detailLevel: detailLevel, sensors: sensorRequirements, createdAt: z.number(), scheduledFor: z.number(), estimatedCompletionTime: z.number(), dueDate: z.number().optional(), recurrence: recurrence.optional(), estimatedCost: z.number(), siteGeometries: z.array(z.enum(["point", "line", "area"])).optional(), siteCount: z.number().int(), bounds: z.array(z.number()).nullable().optional(), deletedAt: z.number().nullable(), updatedAt: z.number(), analysisEnabled: z.boolean()}); export const createApiMissionsResponse = z.object({data: mission, transactionId: 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 }; /** * Create mission * * POST /missions * * Also accepts auth: ApiKeyAuth */ export const createApiMissions = async (args: CreateApiMissionsArgs, options: CreateApiMissionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = 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'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/missions${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(createApiMissionsResponse, await res.json()) };