import {z} from 'zod' export type CreateDatabaseRequest = {database_name: string, platform: 'aws' | 'gcp', primary_region: 'us-east-1' | 'us-east-2' | 'us-west-1' | 'us-west-2' | 'ca-central-1' | 'eu-central-1' | 'eu-west-1' | 'eu-west-2' | 'sa-east-1' | 'ap-south-1' | 'ap-northeast-1' | 'ap-southeast-1' | 'ap-southeast-2' | 'af-south-1' | 'us-central1' | 'us-east4' | 'europe-west1' | 'asia-northeast1', read_regions?: (Array<'us-east-1' | 'us-east-2' | 'us-west-1' | 'us-west-2' | 'ca-central-1' | 'eu-central-1' | 'eu-west-1' | 'eu-west-2' | 'sa-east-1' | 'ap-south-1' | 'ap-northeast-1' | 'ap-southeast-1' | 'ap-southeast-2'>) | undefined, plan?: ('free' | 'payg' | 'fixed_250mb' | 'fixed_1gb' | 'fixed_5gb' | 'fixed_10gb' | 'fixed_50gb' | 'fixed_100gb' | 'fixed_500gb') | undefined, budget?: number | undefined, eviction?: boolean | undefined, tls?: boolean | undefined}; export type CreateApiRedisDatabaseArgs = {body: CreateDatabaseRequest}; export type CreateApiRedisDatabaseOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const database = z.object({database_id: z.string().optional(), database_name: z.string().optional(), database_type: z.string().optional(), region: z.literal("global").optional(), port: z.number().int().optional(), creation_time: z.number().int().optional(), state: z.enum(["active", "suspended", "passive"]).optional(), endpoint: z.string().optional(), rest_token: z.string().optional(), read_only_rest_token: z.string().optional(), password: z.string().optional(), tls: z.boolean().optional(), db_daily_bandwidth_limit: z.number().int().optional(), db_max_clients: z.number().int().optional(), db_max_request_size: z.number().int().optional(), db_disk_threshold: z.number().int().optional(), db_max_entry_size: z.number().int().optional(), db_memory_threshold: z.number().int().optional(), db_max_commands_per_second: z.number().int().optional(), db_request_limit: z.number().int().optional(), type: z.enum(["free", "payg", "pro", "paid"]).optional(), budget: z.number().int().optional(), primary_region: z.enum(["us-east-1", "us-east-2", "us-west-1", "us-west-2", "ca-central-1", "eu-central-1", "eu-west-1", "eu-west-2", "sa-east-1", "ap-south-1", "ap-northeast-1", "ap-southeast-1", "ap-southeast-2", "af-south-1", "us-central1", "us-east4", "europe-west1", "asia-northeast1"]).optional(), primary_members: z.array(z.string()).optional(), all_members: z.array(z.string()).optional(), eviction: z.boolean().optional(), auto_upgrade: z.boolean().optional(), consistent: z.boolean().optional(), modifying_state: z.string().optional(), db_resource_size: z.enum(["S", "M", "L", "XL", "XXL", "3XL"]).optional(), db_type: z.enum(["bolt", "badger", "pebble"]).optional(), db_conn_idle_timeout: z.number().int().optional(), db_lua_timeout: z.number().int().optional(), db_lua_credits_per_min: z.number().int().optional(), db_store_max_idle: z.number().int().optional(), db_max_loads_per_sec: z.number().int().optional(), db_acl_enabled: z.enum(["true", "false"]).optional(), db_acl_default_user_status: z.enum(["true", "false"]).optional(), db_eviction: z.boolean().optional(), last_plan_upgrade_time: z.number().int().optional(), replicas: z.number().int().optional(), user_email: z.string().optional(), daily_backup_enabled: z.boolean().optional(), read_regions: z.array(z.string()).optional(), securityAddons: z.object({ipWhitelisting: z.boolean().optional(), vpcPeering: z.boolean().optional(), privateLink: z.boolean().optional(), tlsMutualAuth: z.boolean().optional(), encryptionAtRest: z.boolean().optional()}).optional(), prometheus_enabled: z.enum(["true", "false"]).optional(), prod_pack_enabled: 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 Redis Database * * POST /redis/database */ export const createApiRedisDatabase = async (args: CreateApiRedisDatabaseArgs, options?: CreateApiRedisDatabaseOptions) => { const {baseUrl = "https://api.upstash.com/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/redis/database${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(database, await res.json()) };