import {z} from 'zod' export type GetApiSpaceSpaceIdBillingSubscriptionArgs = {spaceId: string}; export type GetApiSpaceSpaceIdBillingSubscriptionOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSpaceSpaceIdBillingSubscriptionResponse = z.object({base: z.object({id: z.string(), type: z.enum(["base", "credit", "rowCount", "attachmentSize", "automationRun"]), catalog: z.enum(["cloud", "self-hosted"]), level: z.enum(["free", "pro", "business", "enterprise"]), limit: z.object({maxRows: z.number(), maxSizeAttachments: z.number(), maxNumAutomationRuns: z.number(), maxNumDatabaseConnections: z.number(), maxRevisionHistoryDays: z.number(), maxAutomationHistoryDays: z.number(), automationEnable: z.boolean(), auditLogEnable: z.boolean(), adminPanelEnable: z.boolean(), rowColoringEnable: z.boolean(), buttonFieldEnable: z.boolean(), fieldAIEnable: z.boolean(), userGroupEnable: z.boolean(), advancedExtensionsEnable: z.boolean(), advancedPermissionsEnable: z.boolean(), passwordRestrictedSharesEnable: z.boolean(), authenticationEnable: z.boolean(), domainVerificationEnable: z.boolean(), organizationEnable: z.boolean(), apiRateLimit: z.number(), chatAIEnable: z.boolean(), appEnable: z.boolean(), customDomainEnable: z.boolean(), maxNumAutomationSendEmail: z.number()}), creditSubscribeEnable: z.boolean().optional(), status: z.enum(["active", "canceled", "incomplete", "incomplete_expired", "trialing", "past_due", "unpaid", "paused", "seat_limit_exceeded"]), quantity: z.number(), priceId: z.string().nullable(), unitAmount: z.number(), interval: z.enum(["month", "year"]), currentPeriodStart: z.string().nullable(), currentPeriodEnd: z.string().nullable(), cancelAt: z.string().nullable(), isTrialUsed: z.boolean().optional(), period: z.enum(["month", "year", "lifetime"]).optional(), appSumoTier: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]).optional()}), credit: z.object({id: z.string(), type: z.enum(["base", "credit", "rowCount", "attachmentSize", "automationRun"]), catalog: z.enum(["cloud", "self-hosted"]), unitAmount: z.number(), quantity: z.number(), interval: z.enum(["month", "year"]), cancelAt: z.string().nullable()}).optional(), rowCount: z.object({id: z.string(), type: z.enum(["base", "credit", "rowCount", "attachmentSize", "automationRun"]), catalog: z.enum(["cloud", "self-hosted"]), unitAmount: z.number(), quantity: z.number(), interval: z.enum(["month", "year"]), cancelAt: z.string().nullable()}).optional(), attachmentSize: z.object({id: z.string(), type: z.enum(["base", "credit", "rowCount", "attachmentSize", "automationRun"]), catalog: z.enum(["cloud", "self-hosted"]), unitAmount: z.number(), quantity: z.number(), interval: z.enum(["month", "year"]), cancelAt: z.string().nullable()}).optional(), automationRun: z.object({id: z.string(), type: z.enum(["base", "credit", "rowCount", "attachmentSize", "automationRun"]), catalog: z.enum(["cloud", "self-hosted"]), unitAmount: z.number(), quantity: z.number(), interval: z.enum(["month", "year"]), cancelAt: z.string().nullable()}).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 }; /** * Get subscription detail by spaceId * * GET /space/{spaceId}/billing/subscription */ export const getApiSpaceSpaceIdBillingSubscription = async (args: GetApiSpaceSpaceIdBillingSubscriptionArgs, options?: GetApiSpaceSpaceIdBillingSubscriptionOptions) => { const {baseUrl = "https://app.teable.ai/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/space/${encodeURIComponent(String(args.spaceId))}/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(getApiSpaceSpaceIdBillingSubscriptionResponse, await res.json()) };