import {z} from 'zod' export type GetApiV1IftaTripsArgs = {jurisdictions?: Array | undefined, 'vehicle_ids[]'?: Array | undefined, start_date?: string | undefined, end_date?: string | undefined, fuel_type?: string | undefined, per_page?: number | undefined, page_no?: number | undefined, 'X-Time-Zone'?: string | undefined, 'X-Metric-Units'?: boolean | undefined, 'X-User-Id'?: number | undefined}; export type GetApiV1IftaTripsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1IftaTripsResponse = z.object({ifta_trips: z.array(z.object({ifta_trip: z.object({id: z.number().int().optional(), date: z.string().optional(), jurisdiction: z.string().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(), start_odometer: z.number().optional(), end_odometer: z.number().optional(), calibrated_start_odometer: z.number().optional(), calibrated_end_odometer: z.number().optional(), start_lat: z.number().int().optional(), start_lon: z.number().int().optional(), end_lat: z.number().int().optional(), end_lon: z.number().int().optional(), distance: z.number().optional(), time_zone: z.string().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 of company vehicle's IFTA trips * * GET /v1/ifta/trips */ export const getApiV1IftaTrips = async (args: GetApiV1IftaTripsArgs, options?: GetApiV1IftaTripsOptions) => { 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.jurisdictions ?? []) query.append("jurisdictions", String(item)) for (const item of args['vehicle_ids[]'] ?? []) query.append("vehicle_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.fuel_type != null) query.append("fuel_type", String(args.fuel_type)) 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['X-Time-Zone'] != null) headers.set("X-Time-Zone", String(args['X-Time-Zone'])) if (args['X-Metric-Units'] != null) headers.set("X-Metric-Units", String(args['X-Metric-Units'])) if (args['X-User-Id'] != null) headers.set("X-User-Id", String(args['X-User-Id'])) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ifta/trips${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(getApiV1IftaTripsResponse, await res.json()) };