Skip to content

Commit

Permalink
flash instance returns middleware directly without executing
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbaquerocelis committed Apr 11, 2019
1 parent 92fd748 commit bac4361
Show file tree
Hide file tree
Showing 6 changed files with 1,937 additions and 82 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
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"
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,47 @@ The simplest flash implementation for Express.
## Usage

```bash
npm i flash
npm install flash
```

```js
app.use(session()); // session middleware
app.use(require('flash')());
let flash = require('flash')

app.use(function (req, res) {
app.use(session()) // session middleware
app.use(flash)

app.use(function (req, res, next) {
// flash a message
req.flash('info', 'hello!');
next();
req.flash('info', 'hello!')
next()
})
```
## Using Pug

```jade
```pug
for message in flash
a.alert(class='alert-' + message.type)
p= message.message
```

```jade
while message = flash.shift() // consume messages as jade reads them
Or

```pug
while message = flash.shift() // consume messages as pug reads them
a.alert(class='alert-' + message.type)
p= message.message
```

## Using Nunjucks

```nunjucks
{% set data = flash.shift() %}
<div class="card">
<p class="text"><strong>{{data.type}}:</strong> {{ data.message }}</p>
</div>
```

## API

### req.flash([type], msg)
Expand Down
47 changes: 26 additions & 21 deletions index.js
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
Loading

0 comments on commit bac4361

Please sign in to comment.