module twilio-labs import debug from 'debug' ; import { TwilioServerlessApiClient } from '../client' ; import { EnvironmentVariables , Sid , Variable , VariableList , VariableResource , } from '../types' ; import { ClientApiError } from '../utils/error' ; import { getPaginatedResource } from './utils/pagination' ; import { isSid } from './utils/type-checks' ; const log = debug ( 'twilio-serverless-api:variables' ) ; param { string } key param { string } value param { string } environmentSid param { string } serviceSid param { TwilioServerlessApiClient } 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 as unknown ) as VariableResource ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } } param { string } key param { string } value param { string } variableSid param { string } environmentSid param { string } serviceSid param { TwilioServerlessApiClient } 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 as unknown ) as VariableResource ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } } export param { string } environmentSid param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < VariableResource [ ] > } export async function listVariablesForEnvironment ( environmentSid , serviceSid , client ) { try { return getPaginatedResource < VariableList , VariableResource > ( client , `Services/${ serviceSid }/Environments/${ environmentSid }/Variables` ) ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } } param { EnvironmentVariables } env 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 ; } export param { EnvironmentVariables } envVariables param { string } environmentSid param { string } serviceSid param { TwilioServerlessApiClient } client param { boolean } removeRedundantOnes returns { Promise < void > } export 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 ) ; } } export param { string } variableSid param { string } environmentSid param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < boolean > } export 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 ClientApiError ( err ) ) ; throw err ; } } export param { string [ ] } keys param { string } environmentSid param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < boolean > } export async function removeEnvironmentVariables ( keys , environmentSid , serviceSid , client ) { const existingVariables = await listVariablesForEnvironment ( environmentSid , serviceSid , client ) ; const variableSidMap = new Map < string , Sid > ( ) ; existingVariables . forEach ( ( variableResource ) => { variableSidMap . set ( variableResource . key , variableResource . sid ) ; } ) ; const requests = keys . map ( ( key ) => { const variableSid = variableSidMap . get ( key ) ; if ( isSid ( variableSid ) ) { return deleteEnvironmentVariable ( variableSid , environmentSid , serviceSid , client ) ; } return Promise . resolve ( true ) ; } ) ; await Promise . all ( requests ) ; return true ; }