@twilio-labs/serverless-api
Version:
API-wrapper for the Twilio Serverless API
203 lines (202 loc) • 8.96 kB
JavaScript
;
/** @module @twilio-labs/serverless-api/dist/api */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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>}
*/
function registerVariableInEnvironment(key, value, environmentSid, serviceSid, client) {
return __awaiter(this, void 0, void 0, function* () {
try {
const resp = yield 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>}
*/
function updateVariableInEnvironment(key, value, variableSid, environmentSid, serviceSid, client) {
return __awaiter(this, void 0, void 0, function* () {
try {
const resp = yield 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[]>}
*/
function listVariablesForEnvironment(environmentSid, serviceSid, client) {
return __awaiter(this, void 0, void 0, function* () {
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>}
*/
function setEnvironmentVariables(envVariables, environmentSid, serviceSid, client, removeRedundantOnes = false) {
return __awaiter(this, void 0, void 0, function* () {
const existingVariables = yield 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);
});
yield Promise.all(variableResources);
if (removeRedundantOnes) {
const removeVariablePromises = existingVariables.map((variable) => __awaiter(this, void 0, void 0, function* () {
if (typeof envVariables[variable.key] === 'undefined') {
return deleteEnvironmentVariable(variable.sid, environmentSid, serviceSid, client);
}
}));
yield 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>}
*/
function deleteEnvironmentVariable(variableSid, environmentSid, serviceSid, client) {
return __awaiter(this, void 0, void 0, function* () {
try {
const resp = yield 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>}
*/
function removeEnvironmentVariables(keys, environmentSid, serviceSid, client) {
return __awaiter(this, void 0, void 0, function* () {
const existingVariables = yield 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);
});
yield Promise.all(requests);
return true;
});
}
exports.removeEnvironmentVariables = removeEnvironmentVariables;