import {z} from 'zod' export type GetApiV1PlanArgs = {after?: string | undefined, before?: string | undefined, limit?: number | undefined, page?: number | undefined, since?: string | undefined, sort?: ('asc' | 'desc') | undefined, until?: string | undefined}; export type GetApiV1PlanOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const modelsBillingSource = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const modelsPlanType = z.enum(["1", "2", "3"]); export const modelsBillingScheme = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const modelsRecurringInterval = z.enum(["Week", "Month", "Year"]); export const modelsRecurringUsageType = z.union([z.literal(1), z.literal(2), z.literal(3)]); export const planPlanResponse = z.object({id: z.string().optional(), org_id: z.string().optional(), source: modelsBillingSource.optional(), name: modelsPlanType.optional(), type: z.string().optional(), billing_scheme: modelsBillingScheme.optional(), price: z.string().optional(), currency: z.string().optional(), available_quantity: z.number().int().optional(), trial_period: z.string().optional(), recurring_interval: modelsRecurringInterval.optional(), recurring_interval_count: z.number().int().optional(), recurring_usage_type: modelsRecurringUsageType.optional(), reference: z.string().optional(), enabled: z.boolean().optional(), v1: z.string().optional(), created_by: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), deleted_at: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional(), properties: z.record(z.string(), z.unknown()).optional()}); export const internalPaginatedItemsPlanPlanResponse = z.object({items: z.array(planPlanResponse).optional(), last_update: z.string().optional(), next: z.number().int().optional(), total: z.number().int().optional()}); export const internalPaginatedResponsePlanPlanResponse = z.object({data: internalPaginatedItemsPlanPlanResponse.optional(), error: z.boolean().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 }; /** * List Plans * * GET /v1/plan */ export const getApiV1Plan = async (args: GetApiV1PlanArgs, options: GetApiV1PlanOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.since != null) query.append("since", String(args.since)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.until != null) query.append("until", String(args.until)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/plan${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(internalPaginatedResponsePlanPlanResponse, await res.json()) };