-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flash instance returns middleware directly without executing
- Loading branch information
1 parent
92fd748
commit bac4361
Showing
6 changed files
with
1,937 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
node_js: | ||
- "0.8" | ||
- "0.10" | ||
- "0.11" | ||
- "7" | ||
- "8" | ||
- "9" | ||
- "10" | ||
- "11" | ||
language: node_js | ||
script: "npm run-script test-travis" | ||
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,37 @@ | ||
'use strict' | ||
|
||
var assert = require('assert') | ||
|
||
module.exports = function () { | ||
return function (req, res, next) { | ||
assert(req.session, 'a req.session is required!') | ||
function flash (req, res, next) { | ||
// Let's validate that req.session is required | ||
if (req.session) { | ||
// If session.flash doesn't exists we create a new array | ||
if (!Array.isArray(req.session.flash)) req.session.flash = [] | ||
|
||
res.locals.flash = req.session.flash | ||
req.flash = res.flash = push | ||
req.flash = res.flash = pushMessage | ||
|
||
next() | ||
} else { | ||
let error = new Error('A req.session is required!') | ||
|
||
next(error) | ||
} | ||
} | ||
|
||
function push(type, msg) { | ||
if (!msg) { | ||
msg = type | ||
type = 'info' | ||
} | ||
msg = { | ||
message: msg, | ||
type: type | ||
function pushMessage (type, message) { | ||
let res = this.res || this | ||
let messages = res.locals.flash | ||
// Let's create a new flash message to push | ||
let flashMessage = { | ||
type: message ? type : 'info', | ||
message: message || type | ||
} | ||
var res = this.res || this | ||
var messages = res.locals.flash | ||
|
||
// do not allow duplicate flash messages | ||
for (var i = 0; i < messages.length; i++) { | ||
var message = messages[i] | ||
if (msg.type === message.type && msg.message === message.message) return this | ||
} | ||
messages.push(msg) | ||
let msg = messages.find(msg => msg.type === flashMessage.type && msg.message === flashMessage.message) | ||
if (msg) return this | ||
|
||
messages.push(flashMessage) | ||
return this | ||
} | ||
|
||
module.exports = flash |
Oops, something went wrong.