import {z} from 'zod' export type GetApiV1PlansPlanIdArgs = {planId: string}; export type GetApiV1PlansPlanIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const planDigestTotals = z.object({routes: z.number().int(), stops: z.number().int(), orders: z.number().int(), unplanned: z.number().int(), distanceMeters: z.number().nullable(), durationSeconds: z.number().nullable()}); export const planDigestIssue = z.object({code: z.enum(["ROUTE_NOT_CALCULATED", "ORDER_INPUTS_NOT_CAPTURED"]), routeId: z.string().nullable().optional(), orderId: z.string().nullable().optional(), message: z.string()}); export const planDigestRoute = z.object({routeId: z.string(), label: z.string().nullable(), vehicleId: z.string().nullable(), stops: z.number().int(), orders: z.number().int(), start: z.string().nullable(), end: z.string().nullable(), warnings: z.array(z.enum(["ROUTE_NOT_CALCULATED", "ORDER_INPUTS_NOT_CAPTURED"]))}); export const planDigest = z.object({planId: z.string(), revision: z.number().int(), contentHash: z.string(), isClean: z.boolean(), name: z.string(), source: z.enum(["orders", "routes", "snapshot"]), startDate: z.string().nullable(), endDate: z.string().nullable(), timezoneId: z.string().nullable(), zoneIds: z.array(z.string()), totals: planDigestTotals, issues: z.array(planDigestIssue), issueCount: z.number().int(), routes: z.array(planDigestRoute), unplannedOrderIds: z.array(z.string()), unplannedCount: z.number().int()}); 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 Dispatch Plan * * GET /v1/plans/{planId} */ export const getApiV1PlansPlanId = async (args: GetApiV1PlansPlanIdArgs, options?: GetApiV1PlansPlanIdOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/plans/${encodeURIComponent(String(args.planId))}${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(planDigest, await res.json()) };