session-flash 1.0.12 → 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/lib/index.d.ts +33 -14
- package/lib/index.js +57 -4
- package/package.json +1 -1
- package/lib/flash.js +0 -80
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
|
-
}
|