import {z} from 'zod' export type GetApiV1CameraConnectionsArgs = {'vehicle_ids[]'?: Array | undefined, 'driver_ids[]'?: Array | undefined, start_date?: string | undefined, end_date?: string | undefined, type?: string | undefined, status?: string | undefined, per_page?: number | undefined, page_no?: number | undefined, sort_field?: string | undefined, sort_direction?: string | undefined}; export type GetApiV1CameraConnectionsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1CameraConnectionsResponse = z.object({camera_connections: z.array(z.object({camera_connection: z.object({id: z.number().int().optional(), offline_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), type: z.string().optional(), duration: z.number().optional(), cam_type: z.string().optional(), cam_serial_num: z.string().optional(), sw_pkg_version: z.number().int().optional(), status: z.string().optional(), vehicle: z.object({id: z.number().int().optional(), number: z.string().optional()}).optional(), current_driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), driver_company_id: z.string().optional()}).optional(), previous_driver: z.unknown(), next_driver: z.unknown(), dispatcher: z.unknown()}).optional()})).optional(), per_page: z.number().int().optional(), page_no: z.number().int().optional(), total: z.number().int().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 camera connection events * * GET /v1/camera_connections */ export const getApiV1CameraConnections = async (args: GetApiV1CameraConnectionsArgs, options?: GetApiV1CameraConnectionsOptions) => { 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['vehicle_ids[]'] ?? []) query.append("vehicle_ids[]", String(item)) for (const item of args['driver_ids[]'] ?? []) query.append("driver_ids[]", String(item)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) 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 (args.sort_field != null) query.append("sort_field", String(args.sort_field)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/camera_connections${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(getApiV1CameraConnectionsResponse, await res.json()) };