import {z} from 'zod' export type UpdateApiV1ModelFilesIdPrintSourceArgs = {id: string}; export type UpdateApiV1ModelFilesIdPrintSourceOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV1ModelFilesIdPrintSourceResponse = z.union([z.object({status: z.literal("reading"), sourceVersionId: z.string(), filename: z.string(), bytes: z.number().int().gte(1)}), z.object({status: z.literal("ready"), sourceVersionId: z.string(), filename: z.string(), bytes: z.number().int().gte(1), plates: z.array(z.object({plateNumber: z.number().int().gte(1).lte(256), objects: z.array(z.object({name: z.string().nullable()})), usedSlots: z.array(z.number().int().gte(0).lte(1023)), usage: z.enum(["resolved", "unresolved"])})), slots: z.array(z.object({sourceSlot: z.number().int().gte(0).lte(1023), material: z.string().nullable(), color: z.string().nullable(), profile: z.string().nullable()})), machine: z.object({model: z.string(), nozzleDiameterMm: z.number(), bed: z.string(), compatibleMachines: z.array(z.string()).optional(), compatibleMachinesDeclared: z.boolean().optional()}).nullable(), printerModel: z.string().nullable()}), z.object({status: z.literal("failed"), sourceVersionId: z.string(), filename: z.string(), bytes: z.number().int().gte(1), error: z.object({code: z.enum(["invalid_archive", "unsafe_path", "unsafe_xml", "resource_limit", "invalid_project", "toolpath_only", "processing_timeout", "storage_object_changed", "inspection_interrupted", "upload_interrupted"]), message: 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 }; /** * Make a library project file an inspected print source * * PUT /v1/model-files/{id}/print-source * * Also accepts auth: bearerAuth */ export const updateApiV1ModelFilesIdPrintSource = async (args: UpdateApiV1ModelFilesIdPrintSourceArgs, options?: UpdateApiV1ModelFilesIdPrintSourceOptions) => { 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/model-files/${encodeURIComponent(String(args.id))}/print-source${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiV1ModelFilesIdPrintSourceResponse, await res.json()) };