import {z} from 'zod' export type GetApiV2FormEntriesArgs = {'dispatch_ids[]'?: Array | undefined, 'driver_ids[]'?: Array | undefined, 'vehicle_ids[]'?: Array | undefined, min_created_at?: string | undefined, max_created_at?: string | undefined, per_page?: string | undefined, page_no?: string | undefined}; export type GetApiV2FormEntriesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2FormEntriesResponse = z.object({form_entries: z.array(z.object({form_entry: z.object({id: z.number().int().optional(), form_id: z.string().optional(), form_version: z.number().int().optional(), dispatch_id: z.number().int().optional(), vendor_dispatch_id: z.string().optional(), vendor_stop_id: z.number().int().optional(), driver_id: z.number().int().optional(), vehicle_id: z.unknown(), entry: z.object({bol: z.string().optional(), weight: z.number().int().optional(), scanned_bol_image: z.array(z.object({filename: z.string().optional(), mime_type: z.string().optional(), url: z.string().optional()})).optional(), customer_signature: z.object({filename: z.string().optional(), mime_type: z.string().optional(), url: z.string().optional()}).optional()}).optional(), lat: z.number().int().optional(), lon: z.number().int().optional(), bearing: z.number().int().optional(), odometer: z.number().int().optional(), speed: z.number().int().optional(), location: z.string().optional(), entered_at: z.string().optional(), created_at: z.string().optional(), rg_brg: z.number().int().optional(), rg_km: z.number().int().optional(), rg_match: z.boolean().optional()}).optional()})).optional(), pagination: z.object({per_page: z.number().int().optional(), page_no: z.number().int().optional(), total: z.number().int().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 the form entries (v2) * * GET /v2/form_entries */ export const getApiV2FormEntries = async (args: GetApiV2FormEntriesArgs, options?: GetApiV2FormEntriesOptions) => { 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['dispatch_ids[]'] ?? []) query.append("dispatch_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)) if (args.min_created_at != null) query.append("min_created_at", String(args.min_created_at)) if (args.max_created_at != null) query.append("max_created_at", String(args.max_created_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(/\/+$/, '')}/v2/form_entries${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(getApiV2FormEntriesResponse, await res.json()) };