import {z} from 'zod' export type RailEquipmentIdentifier = {type?: ('CONTAINER_ID' | 'TRAILER_ID' | 'RAIL_CAR_ID') | undefined, value?: string | undefined}; export type RailShipmentIdentifier = {type?: ('BILL_OF_LADING' | 'WAYBILL') | undefined, value?: string | undefined}; export type ZonedDateTimeWindow = {endDateTime?: string | undefined, localTimeZoneIdentifier?: string | undefined, startDateTime?: string | undefined}; export type GeoCoordinates = {latitude?: number | undefined, longitude?: number | undefined}; export type RailAddress = {pointName?: string | undefined, state?: string | undefined}; export type RailLocation = {address?: RailAddress | undefined, id?: string | undefined}; export type RailShipmentStop = {appointmentWindow?: ZonedDateTimeWindow | undefined, geoCoordinates?: GeoCoordinates | undefined, id?: string | undefined, location?: RailLocation | undefined, stopName?: string | undefined, stopNumber?: number | undefined, type?: ('UNKNOWN' | 'ORIGIN' | 'DESTINATION' | 'TRANSFER' | 'OTHER' | 'PORT_OF_LOADING' | 'TRANSSHIPMENT_PORT' | 'PORT_OF_DISCHARGE' | 'PICKUP' | 'DELIVERY' | 'RETURN' | 'WAREHOUSE' | 'HUB' | 'AIRPORT') | undefined}; export type RailShipment = {equipmentIdentifiers?: Array | undefined, id?: number | undefined, masterShipmentId?: string | undefined, railLegId?: string | undefined, shipmentIdentifiers?: Array | undefined, shipmentStops?: Array | undefined, trackingStartDateTime?: string | undefined}; export type CreateApiApiV4RailShipmentsArgs = {body: RailShipment}; export type CreateApiApiV4RailShipmentsOptions = {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 railEquipmentIdentifier = z.object({type: z.enum(["CONTAINER_ID", "TRAILER_ID", "RAIL_CAR_ID"]).optional(), value: z.string().optional()}); export const railShipmentIdentifier = z.object({type: z.enum(["BILL_OF_LADING", "WAYBILL"]).optional(), value: z.string().optional()}); export const zonedDateTimeWindow = z.object({endDateTime: z.string().optional(), localTimeZoneIdentifier: z.string().optional(), startDateTime: z.string().optional()}); export const geoCoordinates = z.object({latitude: z.number().optional(), longitude: z.number().optional()}); export const railAddress = z.object({pointName: z.string().optional(), state: z.string().optional()}); export const railLocation = z.object({address: railAddress.optional(), id: z.string().optional()}); export const railShipmentStop = z.object({appointmentWindow: zonedDateTimeWindow.optional(), geoCoordinates: geoCoordinates.optional(), id: z.string().optional(), location: railLocation.optional(), stopName: z.string().optional(), stopNumber: z.number().int().optional(), type: z.enum(["UNKNOWN", "ORIGIN", "DESTINATION", "TRANSFER", "OTHER", "PORT_OF_LOADING", "TRANSSHIPMENT_PORT", "PORT_OF_DISCHARGE", "PICKUP", "DELIVERY", "RETURN", "WAREHOUSE", "HUB", "AIRPORT"]).optional()}); export const railShipment = z.object({equipmentIdentifiers: z.array(railEquipmentIdentifier).optional(), id: z.number().int().optional(), masterShipmentId: z.string().optional(), railLegId: z.string().optional(), shipmentIdentifiers: z.array(railShipmentIdentifier).optional(), shipmentStops: z.array(railShipmentStop).optional(), trackingStartDateTime: z.string().optional()}); export const railShipmentConfirmation = z.object({infoMessages: z.array(message).optional(), shipment: railShipment.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 }; /** * Initialize a shipment for tracking. * * POST /api/v4/rail/shipments */ export const createApiApiV4RailShipments = async (args: CreateApiApiV4RailShipmentsArgs, options?: CreateApiApiV4RailShipmentsOptions) => { 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/rail/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(railShipmentConfirmation, await res.json()) };