import {z} from 'zod' export type GetApiV1FreightVisibilityVehicleLocationsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1FreightVisibilityVehicleLocationsResponse = z.object({data: z.object({vehicle_details: z.array(z.object({start_time: z.string().optional(), end_time: z.string().optional(), load_id: z.unknown(), load_details: z.unknown(), total_distance: z.unknown(), initial_stop: z.unknown(), final_stop: z.unknown(), vehicle_location: z.object({lat: z.number().int().optional(), lon: z.number().int().optional(), located_at: z.string().optional(), description: z.string().optional()}).optional(), current_driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), username: z.unknown(), email: z.string().optional(), driver_company_id: z.unknown(), status: z.string().optional(), role: z.string().optional()}).optional(), vehicle_id: z.string().optional(), vehicle_name: z.string().optional(), company_id: z.string().optional(), dot_ids: z.array(z.object({})).optional(), trailer_id: z.unknown(), tracking_subscription_id: z.number().int().optional()})).optional()}).optional(), pagination: z.object({per_page: z.number().int().optional(), page_no: z.number().int().optional(), total: z.number().int().optional()}).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 }; /** * Fetch the location of all subscribed vehicles * * GET /v1/freight_visibility/vehicle_locations */ export const getApiV1FreightVisibilityVehicleLocations = async (options?: GetApiV1FreightVisibilityVehicleLocationsOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/freight_visibility/vehicle_locations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1FreightVisibilityVehicleLocationsResponse, await res.json()) };