import {z} from 'zod' export type ServiceTabsGetParametersStatus = 'open' | 'closing' | 'paid' | 'voided' | 'abandoned'; export type GetApiServiceTabsArgs = {status?: ServiceTabsGetParametersStatus | undefined, from_time?: string | undefined, until_time?: string | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiServiceTabsOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceTabStatus = z.enum(["open", "closing", "paid", "voided", "abandoned"]); export const serviceTabPaymentStrategy = z.enum(["card_on_file", "preauthorization"]); export const serviceTabOpeningCommitment = z.enum(["none", "manual_amount", "product", "products"]); export const serviceTabItem = z.object({id: z.string().optional(), price_id: z.string().optional(), name: z.string().optional(), quantity: z.number().int().optional(), amount_in_cents: z.number().int().optional(), currency: z.string().optional()}); export const serviceTab = z.object({id: z.string().optional(), status: serviceTabStatus.optional(), display_name: z.string().optional(), phone: z.string().nullable().optional(), customer_id: z.string().optional(), currency: z.string().optional(), total_in_cents: z.number().int().optional(), payment_strategy: serviceTabPaymentStrategy.optional(), opening_commitment: serviceTabOpeningCommitment.optional(), items: z.array(serviceTabItem).optional(), authorized_amount_in_cents: z.number().int().optional(), authorization_expires_at: z.string().nullable().optional(), checkout_url: z.string().nullable().optional(), opened_at: z.string().optional(), closed_at: z.string().nullable().optional(), created_at: z.string().optional()}); export const getApiServiceTabsResponse = z.array(serviceTab); 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 cuentas abiertas * * GET /service_tabs */ export const getApiServiceTabs = async (args: GetApiServiceTabsArgs, options?: GetApiServiceTabsOptions) => { const {baseUrl = "https://app.recurrente.com/api", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.from_time != null) query.append("from_time", String(args.from_time)) if (args.until_time != null) query.append("until_time", String(args.until_time)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (secretKey !== undefined) headers.set("X-SECRET-KEY", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/service_tabs${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(getApiServiceTabsResponse, await res.json()) };