alexa-response
Advanced tools
Comparing version 3.1.1 to 3.2.0
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var alexaSsml = require('alexa-ssml'); | ||
var babelHelpers = {}; | ||
babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
var CardType = { | ||
LinkAccount: 'LinkAccount', | ||
Simple: 'Simple', | ||
Standard: 'Standard' | ||
}; | ||
var SpeechType = { | ||
PlainText: 'PlainText', | ||
SSML: 'SSML' | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
babelHelpers.classCallCheck = function (instance, Constructor) { | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
@@ -18,3 +151,3 @@ throw new TypeError("Cannot call a class as a function"); | ||
babelHelpers.createClass = function () { | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
@@ -37,3 +170,9 @@ for (var i = 0; i < props.length; i++) { | ||
babelHelpers.extends = Object.assign || function (target) { | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
@@ -52,3 +191,38 @@ var source = arguments[i]; | ||
babelHelpers.objectWithoutProperties = function (obj, keys) { | ||
var get = function get(object, property, receiver) { | ||
if (object === null) object = Function.prototype; | ||
var desc = Object.getOwnPropertyDescriptor(object, property); | ||
if (desc === undefined) { | ||
var parent = Object.getPrototypeOf(object); | ||
if (parent === null) { | ||
return undefined; | ||
} else { | ||
return get(parent, property, receiver); | ||
} | ||
} else if ("value" in desc) { | ||
return desc.value; | ||
} else { | ||
var getter = desc.get; | ||
if (getter === undefined) { | ||
return undefined; | ||
} | ||
return getter.call(receiver); | ||
} | ||
}; | ||
var objectWithoutProperties = function (obj, keys) { | ||
var target = {}; | ||
@@ -65,19 +239,56 @@ | ||
babelHelpers; | ||
var CardType = { | ||
LinkAccount: 'LinkAccount', | ||
Simple: 'Simple', | ||
Standard: 'Standard' | ||
var set = function set(object, property, value, receiver) { | ||
var desc = Object.getOwnPropertyDescriptor(object, property); | ||
if (desc === undefined) { | ||
var parent = Object.getPrototypeOf(object); | ||
if (parent !== null) { | ||
set(parent, property, value, receiver); | ||
} | ||
} else if ("value" in desc && desc.writable) { | ||
desc.value = value; | ||
} else { | ||
var setter = desc.set; | ||
if (setter !== undefined) { | ||
setter.call(receiver, value); | ||
} | ||
} | ||
return value; | ||
}; | ||
var SpeechType = { | ||
PlainText: 'PlainText', | ||
SSML: 'SSML' | ||
var toConsumableArray = function (arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} else { | ||
return Array.from(arr); | ||
} | ||
}; | ||
var Response = function () { | ||
var Response$1 = function () { | ||
function Response() { | ||
var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
babelHelpers.classCallCheck(this, Response); | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, Response); | ||
@@ -87,3 +298,3 @@ this.state = state; | ||
babelHelpers.createClass(Response, [{ | ||
createClass(Response, [{ | ||
key: 'ask', | ||
@@ -96,4 +307,4 @@ value: function ask(text, type) { | ||
value: function say(text, type) { | ||
return new Response(babelHelpers.extends({}, this.state, { | ||
response: babelHelpers.extends({}, this.state.response, { | ||
return new Response(_extends({}, this.state, { | ||
response: _extends({}, this.state.response, { | ||
shouldEndSession: true | ||
@@ -106,5 +317,5 @@ }, outputSpeech(text, type)) | ||
value: function reprompt(text, type) { | ||
return new Response(babelHelpers.extends({}, this.state, { | ||
response: babelHelpers.extends({}, this.state.response, { | ||
reprompt: babelHelpers.extends({}, outputSpeech(text, type)) | ||
return new Response(_extends({}, this.state, { | ||
response: _extends({}, this.state.response, { | ||
reprompt: _extends({}, outputSpeech(text, type)) | ||
}) | ||
@@ -118,7 +329,7 @@ })); | ||
var type = _ref$type === undefined ? CardType.Simple : _ref$type; | ||
var rest = babelHelpers.objectWithoutProperties(_ref, ['type']); | ||
var rest = objectWithoutProperties(_ref, ['type']); | ||
return new Response(babelHelpers.extends({}, this.state, { | ||
response: babelHelpers.extends({}, this.state.response, { | ||
card: babelHelpers.extends({}, rest, type && { type: type }) | ||
return new Response(_extends({}, this.state, { | ||
response: _extends({}, this.state.response, { | ||
card: _extends({}, rest, type && { type: type }) | ||
}) | ||
@@ -130,4 +341,4 @@ })); | ||
value: function attributes(data) { | ||
return new Response(babelHelpers.extends({}, this.state, { | ||
sessionAttributes: babelHelpers.extends({}, this.state.sessionAttributes, data) | ||
return new Response(_extends({}, this.state, { | ||
sessionAttributes: _extends({}, this.state.sessionAttributes, data) | ||
})); | ||
@@ -138,4 +349,4 @@ } | ||
value: function shouldEndSession(_shouldEndSession) { | ||
return new Response(babelHelpers.extends({}, this.state, { | ||
response: babelHelpers.extends({}, this.state.response, { | ||
return new Response(_extends({}, this.state, { | ||
response: _extends({}, this.state.response, { | ||
shouldEndSession: _shouldEndSession | ||
@@ -146,11 +357,31 @@ }) | ||
}, { | ||
key: 'directives', | ||
value: function directives(directive) { | ||
for (var _len = arguments.length, rest = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
rest[_key - 1] = arguments[_key]; | ||
} | ||
var directives = Array.isArray(directive) ? directive : [directive].concat(rest); | ||
var _ref2 = this.state.response || {}; | ||
var _ref2$directives = _ref2.directives; | ||
var previousDirectives = _ref2$directives === undefined ? [] : _ref2$directives; | ||
return new Response(_extends({}, this.state, { | ||
response: _extends({}, this.state.response, { | ||
directives: [].concat(toConsumableArray(previousDirectives), toConsumableArray(directives)) | ||
}) | ||
})); | ||
} | ||
}, { | ||
key: 'build', | ||
value: function build(attributes) { | ||
return babelHelpers.extends({ | ||
return _extends({ | ||
version: '1.0' | ||
}, this.state, { | ||
response: babelHelpers.extends({ | ||
response: _extends({ | ||
shouldEndSession: true | ||
}, this.state.response) | ||
}, attributes || this.state.sessionAttributes ? { sessionAttributes: babelHelpers.extends({}, attributes, this.state.sessionAttributes) } : null); | ||
}, attributes || this.state.sessionAttributes ? { sessionAttributes: _extends({}, attributes, this.state.sessionAttributes) } : null); | ||
} | ||
@@ -161,33 +392,39 @@ }]); | ||
Response.ask = function () { | ||
var _ref2; | ||
return (_ref2 = new Response()).ask.apply(_ref2, arguments); | ||
}; | ||
Response.say = function () { | ||
Response$1.ask = function () { | ||
var _ref3; | ||
return (_ref3 = new Response()).say.apply(_ref3, arguments); | ||
return (_ref3 = new Response$1()).ask.apply(_ref3, arguments); | ||
}; | ||
Response.card = function () { | ||
Response$1.say = function () { | ||
var _ref4; | ||
return (_ref4 = new Response()).card.apply(_ref4, arguments); | ||
return (_ref4 = new Response$1()).say.apply(_ref4, arguments); | ||
}; | ||
Response.reprompt = function () { | ||
Response$1.card = function () { | ||
var _ref5; | ||
return (_ref5 = new Response()).reprompt.apply(_ref5, arguments); | ||
return (_ref5 = new Response$1()).card.apply(_ref5, arguments); | ||
}; | ||
Response.shouldEndSession = function () { | ||
Response$1.reprompt = function () { | ||
var _ref6; | ||
return (_ref6 = new Response()).shouldEndSession.apply(_ref6, arguments); | ||
return (_ref6 = new Response$1()).reprompt.apply(_ref6, arguments); | ||
}; | ||
Response.build = function (params) { | ||
Response$1.shouldEndSession = function () { | ||
var _ref7; | ||
return (_ref7 = new Response$1()).shouldEndSession.apply(_ref7, arguments); | ||
}; | ||
Response$1.directives = function () { | ||
var _ref8; | ||
return (_ref8 = new Response$1()).directives.apply(_ref8, arguments); | ||
}; | ||
Response$1.build = function (params) { | ||
return Object.keys(params).reduce(function (response, action) { | ||
@@ -210,11 +447,13 @@ var options = params[action]; | ||
return response.shouldEndSession(options); | ||
case 'directives': | ||
return response.directives(options); | ||
} | ||
}, new Response()); | ||
}, new Response$1()); | ||
}; | ||
var outputSpeech = function outputSpeech(text) { | ||
var type = arguments.length <= 1 || arguments[1] === undefined ? SpeechType.PlainText : arguments[1]; | ||
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : SpeechType.PlainText; | ||
if (type === SpeechType.SSML || (typeof text === 'undefined' ? 'undefined' : babelHelpers.typeof(text)) === 'object') { | ||
var speech = (typeof text === 'undefined' ? 'undefined' : babelHelpers.typeof(text)) === 'object' ? alexaSsml.renderToString(text) : text; | ||
if (type === SpeechType.SSML || (typeof text === 'undefined' ? 'undefined' : _typeof(text)) === 'object') { | ||
var speech = (typeof text === 'undefined' ? 'undefined' : _typeof(text)) === 'object' ? alexaSsml.renderToString(text) : text; | ||
return { outputSpeech: { type: SpeechType.SSML, ssml: speech } }; | ||
@@ -226,18 +465,54 @@ } else { | ||
var AudioPlayer = { | ||
play: function play(stream) { | ||
return { | ||
type: 'AudioPlayer.Play', | ||
playBehavior: 'REPLACE_ALL', | ||
audioItem: { stream: stream } | ||
}; | ||
}, | ||
enqueue: function enqueue(stream) { | ||
var replaceQueue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
return { | ||
type: 'AudioPlayer.Play', | ||
playBehavior: replaceQueue ? 'REPLACE_ENQUEUED' : 'ENQUEUE', | ||
audioItem: { stream: stream } | ||
}; | ||
}, | ||
stop: function stop() { | ||
return { | ||
type: 'AudioPlayer.Stop' | ||
}; | ||
}, | ||
clearQueue: function clearQueue() { | ||
var stopCurrent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | ||
return { | ||
type: 'AudioPlayer.ClearQueue', | ||
clearBehavior: stopCurrent ? 'CLEAR_ALL' : 'CLEAR_ENQUEUED' | ||
}; | ||
} | ||
}; | ||
// For commonjs compatibility | ||
var ask = Response.ask; | ||
var say = Response.say; | ||
var card = Response.card; | ||
var reprompt = Response.reprompt; | ||
var shouldEndSession = Response.shouldEndSession; | ||
var build = Response.build; | ||
var ask$1 = Response$1.ask; | ||
var say$1 = Response$1.say; | ||
var card$1 = Response$1.card; | ||
var reprompt$1 = Response$1.reprompt; | ||
var shouldEndSession$1 = Response$1.shouldEndSession; | ||
var directives = Response$1.directives; | ||
var build$1 = Response$1.build; | ||
exports.CardType = CardType; | ||
exports.SpeechType = SpeechType; | ||
exports['default'] = Response; | ||
exports.ask = ask; | ||
exports.say = say; | ||
exports.card = card; | ||
exports.reprompt = reprompt; | ||
exports.shouldEndSession = shouldEndSession; | ||
exports.build = build; | ||
exports.AudioPlayer = AudioPlayer; | ||
exports.Response = Response$1; | ||
exports['default'] = Response$1; | ||
exports.ask = ask$1; | ||
exports.say = say$1; | ||
exports.card = card$1; | ||
exports.reprompt = reprompt$1; | ||
exports.shouldEndSession = shouldEndSession$1; | ||
exports.directives = directives; | ||
exports.build = build$1; |
@@ -1,1 +0,1 @@ | ||
"use strict";var alexaSsml=require("alexa-ssml"),babelHelpers={};babelHelpers["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},babelHelpers.classCallCheck=function(e,s){if(!(e instanceof s))throw new TypeError("Cannot call a class as a function")},babelHelpers.createClass=function(){function e(e,s){for(var t=0;t<s.length;t++){var n=s[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(s,t,n){return t&&e(s.prototype,t),n&&e(s,n),s}}(),babelHelpers["extends"]=Object.assign||function(e){for(var s=1;s<arguments.length;s++){var t=arguments[s];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},babelHelpers.objectWithoutProperties=function(e,s){var t={};for(var n in e)s.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t};var CardType={LinkAccount:"LinkAccount",Simple:"Simple",Standard:"Standard"},SpeechType={PlainText:"PlainText",SSML:"SSML"},Response=function(){function e(){var s=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];babelHelpers.classCallCheck(this,e),this.state=s}return babelHelpers.createClass(e,[{key:"ask",value:function(e,s){return this.say(e,s).shouldEndSession(!1)}},{key:"say",value:function(s,t){return new e(babelHelpers["extends"]({},this.state,{response:babelHelpers["extends"]({},this.state.response,{shouldEndSession:!0},outputSpeech(s,t))}))}},{key:"reprompt",value:function(s,t){return new e(babelHelpers["extends"]({},this.state,{response:babelHelpers["extends"]({},this.state.response,{reprompt:babelHelpers["extends"]({},outputSpeech(s,t))})}))}},{key:"card",value:function(s){var t=s.type,n=void 0===t?CardType.Simple:t,r=babelHelpers.objectWithoutProperties(s,["type"]);return new e(babelHelpers["extends"]({},this.state,{response:babelHelpers["extends"]({},this.state.response,{card:babelHelpers["extends"]({},r,n&&{type:n})})}))}},{key:"attributes",value:function(s){return new e(babelHelpers["extends"]({},this.state,{sessionAttributes:babelHelpers["extends"]({},this.state.sessionAttributes,s)}))}},{key:"shouldEndSession",value:function(s){return new e(babelHelpers["extends"]({},this.state,{response:babelHelpers["extends"]({},this.state.response,{shouldEndSession:s})}))}},{key:"build",value:function(e){return babelHelpers["extends"]({version:"1.0"},this.state,{response:babelHelpers["extends"]({shouldEndSession:!0},this.state.response)},e||this.state.sessionAttributes?{sessionAttributes:babelHelpers["extends"]({},e,this.state.sessionAttributes)}:null)}}]),e}();Response.ask=function(){var e;return(e=new Response).ask.apply(e,arguments)},Response.say=function(){var e;return(e=new Response).say.apply(e,arguments)},Response.card=function(){var e;return(e=new Response).card.apply(e,arguments)},Response.reprompt=function(){var e;return(e=new Response).reprompt.apply(e,arguments)},Response.shouldEndSession=function(){var e;return(e=new Response).shouldEndSession.apply(e,arguments)},Response.build=function(e){return Object.keys(e).reduce(function(s,t){var n=e[t],r="string"==typeof n?n:n,o="string"==typeof n?void 0:n.type;switch(t){case"ask":return s.ask(r,o);case"say":return s.say(r,o);case"reprompt":return s.reprompt(r,o);case"card":return s.card(n);case"attributes":return s.attributes(n);case"shouldEndSession":return s.shouldEndSession(n)}},new Response)};var outputSpeech=function(e){var s=arguments.length<=1||void 0===arguments[1]?SpeechType.PlainText:arguments[1];if(s===SpeechType.SSML||"object"===("undefined"==typeof e?"undefined":babelHelpers["typeof"](e))){var t="object"===("undefined"==typeof e?"undefined":babelHelpers["typeof"](e))?alexaSsml.renderToString(e):e;return{outputSpeech:{type:SpeechType.SSML,ssml:t}}}return{outputSpeech:{type:s,text:e}}},ask=Response.ask,say=Response.say,card=Response.card,reprompt=Response.reprompt,shouldEndSession=Response.shouldEndSession,build=Response.build;exports.CardType=CardType,exports.SpeechType=SpeechType,exports["default"]=Response,exports.ask=ask,exports.say=say,exports.card=card,exports.reprompt=reprompt,exports.shouldEndSession=shouldEndSession,exports.build=build; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var alexaSsml=require("alexa-ssml"),CardType={LinkAccount:"LinkAccount",Simple:"Simple",Standard:"Standard"},SpeechType={PlainText:"PlainText",SSML:"SSML"},_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},asyncGenerator=function(){function e(e){this.value=e}function t(t){function n(e,t){return new Promise(function(n,s){var a={key:e,arg:t,resolve:n,reject:s,next:null};i?i=i.next=a:(o=i=a,r(e,t))})}function r(n,o){try{var i=t[n](o),a=i.value;a instanceof e?Promise.resolve(a.value).then(function(e){r("next",e)},function(e){r("throw",e)}):s(i.done?"return":"normal",i.value)}catch(e){s("throw",e)}}function s(e,t){switch(e){case"return":o.resolve({value:t,done:!0});break;case"throw":o.reject(t);break;default:o.resolve({value:t,done:!1})}o=o.next,o?r(o.key,o.arg):i=null}var o,i;this._invoke=n,"function"!=typeof t.return&&(this.return=void 0)}return"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)},{wrap:function(e){return function(){return new t(e.apply(this,arguments))}},await:function(t){return new e(t)}}}(),classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},get=function e(t,n,r){null===t&&(t=Function.prototype);var s=Object.getOwnPropertyDescriptor(t,n);if(void 0===s){var o=Object.getPrototypeOf(t);return null===o?void 0:e(o,n,r)}if("value"in s)return s.value;var i=s.get;if(void 0!==i)return i.call(r)},objectWithoutProperties=function(e,t){var n={};for(var r in e)t.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n},set=function e(t,n,r,s){var o=Object.getOwnPropertyDescriptor(t,n);if(void 0===o){var i=Object.getPrototypeOf(t);null!==i&&e(i,n,r,s)}else if("value"in o&&o.writable)o.value=r;else{var a=o.set;void 0!==a&&a.call(s,r)}return r},toConsumableArray=function(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)},Response$1=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};classCallCheck(this,e),this.state=t}return createClass(e,[{key:"ask",value:function(e,t){return this.say(e,t).shouldEndSession(!1)}},{key:"say",value:function(t,n){return new e(_extends({},this.state,{response:_extends({},this.state.response,{shouldEndSession:!0},outputSpeech(t,n))}))}},{key:"reprompt",value:function(t,n){return new e(_extends({},this.state,{response:_extends({},this.state.response,{reprompt:_extends({},outputSpeech(t,n))})}))}},{key:"card",value:function(t){var n=t.type,r=void 0===n?CardType.Simple:n,s=objectWithoutProperties(t,["type"]);return new e(_extends({},this.state,{response:_extends({},this.state.response,{card:_extends({},s,r&&{type:r})})}))}},{key:"attributes",value:function(t){return new e(_extends({},this.state,{sessionAttributes:_extends({},this.state.sessionAttributes,t)}))}},{key:"shouldEndSession",value:function(t){return new e(_extends({},this.state,{response:_extends({},this.state.response,{shouldEndSession:t})}))}},{key:"directives",value:function t(n){for(var r=arguments.length,s=Array(r>1?r-1:0),o=1;o<r;o++)s[o-1]=arguments[o];var t=Array.isArray(n)?n:[n].concat(s),i=this.state.response||{},a=i.directives,u=void 0===a?[]:a;return new e(_extends({},this.state,{response:_extends({},this.state.response,{directives:[].concat(toConsumableArray(u),toConsumableArray(t))})}))}},{key:"build",value:function(e){return _extends({version:"1.0"},this.state,{response:_extends({shouldEndSession:!0},this.state.response)},e||this.state.sessionAttributes?{sessionAttributes:_extends({},e,this.state.sessionAttributes)}:null)}}]),e}();Response$1.ask=function(){var e;return(e=new Response$1).ask.apply(e,arguments)},Response$1.say=function(){var e;return(e=new Response$1).say.apply(e,arguments)},Response$1.card=function(){var e;return(e=new Response$1).card.apply(e,arguments)},Response$1.reprompt=function(){var e;return(e=new Response$1).reprompt.apply(e,arguments)},Response$1.shouldEndSession=function(){var e;return(e=new Response$1).shouldEndSession.apply(e,arguments)},Response$1.directives=function(){var e;return(e=new Response$1).directives.apply(e,arguments)},Response$1.build=function(e){return Object.keys(e).reduce(function(t,n){var r=e[n],s="string"==typeof r?r:r,o="string"==typeof r?void 0:r.type;switch(n){case"ask":return t.ask(s,o);case"say":return t.say(s,o);case"reprompt":return t.reprompt(s,o);case"card":return t.card(r);case"attributes":return t.attributes(r);case"shouldEndSession":return t.shouldEndSession(r);case"directives":return t.directives(r)}},new Response$1)};var outputSpeech=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:SpeechType.PlainText;if(t===SpeechType.SSML||"object"===("undefined"==typeof e?"undefined":_typeof(e))){var n="object"===("undefined"==typeof e?"undefined":_typeof(e))?alexaSsml.renderToString(e):e;return{outputSpeech:{type:SpeechType.SSML,ssml:n}}}return{outputSpeech:{type:t,text:e}}},AudioPlayer={play:function(e){return{type:"AudioPlayer.Play",playBehavior:"REPLACE_ALL",audioItem:{stream:e}}},enqueue:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return{type:"AudioPlayer.Play",playBehavior:t?"REPLACE_ENQUEUED":"ENQUEUE",audioItem:{stream:e}}},stop:function(){return{type:"AudioPlayer.Stop"}},clearQueue:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];return{type:"AudioPlayer.ClearQueue",clearBehavior:e?"CLEAR_ALL":"CLEAR_ENQUEUED"}}},ask$1=Response$1.ask,say$1=Response$1.say,card$1=Response$1.card,reprompt$1=Response$1.reprompt,shouldEndSession$1=Response$1.shouldEndSession,directives=Response$1.directives,build$1=Response$1.build;exports.CardType=CardType,exports.SpeechType=SpeechType,exports.AudioPlayer=AudioPlayer,exports.Response=Response$1,exports.default=Response$1,exports.ask=ask$1,exports.say=say$1,exports.card=card$1,exports.reprompt=reprompt$1,exports.shouldEndSession=shouldEndSession$1,exports.directives=directives,exports.build=build$1; |
{ | ||
"name": "alexa-response", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "Build JSON responses for Amazon Alexa, easily.", | ||
@@ -34,4 +34,4 @@ "author": "Cameron Hunter <hello@cameronhunter.co.uk>", | ||
"devDependencies": { | ||
"ava": "^0.14.0", | ||
"babel-eslint": "^6.0.0-beta.6", | ||
"ava": "^0.16.0", | ||
"babel-eslint": "^7.0.0", | ||
"babel-plugin-transform-react-jsx": "^6.5.2", | ||
@@ -43,7 +43,7 @@ "babel-polyfill": "^6.7.4", | ||
"babel-register": "^6.5.2", | ||
"eslint": "^2.2.0", | ||
"npm-run-all": "^1.5.1", | ||
"rollup": "^0.25.4", | ||
"eslint": "^3.7.0", | ||
"npm-run-all": "^3.1.0", | ||
"rollup": "^0.36.1", | ||
"rollup-plugin-babel": "^2.4.0", | ||
"rollup-plugin-uglify": "^0.2.0" | ||
"rollup-plugin-uglify": "^1.0.1" | ||
}, | ||
@@ -50,0 +50,0 @@ "ava": { |
@@ -56,3 +56,3 @@ # Alexa Response | ||
```javascript | ||
Reponse.ask("What's your favorite color?") | ||
Response.ask("What's your favorite color?") | ||
.attributes({ question_asked: true }) | ||
@@ -62,2 +62,14 @@ .build(); | ||
### directives(directives: ...Directive) | ||
Used to add directives to the response. A helper is provided to build an audio | ||
directive, however, any directive can be used. This function accepts multiple | ||
directives. | ||
```javascript | ||
Response.say("Playing audio") | ||
.directives(AudioPlayer.play({ url: 'https://stream.com/my-stream.mp3' })) | ||
.build(); | ||
``` | ||
### build(attributes: Optional\<Object\>) | ||
@@ -74,2 +86,18 @@ | ||
### AudioPlayer | ||
This helper is provided to build `AudioPlayer` directives. | ||
```javascript | ||
import Response, { AudioPlayer } from 'alexa-response'; | ||
const { play, enqueue, stop, clearQueue } = AudioPlayer; | ||
Response.directives( | ||
play({ url: 'https://stream.com/my-stream-1.mp3' }), | ||
enqueue({ url: 'https://stream.com/my-stream-2.mp3' }) | ||
) | ||
.build(); | ||
``` | ||
## Inline SSML | ||
@@ -76,0 +104,0 @@ |
120
src/index.js
@@ -1,118 +0,11 @@ | ||
import { ssml, renderToString } from 'alexa-ssml'; | ||
import Response from './Response'; | ||
export const CardType = { | ||
LinkAccount: 'LinkAccount', | ||
Simple: 'Simple', | ||
Standard: 'Standard' | ||
}; | ||
export CardType from './constants/CardType'; | ||
export SpeechType from './constants/SpeechType'; | ||
export const SpeechType = { | ||
PlainText: 'PlainText', | ||
SSML: 'SSML' | ||
}; | ||
export AudioPlayer from './AudioPlayer'; | ||
export Response from './Response'; | ||
export default class Response { | ||
static ask = (...args) => new Response().ask(...args); | ||
static say = (...args) => new Response().say(...args); | ||
static card = (...args) => new Response().card(...args); | ||
static reprompt = (...args) => new Response().reprompt(...args); | ||
static shouldEndSession = (...args) => new Response().shouldEndSession(...args); | ||
static build = (params) => Object.keys(params).reduce((response, action) => { | ||
const options = params[action]; | ||
const text = typeof options === 'string' ? options : options; | ||
const type = typeof options === 'string' ? undefined : options.type; | ||
switch (action) { | ||
case 'ask': return response.ask(text, type); | ||
case 'say': return response.say(text, type); | ||
case 'reprompt': return response.reprompt(text, type); | ||
case 'card': return response.card(options); | ||
case 'attributes': return response.attributes(options); | ||
case 'shouldEndSession': return response.shouldEndSession(options); | ||
} | ||
}, new Response()); | ||
export default Response; | ||
constructor(state = {}) { | ||
this.state = state; | ||
} | ||
ask(text, type) { | ||
return this.say(text, type).shouldEndSession(false); | ||
} | ||
say(text, type) { | ||
return new Response({ | ||
...this.state, | ||
response: { | ||
...this.state.response, | ||
shouldEndSession: true, | ||
...outputSpeech(text, type) | ||
} | ||
}); | ||
} | ||
reprompt(text, type) { | ||
return new Response({ | ||
...this.state, | ||
response: { | ||
...this.state.response, | ||
reprompt: { ...outputSpeech(text, type) } | ||
} | ||
}); | ||
} | ||
card({ type = CardType.Simple, ...rest }) { | ||
return new Response({ | ||
...this.state, | ||
response: { | ||
...this.state.response, | ||
card: { | ||
...rest, | ||
...(type && { type }), | ||
} | ||
} | ||
}); | ||
} | ||
attributes(data) { | ||
return new Response({ | ||
...this.state, | ||
sessionAttributes: { | ||
...this.state.sessionAttributes, | ||
...data | ||
} | ||
}); | ||
} | ||
shouldEndSession(shouldEndSession) { | ||
return new Response({ | ||
...this.state, | ||
response: { | ||
...this.state.response, | ||
shouldEndSession | ||
} | ||
}); | ||
} | ||
build(attributes) { | ||
return { | ||
version: '1.0', | ||
...this.state, | ||
response: { | ||
shouldEndSession: true, | ||
...this.state.response | ||
}, | ||
...(attributes || this.state.sessionAttributes ? { sessionAttributes: { ...attributes, ...this.state.sessionAttributes } } : null) | ||
}; | ||
} | ||
} | ||
const outputSpeech = (text, type = SpeechType.PlainText) => { | ||
if (type === SpeechType.SSML || typeof text === 'object') { | ||
const speech = (typeof text === 'object') ? renderToString(text) : text; | ||
return { outputSpeech: { type: SpeechType.SSML, ssml: speech } }; | ||
} else { | ||
return { outputSpeech: { type, text } }; | ||
} | ||
}; | ||
// For commonjs compatibility | ||
@@ -124,2 +17,3 @@ export const ask = Response.ask; | ||
export const shouldEndSession = Response.shouldEndSession; | ||
export const directives = Response.directives; | ||
export const build = Response.build; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
30419
10
577
120
1