import {z} from 'zod' export type OrderExportRequest = {worker_handle?: (string | null) | undefined, place_handle?: (string | null) | undefined, search_term?: (string | null) | undefined, ops_group_handle?: (string | null) | undefined, order_metadata_filter?: (string | null) | undefined, risk_status?: (string | null) | undefined, region_filter?: (string | null) | undefined, status?: (string | null) | undefined, device_id?: (string | null) | undefined, destination_geohash?: (string | null) | undefined, plan_mode: unknown, plan_id?: (string | null) | undefined, route_handle?: (string | null) | undefined, track_mode: unknown, device_switch_mode: unknown, created_at_date?: (string | null) | undefined, created_at_from?: (string | null) | undefined, created_at_to?: (string | null) | undefined, planned_at_date?: (string | null) | undefined, planned_at_from?: (string | null) | undefined, planned_at_to?: (string | null) | undefined, scheduled_at_date?: (string | null) | undefined, scheduled_at_date_without_tz?: (string | null) | undefined, completed_at_date_without_tz?: (string | null) | undefined, scheduled_at_from?: (string | null) | undefined, scheduled_at_to?: (string | null) | undefined, scheduled_at_without_tz_from?: (string | null) | undefined, scheduled_at_without_tz_to?: (string | null) | undefined, cancelled_at_date?: (string | null) | undefined, cancelled_at_from?: (string | null) | undefined, cancelled_at_to?: (string | null) | undefined, completed_at_date?: (string | null) | undefined, completed_at_from?: (string | null) | undefined, completed_at_to?: (string | null) | undefined, rescheduled_marked_at_date?: (string | null) | undefined, rescheduled_marked_at_from?: (string | null) | undefined, rescheduled_marked_at_to?: (string | null) | undefined, route_started_at_date?: (string | null) | undefined, route_started_at_from?: (string | null) | undefined, route_started_at_to?: (string | null) | undefined, completed_at_without_tz_from?: (string | null) | undefined, completed_at_without_tz_to?: (string | null) | undefined, created_at_without_tz_from?: (string | null) | undefined, created_at_without_tz_to?: (string | null) | undefined, created_at_date_without_tz?: (string | null) | undefined, driver_handle?: (string | null) | undefined, sort_direction: unknown, fulfillment_attempt?: (number | null) | undefined, aggregate?: (boolean | null) | undefined, limit?: (number | null) | undefined, pagination_token?: (string | null) | undefined, send_outage?: (boolean | null) | undefined, tracking_status?: ('tracked' | 'untracked' | 'null' | null) | undefined, filter_outage_category?: (string | null) | undefined, worker_profile_filter?: (unknown | null) | undefined, worker_inside_destination?: (boolean | null) | undefined, order_handle?: (string | null) | undefined, email_recipients?: Array | undefined}; export type CreateApiOrdersExportArgs = {body: OrderExportRequest}; export type CreateApiOrdersExportOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const orderExportResponse = z.object({job_id: z.string(), status: z.string(), message: z.string()}); 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 }; /** * Create an order export job * * POST /orders/export * * Also accepts auth: TokenAuth */ export const createApiOrdersExport = async (args: CreateApiOrdersExportArgs, options?: CreateApiOrdersExportOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/export${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(orderExportResponse, await res.json()) };