UNPKG

@twilio-labs/serverless-api

Version:
182 lines (181 loc) 7.57 kB
"use strict"; /** @module @twilio-labs/serverless-api/dist/api */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeEnvironmentVariables = exports.deleteEnvironmentVariable = exports.setEnvironmentVariables = exports.listVariablesForEnvironment = void 0; const debug_1 = __importDefault(require("debug")); const error_1 = require("../utils/error"); const pagination_1 = require("./utils/pagination"); const type_checks_1 = require("./utils/type-checks"); const log = (0, debug_1.default)('twilio-serverless-api:variables'); /** * Creates a new environment variable for a given environment * * @param {string} key the name of the variable * @param {string} value the value of the variable * @param {string} environmentSid the environment the variable should be created for * @param {string} serviceSid the service that the environment belongs to * @param {TwilioServerlessApiClient} client API client * @returns {Promise<VariableResource>} */ async function registerVariableInEnvironment(key, value, environmentSid, serviceSid, client) { try { const resp = await client.request('post', `Services/${serviceSid}/Environments/${environmentSid}/Variables`, { form: { Key: key, Value: value, }, }); return resp.body; } catch (err) { log('%O', new error_1.ClientApiError(err)); throw err; } } /** * Given the SID of a variable it will update the name and value of the variable * * @param {string} key the name of the variable * @param {string} value the value of the variable * @param {string} variableSid the SID of the existing variable * @param {string} environmentSid the environment the variable belongs to * @param {string} serviceSid the service the environment belongs to * @param {TwilioServerlessApiClient} client API client * @returns {Promise<VariableResource>} */ async function updateVariableInEnvironment(key, value, variableSid, environmentSid, serviceSid, client) { try { const resp = await client.request('post', `Services/${serviceSid}/Environments/${environmentSid}/Variables/${variableSid}`, { form: { Key: key, Value: value, }, }); return resp.body; } catch (err) { log('%O', new error_1.ClientApiError(err)); throw err; } } /** * Lists all variables for a given environment * * @export * @param {string} environmentSid the environment to get the variables for * @param {string} serviceSid the service the environment belongs to * @param {TwilioServerlessApiClient} client API client * @returns {Promise<VariableResource[]>} */ async function listVariablesForEnvironment(environmentSid, serviceSid, client) { try { return (0, pagination_1.getPaginatedResource)(client, `Services/${serviceSid}/Environments/${environmentSid}/Variables`); } catch (err) { log('%O', new error_1.ClientApiError(err)); throw err; } } exports.listVariablesForEnvironment = listVariablesForEnvironment; /** * Convers an object of environment variables into an array of key-value pairs * * @param {EnvironmentVariables} env the object of environment variables * @returns {Variable[]} */ function convertToVariableArray(env) { const output = []; Object.keys(env).forEach((key) => { const value = env[key]; if (typeof value === 'string' || typeof value === 'number') { output.push({ key, value: `${value}` }); } }); return output; } /** * Sets or updates the values passed in an object of environment variables for a specfic environment * * @export * @param {EnvironmentVariables} envVariables the object of variables * @param {string} environmentSid the environment the varibales should be set for * @param {string} serviceSid the service the environment belongs to * @param {TwilioServerlessApiClient} client API client * @param {boolean} [removeRedundantOnes=false] whether to remove variables that are not passed but are currently set * @returns {Promise<void>} */ async function setEnvironmentVariables(envVariables, environmentSid, serviceSid, client, removeRedundantOnes = false) { const existingVariables = await listVariablesForEnvironment(environmentSid, serviceSid, client); const variables = convertToVariableArray(envVariables); const variableResources = variables.map((variable) => { const existingResource = existingVariables.find((res) => res.key === variable.key); if (!existingResource) { return registerVariableInEnvironment(variable.key, variable.value, environmentSid, serviceSid, client); } if (existingResource.value === variable.value) { return Promise.resolve(existingResource); } return updateVariableInEnvironment(variable.key, variable.value, existingResource.sid, environmentSid, serviceSid, client); }); await Promise.all(variableResources); if (removeRedundantOnes) { const removeVariablePromises = existingVariables.map(async (variable) => { if (typeof envVariables[variable.key] === 'undefined') { return deleteEnvironmentVariable(variable.sid, environmentSid, serviceSid, client); } }); await Promise.all(removeVariablePromises); } } exports.setEnvironmentVariables = setEnvironmentVariables; /** * Deletes a given variable from a given environment * * @export * @param {string} variableSid the SID of the variable to delete * @param {string} environmentSid the environment the variable belongs to * @param {string} serviceSid the service the environment belongs to * @param {TwilioServerlessApiClient} client API client instance * @returns {Promise<boolean>} */ async function deleteEnvironmentVariable(variableSid, environmentSid, serviceSid, client) { try { const resp = await client.request('delete', `Services/${serviceSid}/Environments/${environmentSid}/Variables/${variableSid}`); return true; } catch (err) { log('%O', new error_1.ClientApiError(err)); throw err; } } exports.deleteEnvironmentVariable = deleteEnvironmentVariable; /** * Deletes all variables matching the passed keys from an environment * * @export * @param {string[]} keys the keys of the variables to delete * @param {string} environmentSid the environment the variables belong to * @param {string} serviceSid the service the environment belongs to * @param {TwilioServerlessApiClient} client API client instance * @returns {Promise<boolean>} */ async function removeEnvironmentVariables(keys, environmentSid, serviceSid, client) { const existingVariables = await listVariablesForEnvironment(environmentSid, serviceSid, client); const variableSidMap = new Map(); existingVariables.forEach((variableResource) => { variableSidMap.set(variableResource.key, variableResource.sid); }); const requests = keys.map((key) => { const variableSid = variableSidMap.get(key); if ((0, type_checks_1.isSid)(variableSid)) { return deleteEnvironmentVariable(variableSid, environmentSid, serviceSid, client); } return Promise.resolve(true); }); await Promise.all(requests); return true; } exports.removeEnvironmentVariables = removeEnvironmentVariables;