import {z} from 'zod' export type GetApiSkynetTripIdArgs = {id: string, key: unknown}; export type GetApiSkynetTripIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commonStatus = z.string(); export const commonMessage = z.string(); export const tripStop = z.object({name: z.string().optional(), meta_data: z.object({}).optional(), geofence_id: z.string().optional()}); export const trackMetaDataSkynet = z.object({}); export const dtoTrackLocation = z.object({accuracy: z.number().optional(), altitude: z.number().optional(), bearing: z.number().optional(), location: z.object({lat: z.number().optional(), lon: z.number().optional()}).optional(), meta_data: trackMetaDataSkynet.optional(), speed: z.number().optional(), timestamp: z.number().int().optional(), battery_level: z.number().int().optional(), tracking_mode: z.string().optional()}); export const trip = z.object({id: z.string().optional(), asset_id: z.string().optional(), state: z.string().optional(), name: z.string().optional(), description: z.string().optional(), meta_data: z.object({}).optional(), attributes: z.object({}).optional(), started_at: z.number().int().optional(), ended_at: z.number().int().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional(), stops: z.array(tripStop).optional(), route: z.array(dtoTrackLocation).optional()}); export const getApiSkynetTripIdResponse = z.object({status: commonStatus.optional(), message: commonMessage.optional(), data: z.object({trip: trip.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 }; /** * Get a single trip * * GET /skynet/trip/{id} */ export const getApiSkynetTripId = async (args: GetApiSkynetTripIdArgs, options?: GetApiSkynetTripIdOptions) => { const {baseUrl = "https://api.nextbillion.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/skynet/trip/${encodeURIComponent(String(args.id))}${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(getApiSkynetTripIdResponse, await res.json()) };