import {z} from 'zod' export type GetApiOrdersPlansArgs = {fulfillment_date?: (string | null) | undefined, ops_group_handle?: (string | null) | undefined, pagination_token?: (string | null) | undefined, status?: ('failed' | 'completed' | 'planning' | 'null' | null) | undefined, sort_direction?: ('desc' | 'asc' | 'null' | null) | undefined, include_summary?: (boolean | null) | undefined, limit?: (number | null) | undefined}; export type GetApiOrdersPlansOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const planListRoute = z.object({route_handle: z.string(), status: z.enum(["planned", "assigned", "tracking", "completed", "summarizing"])}); export const orderObjectMini = z.object({order_handle: z.string(), status: z.enum(["completed", "cancelled", "disabled", "ongoing", "unplanned", "assigned", "planned", "dispatched", "summarizing"]), type: z.string().nullable().optional(), type_index: z.number().int().nullable().optional(), plan_mode: z.string().nullable().optional(), planning_failure_reason: z.string().nullable().optional()}); export const planSummaryOrders = z.object({total: z.number().int(), planned: z.number().int(), unplanned: z.number().int(), assigned: z.number().int(), completed: z.number().int(), cancelled: z.number().int()}); export const planSummaryEstimate = z.object({distance: z.number().int(), duration: z.number().int()}); export const planSummary = z.object({orders: planSummaryOrders, estimate: planSummaryEstimate, distance: z.number().int(), duration: z.number().int(), routes: z.number().int(), drivers: z.number().int()}); export const planListResponse = z.object({plan_id: z.string(), sequence_number: z.number().int(), routes: z.array(planListRoute).optional(), unplanned_orders: z.array(orderObjectMini).optional(), status: z.enum(["failed", "completed", "planning"]), started_at: z.string(), created_at: z.string(), ops_group_handle: z.string(), fulfillment_date: z.string(), failure_reason: z.string().optional(), summary: planSummary}); export const getPlansResponse = z.object({plans: z.array(planListResponse), 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 Plans * * GET /orders/plans * * Also accepts auth: TokenAuth */ export const getApiOrdersPlans = async (args: GetApiOrdersPlansArgs, options?: GetApiOrdersPlansOptions) => { 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.fulfillment_date != null) query.append("fulfillment_date", String(args.fulfillment_date)) if (args.ops_group_handle != null) query.append("ops_group_handle", String(args.ops_group_handle)) if (args.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.status != null) query.append("status", String(args.status)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.include_summary != null) query.append("include_summary", String(args.include_summary)) if (args.limit != null) query.append("limit", String(args.limit)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/plans${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(getPlansResponse, await res.json()) };