@dwp/govuk-casa 9.3.3 → 9.3.4
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/dist/assets/css/casa.css +1 -1
- package/dist/assets/css/casa.css.map +1 -1
- package/dist/lib/CasaTemplateLoader.js +22 -28
- package/dist/lib/CasaTemplateLoader.js.map +1 -1
- package/dist/lib/JourneyContext.js +86 -112
- package/dist/lib/JourneyContext.js.map +1 -1
- package/dist/lib/MutableRouter.js +71 -80
- package/dist/lib/MutableRouter.js.map +1 -1
- package/dist/lib/Plan.js +15 -26
- package/dist/lib/Plan.js.map +1 -1
- package/dist/lib/ValidationError.js +3 -3
- package/dist/lib/ValidationError.js.map +1 -1
- package/dist/lib/configuration-ingestor.js +21 -10
- package/dist/lib/configuration-ingestor.js.map +1 -1
- package/dist/lib/configure.js +18 -17
- package/dist/lib/configure.js.map +1 -1
- package/dist/lib/end-session.js +5 -5
- package/dist/lib/end-session.js.map +1 -1
- package/dist/lib/field.js +56 -65
- package/dist/lib/field.js.map +1 -1
- package/dist/lib/nunjucks-filters.js +6 -6
- package/dist/lib/nunjucks-filters.js.map +1 -1
- package/dist/lib/nunjucks.js +3 -3
- package/dist/lib/nunjucks.js.map +1 -1
- package/dist/lib/utils.js +6 -1
- package/dist/lib/utils.js.map +1 -1
- package/dist/lib/validators/dateObject.js +23 -14
- package/dist/lib/validators/dateObject.js.map +1 -1
- package/dist/lib/validators/email.js +2 -5
- package/dist/lib/validators/email.js.map +1 -1
- package/dist/lib/validators/inArray.js +2 -5
- package/dist/lib/validators/inArray.js.map +1 -1
- package/dist/lib/validators/nino.js +1 -4
- package/dist/lib/validators/nino.js.map +1 -1
- package/dist/lib/validators/postalAddressObject.js +26 -19
- package/dist/lib/validators/postalAddressObject.js.map +1 -1
- package/dist/lib/validators/range.js +2 -6
- package/dist/lib/validators/range.js.map +1 -1
- package/dist/lib/validators/regex.js +1 -4
- package/dist/lib/validators/regex.js.map +1 -1
- package/dist/lib/validators/required.js +1 -4
- package/dist/lib/validators/required.js.map +1 -1
- package/dist/lib/validators/strlen.js +1 -4
- package/dist/lib/validators/strlen.js.map +1 -1
- package/dist/lib/validators/wordCount.js +1 -4
- package/dist/lib/validators/wordCount.js.map +1 -1
- package/dist/middleware/csrf.js +4 -2
- package/dist/middleware/csrf.js.map +1 -1
- package/dist/middleware/data.js +17 -10
- package/dist/middleware/data.js.map +1 -1
- package/dist/middleware/i18n.d.ts +1 -1
- package/dist/middleware/i18n.js +16 -16
- package/dist/middleware/i18n.js.map +1 -1
- package/dist/middleware/post.js +9 -7
- package/dist/middleware/post.js.map +1 -1
- package/dist/middleware/pre.js +2 -2
- package/dist/middleware/pre.js.map +1 -1
- package/dist/middleware/sanitise-fields.d.ts +1 -1
- package/dist/middleware/sanitise-fields.js +1 -2
- package/dist/middleware/sanitise-fields.js.map +1 -1
- package/dist/middleware/session.js +11 -6
- package/dist/middleware/session.js.map +1 -1
- package/dist/middleware/strip-proxy-path.js +1 -2
- package/dist/middleware/strip-proxy-path.js.map +1 -1
- package/dist/middleware/validate-fields.d.ts +1 -1
- package/dist/middleware/validate-fields.js +1 -2
- package/dist/middleware/validate-fields.js.map +1 -1
- package/dist/routes/journey.js +15 -11
- package/dist/routes/journey.js.map +1 -1
- package/dist/routes/static.js +8 -9
- package/dist/routes/static.js.map +1 -1
- package/package.json +23 -22
- package/src/lib/JourneyContext.js +22 -19
- package/src/lib/MutableRouter.js +2 -2
- package/src/lib/Plan.js +2 -8
- package/src/lib/ValidationError.js +2 -2
- package/src/lib/configuration-ingestor.js +21 -12
- package/src/lib/configure.js +14 -11
- package/src/lib/end-session.js +5 -5
- package/src/lib/nunjucks-filters.js +6 -6
- package/src/lib/nunjucks.js +2 -2
- package/src/lib/validators/dateObject.js +5 -5
- package/src/lib/validators/postalAddressObject.js +8 -10
- package/src/middleware/data.js +4 -7
- package/src/middleware/i18n.js +9 -9
- package/src/middleware/pre.js +1 -1
- package/src/middleware/sanitise-fields.js +1 -2
- package/src/routes/journey.js +2 -2
- package/src/routes/static.js +4 -4
|
@@ -1,16 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
-
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var _CasaTemplateLoader_instances, _CasaTemplateLoader_blockModifiers, _CasaTemplateLoader_applyBlockModifiers;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
const nunjucks_1 = require("nunjucks");
|
|
16
4
|
/**
|
|
@@ -46,6 +34,7 @@ const VALID_BLOCKS = [
|
|
|
46
34
|
* @access private
|
|
47
35
|
*/
|
|
48
36
|
class CasaTemplateLoader extends nunjucks_1.FileSystemLoader {
|
|
37
|
+
#blockModifiers;
|
|
49
38
|
/**
|
|
50
39
|
* Constructor.
|
|
51
40
|
*
|
|
@@ -54,9 +43,7 @@ class CasaTemplateLoader extends nunjucks_1.FileSystemLoader {
|
|
|
54
43
|
*/
|
|
55
44
|
constructor(searchPaths, opts) {
|
|
56
45
|
super(searchPaths, opts);
|
|
57
|
-
|
|
58
|
-
_CasaTemplateLoader_blockModifiers.set(this, void 0);
|
|
59
|
-
__classPrivateFieldSet(this, _CasaTemplateLoader_blockModifiers, [], "f");
|
|
46
|
+
this.#blockModifiers = [];
|
|
60
47
|
}
|
|
61
48
|
/**
|
|
62
49
|
* Extract the source from the given template file.
|
|
@@ -66,7 +53,7 @@ class CasaTemplateLoader extends nunjucks_1.FileSystemLoader {
|
|
|
66
53
|
*/
|
|
67
54
|
getSource(name) {
|
|
68
55
|
const source = super.getSource(name);
|
|
69
|
-
return source ?
|
|
56
|
+
return source ? this.#applyBlockModifiers(name, source) : source;
|
|
70
57
|
}
|
|
71
58
|
/**
|
|
72
59
|
* Add a modification function to the loader.
|
|
@@ -81,23 +68,30 @@ class CasaTemplateLoader extends nunjucks_1.FileSystemLoader {
|
|
|
81
68
|
if (!VALID_BLOCKS.includes(block)) {
|
|
82
69
|
throw new Error(`Block "${String(block)}" is not a recognised template block.`);
|
|
83
70
|
}
|
|
84
|
-
|
|
71
|
+
this.#blockModifiers.push({
|
|
85
72
|
block,
|
|
86
73
|
modifier,
|
|
87
74
|
});
|
|
88
75
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Apply a block modifier to the given source content.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} name Block name
|
|
80
|
+
* @param {string} source Original source pulled from template file
|
|
81
|
+
* @returns {string} The modified source
|
|
82
|
+
*/
|
|
83
|
+
#applyBlockModifiers(name, source) {
|
|
84
|
+
for (let i = 0, l = this.#blockModifiers.length; i < l; i++) {
|
|
85
|
+
// ESLint disabled as `i` is an integer
|
|
86
|
+
/* eslint-disable-next-line security/detect-object-injection */
|
|
87
|
+
const { block, modifier } = this.#blockModifiers[i];
|
|
88
|
+
if (source.src.indexOf(`block ${block}`) > -1) {
|
|
89
|
+
/* eslint-disable-next-line no-param-reassign */
|
|
90
|
+
source.src = source.src.replace(`block ${block} %}`, `block ${block} %}${modifier(name)}`);
|
|
91
|
+
}
|
|
98
92
|
}
|
|
93
|
+
return source;
|
|
99
94
|
}
|
|
100
|
-
|
|
101
|
-
};
|
|
95
|
+
}
|
|
102
96
|
exports.default = CasaTemplateLoader;
|
|
103
97
|
//# sourceMappingURL=CasaTemplateLoader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CasaTemplateLoader.js","sourceRoot":"","sources":["../../src/lib/CasaTemplateLoader.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"CasaTemplateLoader.js","sourceRoot":"","sources":["../../src/lib/CasaTemplateLoader.js"],"names":[],"mappings":";;AAAA,uCAA4C;AAE5C;;;GAGG;AAEH;;;GAGG;AAEH,MAAM,YAAY,GAAG;IACnB,eAAe;IACf,SAAS;IACT,WAAW;IACX,eAAe;IACf,SAAS;IACT,QAAQ;IACR,MAAM;IACN,QAAQ;IACR,WAAW;IACX,cAAc;IACd,MAAM;IACN,WAAW;IACX,UAAU;CACX,CAAC;AAEF;;;;GAIG;AAEH;;;GAGG;AACH,MAAqB,kBAAmB,SAAQ,2BAAgB;IAC9D,eAAe,CAAC;IAEhB;;;;;OAKG;IACH,YAAY,WAAW,EAAE,IAAI;QAC3B,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QAEzB,IAAI,CAAC,eAAe,GAAG,EAAE,CAAC;IAC5B,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,IAAI;QACZ,MAAM,MAAM,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QACrC,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IACnE,CAAC;IAED;;;;;;;OAOG;IACH,WAAW,CAAC,KAAK,EAAE,QAAQ;QACzB,6EAA6E;QAC7E,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CACb,UAAU,MAAM,CAAC,KAAK,CAAC,uCAAuC,CAC/D,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;YACxB,KAAK;YACL,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACH,oBAAoB,CAAC,IAAI,EAAE,MAAM;QAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC5D,uCAAuC;YACvC,+DAA+D;YAC/D,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;YACpD,IAAI,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;gBAC9C,gDAAgD;gBAChD,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAC7B,SAAS,KAAK,KAAK,EACnB,SAAS,KAAK,MAAM,QAAQ,CAAC,IAAI,CAAC,EAAE,CACrC,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAtED,qCAsEC"}
|
|
@@ -1,30 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
-
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
14
|
-
var t = {};
|
|
15
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
16
|
-
t[p] = s[p];
|
|
17
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
18
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
19
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
20
|
-
t[p[i]] = s[p[i]];
|
|
21
|
-
}
|
|
22
|
-
return t;
|
|
23
|
-
};
|
|
24
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
25
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
26
4
|
};
|
|
27
|
-
var _JourneyContext_data, _JourneyContext_validation, _JourneyContext_nav, _JourneyContext_identity, _JourneyContext_eventListeners, _JourneyContext_eventListenerPreState;
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
6
|
exports.validateObjectKey = validateObjectKey;
|
|
30
7
|
/**
|
|
@@ -36,13 +13,15 @@ exports.validateObjectKey = validateObjectKey;
|
|
|
36
13
|
* - Navigation information about how the user got where they are.
|
|
37
14
|
*/
|
|
38
15
|
const lodash_1 = __importDefault(require("lodash"));
|
|
16
|
+
const rfdc_1 = __importDefault(require("rfdc"));
|
|
39
17
|
const ValidationError_js_1 = __importDefault(require("./ValidationError.js"));
|
|
40
18
|
const logger_js_1 = __importDefault(require("./logger.js"));
|
|
41
19
|
const utils_js_1 = require("./utils.js");
|
|
42
20
|
const context_id_generators_js_1 = require("./context-id-generators.js");
|
|
43
|
-
const { isPlainObject, isObject,
|
|
21
|
+
const { isPlainObject, isObject, isEqual } = lodash_1.default; // CommonJS
|
|
44
22
|
const log = (0, logger_js_1.default)("lib:journey-context");
|
|
45
23
|
const uuid = (0, context_id_generators_js_1.uuid)();
|
|
24
|
+
const clone = (0, rfdc_1.default)({ proto: false });
|
|
46
25
|
/**
|
|
47
26
|
* @typedef {import("../casa").ContextEventUserInfo} ContextEventUserInfo
|
|
48
27
|
* @access private
|
|
@@ -78,6 +57,18 @@ function validateObjectKey(key = "") {
|
|
|
78
57
|
}
|
|
79
58
|
/** @memberof module:@dwp/govuk-casa */
|
|
80
59
|
class JourneyContext {
|
|
60
|
+
// Private properties
|
|
61
|
+
#data;
|
|
62
|
+
#validation;
|
|
63
|
+
#nav;
|
|
64
|
+
#identity;
|
|
65
|
+
#eventListeners;
|
|
66
|
+
#eventListenerPreState;
|
|
67
|
+
static DEFAULT_CONTEXT_ID = "default";
|
|
68
|
+
/** @type {symbol} */
|
|
69
|
+
static ID_GENERATOR_REQ_LOG = Symbol("generatedContextIds");
|
|
70
|
+
/** @type {symbol} */
|
|
71
|
+
static ID_GENERATOR_REQ_KEY = Symbol("generateContextId");
|
|
81
72
|
/**
|
|
82
73
|
* Constructor.
|
|
83
74
|
*
|
|
@@ -103,19 +94,12 @@ class JourneyContext {
|
|
|
103
94
|
* others.
|
|
104
95
|
*/
|
|
105
96
|
constructor(data = {}, validation = {}, nav = {}, identity = {}) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
_JourneyContext_eventListenerPreState.set(this, void 0);
|
|
113
|
-
__classPrivateFieldSet(this, _JourneyContext_data, data, "f");
|
|
114
|
-
__classPrivateFieldSet(this, _JourneyContext_validation, validation, "f");
|
|
115
|
-
__classPrivateFieldSet(this, _JourneyContext_nav, nav, "f");
|
|
116
|
-
__classPrivateFieldSet(this, _JourneyContext_identity, identity, "f");
|
|
117
|
-
__classPrivateFieldSet(this, _JourneyContext_eventListeners, [], "f");
|
|
118
|
-
__classPrivateFieldSet(this, _JourneyContext_eventListenerPreState, null, "f");
|
|
97
|
+
this.#data = data;
|
|
98
|
+
this.#validation = validation;
|
|
99
|
+
this.#nav = nav;
|
|
100
|
+
this.#identity = identity;
|
|
101
|
+
this.#eventListeners = [];
|
|
102
|
+
this.#eventListenerPreState = null;
|
|
119
103
|
}
|
|
120
104
|
/**
|
|
121
105
|
* Clone into an object that can be stringified.
|
|
@@ -124,10 +108,10 @@ class JourneyContext {
|
|
|
124
108
|
*/
|
|
125
109
|
toObject() {
|
|
126
110
|
return Object.assign(Object.create(null), {
|
|
127
|
-
data:
|
|
128
|
-
validation:
|
|
129
|
-
nav:
|
|
130
|
-
identity:
|
|
111
|
+
data: clone(this.#data),
|
|
112
|
+
validation: clone(this.#validation),
|
|
113
|
+
nav: clone(this.#nav),
|
|
114
|
+
identity: clone(this.#identity),
|
|
131
115
|
});
|
|
132
116
|
}
|
|
133
117
|
/**
|
|
@@ -150,19 +134,19 @@ class JourneyContext {
|
|
|
150
134
|
return new JourneyContext(data, deserialisedValidation, nav, identity);
|
|
151
135
|
}
|
|
152
136
|
get data() {
|
|
153
|
-
return
|
|
137
|
+
return this.#data;
|
|
154
138
|
}
|
|
155
139
|
set data(value) {
|
|
156
|
-
|
|
140
|
+
this.#data = value;
|
|
157
141
|
}
|
|
158
142
|
get validation() {
|
|
159
|
-
return
|
|
143
|
+
return this.#validation;
|
|
160
144
|
}
|
|
161
145
|
get nav() {
|
|
162
|
-
return
|
|
146
|
+
return this.#nav;
|
|
163
147
|
}
|
|
164
148
|
get identity() {
|
|
165
|
-
return
|
|
149
|
+
return this.#identity;
|
|
166
150
|
}
|
|
167
151
|
/**
|
|
168
152
|
* Get data context for a specific a specific page.
|
|
@@ -173,10 +157,10 @@ class JourneyContext {
|
|
|
173
157
|
*/
|
|
174
158
|
getDataForPage(page) {
|
|
175
159
|
if (typeof page === "string") {
|
|
176
|
-
return
|
|
160
|
+
return this.#data[validateObjectKey(page)];
|
|
177
161
|
}
|
|
178
162
|
if (isPlainObject(page)) {
|
|
179
|
-
return
|
|
163
|
+
return this.#data[validateObjectKey(page.waypoint)];
|
|
180
164
|
}
|
|
181
165
|
throw new TypeError(`Page must be a string or Page object. Got ${typeof page}`);
|
|
182
166
|
}
|
|
@@ -186,7 +170,7 @@ class JourneyContext {
|
|
|
186
170
|
* @returns {object} Page data
|
|
187
171
|
*/
|
|
188
172
|
getData() {
|
|
189
|
-
return
|
|
173
|
+
return this.#data;
|
|
190
174
|
}
|
|
191
175
|
/**
|
|
192
176
|
* Overwrite the data context with a new object.
|
|
@@ -195,7 +179,7 @@ class JourneyContext {
|
|
|
195
179
|
* @returns {JourneyContext} Chain.
|
|
196
180
|
*/
|
|
197
181
|
setData(data) {
|
|
198
|
-
|
|
182
|
+
this.#data = data;
|
|
199
183
|
return this;
|
|
200
184
|
}
|
|
201
185
|
/**
|
|
@@ -208,10 +192,10 @@ class JourneyContext {
|
|
|
208
192
|
*/
|
|
209
193
|
setDataForPage(page, webFormData) {
|
|
210
194
|
if (typeof page === "string") {
|
|
211
|
-
|
|
195
|
+
this.#data[validateObjectKey(page)] = webFormData;
|
|
212
196
|
}
|
|
213
197
|
else if (isPlainObject(page)) {
|
|
214
|
-
|
|
198
|
+
this.#data[validateObjectKey(page.waypoint)] = webFormData;
|
|
215
199
|
}
|
|
216
200
|
else {
|
|
217
201
|
throw new TypeError(`Page must be a string or Page object. Got ${typeof page}`);
|
|
@@ -224,7 +208,7 @@ class JourneyContext {
|
|
|
224
208
|
* @returns {object} All page validation errors.
|
|
225
209
|
*/
|
|
226
210
|
getValidationErrors() {
|
|
227
|
-
return
|
|
211
|
+
return this.#validation;
|
|
228
212
|
}
|
|
229
213
|
/**
|
|
230
214
|
* Removes any validation state for the given page. Clearing validation state
|
|
@@ -236,8 +220,8 @@ class JourneyContext {
|
|
|
236
220
|
*/
|
|
237
221
|
removeValidationStateForPage(pageId) {
|
|
238
222
|
/* eslint-disable-next-line no-unused-vars */
|
|
239
|
-
const
|
|
240
|
-
|
|
223
|
+
const { [pageId]: dummy, ...remaining } = this.#validation;
|
|
224
|
+
this.#validation = { ...remaining };
|
|
241
225
|
return this;
|
|
242
226
|
}
|
|
243
227
|
/**
|
|
@@ -250,7 +234,7 @@ class JourneyContext {
|
|
|
250
234
|
* @returns {JourneyContext} Chain.
|
|
251
235
|
*/
|
|
252
236
|
clearValidationErrorsForPage(pageId) {
|
|
253
|
-
|
|
237
|
+
this.#validation[validateObjectKey(pageId)] = null;
|
|
254
238
|
return this;
|
|
255
239
|
}
|
|
256
240
|
/**
|
|
@@ -265,12 +249,12 @@ class JourneyContext {
|
|
|
265
249
|
if (!Array.isArray(errors)) {
|
|
266
250
|
throw new SyntaxError(`Errors must be an Array. Received ${Object.prototype.toString.call(errors)}`);
|
|
267
251
|
}
|
|
268
|
-
|
|
252
|
+
for (const error of errors) {
|
|
269
253
|
if (!(error instanceof ValidationError_js_1.default)) {
|
|
270
254
|
throw new SyntaxError("Field errors must be a ValidationError");
|
|
271
255
|
}
|
|
272
|
-
}
|
|
273
|
-
|
|
256
|
+
}
|
|
257
|
+
this.#validation[validateObjectKey(pageId)] = errors;
|
|
274
258
|
return this;
|
|
275
259
|
}
|
|
276
260
|
/**
|
|
@@ -281,8 +265,7 @@ class JourneyContext {
|
|
|
281
265
|
* @returns {ValidationError[]} An array of errors
|
|
282
266
|
*/
|
|
283
267
|
getValidationErrorsForPage(pageId) {
|
|
284
|
-
|
|
285
|
-
return (_a = __classPrivateFieldGet(this, _JourneyContext_validation, "f")[validateObjectKey(pageId)]) !== null && _a !== void 0 ? _a : [];
|
|
268
|
+
return this.#validation[validateObjectKey(pageId)] ?? [];
|
|
286
269
|
}
|
|
287
270
|
/**
|
|
288
271
|
* Same as `getValidationErrorsForPage()`, but the return value is an object
|
|
@@ -315,8 +298,7 @@ class JourneyContext {
|
|
|
315
298
|
* @returns {boolean} Result.
|
|
316
299
|
*/
|
|
317
300
|
hasValidationErrorsForPage(pageId) {
|
|
318
|
-
|
|
319
|
-
return ((_b = (_a = __classPrivateFieldGet(this, _JourneyContext_validation, "f")) === null || _a === void 0 ? void 0 : _a[validateObjectKey(pageId)]) === null || _b === void 0 ? void 0 : _b.length) > 0;
|
|
301
|
+
return this.#validation?.[validateObjectKey(pageId)]?.length > 0;
|
|
320
302
|
}
|
|
321
303
|
/**
|
|
322
304
|
* Set language of the context.
|
|
@@ -325,7 +307,7 @@ class JourneyContext {
|
|
|
325
307
|
* @returns {JourneyContext} Chain.
|
|
326
308
|
*/
|
|
327
309
|
setNavigationLanguage(language = "en") {
|
|
328
|
-
|
|
310
|
+
this.#nav.language = language;
|
|
329
311
|
return this;
|
|
330
312
|
}
|
|
331
313
|
/**
|
|
@@ -335,7 +317,7 @@ class JourneyContext {
|
|
|
335
317
|
* @returns {boolean} True if the page is valid.
|
|
336
318
|
*/
|
|
337
319
|
isPageValid(pageId) {
|
|
338
|
-
return
|
|
320
|
+
return this.#validation[validateObjectKey(pageId)] === null;
|
|
339
321
|
}
|
|
340
322
|
/**
|
|
341
323
|
* Remove information about these waypoints.
|
|
@@ -345,16 +327,16 @@ class JourneyContext {
|
|
|
345
327
|
purge(waypoints = []) {
|
|
346
328
|
const newData = Object.create(null);
|
|
347
329
|
const newValidation = Object.create(null);
|
|
348
|
-
const toKeep = Object.keys(
|
|
330
|
+
const toKeep = Object.keys(this.#data).filter((w) => !waypoints.includes(w));
|
|
349
331
|
// ESLint disabled as `i` is an integer
|
|
350
332
|
/* eslint-disable security/detect-object-injection */
|
|
351
333
|
for (let i = 0, l = toKeep.length; i < l; i++) {
|
|
352
|
-
newData[toKeep[i]] =
|
|
353
|
-
newValidation[toKeep[i]] =
|
|
334
|
+
newData[toKeep[i]] = this.#data[toKeep[i]];
|
|
335
|
+
newValidation[toKeep[i]] = this.#validation[toKeep[i]];
|
|
354
336
|
}
|
|
355
337
|
/* eslint-enable security/detect-object-injection */
|
|
356
|
-
|
|
357
|
-
|
|
338
|
+
this.#data = { ...newData };
|
|
339
|
+
this.#validation = { ...newValidation };
|
|
358
340
|
}
|
|
359
341
|
/**
|
|
360
342
|
* Remove validation state from these waypoints. This is useful to quickly
|
|
@@ -383,8 +365,8 @@ class JourneyContext {
|
|
|
383
365
|
* @returns {JourneyContext} Chain
|
|
384
366
|
*/
|
|
385
367
|
addEventListeners(events) {
|
|
386
|
-
|
|
387
|
-
|
|
368
|
+
this.#eventListeners = events;
|
|
369
|
+
this.#eventListenerPreState = this.toObject();
|
|
388
370
|
return this;
|
|
389
371
|
}
|
|
390
372
|
/**
|
|
@@ -400,12 +382,11 @@ class JourneyContext {
|
|
|
400
382
|
* @returns {JourneyContext} Chain
|
|
401
383
|
*/
|
|
402
384
|
applyEventListeners({ event, session, userInfo }) {
|
|
403
|
-
|
|
404
|
-
if (!__classPrivateFieldGet(this, _JourneyContext_eventListeners, "f").length) {
|
|
385
|
+
if (!this.#eventListeners.length) {
|
|
405
386
|
return this;
|
|
406
387
|
}
|
|
407
|
-
const previousContext = JourneyContext.fromObject(
|
|
408
|
-
const listeners =
|
|
388
|
+
const previousContext = JourneyContext.fromObject(this.#eventListenerPreState);
|
|
389
|
+
const listeners = this.#eventListeners.filter((l) => l.event === event);
|
|
409
390
|
// ESLint disabled as `listeners[i]` uses an integer key, and the other keys
|
|
410
391
|
// are derived from the list of `listeners`, which are not manipulated at
|
|
411
392
|
// runtime (only set by dev in code).
|
|
@@ -421,14 +402,14 @@ class JourneyContext {
|
|
|
421
402
|
else if (waypoint && !field) {
|
|
422
403
|
logMessage = `Calling waypoint-specific event handler on "${waypoint}"`;
|
|
423
404
|
runHandler =
|
|
424
|
-
|
|
425
|
-
!isEqual(
|
|
405
|
+
previousContext.data?.[waypoint] !== undefined &&
|
|
406
|
+
!isEqual(this.data?.[waypoint], previousContext.data?.[waypoint]);
|
|
426
407
|
}
|
|
427
408
|
else if (waypoint && field) {
|
|
428
409
|
logMessage = `Calling field-specific event handler on "${waypoint} : ${field}"`;
|
|
429
410
|
runHandler =
|
|
430
|
-
|
|
431
|
-
!isEqual(
|
|
411
|
+
previousContext.data?.[waypoint]?.[field] !== undefined &&
|
|
412
|
+
!isEqual(this.data?.[waypoint]?.[field], previousContext.data?.[waypoint]?.[field]);
|
|
432
413
|
}
|
|
433
414
|
if (runHandler) {
|
|
434
415
|
log.trace(logMessage);
|
|
@@ -483,7 +464,7 @@ class JourneyContext {
|
|
|
483
464
|
* @returns {boolean} True if this is the "default" journey context
|
|
484
465
|
*/
|
|
485
466
|
isDefault() {
|
|
486
|
-
return
|
|
467
|
+
return this.#identity.id === JourneyContext.DEFAULT_CONTEXT_ID;
|
|
487
468
|
}
|
|
488
469
|
/**
|
|
489
470
|
* Initialise session with an empty entry for the "default" context.
|
|
@@ -495,13 +476,13 @@ class JourneyContext {
|
|
|
495
476
|
// For existing sessions that were created prior to `journeyContextList`
|
|
496
477
|
// being remodelled as an array, we need to convert the "legacy" structure
|
|
497
478
|
// into an equivalent array.
|
|
498
|
-
if (isPlainObject(session
|
|
479
|
+
if (isPlainObject(session?.journeyContextList)) {
|
|
499
480
|
log.trace("Session context list already initialised as an object (legacy structure). Will convert from object to array.");
|
|
500
481
|
/* eslint-disable-next-line no-param-reassign */
|
|
501
482
|
session.journeyContextList = Object.entries(session.journeyContextList);
|
|
502
483
|
}
|
|
503
484
|
// Initialise new context list in the session
|
|
504
|
-
if (!
|
|
485
|
+
if (!Object.hasOwn(session, "journeyContextList")) {
|
|
505
486
|
log.trace("Initialising session with a default journey context list");
|
|
506
487
|
/* eslint-disable-next-line no-param-reassign */
|
|
507
488
|
session.journeyContextList = [];
|
|
@@ -545,7 +526,6 @@ class JourneyContext {
|
|
|
545
526
|
* @throws {Error} When generated ID has already been used
|
|
546
527
|
*/
|
|
547
528
|
static generateContextId(req) {
|
|
548
|
-
var _a;
|
|
549
529
|
// Can't generate custom ID when no request object is provided, because the
|
|
550
530
|
// custom generator function itself exists on that object.
|
|
551
531
|
if (!req) {
|
|
@@ -567,7 +547,7 @@ class JourneyContext {
|
|
|
567
547
|
const inSessionIds = JourneyContext.getContexts(req.session)
|
|
568
548
|
.map((c) => c.identity.id)
|
|
569
549
|
.filter((id) => id !== JourneyContext.DEFAULT_CONTEXT_ID);
|
|
570
|
-
const inRequestIds =
|
|
550
|
+
const inRequestIds = req[JourneyContext.ID_GENERATOR_REQ_LOG] ?? [];
|
|
571
551
|
const reservedIds = Array.from(new Set([...inSessionIds, ...inRequestIds]).values());
|
|
572
552
|
// Generate and log the ID
|
|
573
553
|
const id = JourneyContext.validateContextId(req[JourneyContext.ID_GENERATOR_REQ_KEY].call(null, { req, reservedIds }));
|
|
@@ -602,7 +582,7 @@ class JourneyContext {
|
|
|
602
582
|
* @returns {JourneyContext} The discovered JourneyContext instance
|
|
603
583
|
*/
|
|
604
584
|
static getContextById(session, id) {
|
|
605
|
-
const list = new Map(session
|
|
585
|
+
const list = new Map(session?.journeyContextList);
|
|
606
586
|
if (list.has(id)) {
|
|
607
587
|
// ESLint disabled as `id` has been verified as an "own" property
|
|
608
588
|
/* eslint-disable-next-line security/detect-object-injection */
|
|
@@ -619,7 +599,7 @@ class JourneyContext {
|
|
|
619
599
|
*/
|
|
620
600
|
static getContextByName(session, name) {
|
|
621
601
|
if (session) {
|
|
622
|
-
const list = new Map(session
|
|
602
|
+
const list = new Map(session?.journeyContextList);
|
|
623
603
|
const context = [...list.values()].find((c) => c.identity.name === name);
|
|
624
604
|
if (context) {
|
|
625
605
|
return JourneyContext.fromObject(context);
|
|
@@ -636,9 +616,9 @@ class JourneyContext {
|
|
|
636
616
|
*/
|
|
637
617
|
static getContextsByTag(session, tag) {
|
|
638
618
|
if (session) {
|
|
639
|
-
const list = new Map(session
|
|
619
|
+
const list = new Map(session?.journeyContextList);
|
|
640
620
|
return [...list.values()]
|
|
641
|
-
.filter((c) =>
|
|
621
|
+
.filter((c) => c.identity.tags?.includes(tag))
|
|
642
622
|
.map((c) => JourneyContext.fromObject(c));
|
|
643
623
|
}
|
|
644
624
|
return undefined;
|
|
@@ -650,7 +630,7 @@ class JourneyContext {
|
|
|
650
630
|
* @returns {Array} Array of contexts
|
|
651
631
|
*/
|
|
652
632
|
static getContexts(session) {
|
|
653
|
-
if (
|
|
633
|
+
if (session && Object.hasOwn(session, "journeyContextList")) {
|
|
654
634
|
return session.journeyContextList.map(([, contextObj]) => JourneyContext.fromObject(contextObj));
|
|
655
635
|
}
|
|
656
636
|
return [];
|
|
@@ -677,7 +657,7 @@ class JourneyContext {
|
|
|
677
657
|
throw new TypeError("Context must have an ID before storing in session");
|
|
678
658
|
}
|
|
679
659
|
// Initialise the session if necessary
|
|
680
|
-
if (
|
|
660
|
+
if (Object.hasOwn(session, "journeyContextList") === false) {
|
|
681
661
|
JourneyContext.initContextStore(session);
|
|
682
662
|
}
|
|
683
663
|
// Apply context events
|
|
@@ -717,8 +697,7 @@ class JourneyContext {
|
|
|
717
697
|
* @returns {void}
|
|
718
698
|
*/
|
|
719
699
|
static removeContextById(session, id) {
|
|
720
|
-
|
|
721
|
-
const index = ((_a = session === null || session === void 0 ? void 0 : session.journeyContextList) !== null && _a !== void 0 ? _a : []).findIndex(([contextId]) => contextId === id);
|
|
700
|
+
const index = (session?.journeyContextList ?? []).findIndex(([contextId]) => contextId === id);
|
|
722
701
|
if (index > -1) {
|
|
723
702
|
session.journeyContextList.splice(index, 1);
|
|
724
703
|
}
|
|
@@ -741,7 +720,9 @@ class JourneyContext {
|
|
|
741
720
|
* @returns {void}
|
|
742
721
|
*/
|
|
743
722
|
static removeContextsByTag(session, tag) {
|
|
744
|
-
|
|
723
|
+
for (const c of JourneyContext.getContextsByTag(session, tag)) {
|
|
724
|
+
JourneyContext.removeContext(session, c);
|
|
725
|
+
}
|
|
745
726
|
}
|
|
746
727
|
/**
|
|
747
728
|
* Remove call contexts.
|
|
@@ -750,7 +731,9 @@ class JourneyContext {
|
|
|
750
731
|
* @returns {void}
|
|
751
732
|
*/
|
|
752
733
|
static removeContexts(session) {
|
|
753
|
-
|
|
734
|
+
for (const c of JourneyContext.getContexts(session)) {
|
|
735
|
+
JourneyContext.removeContext(session, c);
|
|
736
|
+
}
|
|
754
737
|
}
|
|
755
738
|
/**
|
|
756
739
|
* Extract the Journey Context referred to in the incoming request.
|
|
@@ -765,15 +748,15 @@ class JourneyContext {
|
|
|
765
748
|
static extractContextFromRequest(req) {
|
|
766
749
|
JourneyContext.initContextStore(req.session);
|
|
767
750
|
let contextId;
|
|
768
|
-
if (
|
|
751
|
+
if (req.params && Object.hasOwn(req.params, "contextid")) {
|
|
769
752
|
log.trace("Context ID found in req.params.contextid");
|
|
770
753
|
contextId = String(req.params.contextid);
|
|
771
754
|
}
|
|
772
|
-
else if (
|
|
755
|
+
else if (req.query && Object.hasOwn(req.query, "contextid")) {
|
|
773
756
|
log.trace("Context ID found in req.query.contextid");
|
|
774
757
|
contextId = String(req.query.contextid);
|
|
775
758
|
}
|
|
776
|
-
else if (
|
|
759
|
+
else if (req.body && Object.hasOwn(req.body, "contextid")) {
|
|
777
760
|
log.trace("Context ID found in req.body.contextid");
|
|
778
761
|
contextId = String(req.body.contextid);
|
|
779
762
|
}
|
|
@@ -803,11 +786,9 @@ class JourneyContext {
|
|
|
803
786
|
* @param {string} opts.to Waypoint to skip to.
|
|
804
787
|
*/
|
|
805
788
|
setSkipped(waypoint, opts) {
|
|
806
|
-
var _a;
|
|
807
|
-
var _b;
|
|
808
789
|
// Unset, with setSkipped(a, false)
|
|
809
790
|
if (opts === false) {
|
|
810
|
-
|
|
791
|
+
this.data[waypoint] ??= Object.create(null);
|
|
811
792
|
this.data[waypoint].__skipped__ = undefined;
|
|
812
793
|
this.data[waypoint].__skip__ = undefined;
|
|
813
794
|
}
|
|
@@ -818,7 +799,7 @@ class JourneyContext {
|
|
|
818
799
|
this.data[waypoint].__skip__ = { to: null };
|
|
819
800
|
}
|
|
820
801
|
// Set, with setSkipped(a, { to: b }) and clear data
|
|
821
|
-
else if (typeof
|
|
802
|
+
else if (typeof opts?.to === "string") {
|
|
822
803
|
this.data[waypoint] = Object.create(null);
|
|
823
804
|
this.data[waypoint].__skipped__ = true;
|
|
824
805
|
this.data[waypoint].__skip__ = { to: opts.to };
|
|
@@ -837,21 +818,14 @@ class JourneyContext {
|
|
|
837
818
|
* skipped to a specific page.
|
|
838
819
|
*/
|
|
839
820
|
isSkipped(waypoint, opts) {
|
|
840
|
-
var _a, _b, _c, _d;
|
|
841
821
|
if (opts === undefined) {
|
|
842
|
-
return (
|
|
843
|
-
|
|
822
|
+
return (this.data[waypoint]?.__skipped__ === true ||
|
|
823
|
+
this.data[waypoint]?.__skip__ !== undefined);
|
|
844
824
|
}
|
|
845
825
|
else if (typeof opts.to === "string") {
|
|
846
|
-
return
|
|
826
|
+
return this.data[waypoint]?.__skip__?.to === opts.to;
|
|
847
827
|
}
|
|
848
828
|
}
|
|
849
829
|
}
|
|
850
|
-
_JourneyContext_data = new WeakMap(), _JourneyContext_validation = new WeakMap(), _JourneyContext_nav = new WeakMap(), _JourneyContext_identity = new WeakMap(), _JourneyContext_eventListeners = new WeakMap(), _JourneyContext_eventListenerPreState = new WeakMap();
|
|
851
|
-
JourneyContext.DEFAULT_CONTEXT_ID = "default";
|
|
852
|
-
/** @type {symbol} */
|
|
853
|
-
JourneyContext.ID_GENERATOR_REQ_LOG = Symbol("generatedContextIds");
|
|
854
|
-
/** @type {symbol} */
|
|
855
|
-
JourneyContext.ID_GENERATOR_REQ_KEY = Symbol("generateContextId");
|
|
856
830
|
exports.default = JourneyContext;
|
|
857
831
|
//# sourceMappingURL=JourneyContext.js.map
|