import {z} from 'zod' export type GetApiApiV1ExternalUploadBatchesArgs = {limit?: number | undefined}; export type GetApiApiV1ExternalUploadBatchesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const uploadBatchCreatedEntity = z.object({entityType: z.string(), entityId: z.string(), documentIds: z.array(z.string()), isNew: z.boolean().optional()}); export const uploadBatch = z.object({id: z.string(), label: z.string().nullable().optional(), note: z.string().nullable().optional(), entryType: z.enum(["01", "11", "export", "in_bond", "ca_b3", "ca_clvs", "ca_type_f", "null"]).nullable().optional(), filingCountry: z.string().nullable().optional(), status: z.enum(["pending", "processing", "completed", "failed"]), branchId: z.string().nullable().optional(), documentCount: z.number().int(), sourceFileCount: z.number().int().optional(), duplicateFileCount: z.number().int().optional(), createdEntities: z.array(uploadBatchCreatedEntity), unusedDocumentIds: z.array(z.string()), error: z.string().nullable().optional(), processedAt: z.string().nullable().optional(), createdAt: z.string(), externalShipmentRefId: z.string().nullable().optional(), tmsSource: z.string().nullable().optional()}); export const getApiApiV1ExternalUploadBatchesResponse = z.object({success: z.boolean().optional(), data: z.object({batches: z.array(uploadBatch).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 }; /** * List upload batches * * GET /api/v1/external/upload-batches */ export const getApiApiV1ExternalUploadBatches = async (args: GetApiApiV1ExternalUploadBatchesArgs, options?: GetApiApiV1ExternalUploadBatchesOptions) => { const {baseUrl = "https://api.wove.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/external/upload-batches${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(getApiApiV1ExternalUploadBatchesResponse, await res.json()) };