import {z} from 'zod' export type GetApiShipmentsArgs = {page?: number | undefined, pageSize?: number | undefined, type?: string | undefined, status?: string | undefined}; export type GetApiShipmentsOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const shipmentStatusType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]); export const shipmentDirectionType = z.union([z.literal(0), z.literal(1)]); export const weightUnitType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]); export const weightValueDto = z.object({value: z.number().optional(), unit: weightUnitType.optional()}); export const moneyValueDto = z.object({value: z.number().optional(), currency: z.string().nullable().optional()}); export const shipmentSummaryDto = z.object({guid: z.string().nullable().optional(), type: z.string().nullable().optional(), number: z.string().nullable().optional(), shipperName: z.string().nullable().optional(), consigneeName: z.string().nullable().optional(), status: shipmentStatusType.optional(), direction: shipmentDirectionType.optional(), originPortCode: z.string().nullable().optional(), destinationPortCode: z.string().nullable().optional(), estimatedArrivalDate: z.string().nullable().optional(), actualArrivalDate: z.string().nullable().optional(), totalPieces: z.number().int().nullable().optional(), totalWeight: weightValueDto.optional(), totalValue: moneyValueDto.optional()}); export const shipmentListResponse = z.object({shipments: z.array(shipmentSummaryDto).nullable().optional(), totalCount: z.number().int().optional(), page: z.number().int().optional(), pageSize: z.number().int().optional(), totalPages: z.number().int().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 }; /** * Get a list of shipments * * GET /shipments */ export const getApiShipments = async (args: GetApiShipmentsArgs, options: GetApiShipmentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/shipments${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(shipmentListResponse, await res.json()) };