import {z} from 'zod' export type InnoshipCommonEnumDocumentType = 'ConsignmentNote' | 'ExpressWaybill' | 'CustomsDeclaration' | 'Invoice' | 'WorldEase' | 'ProofOfDelivery'; export type InnoshipCommonEnumFileType = 'Pdf' | 'Zpl' | 'Html'; export type InnoshipCommonEnumFileFormat = 'A4' | 'A6' | 'A5'; export type GetApiApiOrderDownloadDocumentCourierCourierIdAwbAwbArgs = {courierId: number, awb: string, document?: InnoshipCommonEnumDocumentType | undefined, type?: InnoshipCommonEnumFileType | undefined, format?: InnoshipCommonEnumFileFormat | undefined, useFile?: boolean | undefined, 'api-version': string}; export type GetApiApiOrderDownloadDocumentCourierCourierIdAwbAwbOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const innoshipApiServiceContractOrderDocumentResponse = z.object({courier: z.number().int().optional(), contents: z.string().nullable().optional(), documents: z.array(z.string()).nullable().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 }; /** * Gets requested document for the specified courier awb number * * GET /api/Order/DownloadDocument/courier/{courierId}/awb/{awb} */ export const getApiApiOrderDownloadDocumentCourierCourierIdAwbAwb = async (args: GetApiApiOrderDownloadDocumentCourierCourierIdAwbAwbArgs, options: GetApiApiOrderDownloadDocumentCourierCourierIdAwbAwbOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.document != null) query.append("document", String(args.document)) if (args.type != null) query.append("type", String(args.type)) if (args.format != null) query.append("format", String(args.format)) if (args.useFile != null) query.append("useFile", String(args.useFile)) if (args['api-version'] != null) query.append("api-version", String(args['api-version'])) if (args['api-version'] != null) headers.set("api-version", String(args['api-version'])) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/Order/DownloadDocument/courier/${encodeURIComponent(String(args.courierId))}/awb/${encodeURIComponent(String(args.awb))}${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(innoshipApiServiceContractOrderDocumentResponse, await res.json()) };