session-flash 1.0.1 → 1.0.3

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/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 ADDED
@@ -0,0 +1,66 @@
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,12 +1,11 @@
1
1
  {
2
2
  "name": "session-flash",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Flash message middleware for express.",
5
- "main": "./lib/index.cjs",
6
- "module": "./lib/index.mjs",
5
+ "main": "./lib/index.js",
6
+ "types": "./lib/index.d.ts",
7
7
  "exports": {
8
- "require": "./lib/index.cjs",
9
- "import": "./lib/index.mjs"
8
+ "require": "./lib/index.js"
10
9
  },
11
10
  "keywords": [
12
11
  "session-flash",
@@ -21,10 +20,13 @@
21
20
  "license": "MIT",
22
21
  "dependencies": {},
23
22
  "devDependencies": {
23
+ "@types/node": "^25.0.3",
24
+ "typescript": "^5.9.3",
24
25
  "vows": "0.8.x"
25
26
  },
26
27
  "scripts": {
27
- "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
28
+ "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js",
29
+ "build": "tsc && echo 'Build complete'"
28
30
  },
29
31
  "engines": {
30
32
  "node": ">= 18"
package/lib/index.cjs DELETED
@@ -1,62 +0,0 @@
1
- 'use strict';
2
-
3
- const { format } = require('node:util');
4
-
5
- function flash(options = {}) {
6
- const safe = options.unsafe === undefined ? true : !options.unsafe;
7
-
8
- return function flashMiddleware(req, res, next) {
9
- // if (typeof req.flash === 'function' && safe) return next();
10
- if (typeof req.flash === 'function' && safe) {
11
- return next();
12
- }
13
-
14
- Object.defineProperty(req, 'flash', {
15
- configurable: true,
16
- enumerable: false,
17
- writable: false,
18
- value: flashHandler.bind(req)
19
- });
20
-
21
- next();
22
- };
23
- };
24
-
25
- function flashHandler(type, msg) {
26
- if (!this.session) {
27
- throw new Error(
28
- 'req.flash() requires session middleware (express-session)'
29
- );
30
- }
31
-
32
- const store = (this.session.flash ??= Object.create(null));
33
-
34
- // ADD message(s)
35
- if (type && arguments.length > 1) {
36
- if (arguments.length > 2) {
37
- msg = format(...Array.prototype.slice.call(arguments, 1));
38
- }
39
-
40
- if (Array.isArray(msg)) {
41
- store[type] = store[type] || [];
42
- store[type].push(...msg);
43
- return store[type].length;
44
- }
45
-
46
- return (store[type] = store[type] || []).push(msg);
47
- }
48
-
49
- // GET messages of a type
50
- if (type) {
51
- const messages = store[type] || [];
52
- delete store[type];
53
- return messages;
54
- }
55
-
56
- // GET all messages
57
- this.session.flash = Object.create(null);
58
- return store;
59
- }
60
-
61
- module.exports = flash;
62
- module.exports.default = flash;
package/lib/index.mjs DELETED
@@ -1,54 +0,0 @@
1
- import { format } from 'node:util';
2
-
3
- export default function flash(options = {}) {
4
- const safe = options.unsafe === undefined ? true : !options.unsafe;
5
-
6
- return function flashMiddleware(req, res, next) {
7
- if (req.flash && safe) return next();
8
-
9
- Object.defineProperty(req, 'flash', {
10
- configurable: true,
11
- enumerable: false,
12
- writable: false,
13
- value: flashHandler.bind(req)
14
- });
15
-
16
- next();
17
- };
18
- };
19
-
20
- function flashHandler(type, msg) {
21
- if (!this.session) {
22
- throw new Error(
23
- 'req.flash() requires session middleware (express-session)'
24
- );
25
- }
26
-
27
- const store = (this.session.flash ??= Object.create(null));
28
-
29
- // ADD message(s)
30
- if (type && arguments.length > 1) {
31
- if (arguments.length > 2) {
32
- msg = format(...Array.prototype.slice.call(arguments, 1));
33
- }
34
-
35
- if (Array.isArray(msg)) {
36
- store[type] = store[type] || [];
37
- store[type].push(...msg);
38
- return store[type].length;
39
- }
40
-
41
- return (store[type] = store[type] || []).push(msg);
42
- }
43
-
44
- // GET messages of a type
45
- if (type) {
46
- const messages = store[type] || [];
47
- delete store[type];
48
- return messages;
49
- }
50
-
51
- // GET all messages
52
- this.session.flash = Object.create(null);
53
- return store;
54
- }