liveperson-functions-cli
Version:
LivePerson Functions CLI
50 lines • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VaultSecretClient = void 0;
const secretError_1 = require("../errors/secretError");
const errorCodes_1 = require("../errors/errorCodes");
const path_1 = require("path");
const fsDefault = require("fs-extra");
class VaultSecretClient {
constructor(fs = fsDefault) {
this.path = (0, path_1.join)(process.cwd(), 'functions', 'settings.json');
this.fs = fs;
}
readSecret(key) {
return new Promise((resolve, reject) => {
try {
const settings = JSON.parse(this.fs.readFileSync(this.path, 'utf8'));
const secret = settings.secrets.find((e) => e.key === key);
if (!secret) {
throw Error;
}
resolve(secret);
}
catch (err) {
reject(new secretError_1.SecretError(errorCodes_1.ErrorCodes.Secret.NotFound, `There is no Secret ${key} for this account`));
}
});
}
updateSecret(updatedSecret) {
return new Promise((resolve, reject) => {
if (updatedSecret.value.length > 10000) {
throw new secretError_1.SecretError(errorCodes_1.ErrorCodes.Secret.Invalid, 'Provided Secret Value exceeds allowed length of 10000');
}
try {
const settings = JSON.parse(this.fs.readFileSync(this.path, 'utf8'));
const index = settings.secrets.findIndex((e) => e.key === updatedSecret.key);
if (index === -1) {
throw Error;
}
settings.secrets[index] = updatedSecret;
this.fs.writeFileSync(this.path, JSON.stringify(settings, null, 4));
resolve(updatedSecret);
}
catch {
reject(new secretError_1.SecretError(errorCodes_1.ErrorCodes.Secret.Failure, `Updating Secret ${updatedSecret.key} failed`));
}
});
}
}
exports.VaultSecretClient = VaultSecretClient;
//# sourceMappingURL=secretClient.js.map