import {z} from 'zod' export type GetApiOrdersEstimatedRoutesEstimatedRouteIdArgs = {estimated_route_id: string}; export type GetApiOrdersEstimatedRoutesEstimatedRouteIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const polyline = z.object({type: z.string(), coordinates: z.array(z.array(z.unknown()))}); export const locationGeometryResponse = z.object({type: z.string(), coordinates: z.array(z.unknown())}); export const getEstimatedRouteResponse = z.object({estimated_route_id: z.string(), polyline: z.union([polyline, z.unknown()]).optional(), start_location: z.union([locationGeometryResponse, z.unknown()]).optional(), end_location: z.union([locationGeometryResponse, z.unknown()]).optional(), distance: z.number().int().nullable().optional(), duration: z.number().int().nullable().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 estimated route polyline * * GET /orders/estimated-routes/{estimated_route_id} * * Also accepts auth: TokenAuth */ export const getApiOrdersEstimatedRoutesEstimatedRouteId = async (args: GetApiOrdersEstimatedRoutesEstimatedRouteIdArgs, options?: GetApiOrdersEstimatedRoutesEstimatedRouteIdOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/estimated-routes/${encodeURIComponent(String(args.estimated_route_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(getEstimatedRouteResponse, await res.json()) };