import {z} from 'zod' export type GetApiV2OrdersDocumentsArgs = {order_ids?: Array | undefined, types?: (Array<'RETURN_LABEL' | 'CUSTOMER_INVOICE' | 'DELIVERY_SLIP' | 'SHIPMENT_DELIVERY_SLIP' | 'CHANNEL_SPECIFIC'>) | undefined, channel_types?: Array | undefined, updated_from?: string | undefined, page_token?: string | undefined, limit?: number | undefined}; export type GetApiV2OrdersDocumentsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const orderDocumentResponse = z.object({channel_type: z.string().optional(), file_format: z.string(), id: z.string(), name: z.string(), size_in_bytes: z.number().int().gte(0), type: z.enum(["RETURN_LABEL", "CUSTOMER_INVOICE", "DELIVERY_SLIP", "SHIPMENT_DELIVERY_SLIP", "CHANNEL_SPECIFIC"]), updated_at: z.string(), order_id: z.string()}); export const orderDocumentsPageResponse = z.object({next_page_token: z.string().min(1).optional(), previous_page_token: z.string().min(1).optional(), documents: z.array(orderDocumentResponse)}); 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 }; /** * v2-listOrderDocuments - List documents * * GET /v2/orders/documents */ export const getApiV2OrdersDocuments = async (args: GetApiV2OrdersDocumentsArgs, options?: GetApiV2OrdersDocumentsOptions) => { const {baseUrl = "https://miraklconnect.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.order_ids ?? []) query.append("order_ids", String(item)) for (const item of args.types ?? []) query.append("types", String(item)) for (const item of args.channel_types ?? []) query.append("channel_types", String(item)) if (args.updated_from != null) query.append("updated_from", String(args.updated_from)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/orders/documents${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(orderDocumentsPageResponse, await res.json()) };