@dwp/govuk-casa 8.7.12 → 8.8.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/dist/casa.js +2 -1
- package/dist/casa.js.map +1 -0
- package/dist/lib/CasaTemplateLoader.js +1 -0
- package/dist/lib/CasaTemplateLoader.js.map +1 -0
- package/dist/lib/JourneyContext.d.ts +1 -1
- package/dist/lib/JourneyContext.js +2 -1
- package/dist/lib/JourneyContext.js.map +1 -0
- package/dist/lib/MutableRouter.js +1 -0
- package/dist/lib/MutableRouter.js.map +1 -0
- package/dist/lib/Plan.d.ts +2 -1
- package/dist/lib/Plan.js +4 -3
- package/dist/lib/Plan.js.map +1 -0
- package/dist/lib/ValidationError.js +1 -0
- package/dist/lib/ValidationError.js.map +1 -0
- package/dist/lib/ValidatorFactory.d.ts +2 -2
- package/dist/lib/ValidatorFactory.js +3 -2
- package/dist/lib/ValidatorFactory.js.map +1 -0
- package/dist/lib/configuration-ingestor.js +1 -0
- package/dist/lib/configuration-ingestor.js.map +1 -0
- package/dist/lib/configure.js +2 -1
- package/dist/lib/configure.js.map +1 -0
- package/dist/lib/end-session.js +1 -0
- package/dist/lib/end-session.js.map +1 -0
- package/dist/lib/field.js +1 -0
- package/dist/lib/field.js.map +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/logger.js +1 -0
- package/dist/lib/logger.js.map +1 -0
- package/dist/lib/mount.js +3 -2
- package/dist/lib/mount.js.map +1 -0
- package/dist/lib/nunjucks-filters.js +1 -0
- package/dist/lib/nunjucks-filters.js.map +1 -0
- package/dist/lib/nunjucks.js +1 -0
- package/dist/lib/nunjucks.js.map +1 -0
- package/dist/lib/utils.d.ts +45 -27
- package/dist/lib/utils.js +105 -67
- package/dist/lib/utils.js.map +1 -0
- package/dist/lib/validators/dateObject.js +4 -3
- package/dist/lib/validators/dateObject.js.map +1 -0
- package/dist/lib/validators/email.js +1 -0
- package/dist/lib/validators/email.js.map +1 -0
- package/dist/lib/validators/inArray.js +1 -0
- package/dist/lib/validators/inArray.js.map +1 -0
- package/dist/lib/validators/index.js +1 -0
- package/dist/lib/validators/index.js.map +1 -0
- package/dist/lib/validators/nino.js +1 -0
- package/dist/lib/validators/nino.js.map +1 -0
- package/dist/lib/validators/postalAddressObject.d.ts +2 -2
- package/dist/lib/validators/postalAddressObject.js +2 -1
- package/dist/lib/validators/postalAddressObject.js.map +1 -0
- package/dist/lib/validators/regex.js +1 -0
- package/dist/lib/validators/regex.js.map +1 -0
- package/dist/lib/validators/required.js +1 -0
- package/dist/lib/validators/required.js.map +1 -0
- package/dist/lib/validators/strlen.js +1 -0
- package/dist/lib/validators/strlen.js.map +1 -0
- package/dist/lib/validators/wordCount.js +1 -0
- package/dist/lib/validators/wordCount.js.map +1 -0
- package/dist/lib/waypoint-url.js +1 -0
- package/dist/lib/waypoint-url.js.map +1 -0
- package/dist/middleware/body-parser.js +1 -0
- package/dist/middleware/body-parser.js.map +1 -0
- package/dist/middleware/csrf.js +1 -0
- package/dist/middleware/csrf.js.map +1 -0
- package/dist/middleware/data.js +1 -0
- package/dist/middleware/data.js.map +1 -0
- package/dist/middleware/gather-fields.js +1 -0
- package/dist/middleware/gather-fields.js.map +1 -0
- package/dist/middleware/i18n.js +1 -0
- package/dist/middleware/i18n.js.map +1 -0
- package/dist/middleware/post.js +1 -0
- package/dist/middleware/post.js.map +1 -0
- package/dist/middleware/pre.js +1 -0
- package/dist/middleware/pre.js.map +1 -0
- package/dist/middleware/progress-journey.js +1 -0
- package/dist/middleware/progress-journey.js.map +1 -0
- package/dist/middleware/sanitise-fields.js +1 -0
- package/dist/middleware/sanitise-fields.js.map +1 -0
- package/dist/middleware/serve-first-waypoint.js +1 -0
- package/dist/middleware/serve-first-waypoint.js.map +1 -0
- package/dist/middleware/session.js +1 -0
- package/dist/middleware/session.js.map +1 -0
- package/dist/middleware/skip-waypoint.js +1 -0
- package/dist/middleware/skip-waypoint.js.map +1 -0
- package/dist/middleware/steer-journey.js +1 -0
- package/dist/middleware/steer-journey.js.map +1 -0
- package/dist/middleware/strip-proxy-path.js +1 -0
- package/dist/middleware/strip-proxy-path.js.map +1 -0
- package/dist/middleware/validate-fields.js +1 -0
- package/dist/middleware/validate-fields.js.map +1 -0
- package/dist/mjs/esm-wrapper.js +10 -15
- package/dist/routes/ancillary.js +1 -0
- package/dist/routes/ancillary.js.map +1 -0
- package/dist/routes/journey.js +1 -0
- package/dist/routes/journey.js.map +1 -0
- package/dist/routes/static.js +1 -0
- package/dist/routes/static.js.map +1 -0
- package/locales/cy/error.json +1 -1
- package/locales/en/error.json +1 -1
- package/package.json +16 -15
- package/src/casa.js +320 -0
- package/src/lib/CasaTemplateLoader.js +104 -0
- package/src/lib/JourneyContext.js +783 -0
- package/src/lib/MutableRouter.js +310 -0
- package/src/lib/Plan.js +624 -0
- package/src/lib/ValidationError.js +163 -0
- package/src/lib/ValidatorFactory.js +105 -0
- package/src/lib/configuration-ingestor.js +457 -0
- package/src/lib/configure.js +202 -0
- package/src/lib/dirname.cjs +1 -0
- package/src/lib/end-session.js +45 -0
- package/src/lib/field.js +456 -0
- package/src/lib/index.js +33 -0
- package/src/lib/logger.js +16 -0
- package/src/lib/mount.js +127 -0
- package/src/lib/nunjucks-filters.js +150 -0
- package/src/lib/nunjucks.js +53 -0
- package/src/lib/utils.js +232 -0
- package/src/lib/validators/dateObject.js +169 -0
- package/src/lib/validators/email.js +55 -0
- package/src/lib/validators/inArray.js +81 -0
- package/src/lib/validators/index.js +24 -0
- package/src/lib/validators/nino.js +57 -0
- package/src/lib/validators/postalAddressObject.js +162 -0
- package/src/lib/validators/regex.js +48 -0
- package/src/lib/validators/required.js +74 -0
- package/src/lib/validators/strlen.js +66 -0
- package/src/lib/validators/wordCount.js +70 -0
- package/src/lib/waypoint-url.js +93 -0
- package/src/middleware/body-parser.js +31 -0
- package/src/middleware/csrf.js +29 -0
- package/src/middleware/data.js +105 -0
- package/src/middleware/dirname.cjs +1 -0
- package/src/middleware/gather-fields.js +51 -0
- package/src/middleware/i18n.js +106 -0
- package/src/middleware/post.js +61 -0
- package/src/middleware/pre.js +91 -0
- package/src/middleware/progress-journey.js +92 -0
- package/src/middleware/sanitise-fields.js +58 -0
- package/src/middleware/serve-first-waypoint.js +28 -0
- package/src/middleware/session.js +129 -0
- package/src/middleware/skip-waypoint.js +46 -0
- package/src/middleware/steer-journey.js +78 -0
- package/src/middleware/strip-proxy-path.js +56 -0
- package/src/middleware/validate-fields.js +84 -0
- package/src/routes/ancillary.js +29 -0
- package/src/routes/dirname.cjs +1 -0
- package/src/routes/journey.js +212 -0
- package/src/routes/static.js +77 -0
- package/views/casa/components/character-count/README.md +10 -0
- package/views/casa/components/character-count/template.njk +6 -2
- package/views/casa/components/checkboxes/README.md +43 -34
- package/views/casa/components/checkboxes/template.njk +8 -7
- package/views/casa/components/date-input/README.md +11 -1
- package/views/casa/components/date-input/template.njk +6 -4
- package/views/casa/components/input/README.md +9 -0
- package/views/casa/components/input/template.njk +6 -2
- package/views/casa/components/postal-address-object/README.md +10 -0
- package/views/casa/components/postal-address-object/template.njk +20 -5
- package/views/casa/components/radios/README.md +49 -24
- package/views/casa/components/radios/template.njk +6 -3
- package/views/casa/components/select/README.md +65 -0
- package/views/casa/components/select/macro.njk +3 -0
- package/views/casa/components/select/template.njk +49 -0
- package/views/casa/components/textarea/README.md +9 -0
- package/views/casa/components/textarea/template.njk +6 -2
- package/views/casa/layouts/journey.njk +1 -1
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/* eslint-disable sonarjs/no-duplicate-string,class-methods-use-this */
|
|
2
|
+
import { Router } from 'express';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @memberof module:@dwp/govuk-casa
|
|
6
|
+
*/
|
|
7
|
+
export default class MutableRouter {
|
|
8
|
+
/**
|
|
9
|
+
* @type {Array}
|
|
10
|
+
*/
|
|
11
|
+
#stack;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @type {Router}
|
|
15
|
+
*/
|
|
16
|
+
#router;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @type {boolean}
|
|
20
|
+
*/
|
|
21
|
+
#sealed;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A wrapping class to make Express routers mutable. You can append, prepend
|
|
25
|
+
* and remove routes from a MutableRouter before finally "sealing" it and
|
|
26
|
+
* mounting it on your Express app.
|
|
27
|
+
*
|
|
28
|
+
* @class
|
|
29
|
+
*/
|
|
30
|
+
constructor() {
|
|
31
|
+
this.#router = Router();
|
|
32
|
+
this.#stack = [];
|
|
33
|
+
this.#sealed = false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
#append(method, path, ...callbacks) {
|
|
37
|
+
if (this.#sealed) {
|
|
38
|
+
throw new Error('Cannot alter middleware in a sealed mutable router');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.#stack.push({
|
|
42
|
+
method,
|
|
43
|
+
path,
|
|
44
|
+
args: [path, ...callbacks],
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#prepend(method, path, ...callbacks) {
|
|
49
|
+
if (this.#sealed) {
|
|
50
|
+
throw new Error('Cannot alter middleware in a sealed mutable router');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.#stack.splice(0, 0, {
|
|
54
|
+
method,
|
|
55
|
+
path,
|
|
56
|
+
args: [path, ...callbacks],
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// This will replace the first found route, and remove all other routes on the
|
|
61
|
+
// given path
|
|
62
|
+
#replace(method, path, ...callbacks) {
|
|
63
|
+
if (this.#sealed) {
|
|
64
|
+
throw new Error('Cannot alter middleware in a sealed mutable router');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const finder = (command) => `${command.method}|${command.path}` === `${method}|${path}`;
|
|
68
|
+
const index = this.#stack.findIndex(finder);
|
|
69
|
+
|
|
70
|
+
if (index > -1) {
|
|
71
|
+
this.#stack.splice(index, 1, {
|
|
72
|
+
method,
|
|
73
|
+
path,
|
|
74
|
+
args: [path, ...callbacks],
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
this.#stack = this.#stack.filter((command, idx) => idx <= index || !finder(command))
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Seals this router to make it immutable. Returns the ExpressJS router.
|
|
83
|
+
*
|
|
84
|
+
* @returns {Router} ExpressJS Router
|
|
85
|
+
*/
|
|
86
|
+
seal() {
|
|
87
|
+
if (this.#sealed) {
|
|
88
|
+
return this.#router;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this.#stack.forEach(({ method, args }) => {
|
|
92
|
+
// ESLint disabled as `#router` is dev-controlled, and `seal()` is only
|
|
93
|
+
// run at boot-time before any user interaction
|
|
94
|
+
/* eslint-disable-next-line security/detect-object-injection */
|
|
95
|
+
this.#router[method].call(this.#router, ...args);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
this.#sealed = true;
|
|
99
|
+
|
|
100
|
+
return this.#router;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* ------------------------------------------------------------- prependers */
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Prepend middleware function(s) using the `all()` method.
|
|
107
|
+
*
|
|
108
|
+
* @param {string | Function} path route path or middleware function
|
|
109
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
110
|
+
* @returns {void}
|
|
111
|
+
*/
|
|
112
|
+
prependAll(path, ...callbacks) {
|
|
113
|
+
this.#prepend('all', path, ...callbacks);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Prepend middleware function(s) using the `get()` method.
|
|
118
|
+
*
|
|
119
|
+
* @param {string | Function} path route path or middleware function
|
|
120
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
121
|
+
* @returns {void}
|
|
122
|
+
*/
|
|
123
|
+
prependGet(path, ...callbacks) {
|
|
124
|
+
this.#prepend('get', path, ...callbacks);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Prepend middleware function(s) using the `post()` method.
|
|
129
|
+
*
|
|
130
|
+
* @param {string | Function} path route path or middleware function
|
|
131
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
132
|
+
* @returns {void}
|
|
133
|
+
*/
|
|
134
|
+
prependPost(path, ...callbacks) {
|
|
135
|
+
this.#prepend('post', path, ...callbacks);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Prepend middleware function(s) using the `delete()` method.
|
|
140
|
+
*
|
|
141
|
+
* @param {string | Function} path route path or middleware function
|
|
142
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
143
|
+
* @returns {void}
|
|
144
|
+
*/
|
|
145
|
+
prependDelete(path, ...callbacks) {
|
|
146
|
+
this.#prepend('delete', path, ...callbacks);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Prepend middleware function(s) using the `put()` method.
|
|
151
|
+
*
|
|
152
|
+
* @param {string | Function} path route path or middleware function
|
|
153
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
154
|
+
* @returns {void}
|
|
155
|
+
*/
|
|
156
|
+
prependPut(path, ...callbacks) {
|
|
157
|
+
this.#prepend('put', path, ...callbacks);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Prepend middleware function(s) using the `use()` method.
|
|
162
|
+
*
|
|
163
|
+
* @param {string | Function} path route path or middleware function
|
|
164
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
165
|
+
* @returns {void}
|
|
166
|
+
*/
|
|
167
|
+
prependUse(path, ...callbacks) {
|
|
168
|
+
this.#prepend('use', path, ...callbacks);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* -------------------------------------------------------------- replacers */
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Replace middleware function(s) that were mounted using the `all()` method.
|
|
175
|
+
*
|
|
176
|
+
* @param {string | Function} path route path or middleware function
|
|
177
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
178
|
+
* @returns {void}
|
|
179
|
+
*/
|
|
180
|
+
replaceAll(path, ...callbacks) {
|
|
181
|
+
this.#replace('all', path, ...callbacks);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Replace middleware function(s) that were mounted using the `get()` method.
|
|
186
|
+
*
|
|
187
|
+
* @param {string | Function} path route path or middleware function
|
|
188
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
189
|
+
* @returns {void}
|
|
190
|
+
*/
|
|
191
|
+
replaceGet(path, ...callbacks) {
|
|
192
|
+
this.#replace('get', path, ...callbacks);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Replace middleware function(s) that were mounted using the `post()` method.
|
|
197
|
+
*
|
|
198
|
+
* @param {string | Function} path route path or middleware function
|
|
199
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
200
|
+
* @returns {void}
|
|
201
|
+
*/
|
|
202
|
+
replacePost(path, ...callbacks) {
|
|
203
|
+
this.#replace('post', path, ...callbacks);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Replace middleware function(s) that were mounted using the `delete()` method.
|
|
208
|
+
*
|
|
209
|
+
* @param {string | Function} path route path or middleware function
|
|
210
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
211
|
+
* @returns {void}
|
|
212
|
+
*/
|
|
213
|
+
replaceDelete(path, ...callbacks) {
|
|
214
|
+
this.#replace('delete', path, ...callbacks);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Replace middleware function(s) that were mounted using the `put()` method.
|
|
219
|
+
*
|
|
220
|
+
* @param {string | Function} path route path or middleware function
|
|
221
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
222
|
+
* @returns {void}
|
|
223
|
+
*/
|
|
224
|
+
replacePut(path, ...callbacks) {
|
|
225
|
+
this.#replace('put', path, ...callbacks);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Replace middleware function(s) that were mounted using the `use()` method.
|
|
230
|
+
*
|
|
231
|
+
* @param {string | Function} path route path or middleware function
|
|
232
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
233
|
+
* @returns {void}
|
|
234
|
+
*/
|
|
235
|
+
replaceUse(path, ...callbacks) {
|
|
236
|
+
this.#replace('use', path, ...callbacks);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/* ---------------------------------------------- express.Router() wrappers */
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Append middleware function(s) using the `all()` method.
|
|
243
|
+
*
|
|
244
|
+
* @param {string | Function} path route path or middleware function
|
|
245
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
246
|
+
* @returns {void}
|
|
247
|
+
*/
|
|
248
|
+
all(path, ...callbacks) {
|
|
249
|
+
this.#append('all', path, ...callbacks);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Append middleware function(s) using the `get()` method.
|
|
254
|
+
*
|
|
255
|
+
* @param {string | Function} path route path or middleware function
|
|
256
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
257
|
+
* @returns {void}
|
|
258
|
+
*/
|
|
259
|
+
get(path, ...callbacks) {
|
|
260
|
+
this.#append('get', path, ...callbacks);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Append middleware function(s) using the `post()` method.
|
|
265
|
+
*
|
|
266
|
+
* @param {string | Function} path route path or middleware function
|
|
267
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
268
|
+
* @returns {void}
|
|
269
|
+
*/
|
|
270
|
+
post(path, ...callbacks) {
|
|
271
|
+
this.#append('post', path, ...callbacks);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Append middleware function(s) using the `delete()` method.
|
|
276
|
+
*
|
|
277
|
+
* @param {string | Function} path route path or middleware function
|
|
278
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
279
|
+
* @returns {void}
|
|
280
|
+
*/
|
|
281
|
+
delete(path, ...callbacks) {
|
|
282
|
+
this.#append('delete', path, ...callbacks);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Append middleware function(s) using the `put()` method.
|
|
287
|
+
*
|
|
288
|
+
* @param {string | Function} path route path or middleware function
|
|
289
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
290
|
+
* @returns {void}
|
|
291
|
+
*/
|
|
292
|
+
put(path, ...callbacks) {
|
|
293
|
+
this.#append('put', path, ...callbacks);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Append middleware function(s) using the `use()` method.
|
|
298
|
+
*
|
|
299
|
+
* @param {string | Function} path route path or middleware function
|
|
300
|
+
* @param {...Function} callbacks Additional middleware functions
|
|
301
|
+
* @returns {void}
|
|
302
|
+
*/
|
|
303
|
+
use(path, ...callbacks) {
|
|
304
|
+
this.#append('use', path, ...callbacks);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
route() {
|
|
308
|
+
throw new Error('route() method is not supported on MutableRouter. Use verb methods for now.');
|
|
309
|
+
}
|
|
310
|
+
}
|