import {z} from 'zod' export type UpdateApiV1DrivingPeriodsIdArgs = {id: number, body: {driver_id?: number | undefined, notes?: string | undefined}}; export type UpdateApiV1DrivingPeriodsIdOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV1DrivingPeriodsIdResponse = z.object({driving_period: z.object({id: z.number().int().optional(), start_time: z.string().optional(), end_time: z.unknown(), status: z.string().optional(), type: z.string().optional(), annotation_status: z.unknown(), notes: z.unknown(), duration: z.number().optional(), start_kilometers: z.number().optional(), end_kilometers: z.unknown(), source: z.unknown(), driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), username: z.string().optional(), driver_company_id: z.string().optional()}).optional(), vehicle: z.object({id: z.number().int().optional(), number: z.string().optional(), year: z.string().optional(), make: z.string().optional(), model: z.string().optional(), vin: z.string().optional(), metric_units: z.boolean().optional()}).optional(), origin: z.string().optional(), origin_lat: z.number().int().optional(), origin_lon: z.number().int().optional(), destination_lat: z.unknown(), destination_lon: z.unknown(), destination: z.string().optional(), distance: z.unknown(), start_hvb_state_of_charge: z.unknown(), end_hvb_state_of_charge: z.unknown(), start_hvb_lifetime_energy_output: z.unknown(), end_hvb_lifetime_energy_output: z.unknown()}).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 }; /** * Modify a Driving Period * * PUT /v1/driving_periods/{id} */ export const updateApiV1DrivingPeriodsId = async (args: UpdateApiV1DrivingPeriodsIdArgs, options?: UpdateApiV1DrivingPeriodsIdOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/driving_periods/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiV1DrivingPeriodsIdResponse, await res.json()) };