import {z} from 'zod' export type GetApiV3DispatchesArgs = {'ids[]'?: Array | undefined, vendor_ids?: Array | undefined, 'driver_ids[]'?: Array | undefined, 'vehicle_ids[]'?: Array | undefined, statuses?: Array | undefined, include_native?: boolean | undefined, min_updated_at?: string | undefined, max_updated_at?: string | undefined, per_page?: number | undefined, page_no?: number | undefined}; export type GetApiV3DispatchesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3DispatchesResponse = z.union([z.object({dispatches: z.array(z.object({dispatch: z.object({id: z.number().int().optional(), additional_drops: z.unknown(), additional_pickups: z.unknown(), loaded_miles: z.unknown(), order_pieces: z.unknown(), order_volume: z.unknown(), order_weight: z.unknown(), product: z.unknown(), pickup_number: z.unknown(), status: z.string().optional(), status_updated_at: z.string().optional(), temperature_max: z.unknown(), temperature_min: z.unknown(), trailer: z.unknown(), auto_complete_forms: z.boolean().optional(), consignee_refno: z.unknown(), custom_field1: z.unknown(), vendor_id: z.string().optional(), dispatch_stops: z.array(z.object({dispatch_stop: z.object({id: z.number().int().optional(), bill_of_lading: z.unknown(), comments: z.unknown(), dispatch_location_id: z.number().int().optional(), driver_load: z.unknown(), early_date: z.string().optional(), form_ids: z.array(z.string()).optional(), late_date: z.string().optional(), number: z.number().int().optional(), pallets_in: z.unknown(), pallets_out: z.unknown(), pieces: z.unknown(), status: z.string().optional(), type: z.string().optional(), volume: z.unknown(), weight: z.unknown(), form_entry_ids: z.object({}).optional(), dispatch_trip_id: z.number().int().optional(), vendor_id: z.string().optional(), vendor_dispatch_location_id: z.string().optional()}).optional()})).optional(), dispatch_trips: z.array(z.object({dispatch_trip: z.object({id: z.number().int().optional(), status: z.string().optional(), status_updated_at: z.unknown(), loaded_miles: z.unknown(), vendor_id: z.string().optional(), driver_id: z.number().int().optional(), vehicle_id: z.number().int().optional()}).optional()})).optional()}).optional()})).optional(), pagination: z.object({per_page: z.number().int().optional(), page_no: z.number().int().optional(), total: z.number().int().optional()}).optional()}), z.object({dispatches: z.array(z.object({id: z.number().int().optional(), order_number: z.string().optional(), status: z.string().optional(), dispatch_team_driving_enabled_v1: z.boolean().optional(), dispatch_trips: z.array(z.object({id: z.number().int().optional(), driver_id: z.number().int().optional(), co_drivers: z.array(z.number().int()).optional(), vehicle_id: z.number().int().optional(), dispatch_stops: z.array(z.object({dispatch_stop: z.object({id: z.number().int().optional(), vendor_id: z.string().optional(), bill_of_lading: z.unknown(), comments: z.unknown(), driver_load: z.boolean().optional(), early_date: z.string().optional(), form_ids: z.array(z.string()).optional()}).optional()})).optional()})).optional()})).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 }; /** * List company dispatch records (v3) * * GET /v3/dispatches */ export const getApiV3Dispatches = async (args: GetApiV3DispatchesArgs, options?: GetApiV3DispatchesOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['ids[]'] ?? []) query.append("ids[]", String(item)) for (const item of args.vendor_ids ?? []) query.append("vendor_ids", String(item)) for (const item of args['driver_ids[]'] ?? []) query.append("driver_ids[]", String(item)) for (const item of args['vehicle_ids[]'] ?? []) query.append("vehicle_ids[]", String(item)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) if (args.include_native != null) query.append("include_native", String(args.include_native)) if (args.min_updated_at != null) query.append("min_updated_at", String(args.min_updated_at)) if (args.max_updated_at != null) query.append("max_updated_at", String(args.max_updated_at)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page_no != null) query.append("page_no", String(args.page_no)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v3/dispatches${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(getApiV3DispatchesResponse, await res.json()) };