import {z} from 'zod' export type GetApiRecurrencePlansArgs = {page?: number | undefined, size?: number | undefined}; export type GetApiRecurrencePlansOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const planInstallmentsDTOOutput = z.object({installments: z.number().int(), transaction_fee: z.string().optional(), installment_amount: z.string().optional(), total_amount: z.string().optional()}); export const planPaymentMethodDTOOutput = z.object({payment_method: z.string(), installments_options: z.array(planInstallmentsDTOOutput).optional(), transaction_fee: z.string().optional()}); export const planItemCreatedDTO = z.object({product_id: z.string(), amount: z.string(), description: z.string().optional(), type: z.string().optional(), status: z.string().optional(), metadata: z.object({}).optional()}); export const planCreatedDTO = z.object({plan_id: z.string(), interval: z.string(), interval_count: z.number().int(), name: z.string(), status: z.string().optional(), billing_type: z.string(), installments: z.number().int().optional(), payment_methods: z.array(planPaymentMethodDTOOutput), accepted_payment_methods: z.array(z.string()), items: z.array(planItemCreatedDTO), metadata: z.object({}).nullable().optional()}); export const pagePlanCreatedDTO = z.object({items: z.array(planCreatedDTO), total: z.number().int().gte(0).nullable(), page: z.number().int().gte(1).nullable(), size: z.number().int().gte(1).nullable(), pages: z.number().int().gte(0).nullable().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 }; /** * Listar todos os planos * * GET /recurrence/plans/ */ export const getApiRecurrencePlans = async (args: GetApiRecurrencePlansArgs, options?: GetApiRecurrencePlansOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/recurrence/plans/${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(pagePlanCreatedDTO, await res.json()) };