import {z} from 'zod' export type EventEstimateFeesInput = {event: {code: string, external_subscription_id: string, precise_total_amount_cents?: (string | null) | undefined, properties?: ({target_wallet_code?: (string | null) | undefined} | {[key: string]: unknown}) | undefined}}; export type CreateApiEventsEstimateFeesArgs = {body: EventEstimateFeesInput}; export type CreateApiEventsEstimateFeesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "ISK", "JMD", "JPY", "KES", "KGS", "KHR", "KMF", "KRW", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SYP", "SZL", "THB", "TJS", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VND", "VUV", "WST", "XAF", "XCD", "XOF", "XPF", "YER", "ZAR", "ZMW"]); export const feeAmountDetails = z.object({plan_amount_cents: z.number().int().optional(), graduated_ranges: z.array(z.object({units: z.string(), from_value: z.number().int(), to_value: z.number().int().nullable(), flat_unit_amount: z.string(), per_unit_amount: z.string(), per_unit_total_amount: z.string(), total_with_flat_amount: z.string()})).optional(), graduated_percentage_ranges: z.array(z.object({units: z.string(), from_value: z.number().int(), to_value: z.number().int().nullable(), flat_unit_amount: z.string(), rate: z.string(), per_unit_total_amount: z.string(), total_with_flat_amount: z.string()})).optional(), free_units: z.string().optional(), paid_units: z.string().optional(), per_package_size: z.number().int().optional(), per_package_unit_amount: z.string().optional(), per_unit_total_amount: z.string().optional(), units: z.string().optional(), free_events: z.number().int().optional(), rate: z.string().optional(), paid_events: z.number().int().optional(), fixed_fee_unit_amount: z.string().optional(), fixed_fee_total_amount: z.string().optional(), min_max_adjustment_total_amount: z.string().optional(), per_unit_amount: z.string().optional(), flat_unit_amount: z.string().optional()}); export const feeAppliedTaxObject = z.object({lago_id: z.string(), lago_tax_id: z.string().nullable(), tax_name: z.string(), tax_code: z.string(), tax_rate: z.number(), tax_description: z.string().nullable(), amount_cents: z.number().int(), amount_currency: currency, created_at: z.string()}); export const pricingUnitDetailsObject = z.object({lago_pricing_unit_id: z.string(), pricing_unit_code: z.string(), short_name: z.string(), amount_cents: z.number().int(), precise_amount_cents: z.string(), unit_amount_cents: z.number().int(), precise_unit_amount: z.string(), conversion_rate: z.string()}); export const presentationBreakdownsObject = z.array(z.object({presentation_by: z.record(z.string(), z.string()).optional(), units: z.string().optional()})); export const feeObject = z.object({lago_id: z.string().nullable().optional(), lago_charge_id: z.string().nullable().optional(), lago_charge_filter_id: z.string().nullable().optional(), lago_fixed_charge_id: z.string().nullable().optional(), lago_invoice_id: z.string().nullable().optional(), lago_true_up_fee_id: z.string().nullable().optional(), lago_true_up_parent_fee_id: z.string().nullable().optional(), lago_original_fee_id: z.string().nullable().optional(), lago_subscription_id: z.string().nullable().optional(), lago_customer_id: z.string().nullable().optional(), external_customer_id: z.string().nullable().optional(), external_subscription_id: z.string().nullable().optional(), amount_cents: z.number().int(), precise_amount: z.string().optional(), precise_total_amount: z.string().optional(), amount_currency: currency, taxes_amount_cents: z.number().int(), taxes_precise_amount: z.string().optional(), taxes_rate: z.number(), units: z.string(), precise_unit_amount: z.string(), total_aggregated_units: z.string().nullable(), total_amount_cents: z.number().int(), total_amount_currency: currency, events_count: z.number().int().nullable().optional(), pay_in_advance: z.boolean(), invoiceable: z.boolean(), from_date: z.string().nullable().optional(), to_date: z.string().nullable().optional(), payment_status: z.enum(["pending", "succeeded", "failed", "refunded"]), created_at: z.string().nullable().optional(), succeeded_at: z.string().nullable().optional(), failed_at: z.string().nullable().optional(), refunded_at: z.string().nullable().optional(), event_transaction_id: z.string().nullable().optional(), description: z.string().nullable().optional(), precise_coupons_amount_cents: z.string().optional(), sub_total_excluding_taxes_amount_cents: z.number().int(), sub_total_excluding_taxes_precise_amount_cents: z.string(), amount_details: feeAmountDetails.optional(), self_billed: z.boolean().optional(), item: z.object({type: z.enum(["charge", "fixed_charge", "add_on", "subscription", "credit", "commitment"]), code: z.string(), name: z.string(), description: z.string().nullable(), invoice_display_name: z.string(), filter_invoice_display_name: z.string().nullable(), filters: z.record(z.string(), z.array(z.string())).nullable(), lago_item_id: z.string(), item_type: z.enum(["AddOn", "BillableMetric", "Subscription", "WalletTransaction"]), grouped_by: z.record(z.string(), z.string())}), applied_taxes: z.array(feeAppliedTaxObject).optional(), pricing_unit_details: pricingUnitDetailsObject.optional(), presentation_breakdowns: presentationBreakdownsObject.optional()}); export const fees = z.object({fees: z.array(feeObject)}); 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 }; /** * Estimate fees for a pay in advance charge * * POST /events/estimate_fees */ export const createApiEventsEstimateFees = async (args: CreateApiEventsEstimateFeesArgs, options?: CreateApiEventsEstimateFeesOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/events/estimate_fees${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(fees, await res.json()) };