import {z} from 'zod' export type Currency = '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 type WalletFeeTypeEnum = 'charge' | 'subscription' | 'commitment' | 'fixed_charge' | 'credit'; export type InvoiceCustomSectionInput = {skip_invoice_custom_sections?: boolean | undefined, invoice_custom_section_codes?: Array | undefined}; export type PaymentMethodReference = {payment_method_type?: ('provider' | 'manual') | undefined, payment_method_id?: string | undefined}; export type MetadataInput = {[key: string]: string | null}; export type WalletCreateInput = {wallet: {name?: (string | null) | undefined, code?: (string | null) | undefined, priority?: number | undefined, rate_amount: string, currency?: Currency | undefined, paid_credits?: (string | null) | undefined, granted_credits?: (string | null) | undefined, external_customer_id: string, billing_entity_code?: string | undefined, expiration_at?: (string | null) | undefined, invoice_requires_successful_payment?: boolean | undefined, transaction_metadata?: (Array<{key?: string | undefined, value?: string | undefined}> | null) | undefined, transaction_name?: (string | null) | undefined, transaction_priority?: number | undefined, purchase_order_number?: (string | null) | undefined, applies_to?: ({fee_types?: (Array | null) | undefined, billable_metric_codes?: (Array | null) | undefined} | null) | undefined, paid_top_up_min_amount_cents?: (number | null) | undefined, paid_top_up_max_amount_cents?: (number | null) | undefined, ignore_paid_top_up_limits_on_creation?: (boolean | null) | undefined, invoice_custom_section?: InvoiceCustomSectionInput | undefined, recurring_transaction_rules?: (Array<{paid_credits?: (string | null) | undefined, granted_credits?: (string | null) | undefined, grants_target_top_up?: (boolean | null) | undefined, method?: ('fixed' | 'target') | undefined, trigger: 'interval' | 'threshold', interval?: ('weekly' | 'monthly' | 'quarterly' | 'semiannual' | 'yearly') | undefined, started_at?: (string | null) | undefined, expiration_at?: (string | null) | undefined, threshold_credits?: string | undefined, target_ongoing_balance?: string | undefined, invoice_requires_successful_payment?: boolean | undefined, transaction_metadata?: (Array<{key?: string | undefined, value?: string | undefined}>) | undefined, transaction_name?: (string | null) | undefined, purchase_order_number?: (string | null) | undefined, ignore_paid_top_up_limits?: boolean | undefined, invoice_custom_section?: InvoiceCustomSectionInput | undefined, payment_method?: PaymentMethodReference | undefined}> | null) | undefined, payment_method?: PaymentMethodReference | undefined, metadata?: MetadataInput | undefined}}; export type CreateApiWalletsArgs = {body: WalletCreateInput}; export type CreateApiWalletsOptions = {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 wallet = z.object({wallet: walletObject}); 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 wallet * * POST /wallets */ export const createApiWallets = async (args: CreateApiWalletsArgs, options?: CreateApiWalletsOptions) => { 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(/\/+$/, '')}/wallets${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(wallet, await res.json()) };