hapi-stateless-notifications
Advanced tools
Comparing version 1.2.1 to 2.1.0
41
index.js
var P = require('bluebird'); | ||
var TokenFacilitator = require('token-facilitator'); | ||
var collectFailures = require('promise.allrejected'); | ||
var crypto = require('crypto'); | ||
@@ -16,3 +15,15 @@ | ||
request.saveNotifications = function(promises) { | ||
return collectFailures(promises).then(putErrorsInRedis(request.redis, options)); | ||
return P.all(promises.map(function(promise) { | ||
return P.resolve(promise).then(function(successNotice) { | ||
return P.resolve({ | ||
notice: successNotice, | ||
type: 'success' | ||
}); | ||
}).catch(function(errorNotice) { | ||
return P.resolve({ | ||
notice: errorNotice, | ||
type: 'error' | ||
}); | ||
}); | ||
})).then(putNoticesInRedis(request.redis, options)); | ||
}; | ||
@@ -40,2 +51,3 @@ | ||
request.logger.info("Found notices", data); | ||
@@ -52,3 +64,18 @@ | ||
request.response.source.context.notices = data.notices; | ||
var notices = data.notices.filter(function(notice){ | ||
return typeof notice.notice !== "undefined" && !!notice.notice; | ||
}); | ||
Object.assign(request.response.source.context, { | ||
errorNotices: notices.filter(function(notice) { | ||
return notice.type === 'error'; | ||
}).map(function(notice) { | ||
return notice.notice; | ||
}), | ||
successNotices: notices.filter(function(notice) { | ||
return notice.type === 'success'; | ||
}).map(function(notice) { | ||
return notice.notice; | ||
}) | ||
}); | ||
} | ||
@@ -70,5 +97,5 @@ }).catch(function(e) { | ||
function putErrorsInRedis(redis, options) { | ||
return function(errors) { | ||
if (errors.length) { | ||
function putNoticesInRedis(redis, options) { | ||
return function(notices) { | ||
if (notices.length) { | ||
var facilitator = new TokenFacilitator({ | ||
@@ -78,3 +105,3 @@ redis: redis | ||
return P.promisify(facilitator.generate, facilitator)({ | ||
notices: errors | ||
notices: notices | ||
}, { | ||
@@ -81,0 +108,0 @@ timeout: options.timeout || 3600, |
{ | ||
"name": "hapi-stateless-notifications", | ||
"version": "1.2.1", | ||
"version": "2.1.0", | ||
"description": "A simple, explicit-state plugin to pass notices between pages.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
hapi-stateless-notifications | ||
============================ | ||
A plugin to give a hapi `reply` a `reply.saveNotifications()` method that collect non-fatal errors and store them long enough to display on later pages, given the token. | ||
A plugin to give a hapi `reply` a `reply.saveNotifications()` method that collects user notifications and store them long enough to display on later pages, given the token. | ||
@@ -34,3 +34,4 @@ Use | ||
request.saveNotifications([ | ||
Promise.reject("Error message here") | ||
Promise.resolve('Success message here ...'), | ||
Promise.reject('Error message here ...'), | ||
]).then(function (token) { | ||
@@ -41,7 +42,1 @@ // if there's a token, put it in the query of the page you load next as `notice={token}` | ||
``` | ||
Any rejected promises will be collected and their error messages displayed. | ||
Successful promises (and plain values) are ignored. | ||
V2 may separate "store these bits of text" and "collect some failed promises" | ||
but for now they're a single interface. |
12
test.js
@@ -29,3 +29,6 @@ var test = require('tap').test; | ||
request.saveNotifications([ | ||
Promise.reject('boom') | ||
Promise.resolve('yay'), | ||
Promise.reject('boom'), | ||
Promise.reject(''), | ||
Promise.resolve() | ||
]).then(function (token) { | ||
@@ -73,3 +76,8 @@ t.ok(token, 'got token'); | ||
server.inject({ method: "GET", url: '/2?notice=' + token}, function (res) { | ||
t.equal(res.result.trim(), 'notice: boom'); | ||
var renderedNotices = res.result.trim().split('\n').map(function(value) { | ||
return value.trim(); | ||
}) | ||
t.equal(renderedNotices[0], 'success notice: yay'); | ||
t.equal(renderedNotices[1], 'error notice: boom'); | ||
t.equal(renderedNotices.length, 2); | ||
server.stop(); | ||
@@ -76,0 +84,0 @@ client.quit(); |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
8070
172
41
1