import {z} from 'zod' export type GetApiV1SdkAutoRechargeArgs = {customerRef: string}; export type GetApiV1SdkAutoRechargeOptions = {baseUrl?: string | undefined, SecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const autoRechargeTopupDto = z.object({amountMinor: z.number(), currency: z.string(), mode: z.literal("fixed")}); export const autoRechargeTriggerDto = z.object({thresholdAmountMinor: z.number(), type: z.literal("balance")}); export const autoRechargeConfigDto = z.object({enabled: z.boolean(), failureCount: z.number(), fundingSourceType: z.enum(["saved_card", "tokenized_card"]), inFlightPaymentIntentId: z.string().optional(), lastChargeAt: z.string().optional(), lockAcquiredAt: z.string().optional(), maxMonthlySpendMinor: z.number().optional(), monthlySpendMinor: z.number(), monthlySpendPeriod: z.string().optional(), paymentMethodId: z.string().optional(), status: z.enum(["active", "disabled", "failed", "pending_setup"]), topup: autoRechargeTopupDto, trigger: autoRechargeTriggerDto, updatedAt: z.string()}); export const autoRechargeDisplayFormattedDto = z.object({threshold: z.string(), topup: z.string()}); export const autoRechargeDisplayDto = z.object({currency: z.string(), exchangeRate: z.number(), formatted: autoRechargeDisplayFormattedDto, rateSource: z.enum(["parity", "db", "fallback"]), thresholdAmountMajor: z.number(), topupAmountMajor: z.number()}); export const autoRechargeValidationMetaResponse = z.object({maxAmountMajor: z.number(), stripeMinimumMinorByCurrency: z.record(z.string(), z.number())}); export const autoRechargeGetResponse = z.object({config: autoRechargeConfigDto.nullable().optional(), display: autoRechargeDisplayDto.optional(), validation: autoRechargeValidationMetaResponse}); 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 }; /** * Get auto-recharge config for a customer * * GET /v1/sdk/auto-recharge */ export const getApiV1SdkAutoRecharge = async (args: GetApiV1SdkAutoRechargeArgs, options?: GetApiV1SdkAutoRechargeOptions) => { const {baseUrl = "https://api.solvapay.com", fetch: fetchFn = fetch, SecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customerRef != null) query.append("customerRef", String(args.customerRef)) if (SecretKey !== undefined) headers.set('authorization', `Bearer ${SecretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sdk/auto-recharge${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(autoRechargeGetResponse, await res.json()) };