@prairielearn/flash 2.0.6 → 2.0.7
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/CHANGELOG.md +8 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import { HtmlSafeString } from '@prairielearn/html';
|
|
2
|
+
import { type HtmlSafeString } from '@prairielearn/html';
|
|
3
3
|
export type FlashMessageType = 'notice' | 'success' | 'warning' | 'error';
|
|
4
4
|
export interface FlashMessage {
|
|
5
5
|
type: FlashMessageType;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAIrD,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAIrD,OAAO,EAAuB,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE/D,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,CAAC;AAID,MAAM,UAAU,KAAK,CACnB,IAA4C,EAC5C,OAAiC;IAEjC,MAAM,YAAY,GA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AC,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';\n\nimport type { Request, Response, NextFunction } from 'express';\n\nimport { type 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 const session = (req as any).session;\n\n if (!session) {\n throw new Error('@prairielearn/flash requires session support');\n }\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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/flash",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -14,17 +14,17 @@
|
|
|
14
14
|
"test": "c8 mocha src/**/*.test.ts"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@prairielearn/html": "^4.0.
|
|
17
|
+
"@prairielearn/html": "^4.0.6",
|
|
18
18
|
"@types/express": "^4.17.21"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
22
|
-
"@types/node": "^20.
|
|
22
|
+
"@types/node": "^20.17.5",
|
|
23
23
|
"c8": "^10.1.2",
|
|
24
|
-
"chai": "^5.1.
|
|
25
|
-
"mocha": "^10.
|
|
26
|
-
"tsx": "^4.19.
|
|
27
|
-
"typescript": "^5.
|
|
24
|
+
"chai": "^5.1.2",
|
|
25
|
+
"mocha": "^10.8.2",
|
|
26
|
+
"tsx": "^4.19.2",
|
|
27
|
+
"typescript": "^5.6.3"
|
|
28
28
|
},
|
|
29
29
|
"c8": {
|
|
30
30
|
"reporter": [
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { AsyncLocalStorage } from 'node:async_hooks';
|
|
|
2
2
|
|
|
3
3
|
import type { Request, Response, NextFunction } from 'express';
|
|
4
4
|
|
|
5
|
-
import { HtmlSafeString, html } from '@prairielearn/html';
|
|
5
|
+
import { type HtmlSafeString, html } from '@prairielearn/html';
|
|
6
6
|
|
|
7
7
|
const als = new AsyncLocalStorage<FlashStorage>();
|
|
8
8
|
|