alexa-ability
Advanced tools
Comparing version 0.5.2 to 0.6.0
@@ -20,2 +20,6 @@ 'use strict'; | ||
var _noop = require('lodash/noop'); | ||
var _noop2 = _interopRequireDefault(_noop); | ||
var _Request = require('./Request'); | ||
@@ -42,2 +46,6 @@ | ||
var hLog = (0, _debug2.default)('alexa-ability:ability:handle'); | ||
var warnSent = function warnSent() { | ||
return console.warn( // eslint-disable-line no-console | ||
'Request already sent. Don\'t call "next" function after sending response.'); | ||
}; | ||
@@ -50,2 +58,3 @@ var Ability = exports.Ability = function () { | ||
// eslint-disable-line no-unused-vars | ||
this._middleware = []; | ||
@@ -76,5 +85,13 @@ this._handlers = _extends({}, _defaultHandlers.handlers); | ||
key: 'handle', | ||
value: function handle(event, callback) { | ||
value: function handle(event) { | ||
var callback = arguments.length <= 1 || arguments[1] === undefined ? _noop2.default : arguments[1]; | ||
var type = (0, _getEventName.getEventName)(event); | ||
var req = new _Request.Request(event); | ||
req.on('finished', function () { | ||
return callback(null, req); | ||
}); | ||
req.on('failed', function (err) { | ||
return callback(err, req); | ||
}); | ||
@@ -91,9 +108,13 @@ // get possible handlers | ||
var stack = [].concat(this._middleware, handler); | ||
req.on('finished', function () { | ||
return done(); | ||
}); | ||
next(); | ||
return req; | ||
function next(err) { | ||
if (req.sent) { | ||
warnSent(); | ||
return; | ||
} | ||
if (err) { | ||
@@ -107,4 +128,4 @@ hLog('executing error handler'); | ||
if (!fn) { | ||
hLog('executing default handler'); | ||
(0, _resolve.resolve)(defHandler, next, req); | ||
hLog('executing unhandledEvent handler'); | ||
(0, _resolve.resolve)(defHandler, done, req); | ||
return; | ||
@@ -117,6 +138,15 @@ } | ||
// if we ever reach this function then everything has failed | ||
function done(err) { | ||
if (typeof callback === "function") { | ||
callback(err, req); | ||
if (req.sent) { | ||
warnSent(); | ||
return; | ||
} | ||
if (err) { | ||
req.emit('failed', err); | ||
return; | ||
} | ||
req.emit('failed', new Error('Unhandled event.')); | ||
} | ||
@@ -123,0 +153,0 @@ } |
@@ -33,3 +33,4 @@ 'use strict'; | ||
var noErrHandlerWarning = (0, _once2.default)(function () { | ||
return console.warn('Warning: Unhandled error. Add an error handler.'); | ||
return console.warn( // eslint-disable-line no-console | ||
'Warning: Unhandled error. Add an error handler.'); | ||
}); | ||
@@ -39,6 +40,6 @@ | ||
dLog('unhandled request', req); | ||
next(new Error("No event handler found.")); | ||
next(new Error('No event handler found.')); | ||
}), _defineProperty(_handlers, e.unknownEvent, function unknownEventHandler(req, next) { | ||
uLog('unknown request', req); | ||
next(new Error("Unknown request type.")); | ||
next(new Error('Unknown request type.')); | ||
}), _defineProperty(_handlers, e.error, function defaultErrorHandler(err, req, next) { | ||
@@ -45,0 +46,0 @@ eLog('unhandled error', err); |
@@ -31,9 +31,9 @@ 'use strict'; | ||
switch (type) { | ||
case "LaunchRequest": | ||
case 'LaunchRequest': | ||
return e.launch; | ||
case "IntentRequest": | ||
case 'IntentRequest': | ||
return (0, _get2.default)(event, 'request.intent.name'); | ||
case "SessionEndedRequest": | ||
case 'SessionEndedRequest': | ||
return e.end; | ||
@@ -40,0 +40,0 @@ |
@@ -14,6 +14,2 @@ 'use strict'; | ||
var _omit = require('lodash/omit'); | ||
var _omit2 = _interopRequireDefault(_omit); | ||
var _transform = require('lodash/transform'); | ||
@@ -72,5 +68,5 @@ | ||
this._res.card = { | ||
type: "Simple", | ||
title: title, | ||
content: content | ||
content: content, | ||
type: 'Simple' | ||
}; | ||
@@ -101,2 +97,8 @@ return this; | ||
}, { | ||
key: 'fail', | ||
value: function fail(err) { | ||
this.sent = true; | ||
this.emit('failed', err, this); | ||
} | ||
}, { | ||
key: 'toJSON', | ||
@@ -122,3 +124,3 @@ value: function toJSON() { | ||
return isTag ? { type: "SSML", ssml: (0, _alexaSsml.renderToString)(content) } : { type: "PlainText", text: content }; | ||
return isTag ? { type: 'SSML', ssml: (0, _alexaSsml.renderToString)(content) } : { type: 'PlainText', text: content }; | ||
} |
{ | ||
"name": "alexa-ability", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "An Alexa skills framework for node", | ||
@@ -40,2 +40,3 @@ "main": "lib/index.js", | ||
"babel-cli": "^6.4.5", | ||
"babel-eslint": "^4.1.8", | ||
"babel-plugin-transform-react-jsx": "^6.4.0", | ||
@@ -46,2 +47,5 @@ "babel-preset-es2015": "^6.3.13", | ||
"chai": "^3.4.1", | ||
"eslint": "^1.10.3", | ||
"eslint-config-airbnb": "^4.0.0", | ||
"eslint-plugin-react": "^3.16.1", | ||
"mocha": "^2.3.4", | ||
@@ -48,0 +52,0 @@ "sinon": "^1.17.2", |
@@ -21,3 +21,3 @@ # alexa-ability [![Build Status](https://travis-ci.org/nickclaw/alexa-ability.svg?branch=master)](https://travis-ci.org/nickclaw/alexa-ability) | ||
ability.on(events.LAUNCH, function(req) { | ||
ability.on(events.launch, function(req) { | ||
const speech = ( | ||
@@ -41,7 +41,7 @@ <speak> | ||
ability.on('error', function(err, req, next) { | ||
ability.on(events.error, function(err, req, next) { | ||
req.say('Uhoh, something went wrong'); | ||
}); | ||
export const handler = handle(app); | ||
export const handler = handleAbility(ability); | ||
``` | ||
@@ -85,2 +85,3 @@ | ||
A boolean indicating whether this request has been sent. | ||
This will be true after the use calls `end`, `send`, or `fail` on the request instance. | ||
@@ -145,4 +146,8 @@ ##### `request.isNew` | ||
##### `request.fail(err) -> undefined` | ||
Fail. Immediately halts execution of all middleware and handlers. | ||
Requests that are failed will skip the `"error"` handler completely. The argument passed to this function will be passed to the handling callback function. | ||
##### `request.toJSON() -> Object` | ||
Get a properly formatted response JSON object. | ||
Get a properly formatted response JSON response. | ||
@@ -149,0 +154,0 @@ #### events |
@@ -108,3 +108,36 @@ import noop from 'lodash/noop'; | ||
}); | ||
it('should not call more middleware or handlers after request has been sent', function(done) { | ||
const spy = sinon.spy((req, next) => req.end()); | ||
app.use((req, next) => { | ||
req.end(); | ||
next(); | ||
}); | ||
app.on('launch', spy); | ||
app.handle(launchRequest, function(err, req) { | ||
expect(err).to.be.falsy; | ||
expect(spy).to.not.have.been.called; | ||
done(); | ||
}); | ||
}); | ||
it('should should warn when "next" is called after request has been sent', function(done) { | ||
const _oldWarn = console.warn; | ||
console.warn = sinon.spy(_oldWarn); | ||
app.use((req, next) => { | ||
req.end(); | ||
next(); | ||
}); | ||
app.on('launch', req => req.end()); | ||
app.handle(launchRequest, function(err, req) { | ||
process.nextTick(function() { // give it time for warning to happen | ||
expect(err).to.be.falsy; | ||
expect(console.warn).to.have.been.called; | ||
console.warn = _oldWarn; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -92,2 +92,15 @@ import { Ability } from '../src/Ability'; | ||
}); | ||
it('using "req.fail()" should immediately halt execution and fail', function() { | ||
const spy = sinon.spy(req => req.end()); | ||
const err = new Error(); | ||
app.use(req => req.fail(err)); | ||
app.on('launch', spy); | ||
app.handle(launchRequest, function(e, req) { | ||
expect(spy).to.not.have.been.called; | ||
expect(e).to.equal(err); | ||
expect(req.sent).to.equal(true); | ||
}); | ||
}); | ||
}); |
@@ -163,2 +163,24 @@ /** @jsx ssml */ | ||
describe('"fail" function', function() { | ||
it('should not chain', function() { | ||
expect(req.fail(new Error())).to.be.undefined; | ||
}); | ||
it('should emit the "failed" event with error and request', function() { | ||
const spy = sinon.spy(); | ||
const err = new Error(); | ||
req.on('failed', spy); | ||
expect(spy).to.not.have.been.called; | ||
req.fail(err); | ||
expect(spy).to.have.been.calledWith(err, req); | ||
}); | ||
it('should set the sent property to true', function() { | ||
expect(req.sent).to.equal(false); | ||
req.fail(new Error()); | ||
expect(req.sent).to.equal(true); | ||
}); | ||
}); | ||
describe('"toJSON" function', function() { | ||
@@ -165,0 +187,0 @@ |
42634
24
890
167
14