import {z} from 'zod' export type GetApiPublicPricingOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const publicPricing = z.object({currency: z.literal("BRL"), methods: z.array(z.object({method: z.enum(["PIX", "CARD", "BOLETO"]), percent: z.string().nullable(), fixed: z.string().nullable(), minimumFee: z.string().nullable(), minAmount: z.string(), maxAmount: z.string(), settlementDays: z.number().int().gte(0)})), withdrawal: z.object({fee: z.string(), minAmount: z.string(), settlementDays: z.literal(0)}).nullable(), plan: z.object({monthlyFee: z.literal("0.00"), setupFee: z.literal("0.00"), volumeMinimum: z.unknown(), customPricing: z.literal(true)}), signup: z.object({open: z.boolean(), url: z.string(), accountTypes: z.array(z.enum(["INDIVIDUAL", "BUSINESS"])), sandbox: z.boolean()}), updatedAt: z.string().nullable()}); 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 standard pricing * * GET /public/pricing */ export const getApiPublicPricing = async (options?: GetApiPublicPricingOptions) => { const {baseUrl = "https://api.yuvexpay.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/public/pricing${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(publicPricing, await res.json()) };