Your Name 817309ade9
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: add sqlserver admin connection settings
2026-03-24 17:01:50 +08:00

67 lines
2.2 KiB
TypeScript

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<typeof thirdPartySqlServerConfigSchema>;
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,
};
}