import {z} from 'zod' export type GetApiApiV1BillingInvoicesOptions = {baseUrl?: string | undefined, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonValue: z.ZodType = z.union([z.string(), z.number(), z.boolean(), z.array(z.lazy(() => jsonValue)), z.record(z.string(), z.lazy(() => jsonValue))]).nullable(); export const billingSubscription = z.object({id: z.number().int().optional(), stripe_subscription_id: z.string().optional(), stripe_customer_id: z.string().nullable().optional(), plan: z.string().optional(), billing: z.string().nullable().optional(), amount_cents: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), status: z.string().optional(), current_period_start: z.string().nullable().optional(), current_period_end: z.string().nullable().optional(), cancel_at_period_end: z.union([z.number().int(), z.boolean()]).optional(), canceled_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).and(z.record(z.string(), jsonValue)); export const billingInvoice = z.object({id: z.number().int().optional(), stripe_invoice_id: z.string().optional(), invoice_number: z.string().nullable().optional(), amount_paid_cents: z.number().int().nullable().optional(), amount_due_cents: z.number().int().nullable().optional(), subtotal_cents: z.number().int().nullable().optional(), tax_cents: z.number().int().nullable().optional(), tax_rate_pct: z.number().nullable().optional(), currency: z.string().nullable().optional(), status: z.string().nullable().optional(), invoice_pdf: z.string().nullable().optional(), hosted_invoice_url: z.string().nullable().optional(), period_start: z.string().nullable().optional(), period_end: z.string().nullable().optional(), paid_at: z.string().nullable().optional(), created_at: z.string().optional()}).and(z.record(z.string(), jsonValue)); export const billingInvoicesResponse = z.object({ok: z.boolean(), subscription: billingSubscription.nullable(), invoices: z.array(billingInvoice)}); 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 the authenticated user's invoices and subscription * * GET /api/v1/billing/invoices */ export const getApiApiV1BillingInvoices = async (options?: GetApiApiV1BillingInvoicesOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (cookieAuth !== undefined) cookies.push("ob_sess=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/billing/invoices${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(billingInvoicesResponse, await res.json()) };