import {z} from 'zod' export type GetApiV1PrintJobsIdControlStateArgs = {id: number}; export type GetApiV1PrintJobsIdControlStateOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1PrintJobsIdControlStateResponse = z.object({jobId: z.number().int(), status: z.string(), printerId: z.string().nullable(), startAttemptId: z.string().nullable(), stopAvailability: z.enum(["ready", "not_ready", "not_active", "unsupported"]), canCancelWithoutStop: z.boolean(), reason: z.string().nullable(), printerFamily: z.string().nullable(), 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 }; /** * Read job state and the exact current Start Print attempt * * GET /v1/print-jobs/{id}/control-state * * Also accepts auth: bearerAuth */ export const getApiV1PrintJobsIdControlState = async (args: GetApiV1PrintJobsIdControlStateArgs, options?: GetApiV1PrintJobsIdControlStateOptions) => { const {baseUrl = "https://api.printhiv3d.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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))}/control-state${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1PrintJobsIdControlStateResponse, await res.json()) };