alexa-ability-stash

0.2.1 • Public • Published

alexa-ability-stash Build Status

An alexa-ability middleware for persistent user storage.

Warning: Still in development. Not suitable for use in production yet.

Example

import { Ability } from 'alexa-ability';
import { handleAbility } from 'alexa-ability-lambda-handler';
import createStash from 'alexa-ability-stash';
import createRedisStore from 'connect-redis'; // or any compatible express-session store
 
const RedisStore = createRedisStore(createStash);
const store = new RedisStore(options);
const app = new Ability();
 
app.use(createStash({ store }));
 
app.on('LuckyNumberIntent', function(req, next) {
    if (!req.stash.luckyNumber) {
        // persisted "forever"
        req.stash.luckyNumber = Math.floor(Math.random() * 100 + 1);
    }
 
    req.say(`Your lucky number is ${req.stash.luckyNumber}!`).end();
});
 
export const handler = handleAbility(app);

API

createStash(options) -> middleware

Creates a middleware function to handle the stash.

Takes the following options:

  • store - one of the stores defined below
  • genid - a function that takes a request object and returns a string. Returns <appId>:<userId> by default.
  • unset - Behavior when req.stash is deleted. destroy or keep. Defaults to keep.
    • keep The stash will be kept, but modifications made during the request are ignored and not saved.
    • destroy: The stash will be destroyed (deleted) when the response ends.
  • resave Forces the stash to be saved, even if the stash was never modified. Defaults to true.

Attaches two properties to the request object:

  • stash an object you can read and modify. Changes will be saved across requests.
  • stashId the id generated by genid.

Stores

Theoretically all the store supported by express-session should work. In practice, only connect-redis has been tested.

If a store doesn't work for you raise an issue!

Package Sidebar

Install

npm i alexa-ability-stash

Weekly Downloads

1

Version

0.2.1

License

MIT

Last publish

Collaborators

  • nickclaw