UNPKG

ask-sdk-v1adapter

Version:
53 lines 2.37 kB
"use strict"; /* * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Handler = void 0; const ask_sdk_1 = require("ask-sdk"); const eventParser_1 = require("./eventParser"); class Handler { constructor(adapter) { this.adapter = adapter; } canHandle(handlerInput) { if (this.adapter._event.session && this.adapter._event.session.new && this.adapter.listenerCount(`NewSession${this.adapter.state}`) >= 1) { this.targetHandlerName = 'NewSession'; return true; } this.targetHandlerName = (0, eventParser_1.EventParser)(this.adapter._event); return this.adapter.listenerCount(this.targetHandlerName + this.adapter.state) >= 1 || this.adapter.listenerCount(`Unhandled${this.adapter.state}`) >= 1; } handle(handlerInput) { return new Promise((resolve, reject) => { this.adapter.promiseResolve = resolve; try { if (this.adapter.listenerCount(this.targetHandlerName + this.adapter.state) >= 1) { this.adapter.emit(this.targetHandlerName + this.adapter.state); } else if (this.adapter.listenerCount(`Unhandled${this.adapter.state}`) >= 1) { this.adapter.emit(`Unhandled${this.adapter.state}`); } else { reject((0, ask_sdk_1.createAskSdkError)(this.constructor.name, `In state: ${this.adapter.state}. No handler function was defined for event ${this.targetHandlerName} and no 'Unhandled' function was defined.`)); } } catch (e) { reject(e); } }); } } exports.Handler = Handler; //# sourceMappingURL=handler.js.map