import {z} from 'zod' export type GetApiPaymentsBillingArgs = {'x-api-key'?: string | undefined, 'lmnt-auth0'?: (string | null) | undefined}; export type GetApiPaymentsBillingOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const scheduledChange = z.object({price_id: z.string(), plan_name: z.string().nullable().optional(), effective_at: z.number().int()}); export const subscriptionResponse = z.object({price_id: z.string().nullable().optional(), status: z.string().nullable().optional(), current_period_end: z.number().int().nullable().optional(), cancel_at_period_end: z.boolean().optional(), scheduled_change: scheduledChange.nullable().optional()}); export const paymentMethodResponse = z.object({brand: z.string(), last4: z.string()}); export const invoiceItem = z.object({id: z.string(), number: z.string().nullable().optional(), amount_paid: z.number().int(), currency: z.string(), status: z.string().nullable().optional(), created: z.number().int(), hosted_invoice_url: z.string().nullable().optional(), invoice_pdf: z.string().nullable().optional()}); export const billingOverviewResponse = z.object({subscription: subscriptionResponse, payment_method: paymentMethodResponse.nullable().optional(), invoices: z.array(invoiceItem).optional()}); 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 }; /** * Billing Overview Handler * * GET /payments/billing */ export const getApiPaymentsBilling = async (args: GetApiPaymentsBillingArgs, options: GetApiPaymentsBillingOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['lmnt-auth0'] != null) cookies.push("lmnt-auth0=" + encodeURIComponent(String(args['lmnt-auth0']))) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/payments/billing${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(billingOverviewResponse, await res.json()) };