session-flash 1.0.10 → 1.0.11
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/.travis.yml +5 -0
- package/LICENSE +1 -1
- package/README.md +34 -23
- package/lib/flash.js +25 -32
- package/lib/index.d.ts +14 -42
- package/package.json +10 -18
package/.travis.yml
ADDED
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,42 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# connect-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).
|
|
8
12
|
|
|
9
13
|
## Install
|
|
10
14
|
|
|
11
|
-
npm
|
|
15
|
+
$ npm install connect-flash
|
|
12
16
|
|
|
13
17
|
## Usage
|
|
14
18
|
|
|
15
|
-
#### Express
|
|
19
|
+
#### Express 3.x
|
|
16
20
|
|
|
17
21
|
Flash messages are stored in the session. First, setup sessions as usual by
|
|
18
22
|
enabling `cookieParser` and `session` middleware. Then, use `flash` middleware
|
|
19
|
-
provided by
|
|
23
|
+
provided by connect-flash.
|
|
20
24
|
|
|
21
25
|
```javascript
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
26
|
+
var flash = require('connect-flash');
|
|
27
|
+
var app = express();
|
|
39
28
|
|
|
29
|
+
app.configure(function() {
|
|
30
|
+
app.use(express.cookieParser('keyboard cat'));
|
|
31
|
+
app.use(express.session({ cookie: { maxAge: 60000 }}));
|
|
32
|
+
app.use(flash());
|
|
33
|
+
});
|
|
40
34
|
```
|
|
41
35
|
|
|
42
36
|
With the `flash` middleware in place, all requests will have a `req.flash()` function
|
|
@@ -55,8 +49,25 @@ app.get('/', function(req, res){
|
|
|
55
49
|
});
|
|
56
50
|
```
|
|
57
51
|
|
|
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
|
+
|
|
58
69
|
## License
|
|
59
70
|
|
|
60
|
-
[The MIT License]
|
|
71
|
+
[The MIT License](http://opensource.org/licenses/MIT)
|
|
61
72
|
|
|
62
|
-
Copyright (c) 2012-
|
|
73
|
+
Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>
|
package/lib/flash.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Module dependencies.
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
var format = require('util').format;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Expose `flash()` function on requests.
|
|
@@ -11,14 +11,14 @@ const { format } = require('util');
|
|
|
11
11
|
*/
|
|
12
12
|
module.exports = function flash(options) {
|
|
13
13
|
options = options || {};
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
var safe = (options.unsafe === undefined) ? true : !options.unsafe;
|
|
15
|
+
|
|
16
16
|
return function(req, res, next) {
|
|
17
|
-
if (req.flash && safe) return next();
|
|
17
|
+
if (req.flash && safe) { return next(); }
|
|
18
18
|
req.flash = _flash;
|
|
19
19
|
next();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* Queue flash `msg` of the given `type`.
|
|
@@ -41,45 +41,38 @@ module.exports = function flash(options) {
|
|
|
41
41
|
*
|
|
42
42
|
* Formatting:
|
|
43
43
|
*
|
|
44
|
-
* Flash notifications support formatting
|
|
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:
|
|
45
47
|
*
|
|
46
48
|
* req.flash('info', 'email has been sent to %s.', userName);
|
|
47
49
|
*
|
|
48
|
-
*
|
|
50
|
+
* Formatting uses `util.format()`, which is available on Node 0.6+.
|
|
49
51
|
*
|
|
50
52
|
* @param {String} type
|
|
51
|
-
* @param {String
|
|
53
|
+
* @param {String} msg
|
|
52
54
|
* @return {Array|Object|Number}
|
|
53
55
|
* @api public
|
|
54
56
|
*/
|
|
55
57
|
function _flash(type, msg) {
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
msg
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
// Handle array of messages
|
|
68
|
-
if (Array.isArray(msg)) {
|
|
69
|
-
msgs[type] = msgs[type] || [];
|
|
70
|
-
msg.forEach(val => msgs[type].push(val));
|
|
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
|
+
});
|
|
71
69
|
return msgs[type].length;
|
|
72
70
|
}
|
|
73
|
-
|
|
74
|
-
// Handle single message
|
|
75
|
-
msgs[type] = msgs[type] || [];
|
|
76
|
-
return msgs[type].push(msg);
|
|
77
|
-
|
|
71
|
+
return (msgs[type] = msgs[type] || []).push(msg);
|
|
78
72
|
} else if (type) {
|
|
79
|
-
|
|
73
|
+
var arr = msgs[type];
|
|
80
74
|
delete msgs[type];
|
|
81
|
-
return arr;
|
|
82
|
-
|
|
75
|
+
return arr || [];
|
|
83
76
|
} else {
|
|
84
77
|
this.session.flash = {};
|
|
85
78
|
return msgs;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,47 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="express" />
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* Default: true
|
|
10
|
-
*/
|
|
11
|
-
unsafe?: boolean;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Augment Express Request interface to include flash
|
|
16
|
-
*/
|
|
17
|
-
declare global {
|
|
18
|
-
namespace Express {
|
|
19
|
-
interface Request {
|
|
20
|
-
/**
|
|
21
|
-
* Flash messages.
|
|
22
|
-
*
|
|
23
|
-
* Usage:
|
|
24
|
-
* req.flash('info', 'message')
|
|
25
|
-
* req.flash('info')
|
|
26
|
-
* req.flash()
|
|
27
|
-
*/
|
|
28
|
-
flash(type: string, msg: string | string[], ...args: any[]): number;
|
|
29
|
-
flash(type: string): string[];
|
|
30
|
-
flash(): Record<string, string[]>;
|
|
3
|
+
declare namespace Express {
|
|
4
|
+
export interface Request {
|
|
5
|
+
flash(): { [key: string]: string[] };
|
|
6
|
+
flash(message: string): string[];
|
|
7
|
+
flash(type: string, message: string[] | string): number;
|
|
8
|
+
flash(type: string, format: string, ...args: any[]): number;
|
|
31
9
|
}
|
|
10
|
+
}
|
|
32
11
|
|
|
33
|
-
|
|
34
|
-
|
|
12
|
+
declare module "session-flash" {
|
|
13
|
+
import express = require("express");
|
|
14
|
+
interface IConnectFlashOptions {
|
|
15
|
+
unsafe?: boolean | undefined;
|
|
35
16
|
}
|
|
36
|
-
|
|
17
|
+
function e(options?: IConnectFlashOptions): express.RequestHandler;
|
|
18
|
+
export = e;
|
|
37
19
|
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Flash middleware factory
|
|
41
|
-
*
|
|
42
|
-
* @param options FlashOptions
|
|
43
|
-
* @returns Express middleware
|
|
44
|
-
*/
|
|
45
|
-
declare function flash(options?: FlashOptions): express.RequestHandler;
|
|
46
|
-
|
|
47
|
-
export = flash;
|
package/package.json
CHANGED
|
@@ -1,45 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "session-flash",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Flash message middleware for
|
|
3
|
+
"version": "1.0.11",
|
|
4
|
+
"description": "Flash message middleware for Express sessions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"session",
|
|
7
7
|
"express",
|
|
8
8
|
"flash",
|
|
9
9
|
"messages"
|
|
10
10
|
],
|
|
11
|
-
"author":
|
|
12
|
-
|
|
13
|
-
"email": "andrewkhabweri@gmail.com"
|
|
14
|
-
},
|
|
11
|
+
"author": "Andrew Khabweri <andrewkhabweri@gmail.com>",
|
|
12
|
+
"license": "MIT",
|
|
15
13
|
"type": "commonjs",
|
|
16
|
-
"
|
|
17
|
-
{
|
|
18
|
-
"type": "MIT"
|
|
19
|
-
}
|
|
20
|
-
],
|
|
21
|
-
"main": "./lib",
|
|
14
|
+
"main": "./lib/index.js",
|
|
22
15
|
"exports": {
|
|
23
16
|
".": {
|
|
24
17
|
"require": "./lib/index.js",
|
|
25
18
|
"types": "./lib/index.d.ts"
|
|
26
19
|
}
|
|
27
20
|
},
|
|
28
|
-
"files": [
|
|
29
|
-
"lib"
|
|
30
|
-
],
|
|
31
21
|
"dependencies": {},
|
|
32
22
|
"peerDependencies": {
|
|
33
23
|
"express": ">=4",
|
|
34
24
|
"express-session": ">=1.15"
|
|
35
25
|
},
|
|
36
26
|
"devDependencies": {
|
|
37
|
-
"
|
|
27
|
+
"mocha": "^10.2.0",
|
|
28
|
+
"supertest": "^6.3.3",
|
|
29
|
+
"chai": "^4.3.7"
|
|
38
30
|
},
|
|
39
31
|
"scripts": {
|
|
40
|
-
"test": "
|
|
32
|
+
"test": "mocha 'test/**/*.test.js'"
|
|
41
33
|
},
|
|
42
34
|
"engines": {
|
|
43
|
-
"node": ">=18
|
|
35
|
+
"node": ">=18"
|
|
44
36
|
}
|
|
45
37
|
}
|