@twilio-labs/serverless-api
Version:
API-wrapper for the Twilio Serverless API
134 lines (133 loc) • 5.16 kB
JavaScript
/** @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.createEnvironmentIfNotExists = exports.getEnvironmentFromSuffix = exports.listEnvironments = exports.createEnvironmentFromSuffix = exports.getEnvironment = exports.isEnvironmentSid = void 0;
const debug_1 = __importDefault(require("debug"));
const error_1 = require("../utils/error");
const pagination_1 = require("./utils/pagination");
const log = (0, debug_1.default)('twilio-serverless-api:environments');
/**
* Generates a unique name for an environment given a domain suffix
*
* @param {string} suffix
* @returns {string}
*/
function getUniqueNameFromSuffix(suffix) {
return suffix.length === 0 ? 'production' : `${suffix}-environment`;
}
/**
* Checks if a string is an environment SID by checking its prefix and length
*
* @export
* @param {string} str the string to check
* @returns
*/
function isEnvironmentSid(str) {
return str.startsWith('ZE') && str.length === 34;
}
exports.isEnvironmentSid = isEnvironmentSid;
/**
* Retrieves a specific environment using the API
*
* @export
* @param {Sid} environmentSid the environment to retrieve
* @param {Sid} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client
* @returns {Promise<EnvironmentResource>}
*/
async function getEnvironment(environmentSid, serviceSid, client) {
const resp = await client.request('get', `Services/${serviceSid}/Environments/${environmentSid}`);
return resp.body;
}
exports.getEnvironment = getEnvironment;
/**
* Creates a new environment given a domain suffix
*
* @export
* @param {string} domainSuffix the domain suffix for the environment
* @param {string} serviceSid the service to create the environment for
* @param {TwilioServerlessApiClient} client API client
* @returns {Promise<EnvironmentResource>}
*/
async function createEnvironmentFromSuffix(domainSuffix, serviceSid, client) {
const uniqueName = getUniqueNameFromSuffix(domainSuffix);
const form = {
UniqueName: uniqueName,
// this property currently doesn't exist but for the future lets set it
FriendlyName: `${uniqueName} Environment (Created by CLI)`,
};
if (domainSuffix !== '') {
form.DomainSuffix = domainSuffix;
}
const resp = await client.request('post', `Services/${serviceSid}/Environments`, { form });
return resp.body;
}
exports.createEnvironmentFromSuffix = createEnvironmentFromSuffix;
/**
* Lists all environments for a given service
*
* @export
* @param {string} serviceSid the service that the environments belong to
* @param {TwilioServerlessApiClient} client API client
* @returns
*/
async function listEnvironments(serviceSid, client) {
return (0, pagination_1.getPaginatedResource)(client, `Services/${serviceSid}/Environments`);
}
exports.listEnvironments = listEnvironments;
/**
* Looks up an environment given a domain suffix.
*
* @export
* @param {string} domainSuffix the suffix to look for
* @param {string} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client
* @returns {Promise<EnvironmentResource>}
*/
async function getEnvironmentFromSuffix(domainSuffix, serviceSid, client) {
const environments = await listEnvironments(serviceSid, client);
let foundEnvironments = environments.filter((e) => e.domain_suffix === domainSuffix ||
(domainSuffix.length === 0 && e.domain_suffix === null));
let env;
if (foundEnvironments.length > 1) {
// this is an edge case where at one point you could create environments with the same domain suffix
// we'll pick the one that suits our naming convention
env = foundEnvironments.find((e) => e.domain_suffix === domainSuffix &&
e.unique_name === getUniqueNameFromSuffix(domainSuffix));
}
else {
env = foundEnvironments[0];
}
if (!env) {
throw new Error('Could not find environment');
}
return env;
}
exports.getEnvironmentFromSuffix = getEnvironmentFromSuffix;
/**
* Creates an environment if non with the given suffix exists
*
* @export
* @param {string} domainSuffix the domain suffix of the environment
* @param {string} serviceSid the service the environment belongs to
* @param {TwilioServerlessApiClient} client API client
* @returns
*/
async function createEnvironmentIfNotExists(domainSuffixOrSid, serviceSid, client) {
if (isEnvironmentSid(domainSuffixOrSid)) {
return getEnvironment(domainSuffixOrSid, serviceSid, client);
}
return getEnvironmentFromSuffix(domainSuffixOrSid, serviceSid, client).catch((err) => {
try {
return createEnvironmentFromSuffix(domainSuffixOrSid, serviceSid, client);
}
catch (err) {
log('%O', new error_1.ClientApiError(err));
throw err;
}
});
}
exports.createEnvironmentIfNotExists = createEnvironmentIfNotExists;
;