session-flash 1.0.4 → 1.0.6
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 +0 -4
- package/lib/index.d.ts +21 -14
- package/lib/index.mjs +65 -0
- package/package.json +6 -5
- /package/lib/{index.js → index.cjs} +0 -0
package/README.md
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
+
import type { RequestHandler } from "express";
|
|
2
|
+
import "express-serve-static-core";
|
|
1
3
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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: unknown[]): number;
|
|
11
|
+
}
|
|
9
12
|
}
|
|
10
13
|
|
|
14
|
+
/* session-flash module declaration */
|
|
11
15
|
declare module "session-flash" {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
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
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Flash message middleware for express.",
|
|
5
|
-
"main": "./lib/index.
|
|
5
|
+
"main": "./lib/index.cjs",
|
|
6
|
+
"module": "./lib/index.mjs",
|
|
6
7
|
"types": "./lib/index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
|
-
"require": "./lib/index.
|
|
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,8 +23,6 @@
|
|
|
20
23
|
"license": "MIT",
|
|
21
24
|
"dependencies": {},
|
|
22
25
|
"devDependencies": {
|
|
23
|
-
"@types/node": "^25.0.3",
|
|
24
|
-
"typescript": "^5.9.3",
|
|
25
26
|
"vows": "^0.8.3"
|
|
26
27
|
},
|
|
27
28
|
"scripts": {
|
|
File without changes
|