import {z} from 'zod' export type GetApiPrintJobsStatisticsArgs = {page?: number | undefined, page_size?: number | undefined, created_after?: string | undefined, created_before?: string | undefined, modified_after?: string | undefined, modified_before?: string | undefined, status?: string | undefined, id?: string | undefined, ordering?: string | undefined}; export type GetApiPrintJobsStatisticsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPrintJobsStatisticsResponse = z.object({count: z.number().optional(), status: z.enum(["CREATED", "REJECTED", "UNPAID", "PAYMENT_IN_PROGRESS", "PRODUCTION_READY", "PRODUCTION_DELAYED", "IN_PRODUCTION", "ERROR", "SHIPPED", "CANCELED"]).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 }; /** * Retrieve the number of Print-Jobs in each status * * GET /print-jobs/statistics/ */ export const getApiPrintJobsStatistics = async (args: GetApiPrintJobsStatisticsArgs, options?: GetApiPrintJobsStatisticsOptions) => { const {baseUrl = "https://api.lulu.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.modified_after != null) query.append("modified_after", String(args.modified_after)) if (args.modified_before != null) query.append("modified_before", String(args.modified_before)) if (args.status != null) query.append("status", String(args.status)) if (args.id != null) query.append("id", String(args.id)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/print-jobs/statistics/${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(getApiPrintJobsStatisticsResponse, await res.json()) };