import {z} from 'zod' export type GetApiOrdersOrderHandleCandidateWorkersArgs = {order_handle: string, type?: (string | null) | undefined, type_index?: (number | null) | undefined, fulfillment_attempt?: (number | null) | undefined, profile_filter?: (string | null) | undefined, limit?: (number | null) | undefined, skip_already_assigned_worker?: (boolean | null) | undefined, allow_ongoing_order?: (boolean | null) | undefined, destination_geometry?: (string | null) | undefined}; export type GetApiOrdersOrderHandleCandidateWorkersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const locationGeometryResponse = z.object({type: z.string(), coordinates: z.array(z.unknown())}); export const candidateWorkerRoute = z.object({route_handle: z.string(), index: z.number().int()}); export const candidateWorker = z.object({worker_handle: z.string(), profile: z.record(z.string(), z.unknown()).nullable().optional(), order_start_location: z.union([locationGeometryResponse, z.unknown()]).optional(), route: z.union([candidateWorkerRoute, z.unknown()]).optional(), duration: z.number().int().nullable().optional(), distance: z.number().int().nullable().optional()}); export const candidateWorkersResponse = z.object({candidate_workers: z.array(candidateWorker)}); 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 Candidate Workers * * GET /orders/{order_handle}/candidate_workers */ export const getApiOrdersOrderHandleCandidateWorkers = async (args: GetApiOrdersOrderHandleCandidateWorkersArgs, options?: GetApiOrdersOrderHandleCandidateWorkersOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.type_index != null) query.append("type_index", String(args.type_index)) if (args.fulfillment_attempt != null) query.append("fulfillment_attempt", String(args.fulfillment_attempt)) if (args.profile_filter != null) query.append("profile_filter", String(args.profile_filter)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip_already_assigned_worker != null) query.append("skip_already_assigned_worker", String(args.skip_already_assigned_worker)) if (args.allow_ongoing_order != null) query.append("allow_ongoing_order", String(args.allow_ongoing_order)) if (args.destination_geometry != null) query.append("destination_geometry", String(args.destination_geometry)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.order_handle))}/candidate_workers${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(candidateWorkersResponse, await res.json()) };