import {z} from 'zod' export type PatchApiV1PrintJobsIdFilamentSourcesArgs = {id: string, body: {position_basis?: 'source' | undefined, slots: Array<{position: number, material_id: string}>, organizationId?: string | undefined}}; export type PatchApiV1PrintJobsIdFilamentSourcesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiV1PrintJobsIdFilamentSourcesResponse = z.object({success: z.boolean().optional(), job: z.object({id: z.number().optional(), captured_filaments: z.object({printer_id: z.unknown(), captured_at: z.string().optional(), slots: z.array(z.object({position: z.number().optional(), material_id: z.unknown(), material_type: z.unknown(), color: z.unknown()})).optional(), source_assignments: z.array(z.object({position: z.number().optional(), material_id: z.unknown(), material_type: z.unknown(), color: z.unknown()})).optional(), manual_assignment: z.boolean().optional(), position_basis: z.enum(["slot", "source"]).optional()}).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 }; /** * Manually assign filaments to a print job * * PATCH /v1/print-jobs/{id}/filament-sources */ export const patchApiV1PrintJobsIdFilamentSources = async (args: PatchApiV1PrintJobsIdFilamentSourcesArgs, options?: PatchApiV1PrintJobsIdFilamentSourcesOptions) => { const {baseUrl = "https://api.printhiv3d.com", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/print-jobs/${encodeURIComponent(String(args.id))}/filament-sources${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV1PrintJobsIdFilamentSourcesResponse, await res.json()) };