import {z} from 'zod' export type CreateApiV1FuelPurchasesArgs = {source?: string | undefined, body: {vehicle_id: number, driver_id: number, purchased_at: string, jurisdiction: string, fuel_type: string, total_cost?: string | undefined, currency?: string | undefined, fuel: string, fuel_unit: string, ref_no?: string | undefined, vendor?: string | undefined, odometer?: number | undefined, odometer_unit?: string | undefined, receipt?: Array | undefined, location?: string | undefined}}; export type CreateApiV1FuelPurchasesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1FuelPurchasesResponse = z.object({fuel_purchase: z.object({id: z.number().int().optional(), offline_id: z.string().optional(), purchased_at: z.string().optional(), jurisdiction: z.string().optional(), fuel_type: z.string().optional(), ref_no: z.unknown(), vendor: z.unknown(), total_cost: z.number().int().optional(), currency: z.string().optional(), fuel: z.number().int().optional(), fuel_unit: z.string().optional(), odometer: z.unknown(), odometer_unit: z.string().optional(), receipt_upload_url: z.unknown(), receipt_filename: z.unknown(), location: z.string().optional(), uploader: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), role: z.string().optional(), deactivated_at: z.unknown()}).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(), driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), username: z.string().optional(), email: z.string().optional(), driver_company_id: z.unknown(), status: z.string().optional(), role: z.string().optional()}).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 }; /** * Create a new fuel purchase transaction * * POST /v1/fuel_purchases */ export const createApiV1FuelPurchases = async (args: CreateApiV1FuelPurchasesArgs, options?: CreateApiV1FuelPurchasesOptions) => { 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 (args.source != null) query.append("source", String(args.source)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fuel_purchases${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiV1FuelPurchasesResponse, await res.json()) };