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 CHANGED
@@ -35,10 +35,6 @@ app.use(session({
35
35
 
36
36
  app.use(flash());
37
37
 
38
- //HTML escaping enabled by default
39
- //Disable escaping only if you trust input:
40
-
41
- app.use(flash({ unsafe: true }));
42
38
 
43
39
 
44
40
  ```
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
- 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
- }
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
- import express = require("express");
13
- interface IConnectFlashOptions {
14
- unsafe?: boolean | undefined;
15
- }
16
- function e(options?: IConnectFlashOptions): express.RequestHandler;
17
- export = e;
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.4",
3
+ "version": "1.0.6",
4
4
  "description": "Flash message middleware for express.",
5
- "main": "./lib/index.js",
5
+ "main": "./lib/index.cjs",
6
+ "module": "./lib/index.mjs",
6
7
  "types": "./lib/index.d.ts",
7
8
  "exports": {
8
- "require": "./lib/index.js"
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