import {z} from 'zod' export type GetApiWalletsArgs = {page?: number | undefined, per_page?: number | undefined, currency?: string | undefined, external_customer_id?: string | undefined, 'billing_entity_codes[]'?: Array | undefined}; export type GetApiWalletsOptions = {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 walletFeeTypeEnum = z.enum(["charge", "subscription", "commitment", "fixed_charge", "credit"]); export const invoiceCustomSectionObject = z.object({lago_id: z.string(), name: z.string(), code: z.string(), description: z.string().optional(), details: z.string().optional(), display_name: z.string().optional(), applied_to_organization: z.boolean().optional(), organization_id: z.string().optional(), created_at: z.string().optional()}); export const appliedInvoiceCustomSectionObject = z.object({lago_id: z.string(), created_at: z.string(), invoice_custom_section_id: z.string().optional(), invoice_custom_section: invoiceCustomSectionObject}); export const paymentMethodReference = z.object({payment_method_type: z.enum(["provider", "manual"]).optional(), payment_method_id: z.string().optional()}); export const walletRecurringTransactionRule = z.object({lago_id: z.string(), trigger: z.enum(["interval", "threshold"]), method: z.enum(["fixed", "target"]), interval: z.enum(["weekly", "monthly", "quarterly", "semiannual", "yearly", "null"]).nullable(), status: z.enum(["active", "terminated"]), threshold_credits: z.string(), paid_credits: z.string(), granted_credits: z.string(), grants_target_top_up: z.boolean().nullable(), started_at: z.string().nullable(), target_ongoing_balance: z.string().nullable(), created_at: z.string(), expiration_at: z.string().nullable(), invoice_requires_successful_payment: z.boolean(), transaction_metadata: z.array(z.object({key: z.string().optional(), value: z.string().optional()})), transaction_name: z.string().nullable(), ignore_paid_top_up_limits: z.boolean(), applied_invoice_custom_sections: z.array(appliedInvoiceCustomSectionObject), payment_method: paymentMethodReference, purchase_order_number: z.string().nullable()}); export const metadataObject = z.record(z.string(), z.string().nullable()).nullable(); export const walletObject = z.object({lago_id: z.string(), lago_customer_id: z.string(), external_customer_id: z.string(), billing_entity_code: z.string(), status: z.enum(["active", "terminated"]), currency: currency, name: z.string().nullable(), code: z.string().nullable(), priority: z.number().int().gte(1).lte(50), rate_amount: z.string(), credits_balance: z.string(), balance_cents: z.number().int(), consumed_credits: z.string(), created_at: z.string(), expiration_at: z.string().nullable(), last_balance_sync_at: z.string().nullable(), last_consumed_credit_at: z.string().nullable(), terminated_at: z.string().nullable(), invoice_requires_successful_payment: z.boolean(), applies_to: z.object({fee_types: z.array(walletFeeTypeEnum).optional(), billable_metric_codes: z.array(z.string()).optional()}).optional(), recurring_transaction_rules: z.array(walletRecurringTransactionRule).optional(), ongoing_balance_cents: z.number().int(), ongoing_usage_balance_cents: z.number().int(), credits_ongoing_balance: z.string(), credits_ongoing_usage_balance: z.string(), paid_top_up_min_amount_cents: z.number().int().nullable(), paid_top_up_max_amount_cents: z.number().int().nullable(), applied_invoice_custom_sections: z.array(appliedInvoiceCustomSectionObject).optional(), payment_method: paymentMethodReference, purchase_order_number: z.string().nullable(), metadata: metadataObject.optional()}); export const paginationMeta = z.object({current_page: z.number().int(), next_page: z.number().int().nullable().optional(), prev_page: z.number().int().nullable().optional(), total_pages: z.number().int(), total_count: z.number().int()}); export const walletsPaginated = z.object({wallets: z.array(walletObject), meta: paginationMeta}); 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 all wallets * * GET /wallets */ export const getApiWallets = async (args: GetApiWalletsArgs, options?: GetApiWalletsOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.external_customer_id != null) query.append("external_customer_id", String(args.external_customer_id)) for (const item of args['billing_entity_codes[]'] ?? []) query.append("billing_entity_codes[]", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/wallets${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(walletsPaginated, await res.json()) };