import {z} from 'zod' export type VehicleDisplaySize = 'bike' | 'motorbike' | 'cargobike' | 'car' | 'suv' | 'pickup_truck' | 'van' | 'truck' | 'large_van' | 'extra_large_van'; export type VehicleInfo = {type?: (VehicleDisplaySize | null) | undefined, make?: (string | null) | undefined, model?: (string | null) | undefined, licenseplate?: (string | null) | undefined, color?: (string | null) | undefined, year?: (string | null) | undefined}; export type VehicleConfig = {weight?: (number | null) | undefined, height?: (number | null) | undefined, length?: (number | null) | undefined, width?: (number | null) | undefined, axlecount?: (number | null) | undefined, trailercount?: (number | null) | undefined, weightunit?: ('kg' | 'lbs' | null) | undefined, lengthunit?: ('meter' | 'in' | null) | undefined, maxstops?: (number | null) | undefined, maxdistance?: (number | null) | undefined, maxduration?: (number | null) | undefined, speedfactor?: (number | null) | undefined, capabilities?: (Array | null) | undefined, fixedcost?: (number | null) | undefined, costperunitdistance?: (number | null) | undefined, costperunittime?: (number | null) | undefined, vehicleprofile?: (string | null) | undefined, minordersperroute?: (number | null) | undefined, parkingtime?: (number | null) | undefined}; export type CreateVehicleInputSerializer = {name: string, enabled: boolean, externalidentifier?: (string | null) | undefined, vehicleinfo?: (VehicleInfo | null) | undefined, vehicleconfig?: (VehicleConfig | null) | undefined, courierids?: (Array | null) | undefined}; export type CreateApiV1VehicleArgs = {body: CreateVehicleInputSerializer}; export type CreateApiV1VehicleOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const vehicleDisplaySize = z.enum(["bike", "motorbike", "cargobike", "car", "suv", "pickup_truck", "van", "truck", "large_van", "extra_large_van"]); export const vehicleInfo = z.object({type: vehicleDisplaySize.nullable().optional(), make: z.string().nullable().optional(), model: z.string().nullable().optional(), licenseplate: z.string().nullable().optional(), color: z.string().nullable().optional(), year: z.string().nullable().optional()}); export const vehicleConfig = z.object({weight: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), length: z.number().int().nullable().optional(), width: z.number().int().nullable().optional(), axlecount: z.number().int().nullable().optional(), trailercount: z.number().int().nullable().optional(), weightunit: z.enum(["kg", "lbs"]).nullable().optional(), lengthunit: z.enum(["meter", "in"]).nullable().optional(), maxstops: z.number().int().nullable().optional(), maxdistance: z.number().int().nullable().optional(), maxduration: z.number().int().nullable().optional(), speedfactor: z.number().nullable().optional(), capabilities: z.array(z.string()).nullable().optional(), fixedcost: z.number().nullable().optional(), costperunitdistance: z.number().nullable().optional(), costperunittime: z.number().nullable().optional(), vehicleprofile: z.string().nullable().optional(), minordersperroute: z.number().int().nullable().optional(), parkingtime: z.number().nullable().optional()}); export const vehicleResponse = z.object({id: z.string(), name: z.string(), providerid: z.string(), externalidentifier: z.string(), enabled: z.boolean(), vehicleinfo: vehicleInfo, vehicleconfig: vehicleConfig, courierids: z.array(z.string())}); 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 vehicle * * POST /v1/vehicle */ export const createApiV1Vehicle = async (args: CreateApiV1VehicleArgs, options?: CreateApiV1VehicleOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", 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/vehicle${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(vehicleResponse, await res.json()) };