import {z} from 'zod' export type CreateApiV1PrintJobsIdStopArgs = {id: number, body: {receiptId: string, startAttemptId: string}}; export type CreateApiV1PrintJobsIdStopOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1PrintJobsIdStopResponse = z.object({id: z.string(), job_id: z.number().int().gte(1), printer_id: z.string(), start_attempt_id: z.string(), claim_generation: z.string(), expected_device_task_id: z.string(), command_outcome: z.enum(["unknown", "acknowledged", "rejected", "not_delivered"]), confirmation: z.enum(["pending", "confirmed", "unconfirmed", "rejected", "already_ended", "unsupported"]), requested_at: z.string(), deadline_at: z.string(), confirmed_at: z.string().nullable(), abort_acknowledged: z.boolean().optional(), reason: z.enum(["printer_family_unsupported", "job_already_ended"]).nullable(), printerFamily: z.string().nullable(), jobStatus: z.string(), message: z.string().nullable()}); 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 }; /** * Request Stop for one exact active print attempt * * POST /v1/print-jobs/{id}/stop * * Also accepts auth: bearerAuth */ export const createApiV1PrintJobsIdStop = async (args: CreateApiV1PrintJobsIdStopArgs, options?: CreateApiV1PrintJobsIdStopOptions) => { const {baseUrl = "https://api.printhiv3d.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/print-jobs/${encodeURIComponent(String(args.id))}/stop${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(createApiV1PrintJobsIdStopResponse, await res.json()) };