@prairielearn/flash 1.1.8 → 2.0.0
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/.mocharc.cjs +3 -0
- package/CHANGELOG.md +19 -0
- package/dist/index.js +6 -11
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +46 -48
- package/dist/index.test.js.map +1 -1
- package/package.json +6 -5
- package/src/index.test.ts +1 -1
package/.mocharc.cjs
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @prairielearn/flash
|
|
2
2
|
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 4f30b7e: Publish as native ESM
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [4f30b7e]
|
|
12
|
+
- @prairielearn/html@4.0.0
|
|
13
|
+
|
|
14
|
+
## 1.1.9
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- c7e6553: Upgrade all JavaScript dependencies
|
|
19
|
+
- Updated dependencies [c7e6553]
|
|
20
|
+
- @prairielearn/html@3.1.7
|
|
21
|
+
|
|
3
22
|
## 1.1.8
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const html_1 = require("@prairielearn/html");
|
|
6
|
-
const als = new node_async_hooks_1.AsyncLocalStorage();
|
|
7
|
-
function flashMiddleware() {
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { html } from '@prairielearn/html';
|
|
3
|
+
const als = new AsyncLocalStorage();
|
|
4
|
+
export function flashMiddleware() {
|
|
8
5
|
return (req, _res, next) => {
|
|
9
6
|
const flashStorage = makeFlashStorage(req);
|
|
10
7
|
als.run(flashStorage, () => next());
|
|
11
8
|
};
|
|
12
9
|
}
|
|
13
|
-
|
|
14
|
-
function flash(type, message) {
|
|
10
|
+
export function flash(type, message) {
|
|
15
11
|
const flashStorage = als.getStore();
|
|
16
12
|
if (!flashStorage) {
|
|
17
13
|
throw new Error('flash() must be called within a request');
|
|
@@ -34,7 +30,6 @@ function flash(type, message) {
|
|
|
34
30
|
flashStorage.clearAll();
|
|
35
31
|
return messages;
|
|
36
32
|
}
|
|
37
|
-
exports.flash = flash;
|
|
38
33
|
function makeFlashStorage(req) {
|
|
39
34
|
if (!req.session) {
|
|
40
35
|
throw new Error('@prairielearn/flash requires session support');
|
|
@@ -43,7 +38,7 @@ function makeFlashStorage(req) {
|
|
|
43
38
|
return {
|
|
44
39
|
add(type, message) {
|
|
45
40
|
session.flash ??= [];
|
|
46
|
-
session.flash.push({ type, message:
|
|
41
|
+
session.flash.push({ type, message: html `${message}`.toString() });
|
|
47
42
|
},
|
|
48
43
|
get(type) {
|
|
49
44
|
const messages = session.flash ?? [];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAkB,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1D,MAAM,GAAG,GAAG,IAAI,iBAAiB,EAAgB,CAAC;AASlD,MAAM,UAAU,eAAe;IAC7B,OAAO,CAAC,GAAY,EAAE,IAAc,EAAE,IAAkB,EAAE,EAAE;QAC1D,MAAM,YAAY,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;QAC3C,GAAG,CAAC,GAAG,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC,CAAC;AACJ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AAC,IAAsB,EAAE,OAAgC;YAC1D,OAAO,CAAC,KAAK,KAAK,EAAE,CAAC;YACrB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAA,GAAG,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QACrE,CAAC;QACD,GAAG,CAAC,IAAsB;YACxB,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;YACrC,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAqB,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;QAC3E,CAAC;QACD,MAAM;YACJ,OAAO,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;QAC7B,CAAC;QACD,KAAK,CAAC,IAAsB;YAC1B,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,OAAqB,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC;QAChG,CAAC;QACD,QAAQ;YACN,OAAO,CAAC,KAAK,GAAG,EAAE,CAAC;QACrB,CAAC;KACqB,CAAC;AAC3B,CAAC","sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Request, Response, NextFunction } from 'express';\nimport { HtmlSafeString, html } from '@prairielearn/html';\n\nconst als = new AsyncLocalStorage<FlashStorage>();\n\nexport type FlashMessageType = 'notice' | 'success' | 'warning' | 'error';\n\nexport interface FlashMessage {\n type: FlashMessageType;\n message: string;\n}\n\nexport function flashMiddleware() {\n return (req: Request, _res: Response, next: NextFunction) => {\n const flashStorage = makeFlashStorage(req);\n als.run(flashStorage, () => next());\n };\n}\n\nexport function flash(type?: FlashMessageType | FlashMessageType[]): FlashMessage[];\nexport function flash(type: FlashMessageType, message: string | HtmlSafeString): void;\nexport function flash(\n type?: FlashMessageType | FlashMessageType[],\n message?: string | HtmlSafeString,\n) {\n const flashStorage = als.getStore();\n if (!flashStorage) {\n throw new Error('flash() must be called within a request');\n }\n\n if (Array.isArray(type)) {\n const messages = type.flatMap((type) => flashStorage.get(type));\n type.forEach((t) => flashStorage.clear(t));\n return messages;\n }\n\n if (type != null && message != null) {\n flashStorage.add(type, message);\n return;\n }\n\n if (type != null) {\n const message = flashStorage.get(type);\n flashStorage.clear(type);\n return message;\n }\n\n const messages = flashStorage.getAll();\n flashStorage.clearAll();\n return messages;\n}\n\ninterface FlashStorage {\n add(type: FlashMessageType, message: string | HtmlSafeString): void;\n get(type: FlashMessageType): FlashMessage[] | null;\n getAll(): FlashMessage[];\n clear(type: FlashMessageType): void;\n clearAll(): void;\n}\n\nfunction makeFlashStorage(req: Request): FlashStorage {\n if (!req.session) {\n throw new Error('@prairielearn/flash requires session support');\n }\n\n const session = req.session as any;\n\n return {\n add(type: FlashMessageType, message: string | HtmlSafeString) {\n session.flash ??= [];\n session.flash.push({ type, message: html`${message}`.toString() });\n },\n get(type: FlashMessageType) {\n const messages = session.flash ?? [];\n return messages.filter((message: FlashMessage) => message.type === type);\n },\n getAll() {\n return session.flash ?? [];\n },\n clear(type: FlashMessageType) {\n session.flash = session.flash?.filter((message: FlashMessage) => message.type !== type) ?? [];\n },\n clearAll() {\n session.flash = [];\n },\n } satisfies FlashStorage;\n}\n"]}
|
package/dist/index.test.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const index_1 = require("./index");
|
|
5
|
-
const html_1 = require("@prairielearn/html");
|
|
1
|
+
import { assert } from 'chai';
|
|
2
|
+
import { flashMiddleware, flash } from './index.js';
|
|
3
|
+
import { html } from '@prairielearn/html';
|
|
6
4
|
describe('flash', () => {
|
|
7
5
|
it('throws an error if no session present', () => {
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
assert.throw(() => {
|
|
7
|
+
flashMiddleware()({}, {}, () => { });
|
|
10
8
|
});
|
|
11
9
|
});
|
|
12
10
|
it('throws an error when middleware is not used', () => {
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
assert.throw(() => {
|
|
12
|
+
flash('notice', 'Hello world');
|
|
15
13
|
});
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
assert.throw(() => {
|
|
15
|
+
flash('notice', html `<p>hello ${'&'} world</p>`);
|
|
18
16
|
});
|
|
19
17
|
});
|
|
20
18
|
it('adds a flash using string message', () => {
|
|
@@ -22,9 +20,9 @@ describe('flash', () => {
|
|
|
22
20
|
session: {},
|
|
23
21
|
};
|
|
24
22
|
const res = {};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
flashMiddleware()(req, res, () => {
|
|
24
|
+
flash('notice', 'hello world');
|
|
25
|
+
assert.sameDeepMembers(flash(), [{ type: 'notice', message: 'hello world' }]);
|
|
28
26
|
});
|
|
29
27
|
});
|
|
30
28
|
it('adds a flash and escapes unsafe HTML', () => {
|
|
@@ -32,9 +30,9 @@ describe('flash', () => {
|
|
|
32
30
|
session: {},
|
|
33
31
|
};
|
|
34
32
|
const res = {};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
flashMiddleware()(req, res, () => {
|
|
34
|
+
flash('notice', '<b>hello world</b>');
|
|
35
|
+
assert.sameDeepMembers(flash(), [
|
|
38
36
|
{ type: 'notice', message: '<b>hello world</b>' },
|
|
39
37
|
]);
|
|
40
38
|
});
|
|
@@ -44,9 +42,9 @@ describe('flash', () => {
|
|
|
44
42
|
session: {},
|
|
45
43
|
};
|
|
46
44
|
const res = {};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
flashMiddleware()(req, res, () => {
|
|
46
|
+
flash('notice', html `<p>hello ${'&'} world</p>`);
|
|
47
|
+
assert.sameDeepMembers(flash(), [{ type: 'notice', message: '<p>hello & world</p>' }]);
|
|
50
48
|
});
|
|
51
49
|
});
|
|
52
50
|
it('stores multiples flashes with the same type', () => {
|
|
@@ -54,11 +52,11 @@ describe('flash', () => {
|
|
|
54
52
|
session: {},
|
|
55
53
|
};
|
|
56
54
|
const res = {};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
flashMiddleware()(req, res, () => {
|
|
56
|
+
flash('notice', 'hello world');
|
|
57
|
+
flash('notice', '<< goodbye world');
|
|
58
|
+
flash('notice', html `<p>hello ${'&'} world</p>`);
|
|
59
|
+
assert.sameDeepMembers(flash(), [
|
|
62
60
|
{ type: 'notice', message: 'hello world' },
|
|
63
61
|
{ type: 'notice', message: '<< goodbye world' },
|
|
64
62
|
{ type: 'notice', message: '<p>hello & world</p>' },
|
|
@@ -70,15 +68,15 @@ describe('flash', () => {
|
|
|
70
68
|
session: {},
|
|
71
69
|
};
|
|
72
70
|
const res = {};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
flashMiddleware()(req, res, () => {
|
|
72
|
+
flash('notice', 'hello world');
|
|
73
|
+
flash('error', '<< goodbye world');
|
|
74
|
+
flash('success', html `<p>hello ${'&'} world</p>`);
|
|
75
|
+
assert.sameDeepMembers(flash('notice'), [{ type: 'notice', message: 'hello world' }]);
|
|
76
|
+
assert.sameDeepMembers(flash('error'), [
|
|
79
77
|
{ type: 'error', message: '<< goodbye world' },
|
|
80
78
|
]);
|
|
81
|
-
|
|
79
|
+
assert.sameDeepMembers(flash('success'), [
|
|
82
80
|
{ type: 'success', message: '<p>hello & world</p>' },
|
|
83
81
|
]);
|
|
84
82
|
});
|
|
@@ -88,11 +86,11 @@ describe('flash', () => {
|
|
|
88
86
|
session: {},
|
|
89
87
|
};
|
|
90
88
|
const res = {};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
89
|
+
flashMiddleware()(req, res, () => {
|
|
90
|
+
flash('notice', 'hello world');
|
|
91
|
+
flash('error', '<< goodbye world');
|
|
92
|
+
flash('success', html `<p>hello ${'&'} world</p>`);
|
|
93
|
+
assert.sameDeepMembers(flash(), [
|
|
96
94
|
{ type: 'notice', message: 'hello world' },
|
|
97
95
|
{ type: 'error', message: '<< goodbye world' },
|
|
98
96
|
{ type: 'success', message: '<p>hello & world</p>' },
|
|
@@ -104,21 +102,21 @@ describe('flash', () => {
|
|
|
104
102
|
session: {},
|
|
105
103
|
};
|
|
106
104
|
const res = {};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
105
|
+
flashMiddleware()(req, res, () => {
|
|
106
|
+
flash('notice', 'hello world');
|
|
107
|
+
flash('error', '<< goodbye world');
|
|
108
|
+
flash('success', html `<p>hello ${'&'} world</p>`);
|
|
109
|
+
assert.sameDeepMembers(flash('notice'), [{ type: 'notice', message: 'hello world' }]);
|
|
110
|
+
assert.sameDeepMembers(flash('error'), [
|
|
113
111
|
{ type: 'error', message: '<< goodbye world' },
|
|
114
112
|
]);
|
|
115
|
-
|
|
113
|
+
assert.sameDeepMembers(flash('success'), [
|
|
116
114
|
{ type: 'success', message: '<p>hello & world</p>' },
|
|
117
115
|
]);
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
116
|
+
assert.deepEqual(flash('notice'), []);
|
|
117
|
+
assert.deepEqual(flash('error'), []);
|
|
118
|
+
assert.deepEqual(flash('success'), []);
|
|
119
|
+
assert.isEmpty(flash());
|
|
122
120
|
});
|
|
123
121
|
});
|
|
124
122
|
});
|
package/dist/index.test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,OAAO,EAAE,eAAe,EA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AAS,EAAE,OAAO,EAAE,0BAA0B,EAAE;aACzD,CAAC,CAAC;YAEH,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,CAAC;YACtC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;YACrC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,EAAE,CAAC,CAAC;YACvC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { assert } from 'chai';\n\nimport { flashMiddleware, flash } from './index.js';\nimport { html } from '@prairielearn/html';\n\ndescribe('flash', () => {\n it('throws an error if no session present', () => {\n assert.throw(() => {\n flashMiddleware()({} as any, {} as any, () => {});\n });\n });\n\n it('throws an error when middleware is not used', () => {\n assert.throw(() => {\n flash('notice', 'Hello world');\n });\n assert.throw(() => {\n flash('notice', html`<p>hello ${'&'} world</p>`);\n });\n });\n\n it('adds a flash using string message', () => {\n const req = {\n session: {},\n } as any;\n const res = {} as any;\n\n flashMiddleware()(req, res, () => {\n flash('notice', 'hello world');\n\n assert.sameDeepMembers(flash(), [{ type: 'notice', message: 'hello world' }]);\n });\n });\n\n it('adds a flash and escapes unsafe HTML', () => {\n const req = {\n session: {},\n } as any;\n const res = {} as any;\n\n flashMiddleware()(req, res, () => {\n flash('notice', '<b>hello world</b>');\n\n assert.sameDeepMembers(flash(), [\n { type: 'notice', message: '<b>hello world</b>' },\n ]);\n });\n });\n\n it('adds a flash with HTML-safe message', () => {\n const req = {\n session: {},\n } as any;\n const res = {} as any;\n\n flashMiddleware()(req, res, () => {\n flash('notice', html`<p>hello ${'&'} world</p>`);\n\n assert.sameDeepMembers(flash(), [{ type: 'notice', message: '<p>hello & world</p>' }]);\n });\n });\n\n it('stores multiples flashes with the same type', () => {\n const req = {\n session: {},\n } as any;\n const res = {} as any;\n\n flashMiddleware()(req, res, () => {\n flash('notice', 'hello world');\n flash('notice', '<< goodbye world');\n flash('notice', html`<p>hello ${'&'} world</p>`);\n\n assert.sameDeepMembers(flash(), [\n { type: 'notice', message: 'hello world' },\n { type: 'notice', message: '<< goodbye world' },\n { type: 'notice', message: '<p>hello & world</p>' },\n ]);\n });\n });\n\n it('returns flash message for a given type', () => {\n const req = {\n session: {},\n } as any;\n const res = {} as any;\n\n flashMiddleware()(req, res, () => {\n flash('notice', 'hello world');\n flash('error', '<< goodbye world');\n flash('success', html`<p>hello ${'&'} world</p>`);\n\n assert.sameDeepMembers(flash('notice'), [{ type: 'notice', message: 'hello world' }]);\n assert.sameDeepMembers(flash('error'), [\n { type: 'error', message: '<< goodbye world' },\n ]);\n assert.sameDeepMembers(flash('success'), [\n { type: 'success', message: '<p>hello & world</p>' },\n ]);\n });\n });\n\n it('returns all flashes', () => {\n const req = {\n session: {},\n } as any;\n const res = {} as any;\n\n flashMiddleware()(req, res, () => {\n flash('notice', 'hello world');\n flash('error', '<< goodbye world');\n flash('success', html`<p>hello ${'&'} world</p>`);\n\n assert.sameDeepMembers(flash(), [\n { type: 'notice', message: 'hello world' },\n { type: 'error', message: '<< goodbye world' },\n { type: 'success', message: '<p>hello & world</p>' },\n ]);\n });\n });\n\n it('clears flash after it has been read', () => {\n const req = {\n session: {},\n } as any;\n const res = {} as any;\n\n flashMiddleware()(req, res, () => {\n flash('notice', 'hello world');\n flash('error', '<< goodbye world');\n flash('success', html`<p>hello ${'&'} world</p>`);\n\n assert.sameDeepMembers(flash('notice'), [{ type: 'notice', message: 'hello world' }]);\n assert.sameDeepMembers(flash('error'), [\n { type: 'error', message: '<< goodbye world' },\n ]);\n assert.sameDeepMembers(flash('success'), [\n { type: 'success', message: '<p>hello & world</p>' },\n ]);\n\n assert.deepEqual(flash('notice'), []);\n assert.deepEqual(flash('error'), []);\n assert.deepEqual(flash('success'), []);\n assert.isEmpty(flash());\n });\n });\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/flash",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "dist/index.js",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
@@ -10,19 +11,19 @@
|
|
|
10
11
|
"scripts": {
|
|
11
12
|
"build": "tsc",
|
|
12
13
|
"dev": "tsc --watch --preserveWatchOutput",
|
|
13
|
-
"test": "mocha
|
|
14
|
+
"test": "mocha src/**/*.test.ts"
|
|
14
15
|
},
|
|
15
16
|
"dependencies": {
|
|
16
|
-
"@prairielearn/html": "^
|
|
17
|
+
"@prairielearn/html": "^4.0.0",
|
|
17
18
|
"@types/express": "^4.17.21"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
21
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
21
22
|
"@types/express-session": "^1.18.0",
|
|
22
|
-
"@types/node": "^20.
|
|
23
|
+
"@types/node": "^20.12.2",
|
|
23
24
|
"chai": "^4.4.1",
|
|
24
25
|
"mocha": "^10.4.0",
|
|
25
|
-
"tsx": "^4.
|
|
26
|
+
"tsx": "^4.9.3",
|
|
26
27
|
"typescript": "^5.4.3"
|
|
27
28
|
}
|
|
28
29
|
}
|