import { z } from "zod"; export const THIRD_PARTY_SQLSERVER_CONFIG_KEY = "third_party_sqlserver_config"; export const thirdPartySqlServerConfigSchema = z.object({ enabled: z.boolean().optional(), host: z.string().trim().optional(), port: z.coerce.number().int().min(1).max(65535).optional(), database: z.string().trim().optional(), username: z.string().trim().optional(), password: z.string().optional(), encrypt: z.boolean().optional(), trustServerCertificate: z.boolean().optional(), connectionTimeoutMs: z.coerce.number().int().min(1000).max(120000).optional(), requestTimeoutMs: z.coerce.number().int().min(1000).max(120000).optional(), }); export type ThirdPartySqlServerConfig = z.infer; export type NormalizedThirdPartySqlServerConfig = { enabled: boolean; host: string; port: number; database: string; username: string; password: string; encrypt: boolean; trustServerCertificate: boolean; connectionTimeoutMs: number; requestTimeoutMs: number; }; export const defaultThirdPartySqlServerConfig: NormalizedThirdPartySqlServerConfig = { enabled: true, host: "", port: 1433, database: "", username: "", password: "", encrypt: false, trustServerCertificate: true, connectionTimeoutMs: 15000, requestTimeoutMs: 30000, }; export function normalizeThirdPartySqlServerConfig( value: unknown, existingPassword = "", ): NormalizedThirdPartySqlServerConfig { const parsed = thirdPartySqlServerConfigSchema.parse(value || {}); return { enabled: parsed.enabled !== false, host: String(parsed.host || "").trim(), port: Number(parsed.port) || defaultThirdPartySqlServerConfig.port, database: String(parsed.database || "").trim(), username: String(parsed.username || "").trim(), password: String(parsed.password || "").trim() || String(existingPassword || "").trim(), encrypt: parsed.encrypt === true, trustServerCertificate: parsed.trustServerCertificate !== false, connectionTimeoutMs: Number(parsed.connectionTimeoutMs) || defaultThirdPartySqlServerConfig.connectionTimeoutMs, requestTimeoutMs: Number(parsed.requestTimeoutMs) || defaultThirdPartySqlServerConfig.requestTimeoutMs, }; }