import {z} from 'zod' export type GetApiV1DrivingPeriodsArgs = {'driver_ids[]'?: Array | undefined, 'X-Time-Zone'?: string | undefined, 'vehicle_ids[]'?: Array | undefined, type?: string | undefined, status?: string | undefined, annotation_status?: string | undefined, assigned_to_driver?: boolean | undefined, start_date?: string | undefined, end_date?: string | undefined, updated_after?: string | undefined, per_page?: number | undefined, page_no?: number | undefined}; export type GetApiV1DrivingPeriodsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1DrivingPeriodsResponse = z.object({driving_periods: z.array(z.object({driving_period: z.object({id: z.number().int().optional(), start_time: z.string().optional(), end_time: z.string().optional(), status: z.unknown(), type: z.string().optional(), annotation_status: z.unknown(), notes: z.unknown(), duration: z.number().int().optional(), start_kilometers: z.number().int().optional(), end_kilometers: z.number().int().optional(), start_hvb_state_of_charge: z.number().optional(), end_hvb_state_of_charge: z.number().optional(), start_hvb_lifetime_energy_output: z.number().optional(), end_hvb_lifetime_energy_output: z.number().optional(), driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), username: z.unknown(), email: z.string().optional(), driver_company_id: z.unknown(), status: z.string().optional(), role: z.string().optional()}).optional(), vehicle: z.object({id: z.number().int().optional(), number: z.string().optional(), year: z.unknown(), make: z.unknown(), model: z.unknown(), vin: z.unknown(), metric_units: z.boolean().optional()}).optional(), origin: z.string().optional(), origin_lat: z.number().optional(), origin_lon: z.number().optional(), destination_lat: z.number().optional(), destination_lon: z.number().optional(), destination: z.string().optional(), distance: z.string().optional(), source: z.number().int().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 }; /** * Fetch a list of the vehicle's driving periods * * GET /v1/driving_periods */ export const getApiV1DrivingPeriods = async (args: GetApiV1DrivingPeriodsArgs, options?: GetApiV1DrivingPeriodsOptions) => { 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['driver_ids[]'] ?? []) query.append("driver_ids[]", String(item)) if (args['X-Time-Zone'] != null) headers.set("X-Time-Zone", String(args['X-Time-Zone'])) for (const item of args['vehicle_ids[]'] ?? []) query.append("vehicle_ids[]", String(item)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (args.annotation_status != null) query.append("annotation_status", String(args.annotation_status)) if (args.assigned_to_driver != null) query.append("assigned_to_driver", String(args.assigned_to_driver)) 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.updated_after != null) query.append("updated_after", String(args.updated_after)) 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(/\/+$/, '')}/v1/driving_periods${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(getApiV1DrivingPeriodsResponse, await res.json()) };