alexa-ability
Advanced tools
Comparing version 0.9.1 to 0.10.0
@@ -9,3 +9,3 @@ # `Ability` | ||
- [`constructor(options) -> ability`](#constructoroptions---ability) | ||
- [`use(...middleware) -> ability`](#usemiddleware---ability) | ||
- [`use(...handlers) -> ability`](#usehandlers---ability) | ||
- [`on(event, ...handlers) -> ability`](#onevent-handlers---ability) | ||
@@ -24,8 +24,7 @@ - [`onError(handler) -> ability`](#onerrorhandler---ability) | ||
### `use(...middleware) -> ability` | ||
Add middleware functions to your ability. Middleware is called one at a time | ||
in the order added before any event handler is called. Calling this function | ||
multiple times will __add__ middleware, not replace previously added middleware. | ||
### `use(...handlers) -> ability` | ||
Add middleware functions to your ability that will be called for every request. | ||
Middleware functions will be called in the order added. | ||
Each middleware function must accept two arguments: | ||
Each handler function must accept two arguments: | ||
- `req`: the request Object | ||
@@ -58,5 +57,5 @@ - `next`: a function to call when the middleware is finished or has failed. If | ||
### `on(event, ...handlers) -> ability` | ||
Add event handlers to your ability. You can also add event specific middleware | ||
this way. Calling this function multiple times will __add__ handlers, not replace | ||
previously added handlers. | ||
Add event handlers to your ability. This is simply a wrapper around `app.use()`, | ||
except the passed handlers will only be called when the handled event matches | ||
the given one. | ||
@@ -71,9 +70,2 @@ Each handler function must accept two arguments: | ||
```js | ||
// intent specific middleware | ||
function logRequestMiddleware(req, next) { | ||
console.log(req.raw); | ||
next(); | ||
} | ||
// handler | ||
@@ -84,3 +76,12 @@ function handleIntent(req, next) { | ||
app.on('MyIntent', logRequestMiddleware, handleIntent); | ||
app.on('MyIntent', handleIntent); | ||
// is equivalent to | ||
app.use((req, next) => { | ||
if (req.handler === 'MyIntent') { | ||
req.say('Hello World!').end(); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
``` | ||
@@ -87,0 +88,0 @@ |
@@ -6,8 +6,2 @@ # `events` | ||
##### Internal Events | ||
- `events.unhandledEvent`: | ||
- Called if no event handler sent a response. | ||
- Can be caused by: | ||
- A weird request from Amazon. | ||
- You not handling the event sent to your skill. | ||
- Accidentally not ending a request. | ||
- `events.launch` | ||
@@ -14,0 +8,0 @@ - Corresponds to `LaunchRequest` |
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -10,2 +8,4 @@ value: true | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _debug = require('debug'); | ||
@@ -31,14 +31,8 @@ | ||
var _standardEvents = require('./standardEvents'); | ||
var e = _interopRequireWildcard(_standardEvents); | ||
var _resolve = require('./resolve'); | ||
var _verifyApplication = require('./verifyApplication'); | ||
var _verifyApplication = require('./middleware/verifyApplication'); | ||
var _getEventName = require('./getEventName'); | ||
var _handleEvent = require('./middleware/handleEvent'); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -72,5 +66,4 @@ | ||
// eslint-disable-line no-unused-vars | ||
this._middleware = []; | ||
this._stack = []; | ||
this._onError = null; | ||
this._handlers = {}; | ||
@@ -88,3 +81,3 @@ if (options.applicationId) { | ||
value: function use() { | ||
var _middleware; | ||
var _stack; | ||
@@ -101,3 +94,3 @@ for (var _len = arguments.length, fns = Array(_len), _key = 0; _key < _len; _key++) { | ||
(_middleware = this._middleware).push.apply(_middleware, fns); | ||
(_stack = this._stack).push.apply(_stack, fns); | ||
return this; | ||
@@ -108,2 +101,4 @@ } | ||
value: function on(event) { | ||
var _stack2; | ||
for (var _len2 = arguments.length, _handlers = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
@@ -114,3 +109,2 @@ _handlers[_key2 - 1] = arguments[_key2]; | ||
var handlers = (0, _flattenDeep2.default)(_handlers); | ||
var currentHandlers = this._handlers[event] || []; | ||
@@ -123,5 +117,7 @@ (0, _assert2.default)(typeof event === 'string', 'Expected string for event type'); | ||
oLog('adding ' + handlers.length + ' handlers to ' + event + ' event'); | ||
this._handlers[event] = [].concat(_toConsumableArray(currentHandlers), _toConsumableArray(handlers)); | ||
oLog('currently ' + this._handlers[event].length + ' handlers for ' + event + ' event'); | ||
var fns = handlers.map(function (fn) { | ||
return (0, _handleEvent.handleEvent)(event, fn); | ||
}); | ||
oLog('adding ' + fns.length + ' handlers for ' + event + ' event'); | ||
(_stack2 = this._stack).push.apply(_stack2, _toConsumableArray(fns)); | ||
return this; | ||
@@ -140,17 +136,8 @@ } | ||
// get possible handlers | ||
// it's fine if `handler` is null or undefined | ||
// it'll all be caught by the `unhandledEvent` handler | ||
var middleware = this._middleware; | ||
var errHandler = this._onError || _defaultHandlers.defaultHandlers.errorHandler; | ||
var defHandler = this._handlers[e.unhandledEvent] || _defaultHandlers.defaultHandlers.defaultHandler; | ||
var eventName = (0, _getEventName.getEventName)(event); | ||
var handler = eventName ? this._handlers[eventName] : null; | ||
var stack = [].concat(_toConsumableArray(this._stack)); | ||
var index = 0; | ||
// log | ||
if (handler) hLog('handling event: ' + eventName);else hLog('no handler found for event: "' + eventName + '".'); | ||
// build request object and attach listeners | ||
var req = new _Request.Request(event); | ||
req.handler = handler ? eventName : e.unhandledEvent; | ||
req.on('finished', function () { | ||
@@ -163,7 +150,2 @@ return setImmediate(callback, null, req); | ||
// iterate over the stack of middleware and handlers | ||
// kind of like express does | ||
var index = 0; | ||
var stack = [].concat(middleware, handler, defHandler); | ||
// if we ever reach this function then everything has failed | ||
@@ -170,0 +152,0 @@ function done(err) { |
@@ -18,4 +18,3 @@ 'use strict'; | ||
var dLog = (0, _debug2.default)('alexa-ability:defaultEventHandler'); | ||
var eLog = (0, _debug2.default)('alexa-ability:defaultErrorHandler'); | ||
var log = (0, _debug2.default)('alexa-ability:defaultErrorHandler'); | ||
@@ -28,9 +27,4 @@ var noErrHandlerWarning = (0, _once2.default)(function () { | ||
var defaultHandlers = exports.defaultHandlers = { | ||
defaultHandler: function defaultIntentHandler(req, next) { | ||
dLog('unhandled request', req); | ||
next(new Error('No intent handler found.')); | ||
}, | ||
errorHandler: function defaultErrorHandler(err, req, next) { | ||
eLog('unhandled error', err); | ||
log('unhandled error', err); | ||
noErrHandlerWarning(); | ||
@@ -37,0 +31,0 @@ next(err); |
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -10,2 +8,4 @@ value: true | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _get = require('lodash/get'); | ||
@@ -23,2 +23,4 @@ | ||
var _getEventName = require('./getEventName'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -43,3 +45,3 @@ | ||
_this.raw = event; | ||
_this.handler = null; // set by ability | ||
_this.handler = (0, _getEventName.getEventName)(event); | ||
_this.sent = false; | ||
@@ -125,2 +127,3 @@ _this.isNew = (0, _get2.default)(event, 'session.new', false); | ||
return { | ||
@@ -140,3 +143,4 @@ version: version, | ||
Request.prototype.ask = Request.prototype.send; | ||
Request.prototype.tell = Request.prototype.end; |
@@ -11,8 +11,2 @@ 'use strict'; | ||
/** | ||
* For the rare case that the developer hasn't handled | ||
* the request type | ||
*/ | ||
var unhandledEvent = exports.unhandledEvent = 'unhandledEvent'; | ||
/** | ||
* When a user makes a request to an Alexa skill, but | ||
@@ -19,0 +13,0 @@ * did not provide a specific intent |
{ | ||
"name": "alexa-ability", | ||
"version": "0.9.1", | ||
"version": "0.10.0", | ||
"description": "An Alexa skills framework for node", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "./scripts/test.sh" | ||
"build": "rm -rf lib && babel src --out-dir lib", | ||
"lint": "eslint src", | ||
"pretest": "npm run lint", | ||
"test": "mocha test --opts mocha.opts", | ||
"prepublish": "npm run test && npm run build", | ||
"publish:major": "npm version major && npm publish", | ||
"publish:minor": "npm version minor && npm publish", | ||
"publish:patch": "npm version patch && npm publish", | ||
"postpublish": "git push origin master --tags" | ||
}, | ||
@@ -36,6 +44,5 @@ "repository": { | ||
"debug": "^2.2.0", | ||
"lodash": "^4.0.1" | ||
"lodash": "^4.3.0" | ||
}, | ||
"devDependencies": { | ||
"babel": "^6.3.26", | ||
"babel-cli": "^6.4.5", | ||
@@ -42,0 +49,0 @@ "babel-eslint": "^4.1.8", |
@@ -18,5 +18,6 @@ # alexa-ability [![Build Status](https://travis-ci.org/nickclaw/alexa-ability.svg?branch=master)](https://travis-ci.org/nickclaw/alexa-ability) | ||
* [alexa-ability-express-handler](https://npmjs.org/package/alexa-ability-express-handler) - Expose abilities as Express endpoints | ||
* [alexa-ability-timeout](https://npmjs.org/alexa-ability-timeout) - Middleware to prevent your skills from stalling. | ||
* [alexa-ability-context](https://npmjs.org/alexa-ability-context) - Middleware to simplify building multistep conversations. | ||
* [alexa-utterances](https://npmjs.org/package/alexa-utterances) - Easily generate an exhaustive list of utterances from a few template strings. | ||
* [alexa-ssml](https://npmjs.org/package/alexa-ssml) - Manipulate and validate SSML using the [jsx](https://facebook.github.io/react/docs/jsx-in-depth.html) syntax | ||
* [alexa-utterances](https://npmjs.org/package/alexa-utterances) - Easily generate an exhaustive list of utterances from a few template strings. | ||
* [alexa-ability-timeout](https://npmjs.org/alexa-ability-timeout) - Middleware to prevent your skills from stalling. | ||
* [node-lambda](https://www.npmjs.com/package/node-lambda) - A command line interface to package and deploy AWS Lambda functions | ||
@@ -23,0 +24,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12
21
82
38310
516
Updatedlodash@^4.3.0