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 ADDED
@@ -0,0 +1,5 @@
1
+ language: node_js
2
+ node_js:
3
+ - 18
4
+ - 20
5
+ - 22
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
- 'use strict';
2
-
3
- const { format } = require('node:util');
4
-
5
- module.exports = function flash(options = {}) {
6
- const safe = options.unsafe === undefined ? true : !options.unsafe;
7
-
8
- return function flashMiddleware(req, res, next) {
9
- if (req.flash && safe) return next();
10
-
11
- Object.defineProperty(req, 'flash', {
12
- configurable: true,
13
- enumerable: false,
14
- writable: false,
15
- value: flashHandler.bind(req)
16
- });
17
-
18
- next();
19
- };
20
- };
21
-
22
- function flashHandler(type, msg) {
23
- if (!this.session) {
24
- throw new Error(
25
- 'req.flash() requires session middleware (express-session)'
26
- );
27
- }
28
-
29
- const store = (this.session.flash ??= Object.create(null));
30
-
31
- // ADD message(s)
32
- if (type && arguments.length > 1) {
33
- if (arguments.length > 2) {
34
- msg = format(...Array.prototype.slice.call(arguments, 1));
35
- }
36
-
37
- if (Array.isArray(msg)) {
38
- store[type] = store[type] || [];
39
- store[type].push(...msg);
40
- return store[type].length;
41
- }
42
-
43
- return (store[type] = store[type] || []).push(msg);
44
- }
45
-
46
- // GET messages of a type
47
- if (type) {
48
- const messages = store[type] || [];
49
- delete store[type];
50
- return messages;
51
- }
52
-
53
- // GET all messages
54
- this.session.flash = Object.create(null);
55
- return store;
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.0",
3
+ "version": "1.0.2",
4
4
  "description": "Flash message middleware for express.",
5
- "keywords": ["session-flash", "express", "flash", "messages"],
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
- "main": "./lib/index.js",
12
- "dependencies": {
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
+ }