import {z} from 'zod' export type GetApiOrdersRoutesArgs = {completed_at_date?: (string | null) | undefined, completed_at_from?: (string | null) | undefined, completed_at_to?: (string | null) | undefined, created_at_date?: (string | null) | undefined, created_at_from?: (string | null) | undefined, created_at_to?: (string | null) | undefined, worker_handle?: (string | null) | undefined, driver_handle?: (string | null) | undefined, ops_group_handle?: (string | null) | undefined, plan_mode?: ('on_demand' | 'scheduled' | 'manual' | 'null' | null) | undefined, planned_at_date?: (string | null) | undefined, planned_at_from?: (string | null) | undefined, planned_at_to?: (string | null) | undefined, region_filter?: (string | null) | undefined, route_metadata_filter?: (string | null) | undefined, sort_direction?: ('asc' | 'desc' | 'null' | null) | undefined, start_by_date?: (string | null) | undefined, start_by_from?: (string | null) | undefined, start_by_to?: (string | null) | undefined, started_at_date?: (string | null) | undefined, started_at_from?: (string | null) | undefined, started_at_to?: (string | null) | undefined, scheduled_start_at_from?: (string | null) | undefined, scheduled_start_at_to?: (string | null) | undefined, status?: (string | null) | undefined, track_mode?: ('full_shift' | 'on_shift' | 'pre_shift' | 'null' | null) | undefined, pagination_token?: (string | null) | undefined, search_term?: (string | null) | undefined, limit?: (number | null) | undefined, device_switch_mode: unknown, plan_id?: (string | null) | undefined, device_id?: (string | null) | undefined, device_metadata_filter?: (string | null) | undefined, route_handle?: (string | null) | undefined}; export type GetApiOrdersRoutesOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const locationGeometryResponse = z.object({type: z.string(), coordinates: z.array(z.unknown())}); export const driverLocation = z.object({geometry: locationGeometryResponse, bearing: z.number(), accuracy: z.number(), speed: z.number(), recorded_at: z.string()}); export const orderListEstimate = z.object({start_at: z.string().nullable().optional(), start_by: z.string().nullable().optional(), arrive_at: z.string().nullable().optional(), distance: z.number().int().nullable().optional(), duration: z.number().int().nullable().optional(), initial_arrive_at: z.string().nullable().optional(), current_worker_location: z.union([driverLocation, z.unknown()]).optional()}); export const routeListOrder = z.object({order_handle: z.string(), status: z.enum(["completed", "cancelled", "disabled", "ongoing", "unplanned", "assigned", "planned", "dispatched", "summarizing"]), planning_failure_reason: z.string().nullable().optional(), type: z.string().nullable().optional(), type_index: z.number().int().nullable().optional(), started_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), cancelled_at: z.string().nullable().optional(), expected_service_time: z.number().int().nullable().optional(), estimate: z.union([orderListEstimate, z.unknown()]).optional(), actual_service_time: z.number().int().nullable().optional(), tracked_service_time: z.number().int().nullable().optional(), distance: z.number().int().nullable().optional(), duration: z.number().int().nullable().optional()}); export const source = z.object({source: z.string().nullable().optional(), timestamp: z.string().nullable().optional(), "ip-address": z.string().nullable().optional(), device_id: z.string().nullable().optional(), driver_handle: z.string().nullable().optional(), user_id: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const region = z.object({country: z.string(), city: z.string().nullable().optional(), state: z.string().nullable().optional()}); export const routeListEstimate = z.object({start_by: z.string().nullable().optional(), distance: z.number().int(), duration: z.number().int()}); export const routeList = z.object({route_handle: z.string(), status: z.enum(["planned", "assigned", "tracking", "completed", "summarizing"]), version: z.number().int(), created_at: z.string(), embed_url: z.string(), orders: z.array(routeListOrder), created_by: z.union([source, z.unknown()]).optional(), ops_group_handle: z.string().optional(), track_mode: z.enum(["full_shift", "on_shift", "pre_shift", "null"]).nullable().optional(), plan_mode: z.enum(["on_demand", "scheduled", "manual", "null"]).nullable().optional(), started_at: z.string().nullable().optional(), assigned_at: z.string().nullable().optional(), planned_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), device_id: z.string().nullable().optional(), driver_handle: z.string().nullable().optional(), worker_handle: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), region: z.union([region, z.unknown()]).optional(), distance: z.number().int().nullable().optional(), duration: z.number().int().nullable().optional(), estimate: z.union([routeListEstimate, z.unknown()]).optional(), plan_id: z.string().nullable().optional(), device_switch_mode: z.unknown(), candidate_devices: z.array(z.string()).nullable().optional(), completion_source: z.string().nullable().optional(), scheduled_start_at: z.string().nullable().optional()}); export const getRoutesResponse = z.object({routes: z.array(routeList), pagination_token: z.string().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 Routes * * GET /orders/routes/ * * Also accepts auth: TokenAuth */ export const getApiOrdersRoutes = async (args: GetApiOrdersRoutesArgs, options?: GetApiOrdersRoutesOptions) => { 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 (args.completed_at_date != null) query.append("completed_at_date", String(args.completed_at_date)) if (args.completed_at_from != null) query.append("completed_at_from", String(args.completed_at_from)) if (args.completed_at_to != null) query.append("completed_at_to", String(args.completed_at_to)) if (args.created_at_date != null) query.append("created_at_date", String(args.created_at_date)) if (args.created_at_from != null) query.append("created_at_from", String(args.created_at_from)) if (args.created_at_to != null) query.append("created_at_to", String(args.created_at_to)) if (args.worker_handle != null) query.append("worker_handle", String(args.worker_handle)) if (args.driver_handle != null) query.append("driver_handle", String(args.driver_handle)) if (args.ops_group_handle != null) query.append("ops_group_handle", String(args.ops_group_handle)) if (args.plan_mode != null) query.append("plan_mode", String(args.plan_mode)) if (args.planned_at_date != null) query.append("planned_at_date", String(args.planned_at_date)) if (args.planned_at_from != null) query.append("planned_at_from", String(args.planned_at_from)) if (args.planned_at_to != null) query.append("planned_at_to", String(args.planned_at_to)) if (args.region_filter != null) query.append("region_filter", String(args.region_filter)) if (args.route_metadata_filter != null) query.append("route_metadata_filter", String(args.route_metadata_filter)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.start_by_date != null) query.append("start_by_date", String(args.start_by_date)) if (args.start_by_from != null) query.append("start_by_from", String(args.start_by_from)) if (args.start_by_to != null) query.append("start_by_to", String(args.start_by_to)) if (args.started_at_date != null) query.append("started_at_date", String(args.started_at_date)) if (args.started_at_from != null) query.append("started_at_from", String(args.started_at_from)) if (args.started_at_to != null) query.append("started_at_to", String(args.started_at_to)) if (args.scheduled_start_at_from != null) query.append("scheduled_start_at_from", String(args.scheduled_start_at_from)) if (args.scheduled_start_at_to != null) query.append("scheduled_start_at_to", String(args.scheduled_start_at_to)) if (args.status != null) query.append("status", String(args.status)) if (args.track_mode != null) query.append("track_mode", String(args.track_mode)) if (args.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.search_term != null) query.append("search_term", String(args.search_term)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.device_switch_mode != null) query.append("device_switch_mode", String(args.device_switch_mode)) if (args.plan_id != null) query.append("plan_id", String(args.plan_id)) if (args.device_id != null) query.append("device_id", String(args.device_id)) if (args.device_metadata_filter != null) query.append("device_metadata_filter", String(args.device_metadata_filter)) if (args.route_handle != null) query.append("route_handle", String(args.route_handle)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/routes/${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(getRoutesResponse, await res.json()) };