import {z} from 'zod' 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 WalletTransactionCreateInput = {wallet_transaction: {wallet_id: string, name?: (string | null) | undefined, paid_credits?: (string | null) | undefined, granted_credits?: (string | null) | undefined, voided_credits?: (string | null) | undefined, voided_transaction_id?: (string | null) | undefined, priority?: number | undefined, invoice_requires_successful_payment?: boolean | undefined, ignore_paid_top_up_limits?: boolean | undefined, purchase_order_number?: (string | null) | undefined, invoice_custom_section?: InvoiceCustomSectionInput | undefined, payment_method?: PaymentMethodReference | undefined, metadata?: (Array<{key?: string | undefined, value?: string | undefined}> | null) | undefined}}; export type CreateApiWalletTransactionsArgs = {body: WalletTransactionCreateInput}; export type CreateApiWalletTransactionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 walletTransactionObject = z.object({lago_id: z.string(), lago_wallet_id: z.string(), billing_entity_code: z.string(), lago_invoice_id: z.string().nullable(), lago_credit_note_id: z.string().nullable(), lago_voided_invoice_id: z.string().nullable(), status: z.enum(["pending", "settled", "failed"]), source: z.enum(["manual", "interval", "threshold"]), transaction_status: z.enum(["purchased", "granted", "voided", "invoiced"]), transaction_type: z.enum(["inbound", "outbound"]), amount: z.string(), credit_amount: z.string(), invoice_requires_successful_payment: z.boolean(), metadata: z.array(z.object({key: z.string().optional(), value: z.string().optional()})), remaining_amount_cents: z.number().int().nullable(), remaining_credit_amount: z.string().nullable(), priority: z.number().int(), settled_at: z.string().nullable(), failed_at: z.string().nullable(), created_at: z.string(), name: z.string().nullable(), applied_invoice_custom_sections: z.array(appliedInvoiceCustomSectionObject).optional(), payment_method: paymentMethodReference, purchase_order_number: z.string().nullable()}); export const walletTransactions = z.object({wallet_transactions: z.array(walletTransactionObject)}); 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 }; /** * Top up a wallet * * POST /wallet_transactions */ export const createApiWalletTransactions = async (args: CreateApiWalletTransactionsArgs, options?: CreateApiWalletTransactionsOptions) => { 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(/\/+$/, '')}/wallet_transactions${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(walletTransactions, await res.json()) };