new readSetting() and readSecret() APIs
This commit is contained in:
parent
ef4f6ad171
commit
3af82495d1
@ -23,3 +23,20 @@ export async function readSecrets(keys: string[]): Promise<any[]> {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read SECRETS page and retrieve a specific secret
|
||||||
|
export async function readSecret(key: string): Promise<any> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -38,6 +38,23 @@ export async function readSettings<T extends object>(settings: T): Promise<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function readSetting(
|
||||||
|
key: string,
|
||||||
|
defaultValue?: any,
|
||||||
|
): Promise<any> {
|
||||||
|
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.
|
* Convenience function to write a specific set of settings from the `SETTINGS` page.
|
||||||
* If the SETTiNGS page doesn't exist it will create it.
|
* If the SETTiNGS page doesn't exist it will create it.
|
||||||
|
Loading…
Reference in New Issue
Block a user