import { JsonValue } from "type-fest";
import { Archon } from "../Archon.js";

/**
 * Get a secret from the Archon configurator secret store. This includes database passwords, API keys, and other sensitive information.
 * 
 * Make sure the application calling this API has the appropriate permissions to access the secret.
 * @param key The key of the secret to retrieve.
 * @returns The secret value.
 */
export default async function getSecret(key: string): Promise<JsonValue> {
    const response = await Archon.request(`/config/secret/${key}`, "GET");

    if (response.status !== 200) {
        throw new Error(`Failed to retrieve configuration value for key ${key}.`);
    }

    const data = response.data;
    return data.value;
}