import {z} from 'zod' export type GetApiV3FulfillmentJobsArgs = {status?: string | undefined, manufacturer_id?: number | undefined, order_id?: number | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiV3FulfillmentJobsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const productionJob = z.object({id: z.number().int().optional(), order: z.object({id: z.number().int().optional(), number: z.string().optional(), status: z.string().optional(), client: z.string().nullable().optional(), expected_delivery_at: z.string().nullable().optional()}).optional(), line: z.object({id: z.number().int().optional(), name: z.string().optional(), preview: z.string().nullable().optional(), design_id: z.number().int().nullable().optional()}).optional(), manufacturer_id: z.number().int().nullable().optional(), manufacturer_name: z.string().nullable().optional(), kind: z.enum(["production", "rework"]).optional(), status: z.enum(["pending", "sent", "in_progress", "done", "cancelled"]).optional(), allowed_transitions: z.array(z.string()).optional(), quantity: z.number().int().optional(), stl_url: z.string().nullable().optional(), stl_status: z.enum(["ready", "pending", "failed"]).optional(), stl_error: z.string().nullable().optional(), tech_sheet: z.object({}).optional(), notes: z.string().nullable().optional(), sent_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), done_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const getApiV3FulfillmentJobsResponse = z.object({items: z.array(productionJob).optional(), total: z.number().int().optional(), page: z.number().int().optional(), limit: z.number().int().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 }; /** * The workshop queue * * GET /v3/fulfillment/jobs */ export const getApiV3FulfillmentJobs = async (args: GetApiV3FulfillmentJobsArgs, options?: GetApiV3FulfillmentJobsOptions) => { const {baseUrl = "https://api.rhinoartisan.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.manufacturer_id != null) query.append("manufacturer_id", String(args.manufacturer_id)) if (args.order_id != null) query.append("order_id", String(args.order_id)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) headers.set("apikey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/fulfillment/jobs${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(getApiV3FulfillmentJobsResponse, await res.json()) };