import {z} from 'zod' export type GetApiRedisDatabaseIdArgs = {id: string, credentials?: string | undefined}; export type GetApiRedisDatabaseIdOptions = {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 }; /** * Get Database * * GET /redis/database/{id} */ export const getApiRedisDatabaseId = async (args: GetApiRedisDatabaseIdArgs, options?: GetApiRedisDatabaseIdOptions) => { const {baseUrl = "https://api.upstash.com/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.credentials != null) query.append("credentials", String(args.credentials)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/redis/database/${encodeURIComponent(String(args.id))}${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(database, await res.json()) };