alexa-ability-context
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -6,3 +6,5 @@ 'use strict'; | ||
}); | ||
exports.context = undefined; | ||
exports.after = after; | ||
exports.matches = matches; | ||
exports.custom = custom; | ||
@@ -16,8 +18,12 @@ var _last = require('lodash/last'); | ||
/** | ||
* | ||
* | ||
* Middleware creator that only executes the handler when | ||
* the last event matches the given one. | ||
* @param {String} event | ||
* @param {Function} handler | ||
* @return {Function} middleware | ||
*/ | ||
function matches(regex, handler) { | ||
function after(event, handler) { | ||
return function handleContext(req, next) { | ||
return regex.test(req.context.join(':')) ? handler(req, next) : next(); | ||
var lastHandled = (0, _last2.default)(req.context); | ||
return lastHandled && lastHandled.event === event ? handler(req, next) : next(); | ||
}; | ||
@@ -27,8 +33,14 @@ } | ||
/** | ||
* | ||
* | ||
* Middleware creator that only executes the handler when | ||
* the regex matches the requests context | ||
* @param {RegExp|String} regex | ||
* @param {Function} handler | ||
* @return {Function} middleware | ||
*/ | ||
function custom(fn, handler) { | ||
function matches(regex, handler) { | ||
return function handleContext(req, next) { | ||
return fn(req) ? handler(req, next) : next(); | ||
var string = req.context.map(function (e) { | ||
return e.event; | ||
}).join(':'); | ||
return regex.test(string) ? handler(req, next) : next(); | ||
}; | ||
@@ -38,15 +50,12 @@ } | ||
/** | ||
* | ||
* | ||
* Middleware creator that only executes the handler when | ||
* the custom function returns true given the the request | ||
* @param {Function} shouldHandle | ||
* @param {Function} handler | ||
* @return {Function} middleware | ||
*/ | ||
function after(event, handler) { | ||
function custom(shouldHandle, handler) { | ||
return function handleContext(req, next) { | ||
return (0, _last2.default)(req.context) === event ? handler(req, next) : next(); | ||
return shouldHandle(req) ? handler(req, next) : next(); | ||
}; | ||
} | ||
var context = exports.context = { | ||
matches: matches, | ||
custom: custom, | ||
after: after | ||
}; | ||
} |
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.context = exports.trackContext = undefined; | ||
@@ -17,9 +18,8 @@ var _trackContext = require('./trackContext'); | ||
var _context = require('./context'); | ||
var _context2 = require('./context'); | ||
Object.defineProperty(exports, 'context', { | ||
enumerable: true, | ||
get: function get() { | ||
return _context.context; | ||
} | ||
}); | ||
var _context = _interopRequireWildcard(_context2); | ||
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; } } | ||
exports.context = _context; |
@@ -31,2 +31,3 @@ 'use strict'; | ||
var context = (0, _get2.default)(req, 'raw.session.attributes.' + key, []); | ||
var now = { event: req.handler }; | ||
req.context = context; | ||
@@ -36,3 +37,3 @@ | ||
req.on('finished', function () { | ||
return req.session[key] = [].concat(_toConsumableArray(context), [req.handler]); | ||
return req.session[key] = [].concat(_toConsumableArray(context), [now]); | ||
}); | ||
@@ -39,0 +40,0 @@ next(); |
{ | ||
"name": "alexa-ability-context", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Simplify building multistep conversations with context-aware intent handling ", | ||
@@ -10,4 +10,4 @@ "main": "lib/index.js", | ||
"pretest": "npm run lint", | ||
"test": "mocha test --opts mocha.opts", | ||
"prepublish": "npm run test && npm run build", | ||
"test": "mocha test", | ||
"prepublish": "in-publish && npm run test && npm run build || not-in-publish", | ||
"publish:major": "npm version major && npm publish", | ||
@@ -60,2 +60,3 @@ "publish:minor": "npm version minor && npm publish", | ||
"eslint-plugin-react": "^3.16.1", | ||
"in-publish": "^2.0.0", | ||
"mocha": "^2.3.4", | ||
@@ -62,0 +63,0 @@ "sinon": "^1.17.2", |
@@ -54,4 +54,12 @@ # alexa-ability-context [![Build Status](https://travis-ci.org/nickclaw/alexa-ability-context.svg?branch=master)](https://travis-ci.org/nickclaw/alexa-ability-context) | ||
The returned middleware will add an additional property to the request object called `context`. Which will be an array of strings representing every request handled during the session. | ||
The returned middleware will add an additional property to the request object called | ||
`context`. Which will look something like this: | ||
```json | ||
[ | ||
{ "event": "launch" }, | ||
{ "event": "ExampleIntent" } | ||
] | ||
``` | ||
##### `context.after(event, handler) -> handler` | ||
@@ -58,0 +66,0 @@ Creates a new handler function that only executes when the previous intent matches. |
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
9591
93
89
14
8