import {z} from 'zod' export type CreateApiV1JobsJobIdSubtasksArgs = {jobId: string, body: {tasks: Array<{task_type: 'empty_bed_detection' | 'temperature_check' | 'gcode_command' | 'ai_detection' | 'capture_frame' | 'cooldown_check' | 'printer_ready_check' | 'file_upload' | 'start_print' | 'bed_clear' | 'wait_for_temperature' | 'reheat' | 'webhook_signal' | 'mqtt_signal' | 'prime_nozzle' | 'verify_bed_clear' | 'post_complete_bed_detection' | 'bed_occupancy_verification' | 'bed_occupancy_reveal_calibration' | 'vision_guided_bed_clear', phase: 'pre' | 'in_flight' | 'post', is_required?: boolean | undefined, max_retries?: number | undefined, timeout_ms?: number | undefined, parameters?: Record | undefined, schedule_interval_ms?: number | undefined}>}}; export type CreateApiV1JobsJobIdSubtasksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1JobsJobIdSubtasksResponse = z.object({subtasks: z.array(z.object({id: z.string().optional(), task_type: z.string().optional(), phase: z.string().optional(), status: z.string().optional()})).optional()}); 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 sub-tasks for a print job * * POST /v1/jobs/{jobId}/subtasks */ export const createApiV1JobsJobIdSubtasks = async (args: CreateApiV1JobsJobIdSubtasksArgs, options?: CreateApiV1JobsJobIdSubtasksOptions) => { const {baseUrl = "https://api.printhiv3d.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/jobs/${encodeURIComponent(String(args.jobId))}/subtasks${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(createApiV1JobsJobIdSubtasksResponse, await res.json()) };