import {z} from 'zod' export type LaneScoreLaneJsonRequest = {lane_id?: string | undefined, origin: string, destination: string, equipment_type: 'UNKNOWN' | 'VAN' | 'REEFER' | 'FLATBED', origin_country_code: 'UNKNOWN' | 'USA' | 'CAN' | 'MEX', destination_country_code: 'UNKNOWN' | 'USA' | 'CAN' | 'MEX'}; export type LaneScoreJsonRequest = {start_date: string, end_date: string, lanes: Array}; export type CreateApiV2TruckloadCapacitySonarLaneScoresArgs = {'x-api-version'?: string | undefined, body: LaneScoreJsonRequest}; export type CreateApiV2TruckloadCapacitySonarLaneScoresOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const laneScoreLaneJsonResponse = z.object({lane_id: z.string().optional(), start_date: z.string().optional(), end_date: z.string().optional(), origin: z.string().optional(), origin_market: z.string().optional(), origin_country_code: z.string().optional(), destination: z.string().optional(), destination_market: z.string().optional(), destination_country_code: z.string().optional(), equipment_type: z.string().optional(), lane_score: z.number().int().optional(), capacity_indicator: z.string().optional()}); export const laneScoreLaneJsonRequest = z.object({lane_id: z.string().optional(), origin: z.string().min(1), destination: z.string().min(1), equipment_type: z.enum(["UNKNOWN", "VAN", "REEFER", "FLATBED"]), origin_country_code: z.enum(["UNKNOWN", "USA", "CAN", "MEX"]), destination_country_code: z.enum(["UNKNOWN", "USA", "CAN", "MEX"])}); export const laneScoreJsonError = z.object({lane: laneScoreLaneJsonRequest.optional(), error: z.string().optional()}); export const laneScoreJsonResponse = z.object({lanes: z.array(laneScoreLaneJsonResponse), errors: z.array(laneScoreJsonError)}); 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 }; /** * SONAR Lane Scores * * POST /v2/truckload/capacity/sonar_lane_scores */ export const createApiV2TruckloadCapacitySonarLaneScores = async (args: CreateApiV2TruckloadCapacitySonarLaneScoresArgs, options: CreateApiV2TruckloadCapacitySonarLaneScoresOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/truckload/capacity/sonar_lane_scores${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(laneScoreJsonResponse, await res.json()) };