session-flash 1.0.11 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +23 -34
- package/lib/index.d.ts +33 -14
- package/lib/index.js +57 -4
- package/package.json +1 -1
- package/lib/flash.js +0 -80
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,36 +1,42 @@
|
|
|
1
|
-
#
|
|
1
|
+
# session-flash
|
|
2
2
|
|
|
3
3
|
The flash is a special area of the session used for storing messages. Messages
|
|
4
4
|
are written to the flash and cleared after being displayed to the user. The
|
|
5
5
|
flash is typically used in combination with redirects, ensuring that the message
|
|
6
6
|
is available to the next page that is to be rendered.
|
|
7
7
|
|
|
8
|
-
This middleware was extracted from [Express](http://expressjs.com/) 2.x, after
|
|
9
|
-
Express 3.x removed direct support for the flash. connect-flash brings this
|
|
10
|
-
functionality back to Express 3.x, as well as any other middleware-compatible
|
|
11
|
-
framework or application. +1 for [radical reusability](http://substack.net/posts/b96642/the-node-js-aesthetic).
|
|
12
8
|
|
|
13
9
|
## Install
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
npm i session-flash
|
|
16
12
|
|
|
17
13
|
## Usage
|
|
18
14
|
|
|
19
|
-
#### Express
|
|
15
|
+
#### Express 5.x
|
|
20
16
|
|
|
21
17
|
Flash messages are stored in the session. First, setup sessions as usual by
|
|
22
18
|
enabling `cookieParser` and `session` middleware. Then, use `flash` middleware
|
|
23
|
-
provided by
|
|
19
|
+
provided by session-flash.
|
|
24
20
|
|
|
25
21
|
```javascript
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
const app = express();
|
|
23
|
+
const cookieParser = require('cookie-parser');
|
|
24
|
+
const session = require('express-session');
|
|
25
|
+
const flash = require('session-flash');
|
|
26
|
+
|
|
27
|
+
app.use(cookieParser());
|
|
28
|
+
|
|
29
|
+
app.use(session({
|
|
30
|
+
secret: 'session-secret',
|
|
31
|
+
resave: false,
|
|
32
|
+
saveUninitialized: false,
|
|
33
|
+
cookie: { maxAge: 60000 }
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
app.use(flash());
|
|
37
|
+
|
|
38
|
+
|
|
28
39
|
|
|
29
|
-
app.configure(function() {
|
|
30
|
-
app.use(express.cookieParser('keyboard cat'));
|
|
31
|
-
app.use(express.session({ cookie: { maxAge: 60000 }}));
|
|
32
|
-
app.use(flash());
|
|
33
|
-
});
|
|
34
40
|
```
|
|
35
41
|
|
|
36
42
|
With the `flash` middleware in place, all requests will have a `req.flash()` function
|
|
@@ -49,25 +55,8 @@ app.get('/', function(req, res){
|
|
|
49
55
|
});
|
|
50
56
|
```
|
|
51
57
|
|
|
52
|
-
## Examples
|
|
53
|
-
|
|
54
|
-
For an example using connect-flash in an Express 3.x app, refer to the [express3](https://github.com/jaredhanson/connect-flash/tree/master/examples/express3)
|
|
55
|
-
example.
|
|
56
|
-
|
|
57
|
-
## Tests
|
|
58
|
-
|
|
59
|
-
$ npm install --dev
|
|
60
|
-
$ make test
|
|
61
|
-
|
|
62
|
-
[](http://travis-ci.org/jaredhanson/connect-flash)
|
|
63
|
-
|
|
64
|
-
## Credits
|
|
65
|
-
|
|
66
|
-
- [Jared Hanson](http://github.com/jaredhanson)
|
|
67
|
-
- [TJ Holowaychuk](https://github.com/visionmedia)
|
|
68
|
-
|
|
69
58
|
## License
|
|
70
59
|
|
|
71
|
-
[The MIT License]
|
|
60
|
+
[The MIT License]
|
|
72
61
|
|
|
73
|
-
Copyright (c) 2012-
|
|
62
|
+
Copyright (c) 2012-2026
|
package/lib/index.d.ts
CHANGED
|
@@ -1,19 +1,38 @@
|
|
|
1
1
|
/// <reference types="express" />
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
import 'express';
|
|
4
|
+
|
|
5
|
+
declare module 'express-serve-static-core' {
|
|
6
|
+
interface Request {
|
|
7
|
+
/**
|
|
8
|
+
* Get all flash messages and clear them
|
|
9
|
+
*/
|
|
10
|
+
flash(): Record<string, string[]>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Get messages of a specific type and clear them
|
|
14
|
+
*/
|
|
15
|
+
flash(type: string): string[];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Add one or more flash messages
|
|
19
|
+
*/
|
|
20
|
+
flash(type: string, message: string | string[]): number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Add a formatted flash message (util.format)
|
|
24
|
+
*/
|
|
25
|
+
flash(type: string, format: string, ...args: any[]): number;
|
|
26
|
+
}
|
|
10
27
|
}
|
|
11
28
|
|
|
12
|
-
declare module
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
29
|
+
declare module 'session-flash' {
|
|
30
|
+
import { RequestHandler } from 'express';
|
|
31
|
+
|
|
32
|
+
interface FlashOptions {
|
|
33
|
+
unsafe?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const flash: (options?: FlashOptions) => RequestHandler;
|
|
37
|
+
export = flash;
|
|
19
38
|
}
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { format } = require('node:util');
|
|
4
|
+
|
|
5
|
+
module.exports = function flash(options) {
|
|
6
|
+
options = options || {};
|
|
7
|
+
|
|
8
|
+
const safe = options.unsafe === undefined
|
|
9
|
+
? true
|
|
10
|
+
: !options.unsafe;
|
|
11
|
+
|
|
12
|
+
return function flashMiddleware(req, res, next) {
|
|
13
|
+
if (req.flash && safe) {
|
|
14
|
+
return next();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
req.flash = _flash;
|
|
18
|
+
next();
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function _flash(type, msg) {
|
|
23
|
+
if (this.session === undefined) {
|
|
24
|
+
throw new Error('req.flash() requires sessions');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const flash = this.session.flash || (this.session.flash = {});
|
|
28
|
+
|
|
29
|
+
// SET
|
|
30
|
+
if (type && msg !== undefined) {
|
|
31
|
+
// util.format support
|
|
32
|
+
if (arguments.length > 2) {
|
|
33
|
+
msg = format.apply(null, Array.prototype.slice.call(arguments, 1));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Array support
|
|
37
|
+
if (Array.isArray(msg)) {
|
|
38
|
+
msg.forEach(val => {
|
|
39
|
+
(flash[type] = flash[type] || []).push(val);
|
|
40
|
+
});
|
|
41
|
+
return flash[type].length;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (flash[type] = flash[type] || []).push(msg);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// GET by type
|
|
48
|
+
if (type) {
|
|
49
|
+
const msgs = flash[type];
|
|
50
|
+
delete flash[type];
|
|
51
|
+
return msgs || [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// GET all
|
|
55
|
+
this.session.flash = {};
|
|
56
|
+
return flash;
|
|
57
|
+
}
|
package/package.json
CHANGED
package/lib/flash.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module dependencies.
|
|
3
|
-
*/
|
|
4
|
-
var format = require('util').format;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Expose `flash()` function on requests.
|
|
8
|
-
*
|
|
9
|
-
* @return {Function}
|
|
10
|
-
* @api public
|
|
11
|
-
*/
|
|
12
|
-
module.exports = function flash(options) {
|
|
13
|
-
options = options || {};
|
|
14
|
-
var safe = (options.unsafe === undefined) ? true : !options.unsafe;
|
|
15
|
-
|
|
16
|
-
return function(req, res, next) {
|
|
17
|
-
if (req.flash && safe) { return next(); }
|
|
18
|
-
req.flash = _flash;
|
|
19
|
-
next();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Queue flash `msg` of the given `type`.
|
|
25
|
-
*
|
|
26
|
-
* Examples:
|
|
27
|
-
*
|
|
28
|
-
* req.flash('info', 'email sent');
|
|
29
|
-
* req.flash('error', 'email delivery failed');
|
|
30
|
-
* req.flash('info', 'email re-sent');
|
|
31
|
-
* // => 2
|
|
32
|
-
*
|
|
33
|
-
* req.flash('info');
|
|
34
|
-
* // => ['email sent', 'email re-sent']
|
|
35
|
-
*
|
|
36
|
-
* req.flash('info');
|
|
37
|
-
* // => []
|
|
38
|
-
*
|
|
39
|
-
* req.flash();
|
|
40
|
-
* // => { error: ['email delivery failed'], info: [] }
|
|
41
|
-
*
|
|
42
|
-
* Formatting:
|
|
43
|
-
*
|
|
44
|
-
* Flash notifications also support arbitrary formatting support.
|
|
45
|
-
* For example you may pass variable arguments to `req.flash()`
|
|
46
|
-
* and use the %s specifier to be replaced by the associated argument:
|
|
47
|
-
*
|
|
48
|
-
* req.flash('info', 'email has been sent to %s.', userName);
|
|
49
|
-
*
|
|
50
|
-
* Formatting uses `util.format()`, which is available on Node 0.6+.
|
|
51
|
-
*
|
|
52
|
-
* @param {String} type
|
|
53
|
-
* @param {String} msg
|
|
54
|
-
* @return {Array|Object|Number}
|
|
55
|
-
* @api public
|
|
56
|
-
*/
|
|
57
|
-
function _flash(type, msg) {
|
|
58
|
-
if (this.session === undefined) throw Error('req.flash() requires sessions');
|
|
59
|
-
var msgs = this.session.flash = this.session.flash || {};
|
|
60
|
-
if (type && msg) {
|
|
61
|
-
// util.format is available in Node.js 0.6+
|
|
62
|
-
if (arguments.length > 2 && format) {
|
|
63
|
-
var args = Array.prototype.slice.call(arguments, 1);
|
|
64
|
-
msg = format.apply(undefined, args);
|
|
65
|
-
} else if (Array.isArray(msg)) {
|
|
66
|
-
msg.forEach(function(val){
|
|
67
|
-
(msgs[type] = msgs[type] || []).push(val);
|
|
68
|
-
});
|
|
69
|
-
return msgs[type].length;
|
|
70
|
-
}
|
|
71
|
-
return (msgs[type] = msgs[type] || []).push(msg);
|
|
72
|
-
} else if (type) {
|
|
73
|
-
var arr = msgs[type];
|
|
74
|
-
delete msgs[type];
|
|
75
|
-
return arr || [];
|
|
76
|
-
} else {
|
|
77
|
-
this.session.flash = {};
|
|
78
|
-
return msgs;
|
|
79
|
-
}
|
|
80
|
-
}
|