import {z} from 'zod' export type PaymentV1PreviewTopupRequest = {amountMicros?: string | undefined, credits?: string | undefined, currency?: string | undefined, packageId?: string | undefined}; export type CreateApiV1ConsolePaymentTopupPreviewArgs = {body: PaymentV1PreviewTopupRequest}; export type CreateApiV1ConsolePaymentTopupPreviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const paymentV1PreviewTopupReply = z.object({appliedMultiplierBps: z.number().int().optional(), baseCreditedMicros: z.string().optional(), bonusCreditsApplied: z.string().optional(), creditedMicros: z.string().optional(), currency: z.string().optional(), internalCredits: z.string().optional(), matchedRuleId: z.string().optional(), payAmountMicros: z.string().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 }; /** * PreviewTopup 充值预览:输入金额返回到账积分 + 阶梯赠送报价(不下单、不调支付)。 * * POST /v1/console/payment/topup/preview */ export const createApiV1ConsolePaymentTopupPreview = async (args: CreateApiV1ConsolePaymentTopupPreviewArgs, options: CreateApiV1ConsolePaymentTopupPreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/console/payment/topup/preview${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(paymentV1PreviewTopupReply, await res.json()) };