import {z} from 'zod' export type GetApiSubscriptionsMeOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const planScope = z.enum(["PRUEBA", "API_GLOBAL", "OMNICANAL_EMPRESA"]); export const saasSubscriptionBillingDataOut = z.object({numero_documento: z.string().nullable().optional(), razon_social: z.string().nullable().optional()}); export const saasSubscriptionOut = z.object({id: z.number().int(), user_id: z.number().int(), id_plan: z.number().int().nullable().optional(), nombre_plan: z.string().nullable().optional(), plan_alcance: planScope.nullable().optional(), status: z.string(), tarifa_minima: z.string(), descuento: z.string(), dia_corte: z.number().int(), dias_gracia: z.number().int(), dias_adicionales: z.number().int(), dias_adicionales_fin_de_mes: z.number().int(), doc_type: z.string().nullable().optional(), num_doc: z.string().nullable().optional(), aceptacion_terminos: z.boolean(), terms_acceptance_date: z.string().nullable(), migrated_legacy: z.boolean(), fecha_de_creacion: z.string(), fecha_de_actualizacion: z.string(), datos_facturacion: saasSubscriptionBillingDataOut.nullable().optional()}); export const getApiSubscriptionsMeResponse = z.array(saasSubscriptionOut); 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 My Subscriptions * * GET /subscriptions/me */ export const getApiSubscriptionsMe = async (options: GetApiSubscriptionsMeOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/subscriptions/me${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(getApiSubscriptionsMeResponse, await res.json()) };