import {z} from 'zod' export type GetApiOrdersOrderHandleWebhooksArgs = {order_handle: string, value?: (string | null) | undefined, from_time?: (string | null) | undefined, to_time?: (string | null) | undefined, fulfillment_attempt?: (number | null) | undefined, pagination_token?: (string | null) | undefined, limit?: (number | null) | undefined, sort_direction?: ('desc' | 'asc' | 'null' | null) | undefined, debug_level?: (string | null) | undefined}; export type GetApiOrdersOrderHandleWebhooksOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pointLocationGeometryResponse = z.object({type: z.literal("Point"), coordinates: z.array(z.number())}); export const orderAssignedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("assigned"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), assigned_at: z.string()}); export const orderStartedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("started"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const orderOnDeviceGeofenceCheckedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("on_device_geofence_checked"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), is_inside: z.boolean(), location_accuracy: z.number().int().nullable().optional()}); export const orderFirstEtaWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("first_eta"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), remaining_duration: z.number().int(), remaining_distance: z.number().int(), estimated_route_id: z.string().nullable().optional()}); export const orderEtaWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("eta"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), remaining_duration: z.number().int(), remaining_distance: z.number().int(), estimated_route_id: z.string().nullable().optional(), driver_location: z.union([pointLocationGeometryResponse, z.unknown()]).optional(), location_recorded_at: z.string().nullable().optional(), in_outage: z.boolean().nullable().optional(), scheduled_at: z.string().nullable().optional()}); export const orderEtaChangeWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("eta_change"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), remaining_duration: z.number().int(), remaining_distance: z.number().int(), estimated_route_id: z.string().nullable().optional(), earliest_possible_arrival_time: z.string().nullable().optional(), change_reason: z.unknown()}); export const orderNMinutesAwayWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("n_minutes_away"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), remaining_duration: z.number().int(), remaining_distance: z.number().int(), estimated_route_id: z.string().nullable().optional(), remaining_minutes: z.number().int()}); export const orderDeviceSwitchedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("device_switched"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), previous_device_id: z.string(), new_device_id: z.string()}); export const orderDisabledWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("disabled"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const orderEnabledWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("enabled"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const orderUpdatedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("updated"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const orderDelayedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("delayed"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), arrive_at: z.string(), scheduled_at: z.string().nullable().optional(), initial_eta: z.string().nullable().optional()}); export const orderRiskUpdatedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("risk_updated"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), risks: z.record(z.string(), z.unknown())}); export const webhookArrivalData = z.object({location: pointLocationGeometryResponse, recorded_at: z.string(), location_accuracy: z.number().nullable().optional()}); export const orderArrivedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("arrived"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), place_handle: z.string().nullable().optional(), scheduled_at: z.string().nullable().optional(), initial_eta: z.string().nullable().optional(), arrival: webhookArrivalData}); export const webhookExitData = z.object({location: z.union([pointLocationGeometryResponse, z.unknown()]).optional(), recorded_at: z.string(), location_accuracy: z.number().nullable().optional()}); export const orderExitedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("exited"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), place_handle: z.string().nullable().optional(), scheduled_at: z.string().nullable().optional(), initial_eta: z.string().nullable().optional(), exit: webhookExitData, reason: z.unknown()}); export const orderCompletedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("completed"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), distance: z.number().int().nullable().optional(), duration: z.number().int(), deviation: z.number().int().nullable().optional(), actual_service_time: z.number().int().nullable().optional()}); export const orderCancelledWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("cancelled"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), distance: z.number().int().nullable().optional(), duration: z.number().int(), cancellation_reason: z.string().nullable().optional()}); export const breakTimeTuple = z.object({start_time: z.string(), end_time: z.string()}); export const billingBreakTimeModelFine = z.object({id: z.string(), ht_val: z.union([breakTimeTuple, z.unknown()]).optional(), worker_val: z.union([breakTimeTuple, z.unknown()]).optional(), facility_val: z.union([breakTimeTuple, z.unknown()]).optional()}); export const billingConstituents = z.object({travel_time: z.number().int(), service_time: z.number().int(), over_time: z.number().int(), arrival_delay: z.number().int(), post_shift_tracking_time: z.number().int(), break_time: z.array(billingBreakTimeModelFine).nullable().optional()}); export const orderBillingDetailsUpdatedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("billing_details_updated"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), billing_details: billingConstituents, billable_hours: z.number().int()}); export const imageAttachmentResponse = z.object({type: z.literal("image"), url: z.string(), caption: z.string().nullable().optional(), attachment_id: z.string().nullable().optional()}); export const noteAttachmentResponse = z.object({type: z.literal("note"), data: z.string(), attachment_id: z.string().nullable().optional()}); export const signatureAttachmentResponse = z.object({type: z.literal("signature"), url: z.string(), attachment_id: z.string().nullable().optional()}); export const orderTaskResponse = z.object({label: z.string(), task_definition_id: z.string().nullable().optional(), description: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), priority: z.enum(["none", "low", "medium", "high", "null"]).nullable().optional(), task_id: z.string(), status: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), attachments: z.array(z.union([imageAttachmentResponse, noteAttachmentResponse, signatureAttachmentResponse])).nullable().optional()}); export const orderTaskCreatedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("task_created"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), task: orderTaskResponse, scheduled_at: z.string().nullable().optional()}); export const orderTaskUpdatedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("task_updated"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), task: orderTaskResponse, scheduled_at: z.string().nullable().optional()}); export const orderTaskCompletedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("task_completed"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), task: orderTaskResponse, scheduled_at: z.string().nullable().optional()}); export const orderWorkerDepartedWebhookEventData = z.object({order_handle: z.string(), route_handle: z.string(), value: z.literal("worker_departed"), status: z.string(), share_url: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), departed_at: z.string(), start_by: z.string().nullable().optional(), departure_location: z.union([pointLocationGeometryResponse, z.unknown()]).optional()}); export const orderWebhookEvent = z.object({account_id: z.string(), recorded_at: z.string(), created_at: z.string(), device_id: z.string(), worker_handle: z.string().nullable().optional(), driver_handle: z.string().nullable().optional(), type: z.literal("order"), location: z.union([pointLocationGeometryResponse, z.unknown()]).optional(), version: z.string(), data: z.union([orderAssignedWebhookEventData, orderStartedWebhookEventData, orderOnDeviceGeofenceCheckedWebhookEventData, orderFirstEtaWebhookEventData, orderEtaWebhookEventData, orderEtaChangeWebhookEventData, orderNMinutesAwayWebhookEventData, orderDeviceSwitchedWebhookEventData, orderDisabledWebhookEventData, orderEnabledWebhookEventData, orderUpdatedWebhookEventData, orderDelayedWebhookEventData, orderRiskUpdatedWebhookEventData, orderArrivedWebhookEventData, orderExitedWebhookEventData, orderCompletedWebhookEventData, orderCancelledWebhookEventData, orderBillingDetailsUpdatedWebhookEventData, orderTaskCreatedWebhookEventData, orderTaskUpdatedWebhookEventData, orderTaskCompletedWebhookEventData, orderWorkerDepartedWebhookEventData]), delivery_http_status: z.number().int().nullable().optional(), retry_count: z.number().int().nullable().optional(), delivery_http_response_text: z.string().nullable().optional()}); export const getOrderWebhooksResponse = z.object({webhooks: z.array(orderWebhookEvent).nullable().optional(), 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 Order Webhooks * * GET /orders/{order_handle}/webhooks * * Also accepts auth: TokenAuth */ export const getApiOrdersOrderHandleWebhooks = async (args: GetApiOrdersOrderHandleWebhooksArgs, options?: GetApiOrdersOrderHandleWebhooksOptions) => { 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.value != null) query.append("value", String(args.value)) if (args.from_time != null) query.append("from_time", String(args.from_time)) if (args.to_time != null) query.append("to_time", String(args.to_time)) if (args.fulfillment_attempt != null) query.append("fulfillment_attempt", String(args.fulfillment_attempt)) if (args.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.debug_level != null) query.append("debug_level", String(args.debug_level)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.order_handle))}/webhooks${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(getOrderWebhooksResponse, await res.json()) };