import {z} from 'zod' export type GetApiTransportsTransportIdArgs = {transportId: number}; export type GetApiTransportsTransportIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressData = z.object({name: z.string().optional(), company: z.string().optional(), phone: z.string().optional(), email: z.string().optional(), zip: z.string().optional(), city: z.string(), state: z.string().optional(), countryIso2: z.string(), street: z.string(), street2: z.string().optional()}); export const transportPositionData = z.object({deliveryType: z.enum(["CONTAINER", "CARTON", "EUPAL", "EWPAL"]), containerType: z.string().optional(), quantity: z.number().int().gte(1), unitWeightKg: z.number().gt(0), unitLengthM: z.number().gt(0), unitWidthM: z.number().gt(0), unitHeightM: z.number().gt(0), contentDescription: z.string()}); export const transportGet = z.object({shipTo: addressData, sellerId: z.number().int(), pickupDate: z.string().optional(), deliveryDate: z.string().optional(), shipFrom: addressData, positions: z.array(transportPositionData), id: z.number().int().optional(), created: z.string().optional(), reference: z.string().optional(), exportedAt: z.string().optional(), status: z.enum(["CREATED", "TRANSMITTED"]).optional(), freightOrderIdentifier: z.string().optional(), requestId: 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 }; /** * Get details of transport inquiry by transportId * * GET /transports/{transportId} */ export const getApiTransportsTransportId = async (args: GetApiTransportsTransportIdArgs, options?: GetApiTransportsTransportIdOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/transports/${encodeURIComponent(String(args.transportId))}${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(transportGet, await res.json()) };