import {z} from 'zod' export type DateTimeWithZone = string; export type FeeType = 'SUBSCRIPTION' | 'COMMITMENT' | 'CHARGE'; export type PrepaidCreditAppliesTo = {fee_types?: Array | undefined}; export type RecurringTransactionTrigger = 'INTERVAL' | 'THRESHOLD'; export type RecurringTransactionMethod = 'FIXED' | 'TARGET'; export type BillingCycle = 'WEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY'; export type RecurringTransactionRule = {trigger: RecurringTransactionTrigger, paid_credits?: string | undefined, granted_credits?: string | undefined, method: RecurringTransactionMethod, interval?: BillingCycle | undefined, started_at?: DateTimeWithZone | undefined, expiration_at?: DateTimeWithZone | undefined, threshold_credits?: string | undefined, target_ongoing_balance?: string | undefined}; export type WalletUpdate = {name?: string | undefined, expiration_at?: DateTimeWithZone | undefined, applies_to?: PrepaidCreditAppliesTo | undefined, recurring_transaction_rules?: Array | undefined}; export type UpdateApiWalletsWalletIdArgs = {wallet_id: string, body: WalletUpdate}; export type UpdateApiWalletsWalletIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const walletStatus = z.enum(["ACTIVE", "TERMINATED", "PENDING"]); export const currency = z.string().min(3).max(3); export const amount = z.object({value: z.number().gte(0), currency_code: currency}); export const dateTimeWithZone = z.string(); export const feeType = z.enum(["SUBSCRIPTION", "COMMITMENT", "CHARGE"]); export const prepaidCreditAppliesTo = z.object({fee_types: z.array(feeType).optional()}); export const recurringTransactionTrigger = z.enum(["INTERVAL", "THRESHOLD"]); export const recurringTransactionMethod = z.enum(["FIXED", "TARGET"]); export const billingCycle = z.enum(["WEEKLY", "MONTHLY", "QUARTERLY", "YEARLY"]); export const recurringTransactionRule = z.object({trigger: recurringTransactionTrigger, paid_credits: z.string().optional(), granted_credits: z.string().optional(), method: recurringTransactionMethod, interval: billingCycle.optional(), started_at: dateTimeWithZone.optional(), expiration_at: dateTimeWithZone.optional(), threshold_credits: z.string().optional(), target_ongoing_balance: z.string().optional()}); export const walletId = z.string(); export const walletWithId = z.object({external_customer_id: z.string(), status: walletStatus, available_balance: amount.optional(), ongoing_usage: z.object({value: z.number().gte(0), currency_code: currency}).optional(), net_balance: z.object({value: z.number().gte(0), currency_code: currency}).optional(), name: z.string().optional(), rate_amount: z.string(), available_credits: z.string().optional(), ongoing_usage_credits: z.string().optional(), net_balance_credits: z.string().optional(), consumed_credits: z.string().optional(), created_at: dateTimeWithZone.optional(), expiration_at: dateTimeWithZone.optional(), terminated_at: dateTimeWithZone.optional(), applies_to: prepaidCreditAppliesTo.optional(), recurring_transaction_rules: z.array(recurringTransactionRule).optional(), id: walletId.optional()}); 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 }; /** * Update a wallet * * PUT /wallets/{wallet_id} */ export const updateApiWalletsWalletId = async (args: UpdateApiWalletsWalletIdArgs, options?: UpdateApiWalletsWalletIdOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", 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/${encodeURIComponent(String(args.wallet_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(walletWithId, await res.json()) };