import {z} from 'zod' export type AlertThresholdInput = {code?: (string | null) | undefined, notify_on?: (Array<'triggered' | 'resolved'>) | undefined, recurring?: boolean | undefined, value: number | string}; export type WalletAlertCreateInput = {alert: {code: string, name?: (string | null) | undefined, thresholds: Array, alert_type: 'wallet_balance_amount' | 'wallet_credits_balance' | 'wallet_ongoing_balance_amount' | 'wallet_credits_ongoing_balance'}}; export type WalletAlertBatchCreateInput = {alerts: Array<{code: string, name?: (string | null) | undefined, thresholds: Array, alert_type: 'wallet_balance_amount' | 'wallet_credits_balance' | 'wallet_ongoing_balance_amount' | 'wallet_credits_ongoing_balance'}>}; export type CreateApiCustomersExternalCustomerIdWalletsWalletCodeAlertsArgs = {body: WalletAlertCreateInput | WalletAlertBatchCreateInput}; export type CreateApiCustomersExternalCustomerIdWalletsWalletCodeAlertsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const alertThresholdObject = z.object({code: z.string().nullable(), notify_on: z.array(z.enum(["triggered", "resolved"])), recurring: z.boolean(), value: z.string()}); export const walletAlertObject = z.object({lago_id: z.string(), lago_organization_id: z.string(), external_subscription_id: z.unknown(), lago_wallet_id: z.string(), wallet_code: z.string(), billable_metric: z.unknown(), alert_type: z.enum(["wallet_balance_amount", "wallet_credits_balance", "wallet_ongoing_balance_amount", "wallet_credits_ongoing_balance"]), code: z.string(), name: z.string().nullable(), direction: z.enum(["increasing", "decreasing"]), previous_value: z.string(), last_processed_at: z.string().nullable(), thresholds: z.array(alertThresholdObject), created_at: z.string()}); export const walletAlert = z.object({alert: walletAlertObject}); 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 walletAlerts = z.object({alerts: z.array(walletAlertObject), meta: paginationMeta.optional()}); export const createApiCustomersExternalCustomerIdWalletsWalletCodeAlertsResponse = z.union([walletAlert, walletAlerts]); 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 wallet alert(s) * * POST /customers/{external_customer_id}/wallets/{wallet_code}/alerts */ export const createApiCustomersExternalCustomerIdWalletsWalletCodeAlerts = async (args: CreateApiCustomersExternalCustomerIdWalletsWalletCodeAlertsArgs, options?: CreateApiCustomersExternalCustomerIdWalletsWalletCodeAlertsOptions) => { 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(/\/+$/, '')}/customers/${encodeURIComponent(String(args.external_customer_id))}/wallets/${encodeURIComponent(String(args.wallet_code))}/alerts${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(createApiCustomersExternalCustomerIdWalletsWalletCodeAlertsResponse, await res.json()) };