import {z} from 'zod' export type RefuelingType = 'transaction' | 'vehicle' | 'card'; export type TransactionDateType = 'TRANSACTION' | 'SETTLEMENT' | 'UPDATE'; export type ISO3166Country = 'ASC' | 'AND' | 'ARE' | 'AFG' | 'ATG' | 'AIA' | 'ALB' | 'ARM' | 'ANT' | 'AGO' | 'ATA' | 'ARG' | 'ASM' | 'AUT' | 'AUS' | 'ABW' | 'ALA' | 'AZE' | 'BIH' | 'BRB' | 'BGD' | 'BEL' | 'BFA' | 'BGR' | 'BHR' | 'BDI' | 'BEN' | 'BLM' | 'BMU' | 'BRN' | 'BOL' | 'BES' | 'BRA' | 'BHS' | 'BTN' | 'BUR' | 'BVT' | 'BWA' | 'BLR' | 'BLZ' | 'CAN' | 'CCK' | 'COD' | 'CAF' | 'COG' | 'CHE' | 'CIV' | 'COK' | 'CHL' | 'CMR' | 'CHN' | 'COL' | 'CPT' | 'CRI' | 'CSXX' | 'CUB' | 'CPV' | 'CUW' | 'CXR' | 'CYP' | 'CZE' | 'DEU' | 'DGA' | 'DJI' | 'DNK' | 'DMA' | 'DOM' | 'DZA' | 'ECU' | 'EST' | 'EGY' | 'ESH' | 'ERI' | 'ESP' | 'ETH' | 'FIN' | 'FJI' | 'FLK' | 'FSM' | 'FRO' | 'FRA' | 'FXX' | 'GAB' | 'GBR' | 'GRD' | 'GEO' | 'GUF' | 'GGY' | 'GHA' | 'GIB' | 'GRL' | 'GMB' | 'GIN' | 'GLP' | 'GNQ' | 'GRC' | 'SGS' | 'GTM' | 'GUM' | 'GNB' | 'GUY' | 'HKG' | 'HMD' | 'HND' | 'HRV' | 'HTI' | 'HUN' | 'IDN' | 'IRL' | 'ISR' | 'IMN' | 'IND' | 'IOT' | 'IRQ' | 'IRN' | 'ISL' | 'ITA' | 'JEY' | 'JAM' | 'JOR' | 'JPN' | 'KEN' | 'KGZ' | 'KHM' | 'KIR' | 'COM' | 'KNA' | 'PRK' | 'KOR' | 'KWT' | 'CYM' | 'KAZ' | 'LAO' | 'LBN' | 'LCA' | 'LIE' | 'LKA' | 'LBR' | 'LSO' | 'LTU' | 'LUX' | 'LVA' | 'LBY' | 'MAR' | 'MCO' | 'MDA' | 'MNE' | 'MAF' | 'MDG' | 'MHL' | 'MKD' | 'MLI' | 'MMR' | 'MNG' | 'MAC' | 'MNP' | 'MTQ' | 'MRT' | 'MSR' | 'MLT' | 'MUS' | 'MDV' | 'MWI' | 'MEX' | 'MYS' | 'MOZ' | 'NAM' | 'NCL' | 'NER' | 'NFK' | 'NGA' | 'NIC' | 'NLD' | 'NOR' | 'NPL' | 'NRU' | 'NTZ' | 'NIU' | 'NZL' | 'OMN' | 'PAN' | 'PER' | 'PYF' | 'PNG' | 'PHL' | 'PAK' | 'POL' | 'SPM' | 'PCN' | 'PRI' | 'PSE' | 'PRT' | 'PLW' | 'PRY' | 'QAT' | 'REU' | 'ROU' | 'ROM' | 'SRB' | 'RUS' | 'RWA' | 'SAU' | 'SLB' | 'SYC' | 'SDN' | 'SWE' | 'SGP' | 'SHN' | 'SVN' | 'SJM' | 'SVK' | 'SLE' | 'SMR' | 'SEN' | 'SOM' | 'SUR' | 'SSD' | 'STP' | 'SUN' | 'SLV' | 'SXM' | 'SYR' | 'SWZ' | 'TAA' | 'TCA' | 'TCD' | 'ATF' | 'TGO' | 'THA' | 'TJK' | 'TKL' | 'TLS' | 'TKM' | 'TUN' | 'TON' | 'TUR' | 'TTO' | 'TUV' | 'TWN' | 'TZA' | 'UKR' | 'UGA' | 'UMI' | 'USA' | 'URY' | 'UZB' | 'VAT' | 'VCT' | 'VEN' | 'VGB' | 'VIR' | 'VNM' | 'VUT' | 'WLF' | 'WSM' | 'XKX' | 'XXK' | 'QZZ' | 'YEM' | 'MYT' | 'YUCS' | 'ZAF' | 'ZMB' | 'ZRCD' | 'ZWE'; export type TransactionStatus = 'Authorised' | 'Settled' | 'Cancelled' | 'Declined' | 'DisputeFailed' | 'DisputeOpened' | 'DisputeWon' | 'Moved' | 'Unknown'; export type FuelType = 'PETROL' | 'DIESEL' | 'METHANE' | 'LPG' | 'LNG' | 'ELECTRIC' | 'UNKNOWN'; export type Direction = 'ASC' | 'DESC'; export type GetApiBusinessV2RefuelingArgs = {type?: RefuelingType | undefined, id?: string | undefined, publicId?: string | undefined, fromDate?: string | undefined, toDate?: string | undefined, dateType?: TransactionDateType | undefined, txCountry?: ISO3166Country | undefined, txStatus?: Array | undefined, txFuelType?: FuelType | undefined, showFuelDetails?: boolean | undefined, p?: number | undefined, s?: number | undefined, d?: Direction | undefined, props?: Array | undefined, 'X-Soldo-Fingerprint': string, 'X-Soldo-Fingerprint-Signature': string}; export type GetApiBusinessV2RefuelingOptions = {baseUrl?: string | undefined, standardAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatus = z.enum(["Authorised", "Settled", "Cancelled", "Declined", "DisputeFailed", "DisputeOpened", "DisputeWon", "Moved", "Unknown"]); export const transactionSign = z.enum(["Negative", "Positive", "None"]); export const iSO4217Currency = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BOV", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLF", "CLP", "CNY", "COP", "COU", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MRU", "MUR", "MVR", "MWK", "MXN", "MXV", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLE", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "USN", "UYI", "UYU", "UYW", "UZS", "VED", "VEF", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XOF", "XPD", "XPF", "XPT", "XSU", "XTS", "XUA", "XXX", "YER", "ZAR", "ZMW", "ZWL"]); export const iSO3166Country = z.enum(["ASC", "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", "ANT", "AGO", "ATA", "ARG", "ASM", "AUT", "AUS", "ABW", "ALA", "AZE", "BIH", "BRB", "BGD", "BEL", "BFA", "BGR", "BHR", "BDI", "BEN", "BLM", "BMU", "BRN", "BOL", "BES", "BRA", "BHS", "BTN", "BUR", "BVT", "BWA", "BLR", "BLZ", "CAN", "CCK", "COD", "CAF", "COG", "CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CPT", "CRI", "CSXX", "CUB", "CPV", "CUW", "CXR", "CYP", "CZE", "DEU", "DGA", "DJI", "DNK", "DMA", "DOM", "DZA", "ECU", "EST", "EGY", "ESH", "ERI", "ESP", "ETH", "FIN", "FJI", "FLK", "FSM", "FRO", "FRA", "FXX", "GAB", "GBR", "GRD", "GEO", "GUF", "GGY", "GHA", "GIB", "GRL", "GMB", "GIN", "GLP", "GNQ", "GRC", "SGS", "GTM", "GUM", "GNB", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN", "IDN", "IRL", "ISR", "IMN", "IND", "IOT", "IRQ", "IRN", "ISL", "ITA", "JEY", "JAM", "JOR", "JPN", "KEN", "KGZ", "KHM", "KIR", "COM", "KNA", "PRK", "KOR", "KWT", "CYM", "KAZ", "LAO", "LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU", "LUX", "LVA", "LBY", "MAR", "MCO", "MDA", "MNE", "MAF", "MDG", "MHL", "MKD", "MLI", "MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR", "MLT", "MUS", "MDV", "MWI", "MEX", "MYS", "MOZ", "NAM", "NCL", "NER", "NFK", "NGA", "NIC", "NLD", "NOR", "NPL", "NRU", "NTZ", "NIU", "NZL", "OMN", "PAN", "PER", "PYF", "PNG", "PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT", "PLW", "PRY", "QAT", "REU", "ROU", "ROM", "SRB", "RUS", "RWA", "SAU", "SLB", "SYC", "SDN", "SWE", "SGP", "SHN", "SVN", "SJM", "SVK", "SLE", "SMR", "SEN", "SOM", "SUR", "SSD", "STP", "SUN", "SLV", "SXM", "SYR", "SWZ", "TAA", "TCA", "TCD", "ATF", "TGO", "THA", "TJK", "TKL", "TLS", "TKM", "TUN", "TON", "TUR", "TTO", "TUV", "TWN", "TZA", "UKR", "UGA", "UMI", "USA", "URY", "UZB", "VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "VUT", "WLF", "WSM", "XKX", "XXK", "QZZ", "YEM", "MYT", "YUCS", "ZAF", "ZMB", "ZRCD", "ZWE"]); export const fuelType = z.enum(["PETROL", "DIESEL", "METHANE", "LPG", "LNG", "ELECTRIC", "UNKNOWN"]); export const refueling = z.object({id: z.string().optional(), public_id: z.string().optional(), transaction_id: z.string().optional(), card_id: z.string().optional(), tx_status: transactionStatus.optional(), transaction_sign: transactionSign.optional(), tx_amount: z.number().optional(), tx_amount_currency: iSO4217Currency.optional(), tx_date: z.string().optional(), tx_settlement_date: z.string().optional(), update_time: z.string().optional(), tx_vat_amount: z.number().optional(), tx_vat_currency: iSO4217Currency.optional(), tx_country: iSO3166Country.optional(), vehicle_id: z.string().optional(), plate: z.string().optional(), vehicle_fuel_type: fuelType.optional(), vat_deductibility_percentage: z.string().optional(), mileage: z.number().int().optional(), quantity: z.number().optional(), unit_price: z.number().optional(), tx_fuel_type: fuelType.optional(), invoice_id: z.number().int().optional(), invoice_number: z.string().optional(), invoice_date: z.string().optional(), invoice_total_amount: z.number().optional(), invoice_row: z.number().int().optional(), invoice_row_amount: z.number().optional(), supplier_vat_id: z.string().optional(), dri: z.string().optional(), to_be_ignored: z.boolean().optional()}); export const refuelings = z.object({total: z.number().int().optional(), pages: z.number().int().optional(), page_size: z.number().int().optional(), current_page: z.number().int().optional(), results_size: z.number().int().optional(), results: z.array(refueling).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 }; /** * Search Refueling Transactions * * GET /business/v2/refueling */ export const getApiBusinessV2Refueling = async (args: GetApiBusinessV2RefuelingArgs, options?: GetApiBusinessV2RefuelingOptions) => { const {baseUrl = "https://api.soldo.com", fetch: fetchFn = fetch, standardAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.id != null) query.append("id", String(args.id)) if (args.publicId != null) query.append("publicId", String(args.publicId)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.dateType != null) query.append("dateType", String(args.dateType)) if (args.txCountry != null) query.append("txCountry", String(args.txCountry)) for (const item of args.txStatus ?? []) query.append("txStatus", String(item)) if (args.txFuelType != null) query.append("txFuelType", String(args.txFuelType)) if (args.showFuelDetails != null) query.append("showFuelDetails", String(args.showFuelDetails)) if (args.p != null) query.append("p", String(args.p)) if (args.s != null) query.append("s", String(args.s)) if (args.d != null) query.append("d", String(args.d)) for (const item of args.props ?? []) query.append("props", String(item)) if (args['X-Soldo-Fingerprint'] != null) headers.set("X-Soldo-Fingerprint", String(args['X-Soldo-Fingerprint'])) if (args['X-Soldo-Fingerprint-Signature'] != null) headers.set("X-Soldo-Fingerprint-Signature", String(args['X-Soldo-Fingerprint-Signature'])) if (standardAuth !== undefined) headers.set('authorization', `Bearer ${standardAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/business/v2/refueling${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(refuelings, await res.json()) };