import {z} from 'zod' export type OceanAttribute = {name?: string | undefined, value?: string | undefined, values?: Array | undefined}; export type OceanEquipmentIdentifier = {type?: 'CONTAINER_ID' | undefined, value?: string | undefined}; export type OceanShipmentIdentifier = {type?: ('BILL_OF_LADING' | 'BOOKING_NUMBER') | undefined, value?: string | undefined}; export type OceanShipment = {attributes?: Array | undefined, carrierSCACCode?: string | undefined, equipmentIdentifier?: OceanEquipmentIdentifier | undefined, masterShipmentId?: string | undefined, oceanLegId?: string | undefined, shipmentId?: string | undefined, shipmentIdentifiers?: Array | undefined}; export type CreateApiApiV4OceanShipmentsArgs = {body: OceanShipment}; export type CreateApiApiV4OceanShipmentsOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const message = z.object({diagnostic: z.string().optional(), message: z.string().optional(), severity: z.enum(["ERROR", "WARNING", "INFO"]).optional(), source: z.enum(["SYSTEM", "CAPACITY_PROVIDER"]).optional()}); export const oceanAttribute = z.object({name: z.string().optional(), value: z.string().optional(), values: z.array(z.string()).optional()}); export const oceanEquipmentIdentifier = z.object({type: z.literal("CONTAINER_ID").optional(), value: z.string().optional()}); export const oceanShipmentIdentifier = z.object({type: z.enum(["BILL_OF_LADING", "BOOKING_NUMBER"]).optional(), value: z.string().optional()}); export const oceanShipment = z.object({attributes: z.array(oceanAttribute).optional(), carrierSCACCode: z.string().optional(), equipmentIdentifier: oceanEquipmentIdentifier.optional(), masterShipmentId: z.string().optional(), oceanLegId: z.string().optional(), shipmentId: z.string().optional(), shipmentIdentifiers: z.array(oceanShipmentIdentifier).optional()}); export const oceanShipmentConfirmation = z.object({infoMessages: z.array(message).optional(), shipment: oceanShipment.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 tracked ocean shipment from a container id or vessel id. * * POST /api/v4/ocean/shipments */ export const createApiApiV4OceanShipments = async (args: CreateApiApiV4OceanShipmentsArgs, options?: CreateApiApiV4OceanShipmentsOptions) => { const {baseUrl = "https://na12.api.project44.com", fetch: fetchFn = fetch, oAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oAuth2 !== undefined) headers.set('authorization', `Bearer ${oAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/ocean/shipments${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(oceanShipmentConfirmation, await res.json()) };