import {z} from 'zod' export type BatchLabelsCreate = {shipments: Array<{easyship_shipment_id: unknown, courier_service_id?: string | undefined}>}; export type CreateApi202409BatchesLabelsArgs = {body: BatchLabelsCreate}; export type CreateApi202409BatchesLabelsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const batchSingle = z.object({id: z.unknown().optional(), state: z.unknown().optional(), type: z.unknown().optional(), started_at: z.unknown().optional(), finished_at: z.unknown().optional(), created_at: z.unknown().optional(), updated_at: z.unknown().optional(), total_count: z.number().int().optional(), failed_count: z.number().int().optional(), processed_count: z.number().int().optional(), processing_count: z.number().int().optional(), created_count: z.number().int().optional()}); export const meta = z.object({request_id: z.string().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 }; /** * Create a Batch of Labels * * POST /2024-09/batches/labels */ export const createApi202409BatchesLabels = async (args: CreateApi202409BatchesLabelsArgs, options?: CreateApi202409BatchesLabelsOptions) => { const {baseUrl = "https://public-api.easyship.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/2024-09/batches/labels${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(batchSingle, await res.json()) };