session-flash 1.0.3 → 1.0.5

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/README.md CHANGED
@@ -35,6 +35,8 @@ app.use(session({
35
35
 
36
36
  app.use(flash());
37
37
 
38
+
39
+
38
40
  ```
39
41
 
40
42
  With the `flash` middleware in place, all requests will have a `req.flash()` function
package/lib/index.cjs ADDED
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ const { format } = require('util');
4
+
5
+ /**
6
+ * Expose `flash()` function on requests.
7
+ *
8
+ * @param {Object} options
9
+ * @return {Function}
10
+ * @api public
11
+ */
12
+ module.exports = function flash(options = {}) {
13
+ const safe = options.unsafe === undefined ? true : !options.unsafe;
14
+
15
+ return function flashMiddleware(req, res, next) {
16
+ if (req.flash && safe) return next();
17
+ req.flash = _flash;
18
+ next();
19
+ };
20
+ };
21
+
22
+ /**
23
+ * Queue or retrieve flash messages.
24
+ *
25
+ * @param {String} type
26
+ * @param {String|Array} msg
27
+ * @return {Array|Object|Number}
28
+ * @api public
29
+ */
30
+ function _flash(type, msg) {
31
+ if (!this.session) {
32
+ throw new Error('req.flash() requires sessions');
33
+ }
34
+
35
+ const flashStore = (this.session.flash ||= {});
36
+
37
+ // SET
38
+ if (type && msg !== undefined) {
39
+ // Support util.format
40
+ if (arguments.length > 2) {
41
+ msg = format.apply(null, Array.prototype.slice.call(arguments, 1));
42
+ }
43
+
44
+ // Support array of messages
45
+ if (Array.isArray(msg)) {
46
+ msg.forEach(val => {
47
+ (flashStore[type] ||= []).push(val);
48
+ });
49
+ return flashStore[type].length;
50
+ }
51
+
52
+ return (flashStore[type] ||= []).push(msg);
53
+ }
54
+
55
+ // GET by type
56
+ if (type) {
57
+ const messages = flashStore[type] || [];
58
+ delete flashStore[type];
59
+ return messages;
60
+ }
61
+
62
+ // GET all
63
+ this.session.flash = {};
64
+ return flashStore;
65
+ }
package/lib/index.mjs ADDED
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ import { format } from 'util';
4
+
5
+ /**
6
+ * Expose `flash()` function on requests.
7
+ *
8
+ * @param {Object} options
9
+ * @return {Function}
10
+ * @api public
11
+ */
12
+ export default function flash(options = {}) {
13
+ const safe = options.unsafe === undefined ? true : !options.unsafe;
14
+
15
+ return function flashMiddleware(req, res, next) {
16
+ if (req.flash && safe) return next();
17
+ req.flash = _flash;
18
+ next();
19
+ };
20
+ };
21
+
22
+ /**
23
+ * Queue or retrieve flash messages.
24
+ *
25
+ * @param {String} type
26
+ * @param {String|Array} msg
27
+ * @return {Array|Object|Number}
28
+ * @api public
29
+ */
30
+ function _flash(type, msg) {
31
+ if (!this.session) {
32
+ throw new Error('req.flash() requires sessions');
33
+ }
34
+
35
+ const flashStore = (this.session.flash ||= {});
36
+
37
+ // SET
38
+ if (type && msg !== undefined) {
39
+ // Support util.format
40
+ if (arguments.length > 2) {
41
+ msg = format.apply(null, Array.prototype.slice.call(arguments, 1));
42
+ }
43
+
44
+ // Support array of messages
45
+ if (Array.isArray(msg)) {
46
+ msg.forEach(val => {
47
+ (flashStore[type] ||= []).push(val);
48
+ });
49
+ return flashStore[type].length;
50
+ }
51
+
52
+ return (flashStore[type] ||= []).push(msg);
53
+ }
54
+
55
+ // GET by type
56
+ if (type) {
57
+ const messages = flashStore[type] || [];
58
+ delete flashStore[type];
59
+ return messages;
60
+ }
61
+
62
+ // GET all
63
+ this.session.flash = {};
64
+ return flashStore;
65
+ }
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "session-flash",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Flash message middleware for express.",
5
- "main": "./lib/index.js",
5
+ "main": "./lib/index.cjs",
6
+ "module": "./lib/index.mjs",
6
7
  "types": "./lib/index.d.ts",
7
8
  "exports": {
8
- "require": "./lib/index.js"
9
+ "require": "./lib/index.cjs",
10
+ "import": "./lib/index.mjs",
11
+ "types": "./lib/index.d.ts"
9
12
  },
10
13
  "keywords": [
11
14
  "session-flash",
@@ -20,9 +23,7 @@
20
23
  "license": "MIT",
21
24
  "dependencies": {},
22
25
  "devDependencies": {
23
- "@types/node": "^25.0.3",
24
- "typescript": "^5.9.3",
25
- "vows": "0.8.x"
26
+ "vows": "^0.8.3"
26
27
  },
27
28
  "scripts": {
28
29
  "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js",
package/lib/index.js DELETED
@@ -1,66 +0,0 @@
1
- /**
2
- * Simple session flash middleware
3
- * Requires express-session to be set up first
4
- */
5
-
6
- module.exports = function sessionFlash(options) {
7
- options = options || {};
8
- const unsafe = options.unsafe === true;
9
-
10
- return function (req, res, next) {
11
- if (!req.session) {
12
- throw new Error("session-flash requires express-session");
13
- }
14
-
15
- if (!req.session.flash) {
16
- req.session.flash = {};
17
- }
18
-
19
- /**
20
- * flash()
21
- * flash(type)
22
- * flash(type, message)
23
- * flash(type, format, ...args)
24
- */
25
- req.flash = function (type, message) {
26
- // flash() → return all messages
27
- if (arguments.length === 0) {
28
- const flash = req.session.flash;
29
- req.session.flash = {};
30
- return flash;
31
- }
32
-
33
- // flash(type) → return messages for type
34
- if (arguments.length === 1) {
35
- const messages = req.session.flash[type] || [];
36
- delete req.session.flash[type];
37
- return messages;
38
- }
39
-
40
- // flash(type, message | format, ...args)
41
- let msg;
42
-
43
- if (typeof message === "string" && arguments.length > 2) {
44
- // format string with args
45
- const args = Array.prototype.slice.call(arguments, 2);
46
- msg = message.replace(/%s/g, () => args.shift());
47
- } else {
48
- msg = message;
49
- }
50
-
51
- if (!Array.isArray(req.session.flash[type])) {
52
- req.session.flash[type] = [];
53
- }
54
-
55
- if (Array.isArray(msg)) {
56
- req.session.flash[type].push.apply(req.session.flash[type], msg);
57
- return msg.length;
58
- } else {
59
- req.session.flash[type].push(msg);
60
- return 1;
61
- }
62
- };
63
-
64
- next();
65
- };
66
- };