import {z} from 'zod' export type ModelsRecurringInterval = 'Week' | 'Month' | 'Year'; export type PlanCreatePlanRequest = {amount?: string | undefined, currency?: string | undefined, description?: string | undefined, interval?: ModelsRecurringInterval | undefined, intervalCount?: number | undefined, metadata?: {[key: string]: unknown} | undefined, name?: string | undefined, reference?: string | undefined, trial_period?: string | undefined}; export type CreateApiV1PlanArgs = {body: PlanCreatePlanRequest}; export type CreateApiV1PlanOptions = {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 internalResponsePlanPlanResponse = z.object({data: planPlanResponse.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 }; /** * Create Plan * * POST /v1/plan */ export const createApiV1Plan = async (args: CreateApiV1PlanArgs, options: CreateApiV1PlanOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(internalResponsePlanPlanResponse, await res.json()) };