import * as express from 'express' ; import { Context } from './context' ; import Clova from './types' ; export class SkillConfigurator implements Clova . SkillConfigurator < Context > { public config : { requestHandlers : { [ index ] : ( ctx : Context ) => void ; } ; } ; constructor ( ) { this . config = { requestHandlers : { } , } ; } param { Clova . RequestHandlerType } requestType param { ( ctx : Context ) => void } requestHandler returns public on ( requestType , requestHandler ) { if ( ! this . config . requestHandlers [ requestType ] ) { this . config . requestHandlers [ requestType ] = requestHandler ; } return this ; } param requestHandler public onLaunchRequest ( requestHandler ) { this . on ( 'LaunchRequest' , requestHandler ) ; return this ; } param requestHandler public onIntentRequest ( requestHandler ) { this . on ( 'IntentRequest' , requestHandler ) ; return this ; } param requestHandler public onEventRequest ( requestHandler ) { this . on ( 'EventRequest' , requestHandler ) ; return this ; } param requestHandler public onSessionEndedRequest ( requestHandler ) { this . on ( 'SessionEndedRequest' , requestHandler ) ; return this ; } returns { ( req : express . Request , res : express . Response ) => void } memberOf public handle ( ) { return async ( req , res ) => { const ctx = new Context ( req . body ) ; try { const requestType = ctx . requestObject . request . type ; const requestHandler = this . config . requestHandlers [ requestType ] ; if ( requestHandler ) { await requestHandler . call ( ctx , ctx ) ; res . json ( ctx . responseObject ) ; } else { throw new Error ( `Unable to find requestHandler for '${ requestType }'` ) ; } } catch ( error ) { console . error ( error . message ) ; res . sendStatus ( 500 ) ; } } ; } returns { ( event : Clova . RequestBody ) => Promise < Clova . ResponseBody > } memberOf public lambda ( ) { return async ( event ) => { const ctx = new Context ( event ) ; const requestType = ctx . requestObject . request . type ; const requestHandler = this . config . requestHandlers [ requestType ] ; if ( requestHandler ) { await requestHandler . call ( ctx , ctx ) ; return ctx . responseObject ; } else { throw new Error ( `Unable to find requestHandler for '${ requestType }'` ) ; } } ; } returns { ( req : express . Request , res : express . Response ) => void } memberOf public firebase ( ) { return this . handle ( ) ; } } export default class Client { returns public static configureSkill ( ) { return new SkillConfigurator ( ) ; } }