import {z} from 'zod' export type VinList = Array; export type CreateApiV1VinsArgs = {body: {vinList?: VinList | undefined}}; export type CreateApiV1VinsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const vins = z.object({commodityVin: z.string().optional(), carId: z.string().optional(), destinationRailNetworkLocationName: z.string().optional(), estimatedShipmentAvailabilityDate: z.string().optional(), estimatedShipmentAvailabilityTime: z.string().optional(), finalScheduledEventDescription: z.string().optional(), finalDestinationRailNetworkLocationName: z.string().optional(), lastEventDate: z.string().optional(), lastEventDescription: z.string().optional(), lastEventTime: z.string().optional(), lastEventRailNetworkLocationName: z.string().optional(), lastReportingSCAC: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), message: z.string().optional(), nextSCAC: z.string().optional(), nextScheduledEventDate: z.string().optional(), nextScheduledEventDescription: z.string().optional(), nextScheduledEventStateCode: z.string().optional(), nextScheduledEventStation333: z.string().optional(), nextScheduledEventTime: z.string().optional(), originRailNetworkLocationName: z.string().optional(), shipmentExceptionDescription: z.string().optional(), trainId: z.string().optional()}); export const createApiV1VinsResponse = z.object({elements: z.array(vins).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 }; /** * VINS - Returns tracing details for requested VINS, up to 300 at a time. * * POST /v1/vins */ export const createApiV1Vins = async (args: CreateApiV1VinsArgs, options?: CreateApiV1VinsOptions) => { const {baseUrl = "https://api.bnsf.com:6443", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/vins${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(createApiV1VinsResponse, await res.json()) };