module twilio-labs import debug from 'debug' ; import { TwilioServerlessApiClient } from '../client' ; import { ServiceList , ServiceResource , Sid } from '../types' ; import { ClientApiError } from '../utils/error' ; import { getPaginatedResource } from './utils/pagination' ; const log = debug ( 'twilio-serverless-api:services' ) ; export param { string } serviceName param { TwilioServerlessApiClient } client returns { Promise < string > } export async function createService ( serviceName , client ) { try { const resp = await client . request ( 'post' , 'Services' , { form : { UniqueName : serviceName , FriendlyName : serviceName , IncludeCredentials : true , } , } ) ; const service = ( resp . body as unknown ) as ServiceResource ; return service . sid ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } } export param { TwilioServerlessApiClient } client returns { Promise < ServiceResource [ ] > } export async function listServices ( client ) { return getPaginatedResource < ServiceList , ServiceResource > ( client , 'Services' ) ; } export param { string } uniqueName param { TwilioServerlessApiClient } client returns { ( Promise < string | undefined > ) } export async function findServiceSid ( uniqueName , client ) { try { const services = await listServices ( client ) ; for ( let service of services ) { if ( service . unique_name === uniqueName ) { return service . sid ; } } } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } return undefined ; } export async function getService ( sid , client ) { try { const resp = await client . request ( 'get' , `Services/${ sid }` ) ; return ( resp . body as unknown ) as ServiceResource ; } catch ( err ) { log ( '%O' , new ClientApiError ( err ) ) ; throw err ; } }