session-flash 1.0.0 → 1.0.2
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 +5 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +66 -56
- package/package.json +21 -5
- package/tsconfig.json +25 -0
package/.travis.yml
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
declare namespace Express {
|
|
3
|
+
export interface Request {
|
|
4
|
+
flash(): { [key: string]: string[] };
|
|
5
|
+
flash(message: string): string[];
|
|
6
|
+
flash(type: string, message: string[] | string): number;
|
|
7
|
+
flash(type: string, format: string, ...args: any[]): number;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module "session-flash" {
|
|
12
|
+
import express = require("express");
|
|
13
|
+
interface IConnectFlashOptions {
|
|
14
|
+
unsafe?: boolean | undefined;
|
|
15
|
+
}
|
|
16
|
+
function e(options?: IConnectFlashOptions): express.RequestHandler;
|
|
17
|
+
export = e;
|
|
18
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,56 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,18 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "session-flash",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Flash message middleware for express.",
|
|
5
|
-
"
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"types": "./lib/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"require": "./lib/index.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"session-flash",
|
|
12
|
+
"express",
|
|
13
|
+
"flash",
|
|
14
|
+
"messages"
|
|
15
|
+
],
|
|
6
16
|
"author": {
|
|
7
17
|
"name": "Andrew Khabweri",
|
|
8
18
|
"email": "andrewkhabweri@gmail.com"
|
|
9
19
|
},
|
|
10
20
|
"license": "MIT",
|
|
11
|
-
"
|
|
12
|
-
"
|
|
21
|
+
"dependencies": {},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^25.0.3",
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"vows": "0.8.x"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js",
|
|
29
|
+
"build": "tsc && echo 'Build complete'"
|
|
13
30
|
},
|
|
14
31
|
"engines": {
|
|
15
32
|
"node": ">= 18"
|
|
16
33
|
}
|
|
17
|
-
|
|
18
34
|
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"checkJs": false,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"emitDeclarationOnly": true,
|
|
7
|
+
|
|
8
|
+
"rootDir": ".",
|
|
9
|
+
"outDir": ".",
|
|
10
|
+
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"moduleResolution": "node",
|
|
13
|
+
"target": "ES2020",
|
|
14
|
+
"module": "CommonJS",
|
|
15
|
+
|
|
16
|
+
"types": ["node"],
|
|
17
|
+
"skipLibCheck": true
|
|
18
|
+
},
|
|
19
|
+
"include": [
|
|
20
|
+
"lib/index.js"
|
|
21
|
+
],
|
|
22
|
+
"exclude": [
|
|
23
|
+
"node_modules"
|
|
24
|
+
]
|
|
25
|
+
}
|