import {z} from 'zod' export type OrderAttachmentPost = {filename?: string | undefined, printerType: 'LABEL_PRINTER' | 'DOCUMENT_PRINTER' | 'DOCUMENT_PRINTER_2_A6' | 'DOCUMENT_PRINTER_2_FOLIO', attachmentPlacement: 'INSIDE_PACKAGE' | 'OUTSIDE_PACKAGE', copyCount: number, printingType?: ('ORDER' | 'SHIPMENT') | undefined, fileKey?: string | undefined, mimeType?: string | undefined}; export type CreateApiOrdersOrderIdAttachmentsArgs = {orderId: number, body: OrderAttachmentPost}; export type CreateApiOrdersOrderIdAttachmentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({link: z.string().optional()}); export const orderAttachmentGet = z.object({filename: z.string().optional(), printerType: z.enum(["LABEL_PRINTER", "DOCUMENT_PRINTER", "DOCUMENT_PRINTER_2_A6", "DOCUMENT_PRINTER_2_FOLIO"]), attachmentPlacement: z.enum(["INSIDE_PACKAGE", "OUTSIDE_PACKAGE"]), copyCount: z.number().int().gte(1), printingType: z.enum(["ORDER", "SHIPMENT"]).optional(), fileKey: z.string().optional(), mimeType: z.string().optional(), id: z.number().int().optional(), url: link.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 }; /** * Stores the order attachment information * * POST /orders/{orderId}/attachments */ export const createApiOrdersOrderIdAttachments = async (args: CreateApiOrdersOrderIdAttachmentsArgs, options?: CreateApiOrdersOrderIdAttachmentsOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.orderId))}/attachments${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(orderAttachmentGet, await res.json()) };