module twilio-labs import debug from 'debug' ; import { TwilioServerlessApiClient } from '../client' ; import { EnvironmentList , EnvironmentResource , Sid } from '../types' ; import { ClientApiError } from '../utils/error' ; import { getPaginatedResource } from './utils/pagination' ; const log = debug ( 'twilio-serverless-api:environments' ) ; param { string } suffix returns { string } function getUniqueNameFromSuffix ( suffix ) { return suffix . length === 0 ? 'production' : `${ suffix }-environment` ; } export param { string } str returns export function isEnvironmentSid ( str ) { return str . startsWith ( 'ZE' ) && str . length === 34 ; } export param { Sid } environmentSid param { Sid } serviceSid param { TwilioServerlessApiClient } client returns { Promise < EnvironmentResource > } export async function getEnvironment ( environmentSid , serviceSid , client ) { const resp = await client . request ( 'get' , `Services/${ serviceSid }/Environments/${ environmentSid }` ) ; return ( resp . body as unknown ) as EnvironmentResource ; } export param { string } domainSuffix param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < EnvironmentResource > } export async function createEnvironmentFromSuffix ( domainSuffix , serviceSid , client ) { const uniqueName = getUniqueNameFromSuffix ( domainSuffix ) ; const form = { UniqueName : uniqueName , FriendlyName : `${ uniqueName } Environment (Created by CLI)` , } ; if ( domainSuffix !== '' ) { form . DomainSuffix = domainSuffix ; } const resp = await client . request ( 'post' , `Services/${ serviceSid }/Environments` , { form } ) ; return ( resp . body as unknown ) as EnvironmentResource ; } export param { string } serviceSid param { TwilioServerlessApiClient } client returns export async function listEnvironments ( serviceSid , client ) { return getPaginatedResource < EnvironmentList , EnvironmentResource > ( client , `Services/${ serviceSid }/Environments` ) ; } export param { string } domainSuffix param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < EnvironmentResource > } export 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 ) { 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 ; } export param { string } domainSuffix param { string } serviceSid param { TwilioServerlessApiClient } client returns export 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 ClientApiError ( err ) ) ; throw err ; } } ) ; }