import {z} from 'zod' export type GetApiAdminPaymentOrdersArgs = {offset?: number | undefined, limit?: number | undefined, search?: string | undefined}; export type GetApiAdminPaymentOrdersOptions = {baseUrl: string, HTTPBearer?: string | undefined, 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 }; /** * List Payment Orders * * GET /admin/payment-orders */ export const getApiAdminPaymentOrders = async (args: GetApiAdminPaymentOrdersArgs, options: GetApiAdminPaymentOrdersOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = 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)) if (args.search != null) query.append("search", String(args.search)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/admin/payment-orders${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()) };