import {z} from 'zod' export type GetApiSubscriptionsSubscriptionIdIncrementsArgs = {subscription_id: string}; export type GetApiSubscriptionsSubscriptionIdIncrementsOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiSubscriptionsSubscriptionIdIncrementsResponse = z.object({data: z.array(z.object({id: z.string().optional(), value: z.number().int().optional(), increment_type: z.string().optional(), status: z.string().optional(), created_at: z.string().optional(), subscription: z.object({id: z.string().optional(), code: z.string().optional(), start_at: z.string().optional(), interval: z.string().optional(), interval_count: z.number().int().optional(), billing_type: z.string().optional(), next_billing_at: z.string().optional(), payment_method: z.string().optional(), currency: z.string().optional(), installments: z.number().int().optional(), status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional()})).optional(), paging: z.object({total: z.number().int().optional()}).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 incrementos * * GET /subscriptions/{subscription_id}/increments/ */ export const getApiSubscriptionsSubscriptionIdIncrements = async (args: GetApiSubscriptionsSubscriptionIdIncrementsArgs, options?: GetApiSubscriptionsSubscriptionIdIncrementsOptions) => { const {baseUrl = "https://api.mundipagg.com/core/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/subscriptions/${encodeURIComponent(String(args.subscription_id))}/increments/${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(getApiSubscriptionsSubscriptionIdIncrementsResponse, await res.json()) };