import {z} from 'zod' export type GetApiBillingSubscriptionOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionResponse = z.object({balance_display: z.string(), balance_microdollars: z.number().int(), billing_cycle: z.enum(["monthly", "yearly"]), cancel_at_period_end: z.boolean(), canceled_at: z.string().optional(), current_period_end_at: z.string(), current_period_start_at: z.string(), id: z.string(), included_balance_display: z.string(), included_balance_microdollars: z.number().int(), max_concurrent_runs: z.number().int(), monthly_price_microdollars: z.number().int(), pending_downgrade_at: z.string().optional(), pending_downgrade_plan_id: z.string().optional(), plan_id: z.string(), plan_name: z.string(), price_per_second_microdollars: z.number().int(), status: z.enum(["active", "canceled", "past_due", "trialing", "incomplete", "incomplete_expired", "unpaid"]), trial_end_at: z.string().optional(), yearly_price_microdollars: z.number().int()}); 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 }; /** * Get current plan * * GET /billing/subscription */ export const getApiBillingSubscription = async (options?: GetApiBillingSubscriptionOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/billing/subscription${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(subscriptionResponse, await res.json()) };