import {z} from 'zod' export type GetApiPaymentOrdersSubscriptionIdArgs = {subscription_id: number, offset?: number | undefined, limit?: number | undefined}; export type GetApiPaymentOrdersSubscriptionIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const saasPaymentOrderOut = z.object({serie: z.string().max(10).optional(), numero: z.number().int(), subscription_id: z.number().int(), periodo: z.string().max(7), tipo_orden: z.string().max(20), total: z.string(), estado: z.string().max(20).optional(), fecha_emision: z.string(), fecha_fin: z.string().nullable().optional(), fecha_bloqueo: z.string(), url_factura_emitida: z.string().max(255).nullable().optional(), id: z.number().int(), usuario_id: z.number().int().nullable().optional(), usuario: z.string().max(255).nullable().optional(), correo_usuario: z.string().max(255).nullable().optional(), plan: z.string().max(255).nullable().optional(), fecha_de_creacion: z.string().nullable().optional(), respuestas_facturacion: z.array(z.record(z.string(), z.unknown())).nullable().optional(), historial_notificaciones: z.array(z.record(z.string(), z.unknown())).nullable().optional(), fecha_notificacion_proximo_pago: z.string().nullable().optional(), usuario_creador_id: z.number().int().nullable().optional(), archivo_pdf: z.string()}); export const saasPaymentOrderResource = z.object({total: z.number().int(), limit: z.number().int(), offset: z.number().int(), total_pages: z.number().int(), current_page: z.number().int(), docs: z.array(saasPaymentOrderOut)}); 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 Órdenes de Pago SaaS * * GET /payment-orders/{subscription_id} */ export const getApiPaymentOrdersSubscriptionId = async (args: GetApiPaymentOrdersSubscriptionIdArgs, options: GetApiPaymentOrdersSubscriptionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/payment-orders/${encodeURIComponent(String(args.subscription_id))}${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(saasPaymentOrderResource, await res.json()) };