import {z} from 'zod' export type GetApiChargingEntriesIdArgs = {id: string}; export type GetApiChargingEntriesIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const geolocation = z.object({gps_device: z.string().nullable().optional(), original_vendor: z.string().nullable().optional(), exception_distance_in_mi: z.number().nullable().optional(), exception_distance_in_km: z.number().nullable().optional()}).nullable(); export const id = z.number().int().gte(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const commentableType = z.enum(["Contact", "FleetcorCard", "FuelEntry", "InventoryJournalEntry", "Issue", "Part", "PurchaseOrder", "ServiceEntry", "ServiceReminder", "SubmittedInspectionItem", "Vehicle", "VehicleAssignment", "Vendor", "WexCard", "WorkOrder"]); export const comment = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), commentable_type: commentableType, commentable_id: id, user_id: id, title: z.string().optional(), comment: z.string(), user_full_name: z.string(), user_image_url: z.string().nullable(), rich_content: z.object({}).nullable(), html_content: z.string().nullable(), with_mentions: z.boolean()}).nullable(); export const documentableType = z.enum(["ServiceEntry", "FuelEntry", "Issue", "WorkOrder", "ExpenseEntry", "Vehicle"]); export const document = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), name: z.string(), description: z.string(), documentable_id: id, documentable_type: documentableType, file_name: z.string(), file_mime_type: z.string(), file_size: z.number(), file_url: z.string(), full_file_url: z.string()}).nullable(); export const imageableType = z.enum(["ExpenseEntry", "WorkOrder", "Equipment", "IssueResolution", "ServiceEntry", "Vendor", "ServiceProgram", "Issue", "Contact", "Part", "Vehicle", "FuelEntry", "SubmittedInspectionItem"]); export const image = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), name: z.string().nullable(), description: z.string().nullable(), imageable_id: id, imageable_type: imageableType, file_name: z.string(), file_mime_type: z.string(), file_size: z.number(), file_url: z.string(), full_url: z.string()}).nullable(); export const chargingEntry = z.object({additional_fees_cents: z.number().int().nullable(), approved_at: z.string().nullable(), approved_by_id: z.number().int().nullable(), cost_per_hr: z.string().nullable(), cost_per_km: z.string().nullable(), cost_per_mi: z.string().nullable(), discount_cents: z.number().int().nullable(), duration_seconds: z.number().int().gte(0), ended_at: z.string().nullable(), geolocation: geolocation, km_per_kwh: z.string(), mi_per_kwh: z.string(), personal: z.boolean(), price_per_kwh_cents: z.string().nullable(), reference: z.string().nullable(), started_at: z.string(), status: z.enum(["in_progress", "target_reached", "ready_for_review", "manually_completed", "approved", "auto_approved", "unknown"]), total_amount_cents: z.number().int().nullable(), total_kwh: z.string(), usage_in_hr: z.string().nullable(), usage_in_km: z.string().nullable(), usage_in_mi: z.string().nullable(), vehicle_id: z.number().int().gte(1), vendor_id: z.number().int().gte(1).nullable(), comments_count: z.number().int(), documents_count: z.number().int(), images_count: z.number().int(), comments: z.array(comment).optional(), documents: z.array(document).optional(), images: z.array(image).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 }; /** * Retrieve Charging Entry * * GET /charging_entries/{id} */ export const getApiChargingEntriesId = async (args: GetApiChargingEntriesIdArgs, options?: GetApiChargingEntriesIdOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/charging_entries/${encodeURIComponent(String(args.id))}${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(chargingEntry, await res.json()) };