session-flash 1.0.10 → 1.0.12

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 ADDED
@@ -0,0 +1,5 @@
1
+ language: node_js
2
+ node_js:
3
+ - 18
4
+ - 20
5
+ - 22
package/lib/flash.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Module dependencies.
3
3
  */
4
- const { format } = require('util');
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
- const safe = options.unsafe === undefined ? true : !options.unsafe;
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
- * Uses `util.format()`.
50
+ * Formatting uses `util.format()`, which is available on Node 0.6+.
49
51
  *
50
52
  * @param {String} type
51
- * @param {String|Array} msg
53
+ * @param {String} msg
52
54
  * @return {Array|Object|Number}
53
55
  * @api public
54
56
  */
55
57
  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));
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
- const arr = msgs[type] || [];
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
- import * as express from 'express';
1
+ /// <reference types="express" />
2
2
 
3
- /**
4
- * Options for flash middleware
5
- */
6
- export interface FlashOptions {
7
- /**
8
- * If true, middleware will not override existing req.flash
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
- interface Session {
34
- flash?: Record<string, string[]>;
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.10",
4
- "description": "Flash message middleware for Session.",
3
+ "version": "1.0.12",
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
- "name": "Andrew Khabweri",
13
- "email": "andrewkhabweri@gmail.com"
14
- },
11
+ "author": "Andrew Khabweri <andrewkhabweri@gmail.com>",
12
+ "license": "MIT",
15
13
  "type": "commonjs",
16
- "licenses": [
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
- "vows": "0.8.x"
27
+ "mocha": "^10.2.0",
28
+ "supertest": "^6.3.3",
29
+ "chai": "^4.3.7"
38
30
  },
39
31
  "scripts": {
40
- "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
32
+ "test": "mocha 'test/**/*.test.js'"
41
33
  },
42
34
  "engines": {
43
- "node": ">=18.0.0"
35
+ "node": ">=18"
44
36
  }
45
37
  }