alexa-annotations
Advanced tools
Comparing version 4.0.1 to 5.0.0
@@ -197,5 +197,6 @@ 'use strict'; | ||
var actual = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var isOK = !expected.applicationId || expected.applicationId === actual.applicationId; | ||
return isOK ? Promise.resolve() : Promise.reject(Unauthorized); | ||
return new Promise(function (resolve, reject) { | ||
var isOK = !expected.applicationId || expected.applicationId === actual.applicationId; | ||
return isOK ? resolve() : reject(Unauthorized); | ||
}); | ||
}; | ||
@@ -205,26 +206,20 @@ | ||
return function (Skill) { | ||
return function (event, context) { | ||
var _ref = context || {}; | ||
return function (event, context, callback) { | ||
var _ref = event || {}; | ||
var succeed = _ref.succeed; | ||
var fail = _ref.fail; | ||
var request = _ref.request; | ||
var session = _ref.session; | ||
var _ref2 = event || {}; | ||
var _ref2 = session || {}; | ||
var request = _ref2.request; | ||
var session = _ref2.session; | ||
var application = _ref2.application; | ||
var attributes = _ref2.attributes; | ||
var _ref3 = session || {}; | ||
var application = _ref3.application; | ||
var attributes = _ref3.attributes; | ||
return isAuthorized(options, application).then(function () { | ||
var skill = new Skill(attributes); | ||
return skill.route && skill.route(request) || Promise.reject(NotFound); | ||
return new Skill(attributes).route(request) || Promise.reject(NotFound); | ||
}).then(function (response) { | ||
return response.build && typeof response.build === 'function' ? response.build(attributes) : response; | ||
return typeof response.build === 'function' ? response.build(attributes) : response; | ||
}).then(function (response) { | ||
succeed && succeed(response); | ||
callback && callback(null, response); | ||
return response; | ||
@@ -234,4 +229,11 @@ }).catch(function () { | ||
fail && fail(error); | ||
callback && callback(error); | ||
return error; | ||
}).then(function (response) { | ||
if (options.logging !== false) { | ||
var name = typeof options.logging === 'string' ? options.logging : 'Skill'; | ||
console.log('[' + name + ']', JSON.stringify({ request: request, response: response })); | ||
} | ||
return response; | ||
}); | ||
@@ -238,0 +240,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e["default"]:e}var Promise=_interopDefault(require("bluebird")),$inject_Object_values=_interopDefault(require("object-values")),babelHelpers={};babelHelpers.classCallCheck=function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")},babelHelpers.createClass=function(){function e(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(n,t,r){return t&&e(n.prototype,t),r&&e(n,r),n}}(),babelHelpers.defineProperty=function(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e},babelHelpers["extends"]=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},babelHelpers.slicedToArray=function(){function e(e,n){var t=[],r=!0,u=!1,a=void 0;try{for(var o,s=e[Symbol.iterator]();!(r=(o=s.next()).done)&&(t.push(o.value),!n||t.length!==n);r=!0);}catch(i){u=!0,a=i}finally{try{!r&&s["return"]&&s["return"]()}finally{if(u)throw a}}return t}return function(n,t){if(Array.isArray(n))return n;if(Symbol.iterator in Object(n))return e(n,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();var Unauthorized={code:401,message:"Unauthorized application ID"},NotFound={code:404,message:"Route not found"},InternalServer={code:500,message:"Internal skill error"},_ErrorCode=Object.freeze({Unauthorized:Unauthorized,NotFound:NotFound,InternalServer:InternalServer}),_class,_temp,Request=(_temp=_class=function(){function e(){var n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];babelHelpers.classCallCheck(this,e),this.state=n}return babelHelpers.createClass(e,[{key:"session",value:function(n){return new e(babelHelpers["extends"]({},this.state,{session:babelHelpers["extends"]({},this.state.session,n)}))}},{key:"intent",value:function(n){var t=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=Object.entries(t).reduce(function(e,n){var t=babelHelpers.slicedToArray(n,2),r=t[0],u=t[1];return babelHelpers["extends"]({},e,babelHelpers.defineProperty({},r,{name:r,value:u}))},{});return new e(babelHelpers["extends"]({},this.state,{request:{type:"IntentRequest",intent:babelHelpers["extends"]({name:n},Object.keys(r).length?{slots:r}:null)}}))}},{key:"launchRequest",value:function(){return new e(babelHelpers["extends"]({},this.state,{request:{type:"LaunchRequest"}}))}},{key:"sessionEndedRequest",value:function(){return new e(babelHelpers["extends"]({},this.state,{request:{type:"SessionEndedRequest"}}))}},{key:"build",value:function(){return babelHelpers["extends"]({},this.state)}}]),e}(),_class.session=function(){var e;return(e=new Request).session.apply(e,arguments)},_class.intent=function(){var e;return(e=new Request).intent.apply(e,arguments)},_class.launchRequest=function(){var e;return(e=new Request).launchRequest.apply(e,arguments)},_class.sessionEndedRequest=function(){var e;return(e=new Request).sessionEndedRequest.apply(e,arguments)},_temp),isAuthorized=function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],t=!e.applicationId||e.applicationId===n.applicationId;return t?Promise.resolve():Promise.reject(Unauthorized)},SkillAnnotation=function(e){return function(n){return function(t,r){var u=r||{},a=u.succeed,o=u.fail,s=t||{},i=s.request,l=s.session,c=l||{},d=c.application,f=c.attributes;return isAuthorized(e,d).then(function(){var e=new n(f);return e.route&&e.route(i)||Promise.reject(NotFound)}).then(function(e){return e.build&&"function"==typeof e.build?e.build(f):e}).then(function(e){return a&&a(e),e})["catch"](function(){var e=arguments.length<=0||void 0===arguments[0]?InternalServer:arguments[0];return o&&o(e),e})}}},_Skill=function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="function"==typeof e,t=n?{}:e,r=n&&e;return n?SkillAnnotation(t)(r):SkillAnnotation(t)},annotation=function(e){var n=arguments.length<=1||void 0===arguments[1]?function(e){return e}:arguments[1];return function(t,r){var u=t.route||function(){return!1};return t.route=function(a){return u.call(this,a)||e(a)&&t[r].call(this,n(a),a)},t}},Launch=annotation(function(e){var n=e.type;return"LaunchRequest"===n}),SessionEnded=annotation(function(e){var n=e.type;return"SessionEndedRequest"===n}),Intent=function(){for(var e=arguments.length,n=Array(e),t=0;e>t;t++)n[t]=arguments[t];return annotation(function(e){var t=e.type,r=e.intent,u=void 0===r?{}:r;return"IntentRequest"===t&&n.indexOf(u.name)>=0},function(e){var n=e.intent,t=void 0===n?{}:n;return $inject_Object_values(t.slots||{}).reduce(function(e,n){var t=n.name,r=n.value;return t&&null!=r?babelHelpers["extends"]({},e,babelHelpers.defineProperty({},t,r)):e},{})})};exports.ErrorCode=_ErrorCode,exports.Request=Request,exports.Skill=_Skill,exports.Launch=Launch,exports.Intent=Intent,exports.SessionEnded=SessionEnded; | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e["default"]:e}var Promise=_interopDefault(require("bluebird")),$inject_Object_values=_interopDefault(require("object-values")),babelHelpers={};babelHelpers.classCallCheck=function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")},babelHelpers.createClass=function(){function e(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(n,t,r){return t&&e(n.prototype,t),r&&e(n,r),n}}(),babelHelpers.defineProperty=function(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e},babelHelpers["extends"]=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},babelHelpers.slicedToArray=function(){function e(e,n){var t=[],r=!0,o=!1,u=void 0;try{for(var i,s=e[Symbol.iterator]();!(r=(i=s.next()).done)&&(t.push(i.value),!n||t.length!==n);r=!0);}catch(a){o=!0,u=a}finally{try{!r&&s["return"]&&s["return"]()}finally{if(o)throw u}}return t}return function(n,t){if(Array.isArray(n))return n;if(Symbol.iterator in Object(n))return e(n,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();var Unauthorized={code:401,message:"Unauthorized application ID"},NotFound={code:404,message:"Route not found"},InternalServer={code:500,message:"Internal skill error"},_ErrorCode=Object.freeze({Unauthorized:Unauthorized,NotFound:NotFound,InternalServer:InternalServer}),_class,_temp,Request=(_temp=_class=function(){function e(){var n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];babelHelpers.classCallCheck(this,e),this.state=n}return babelHelpers.createClass(e,[{key:"session",value:function(n){return new e(babelHelpers["extends"]({},this.state,{session:babelHelpers["extends"]({},this.state.session,n)}))}},{key:"intent",value:function(n){var t=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],r=Object.entries(t).reduce(function(e,n){var t=babelHelpers.slicedToArray(n,2),r=t[0],o=t[1];return babelHelpers["extends"]({},e,babelHelpers.defineProperty({},r,{name:r,value:o}))},{});return new e(babelHelpers["extends"]({},this.state,{request:{type:"IntentRequest",intent:babelHelpers["extends"]({name:n},Object.keys(r).length?{slots:r}:null)}}))}},{key:"launchRequest",value:function(){return new e(babelHelpers["extends"]({},this.state,{request:{type:"LaunchRequest"}}))}},{key:"sessionEndedRequest",value:function(){return new e(babelHelpers["extends"]({},this.state,{request:{type:"SessionEndedRequest"}}))}},{key:"build",value:function(){return babelHelpers["extends"]({},this.state)}}]),e}(),_class.session=function(){var e;return(e=new Request).session.apply(e,arguments)},_class.intent=function(){var e;return(e=new Request).intent.apply(e,arguments)},_class.launchRequest=function(){var e;return(e=new Request).launchRequest.apply(e,arguments)},_class.sessionEndedRequest=function(){var e;return(e=new Request).sessionEndedRequest.apply(e,arguments)},_temp),isAuthorized=function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return new Promise(function(t,r){var o=!e.applicationId||e.applicationId===n.applicationId;return o?t():r(Unauthorized)})},SkillAnnotation=function(e){return function(n){return function(t,r,o){var u=t||{},i=u.request,s=u.session,a=s||{},l=a.application,c=a.attributes;return isAuthorized(e,l).then(function(){return new n(c).route(i)||Promise.reject(NotFound)}).then(function(e){return"function"==typeof e.build?e.build(c):e}).then(function(e){return o&&o(null,e),e})["catch"](function(){var e=arguments.length<=0||void 0===arguments[0]?InternalServer:arguments[0];return o&&o(e),e}).then(function(n){if(e.logging!==!1){var t="string"==typeof e.logging?e.logging:"Skill";console.log("["+t+"]",JSON.stringify({request:i,response:n}))}return n})}}},_Skill=function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n="function"==typeof e,t=n?{}:e,r=n&&e;return n?SkillAnnotation(t)(r):SkillAnnotation(t)},annotation=function(e){var n=arguments.length<=1||void 0===arguments[1]?function(e){return e}:arguments[1];return function(t,r){var o=t.route||function(){return!1};return t.route=function(u){return o.call(this,u)||e(u)&&t[r].call(this,n(u),u)},t}},Launch=annotation(function(e){var n=e.type;return"LaunchRequest"===n}),SessionEnded=annotation(function(e){var n=e.type;return"SessionEndedRequest"===n}),Intent=function(){for(var e=arguments.length,n=Array(e),t=0;e>t;t++)n[t]=arguments[t];return annotation(function(e){var t=e.type,r=e.intent,o=void 0===r?{}:r;return"IntentRequest"===t&&n.indexOf(o.name)>=0},function(e){var n=e.intent,t=void 0===n?{}:n;return $inject_Object_values(t.slots||{}).reduce(function(e,n){var t=n.name,r=n.value;return t&&null!=r?babelHelpers["extends"]({},e,babelHelpers.defineProperty({},t,r)):e},{})})};exports.ErrorCode=_ErrorCode,exports.Request=Request,exports.Skill=_Skill,exports.Launch=Launch,exports.Intent=Intent,exports.SessionEnded=SessionEnded; |
@@ -9,3 +9,3 @@ { | ||
"description": "Create Alexa skills for the Amazon Echo, easily.", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"main": "build/index.min.js", | ||
@@ -12,0 +12,0 @@ "jsnext:main": "src/index.js", |
import { Unauthorized, NotFound, InternalServer } from './error-codes'; | ||
const isAuthorized = (expected = {}, actual = {}) => { | ||
const isAuthorized = (expected = {}, actual = {}) => new Promise((resolve, reject) => { | ||
const isOK = !expected.applicationId || expected.applicationId === actual.applicationId; | ||
return isOK ? Promise.resolve() : Promise.reject(Unauthorized); | ||
}; | ||
return isOK ? resolve() : reject(Unauthorized); | ||
}); | ||
const SkillAnnotation = (options) => (Skill) => (event, context) => { | ||
const { succeed, fail } = context || {}; | ||
const SkillAnnotation = (options) => (Skill) => (event, context, callback) => { | ||
const { request, session } = event || {}; | ||
@@ -14,12 +13,18 @@ const { application, attributes } = session || {}; | ||
return isAuthorized(options, application).then(() => { | ||
const skill = new Skill(attributes); | ||
return (skill.route && skill.route(request)) || Promise.reject(NotFound); | ||
return new Skill(attributes).route(request) || Promise.reject(NotFound); | ||
}).then(response => { | ||
return (response.build && typeof response.build === 'function') ? response.build(attributes) : response; | ||
return (typeof response.build === 'function') ? response.build(attributes) : response; | ||
}).then(response => { | ||
succeed && succeed(response); | ||
callback && callback(null, response); | ||
return response; | ||
}).catch((error = InternalServer) => { | ||
fail && fail(error); | ||
callback && callback(error); | ||
return error; | ||
}).then(response => { | ||
if (options.logging !== false) { | ||
const name = (typeof options.logging === 'string') ? options.logging : 'Skill'; | ||
console.log(`[${name}]`, JSON.stringify({ request, response })); | ||
} | ||
return response; | ||
}); | ||
@@ -26,0 +31,0 @@ }; |
23293
375