// SPDX-License-Identifier: MIT import { validate as validateUuid } from "uuid"; import { getConfig } from "@slawek/sk-tools"; type Config = { tenantId: string; clientId: string; }; export async function loadConfig(configName: string): Promise { if (typeof configName !== "string" || configName.trim() === "") { throw new Error( 'Invalid config name. Expected a non-empty string like "public-config" or "confidential-config".', ); } const envConfig = { tenantId: process.env.AZURE_TENANT_ID, clientId: process.env.AZURE_CLIENT_ID, }; const json = (await getConfig("sk-az-tools", configName)) as Record; const tenantId = (typeof json.tenantId === "string" && json.tenantId ? json.tenantId : envConfig.tenantId) ?? ""; const clientId = (typeof json.clientId === "string" && json.clientId ? json.clientId : envConfig.clientId) ?? ""; if (!validateUuid(tenantId ?? "") || !validateUuid(clientId ?? "")) { throw new Error("tenantId and clientId must be valid GUIDs."); } return { tenantId, clientId, }; }