UNPKG

@twilio-labs/serverless-api

Version:
153 lines (152 loc) 6.32 kB
"use strict"; /** @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.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>} */ function getEnvironment(environmentSid, serviceSid, client) { return __awaiter(this, void 0, void 0, function* () { const resp = yield 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>} */ function createEnvironmentFromSuffix(domainSuffix, serviceSid, client) { return __awaiter(this, void 0, void 0, function* () { 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 = yield 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 */ function listEnvironments(serviceSid, client) { return __awaiter(this, void 0, void 0, function* () { 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>} */ function getEnvironmentFromSuffix(domainSuffix, serviceSid, client) { return __awaiter(this, void 0, void 0, function* () { const environments = yield 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 */ function createEnvironmentIfNotExists(domainSuffixOrSid, serviceSid, client) { return __awaiter(this, void 0, void 0, function* () { 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;