From 3af82495d126330863599c36a7b3df54b91efcce Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 16 Dec 2022 16:35:23 +0100 Subject: [PATCH] new readSetting() and readSecret() APIs --- plug-api/lib/secrets_page.ts | 17 +++++++++++++++++ plug-api/lib/settings_page.ts | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/plug-api/lib/secrets_page.ts b/plug-api/lib/secrets_page.ts index ff888c1..7dd1eaf 100644 --- a/plug-api/lib/secrets_page.ts +++ b/plug-api/lib/secrets_page.ts @@ -23,3 +23,20 @@ export async function readSecrets(keys: string[]): Promise { throw e; } } + +// Read SECRETS page and retrieve a specific secret +export async function readSecret(key: string): Promise { + try { + const allSecrets = await readYamlPage("SECRETS", ["yaml", "secrets"]); + const val = allSecrets[key]; + if (val === undefined) { + throw new Error(`No such secret: ${key}`); + } + return val; + } catch (e: any) { + if (e.message === "Page not found") { + throw new Error(`No such secret: ${key}`); + } + throw e; + } +} diff --git a/plug-api/lib/settings_page.ts b/plug-api/lib/settings_page.ts index 0181af6..1a11b4a 100644 --- a/plug-api/lib/settings_page.ts +++ b/plug-api/lib/settings_page.ts @@ -38,6 +38,23 @@ export async function readSettings(settings: T): Promise { } } +export async function readSetting( + key: string, + defaultValue?: any, +): Promise { + try { + const allSettings = (await readYamlPage(SETTINGS_PAGE, ["yaml"])) || {}; + const val = allSettings[key]; + return val === undefined ? defaultValue : val; + } catch (e: any) { + if (e.message === "Page not found") { + // No settings yet, return default values for all + return defaultValue; + } + throw e; + } +} + /** * Convenience function to write a specific set of settings from the `SETTINGS` page. * If the SETTiNGS page doesn't exist it will create it.