UNPKG

@exweiv/wix-secret-helpers

Version:

Some basic helper functions for secrets in Wix, works with @wix/secrets SDK module.

83 lines (82 loc) 3.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSecretValue = getSecretValue; const node_cache_1 = __importDefault(require("@cacheable/node-cache")); const essentials_1 = require("@wix/essentials"); const secrets_1 = require("@wix/secrets"); const neverthrow_1 = require("neverthrow"); const PREFIX = "Secret Helpers Error:"; const secretsCache = new node_cache_1.default({ stdTTL: 360, deleteOnExpire: true, checkperiod: 120 }); const parsedCache = new node_cache_1.default({ stdTTL: 360, deleteOnExpire: true, checkperiod: 120 }); async function getSecretValue({ secretName, elevateAccess = true, cacheEnabled = true, parseJSON = false }) { if (!secretName) { throw new Error(`${PREFIX} secretName must be a valid string value!`); } if (cacheEnabled === false) { const response = await getSecret(secretName, parseJSON, elevateAccess); if (response.isOk()) { return response.value; } else { throw new Error(response.error.message); } } const cachedSecret = secretsCache.get(secretName); if (cachedSecret) { const response = parseJSON ? returnWithParse(cachedSecret, secretName) : (0, neverthrow_1.ok)(cachedSecret); if (response.isOk()) { return response.value; } else { throw new Error(response.error.message); } } else { const response = await getSecret(secretName, parseJSON, elevateAccess); if (response.isOk()) { return response.value; } else { throw new Error(response.error.message); } } } function returnWithParse(jsonString, secretName) { try { if (parsedCache.has(secretName)) { const cachedParsed = parsedCache.get(secretName); return (0, neverthrow_1.ok)(cachedParsed); } const parsed = JSON.parse(jsonString); parsedCache.set(secretName, parsed); return (0, neverthrow_1.ok)(parsed); } catch (e) { const error = e instanceof Error ? e : new Error(String(e)); return (0, neverthrow_1.err)(error); } } async function getSecret(secretName, parseJSON = false, elevateAccess = true) { const getSecretValue = elevateAccess ? essentials_1.auth.elevate(secrets_1.secrets.getSecretValue) : secrets_1.secrets.getSecretValue; const wixSecretResponse = await (0, neverthrow_1.fromPromise)(getSecretValue(secretName), (e) => (e instanceof Error ? e : new Error(String(e)))); if (wixSecretResponse.isOk()) { const { value } = wixSecretResponse.value; if (!value) { return (0, neverthrow_1.err)(new Error(`${PREFIX} secret value is undefined! Response from Wix: ${value}`)); } secretsCache.set(secretName, value); if (parseJSON) { return returnWithParse(value, secretName); } else { return (0, neverthrow_1.ok)(value); } } else { return (0, neverthrow_1.err)(new Error(`${PREFIX} unable to get the secret value from Wix!\n ${wixSecretResponse.error.message}`)); } } exports.default = { getSecretValue };