module twilio-labs import debug from 'debug' ; import querystring , { ParsedUrlQueryInput } from 'querystring' ; import { TwilioServerlessApiClient } from '../client' ; import { BuildConfig , BuildList , BuildResource , BuildStatus , BuildStatusResource , } from '../types' ; import { DeployStatus } from '../types/consts' ; import { ClientApiError } from '../utils/error' ; import { sleep } from '../utils/sleep' ; import { getPaginatedResource } from './utils/pagination' ; import events = require ( 'events' ) ; const log = debug ( 'twilio-serverless-api:builds' ) ; export param { string } buildSid param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < BuildResource > } export async function getBuild ( buildSid , serviceSid , client ) { const resp = await client . request ( 'get' , `Services/${ serviceSid }/Builds/${ buildSid }` ) ; return ( resp . body as unknown ) as BuildResource ; } param { string } buildSid param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < BuildStatus > } export async function getBuildStatus ( buildSid , serviceSid , client ) { try { const resp = await client . request ( 'get' , `Services/${ serviceSid }/Builds/${ buildSid }/Status` ) ; return ( ( resp . body as unknown ) as BuildStatusResource ) . status ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } } export param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < BuildResource [ ] > } export async function listBuilds ( serviceSid , client ) { return getPaginatedResource < BuildList , BuildResource > ( client , `Services/${ serviceSid }/Builds` ) ; } export param { BuildConfig } config param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < BuildResource > } export async function triggerBuild ( config , serviceSid , client ) { const { functionVersions , dependencies , assetVersions , runtime } = config ; try { const body = { } ; if ( Array . isArray ( dependencies ) && dependencies . length > 0 ) { const dependencyString = `"${ JSON . stringify ( dependencies ) }"` ; body . Dependencies = dependencyString ; } if ( Array . isArray ( functionVersions ) && functionVersions . length > 0 ) { body . FunctionVersions = functionVersions ; } if ( Array . isArray ( assetVersions ) && assetVersions . length > 0 ) { body . AssetVersions = assetVersions ; } if ( runtime ) { body . Runtime = runtime ; } const resp = await client . request ( 'post' , `Services/${ serviceSid }/Builds` , { responseType : 'json' , headers : { 'Content-Type' : 'application/x-www-form-urlencoded' , } , body : querystring . stringify ( body ) , } ) ; return resp . body as BuildResource ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } } export param { string } buildSid param { string } serviceSid param { TwilioServerlessApiClient } client param { events . EventEmitter } eventEmitter param { number } timeout returns { Promise < void > } export function waitForSuccessfulBuild ( buildSid , serviceSid , client , eventEmitter , timeout = 5 * 60 * 1000 ) { return new Promise ( async ( resolve , reject ) => { const startTime = Date . now ( ) ; let isBuilt = false ; while ( ! isBuilt ) { if ( Date . now ( ) - startTime > timeout ) { eventEmitter . emit ( 'status-update' , { status : DeployStatus . TIMED_OUT , message : 'Deployment took too long' , } ) ; reject ( new Error ( 'Timeout' ) ) ; } const status = await getBuildStatus ( buildSid , serviceSid , client ) ; isBuilt = status === 'completed' ; if ( isBuilt ) { break ; } const hasFailed = status === 'failed' ; if ( hasFailed ) { reject ( status ) ; return ; } eventEmitter . emit ( 'status-update' , { status : DeployStatus . BUILDING , message : `Waiting for deployment. Current status: ${ status }` , } ) ; await sleep ( 1000 ) ; } resolve ( ) ; } ) ; } export param { string } buildSid param { string } environmentSid param { string } serviceSid param { TwilioServerlessApiClient } client returns { Promise < any > } export async function activateBuild ( buildSid , environmentSid , serviceSid , client ) { try { const resp = await client . request ( 'post' , `Services/${ serviceSid }/Environments/${ environmentSid }/Deployments` , { form : { BuildSid : buildSid , } , } ) ; return resp . body ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } }