Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alexa-ability-context

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alexa-ability-context - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

12

lib/context.js

@@ -14,2 +14,10 @@ 'use strict';

var _map = require('lodash/map');
var _map2 = _interopRequireDefault(_map);
var _compact = require('lodash/compact');
var _compact2 = _interopRequireDefault(_compact);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -40,5 +48,3 @@

return function handleContext(req, next) {
var string = req.context.map(function (e) {
return e.event;
}).join(':');
var string = (0, _compact2.default)((0, _map2.default)(req.context, 'event')).join(':');
return regex.test(string) ? handler(req, next) : next();

@@ -45,0 +51,0 @@ };

@@ -12,6 +12,8 @@ 'use strict';

var _clone = require('lodash/clone');
var _clone2 = _interopRequireDefault(_clone);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(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); } }
function trackContext() {

@@ -22,2 +24,4 @@ var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

var key = _ref$key === undefined ? '__context__' : _ref$key;
var _ref$property = _ref.property;
var property = _ref$property === undefined ? 'context' : _ref$property;

@@ -27,14 +31,32 @@ // return middleware

// self aware
if (req.context) {
if (req[property]) {
return next();
}
// get current context
var context = (0, _get2.default)(req, 'raw.session.attributes.' + key, []);
// prepare current context
var context = (0, _clone2.default)((0, _get2.default)(req, 'raw.session.attributes.' + key, []));
var shouldSave = true;
var now = { event: req.handler };
req.context = context;
Object.defineProperty(context, 'destroy', {
value: function value() {
context.splice(0, context.length);
}
});
Object.defineProperty(context, 'skip', {
value: function value() {
shouldSave = false;
}
});
Object.defineProperty(context, 'now', { value: now });
req[property] = context;
// before the request sends update the __context__ property in the session
// before the request sends, update the context in the session
req.on('finished', function () {
return req.session[key] = [].concat(_toConsumableArray(context), [now]);
var newContext = (0, _clone2.default)(context);
if (shouldSave) {
newContext.push(now);
}
req.session[key] = newContext;
});

@@ -41,0 +63,0 @@ next();

{
"name": "alexa-ability-context",
"version": "0.3.0",
"version": "0.4.0",
"description": "Simplify building multistep conversations with context-aware intent handling ",

@@ -49,3 +49,3 @@ "main": "lib/index.js",

"devDependencies": {
"alexa-ability": "^0.9.1",
"alexa-ability": "^0.11.1",
"babel-cli": "^6.4.5",

@@ -52,0 +52,0 @@ "babel-eslint": "^4.1.8",

@@ -49,8 +49,10 @@ # alexa-ability-context [![Build Status](https://travis-ci.org/nickclaw/alexa-ability-context.svg?branch=master)](https://travis-ci.org/nickclaw/alexa-ability-context)

##### `trackContext(options) -> middleware`
#### `trackContext(options) -> middleware`
A middleware factory that takes an optional `options` object. The currently supported
options are:
- `key`: defaults to `__context__`, the session key to store the context between requests.
- `property`: defaults `context`, the property to expose the context object as on the request.
The returned middleware will add an additional property to the request object called
#### `req.context`
The `trackContext` middleware will add an additional property to the request object called
`context`. Which will look something like this:

@@ -65,2 +67,14 @@

##### `req.context.now`
The object that will be added to the context for future requests. By default it is just
`{ event: 'name' }`, but you're free to modify it or add properties however you like.
##### `req.context.destroy()`
Clears the context.
##### `req.context.skip()`
Causes the `req.context.now` object to not be persisted.
#### `context`
##### `context.after(event, handler) -> handler`

@@ -67,0 +81,0 @@ Creates a new handler function that only executes when the previous intent matches.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc