UNPKG

ask-cli-x

Version:

Alexa Skills Kit (ASK) Command Line Interfaces

60 lines (59 loc) 3.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCallMatcher = void 0; const acdl_1 = require("@alexa/acdl"); const cli_error_1 = __importDefault(require("../../../exceptions/cli-error")); const getCallMatcher = (call, project) => { var _a, _b, _c; if ((_b = (_a = call.name) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.includes("response")) { // ResponseAction // TODO: may need to apply the current state to the project. const callProperties = getResponseArguments(call, project); return (candidate, candidateProject) => { var _a, _b, _c, _d; if (!(candidate instanceof acdl_1.Call) || !((_b = (_a = candidate.name) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.includes("response"))) { return false; } const candidateProps = getResponseArguments(candidate, candidateProject); return (callProperties.dialogActName == candidateProps.dialogActName && !hasArgumentMismatch((_c = candidateProps.argList) === null || _c === void 0 ? void 0 : _c.getArguments(), (_d = callProperties.argList) === null || _d === void 0 ? void 0 : _d.getArguments())); }; } else { const correctedApiName = (_c = call.name) === null || _c === void 0 ? void 0 : _c.name; return (candidate) => { var _a; const callCandidate = candidate instanceof acdl_1.NameDeclaration ? candidate.expression : candidate; return callCandidate instanceof acdl_1.Call && ((_a = callCandidate.name) === null || _a === void 0 ? void 0 : _a.name) === correctedApiName; }; } }; exports.getCallMatcher = getCallMatcher; const getResponseArguments = (call, project) => { var _a, _b, _c, _d, _e; const checker = project.getTypeChecker(); const apply = checker.getApply(call); const dialogActArg = (_a = apply === null || apply === void 0 ? void 0 : apply.getArgument("act")) === null || _a === void 0 ? void 0 : _a.value; const correctedDialogActName = dialogActArg ? checker.qualifyName(call, dialogActArg.name) : undefined; const correctedRespName = (_d = (_c = (_b = apply === null || apply === void 0 ? void 0 : apply.getArgument("response")) === null || _b === void 0 ? void 0 : _b.value) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.name; const payload = (_e = apply === null || apply === void 0 ? void 0 : apply.getArgument("payload")) === null || _e === void 0 ? void 0 : _e.value; if (payload && !(payload instanceof acdl_1.Call)) { throw new cli_error_1.default("Expected the response payload to be a call."); } const payloadApply = payload ? checker.getApply(payload) : undefined; return { dialogActName: correctedDialogActName, responseName: correctedRespName, argList: payloadApply }; }; const hasArgumentMismatch = (args, correctedArgs) => { const _args = args !== null && args !== void 0 ? args : {}; const _correctedArgs = correctedArgs !== null && correctedArgs !== void 0 ? correctedArgs : {}; if (!_args) return !!_correctedArgs; if (!correctedArgs) return !!args; if (Object.values(_args) != Object.values(_correctedArgs)) return false; return !Object.entries(_args).every(([name, arg]) => { var _a, _b, _c, _d; return correctedArgs[name] && ((_b = (_a = correctedArgs[name].value) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.name) === ((_d = (_c = arg.value) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.name); }); };