session-flash 1.0.7 → 1.0.8

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 CHANGED
@@ -1,5 +1,6 @@
1
- language: node_js
2
- node_js:
3
- - 18
4
- - 20
5
- - 22
1
+ language: "node_js"
2
+ node_js:
3
+ - 0.4
4
+ - 0.6
5
+ - 0.8
6
+
package/lib/flash.js ADDED
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Module dependencies.
3
+ */
4
+ const { format } = require('util');
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
+ const 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 support formatting:
45
+ *
46
+ * req.flash('info', 'email has been sent to %s.', userName);
47
+ *
48
+ * Uses `util.format()`.
49
+ *
50
+ * @param {String} type
51
+ * @param {String|Array} msg
52
+ * @return {Array|Object|Number}
53
+ * @api public
54
+ */
55
+ function _flash(type, msg) {
56
+ if (!this.session) throw new Error('req.flash() requires sessions');
57
+
58
+ const msgs = this.session.flash = this.session.flash || {};
59
+
60
+ if (type && msg !== undefined) {
61
+ // Format message if multiple arguments
62
+ if (arguments.length > 2) {
63
+ const args = Array.from(arguments).slice(1);
64
+ msg = format(...args);
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));
71
+ return msgs[type].length;
72
+ }
73
+
74
+ // Handle single message
75
+ msgs[type] = msgs[type] || [];
76
+ return msgs[type].push(msg);
77
+
78
+ } else if (type) {
79
+ const arr = msgs[type] || [];
80
+ delete msgs[type];
81
+ return arr;
82
+
83
+ } else {
84
+ this.session.flash = {};
85
+ return msgs;
86
+ }
87
+ }
package/lib/index.js ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Expose middleware.
3
+ */
4
+ exports = module.exports = require('./flash');
package/package.json CHANGED
@@ -1,35 +1,23 @@
1
1
  {
2
2
  "name": "session-flash",
3
- "version": "1.0.7",
4
- "description": "Flash message middleware for express.",
5
- "main": "./lib/index.cjs",
6
- "module": "./lib/index.mjs",
7
- "types": "./lib/index.d.ts",
8
- "exports": {
9
- "require": "./lib/index.cjs",
10
- "import": "./lib/index.mjs",
11
- "types": "./lib/index.d.ts"
12
- },
13
- "keywords": [
14
- "session-flash",
15
- "express",
16
- "flash",
17
- "messages"
18
- ],
3
+ "version": "1.0.8",
4
+ "description": "Flash message middleware for Session.",
5
+ "keywords": ["session", "express", "flash", "messages"],
19
6
  "author": {
20
7
  "name": "Andrew Khabweri",
21
8
  "email": "andrewkhabweri@gmail.com"
22
9
  },
23
- "license": "MIT",
24
- "dependencies": {},
10
+ "licenses": [ {
11
+ "type": "MIT"
12
+ } ],
13
+ "main": "./lib/index.js",
14
+ "dependencies": {
15
+ },
25
16
  "devDependencies": {
26
- "vows": "^0.8.3"
17
+ "vows": "0.8.x"
27
18
  },
28
19
  "scripts": {
29
- "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js",
30
- "build": "tsc && echo 'Build complete'"
20
+ "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
31
21
  },
32
- "engines": {
33
- "node": ">= 18"
34
- }
22
+ "engines": { "node": ">= 0.8.0" }
35
23
  }
package/lib/index.cjs DELETED
@@ -1,65 +0,0 @@
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.d.ts DELETED
@@ -1,25 +0,0 @@
1
- import type { RequestHandler } from "express";
2
- import "express-serve-static-core";
3
-
4
- /* Express Request augmentation */
5
- declare module "express-serve-static-core" {
6
- interface Request {
7
- flash(): { [key: string]: string[] };
8
- flash(type: string): string[];
9
- flash(type: string, message: string | string[]): number;
10
- flash(type: string, format: string, ...args: any[]): number;
11
- }
12
- }
13
-
14
- /* session-flash module declaration */
15
- declare module "session-flash" {
16
- interface SessionFlashOptions {
17
- unsafe?: boolean;
18
- }
19
-
20
- function sessionFlash(
21
- options?: SessionFlashOptions
22
- ): RequestHandler;
23
-
24
- export = sessionFlash;
25
- }
package/lib/index.mjs DELETED
@@ -1,65 +0,0 @@
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
- }