@dwp/govuk-casa 7.0.6 → 8.0.0-alpha1

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.
Files changed (219) hide show
  1. package/README.md +22 -17
  2. package/dist/{casa → assets}/css/casa-ie8.css +1 -1
  3. package/dist/assets/css/casa.css +1 -0
  4. package/dist/casa.d.ts +10 -0
  5. package/dist/casa.js +44 -0
  6. package/dist/lib/CasaTemplateLoader.d.ts +19 -0
  7. package/dist/lib/CasaTemplateLoader.js +57 -0
  8. package/dist/lib/JourneyContext.d.ts +255 -0
  9. package/dist/lib/JourneyContext.js +681 -0
  10. package/dist/lib/MutableRouter.d.ts +155 -0
  11. package/dist/lib/MutableRouter.js +272 -0
  12. package/dist/lib/Plan.d.ts +119 -0
  13. package/dist/lib/Plan.js +382 -0
  14. package/dist/lib/ValidationError.d.ts +70 -0
  15. package/dist/lib/ValidationError.js +156 -0
  16. package/dist/lib/ValidatorFactory.d.ts +24 -0
  17. package/dist/lib/ValidatorFactory.js +87 -0
  18. package/dist/lib/configure.d.ts +205 -0
  19. package/dist/lib/configure.js +215 -0
  20. package/dist/lib/dirname.cjs +1 -0
  21. package/dist/lib/end-session.d.ts +12 -0
  22. package/dist/lib/end-session.js +24 -0
  23. package/dist/lib/field.d.ts +79 -0
  24. package/dist/lib/field.js +223 -0
  25. package/dist/lib/logger.d.ts +8 -0
  26. package/dist/lib/logger.js +19 -0
  27. package/dist/lib/nunjucks-filters.d.ts +26 -0
  28. package/dist/lib/nunjucks-filters.js +112 -0
  29. package/dist/lib/nunjucks.d.ts +22 -0
  30. package/dist/lib/nunjucks.js +49 -0
  31. package/dist/lib/utils.d.ts +22 -0
  32. package/dist/lib/utils.js +44 -0
  33. package/dist/lib/validators/dateObject.d.ts +4 -0
  34. package/dist/lib/validators/dateObject.js +135 -0
  35. package/dist/lib/validators/email.d.ts +4 -0
  36. package/dist/lib/validators/email.js +46 -0
  37. package/dist/lib/validators/inArray.d.ts +4 -0
  38. package/dist/lib/validators/inArray.js +60 -0
  39. package/dist/lib/validators/index.d.ts +21 -0
  40. package/dist/lib/validators/index.js +47 -0
  41. package/dist/lib/validators/nino.d.ts +4 -0
  42. package/dist/lib/validators/nino.js +46 -0
  43. package/dist/lib/validators/postalAddressObject.d.ts +4 -0
  44. package/dist/lib/validators/postalAddressObject.js +123 -0
  45. package/dist/lib/validators/regex.d.ts +4 -0
  46. package/dist/lib/validators/regex.js +40 -0
  47. package/dist/lib/validators/required.d.ts +4 -0
  48. package/dist/lib/validators/required.js +56 -0
  49. package/dist/lib/validators/strlen.d.ts +4 -0
  50. package/dist/lib/validators/strlen.js +51 -0
  51. package/dist/lib/validators/wordCount.d.ts +5 -0
  52. package/dist/lib/validators/wordCount.js +54 -0
  53. package/dist/lib/waypoint-url.d.ts +17 -0
  54. package/dist/lib/waypoint-url.js +46 -0
  55. package/dist/middleware/body-parser.d.ts +1 -0
  56. package/dist/middleware/body-parser.js +24 -0
  57. package/dist/middleware/csrf.d.ts +1 -0
  58. package/dist/middleware/csrf.js +31 -0
  59. package/dist/middleware/data.d.ts +6 -0
  60. package/dist/middleware/data.js +53 -0
  61. package/dist/middleware/dirname.cjs +1 -0
  62. package/dist/middleware/gather-fields.d.ts +5 -0
  63. package/dist/middleware/gather-fields.js +39 -0
  64. package/dist/middleware/i18n.d.ts +4 -0
  65. package/dist/middleware/i18n.js +87 -0
  66. package/dist/middleware/post.d.ts +1 -0
  67. package/dist/middleware/post.js +42 -0
  68. package/dist/middleware/pre.d.ts +3 -0
  69. package/dist/middleware/pre.js +38 -0
  70. package/dist/middleware/progress-journey.d.ts +6 -0
  71. package/dist/middleware/progress-journey.js +82 -0
  72. package/dist/middleware/sanitise-fields.d.ts +5 -0
  73. package/dist/middleware/sanitise-fields.js +48 -0
  74. package/dist/middleware/session.d.ts +10 -0
  75. package/dist/middleware/session.js +115 -0
  76. package/dist/middleware/skip-waypoint.d.ts +5 -0
  77. package/dist/middleware/skip-waypoint.js +40 -0
  78. package/dist/middleware/steer-journey.d.ts +6 -0
  79. package/dist/middleware/steer-journey.js +44 -0
  80. package/dist/middleware/validate-fields.d.ts +7 -0
  81. package/dist/middleware/validate-fields.js +76 -0
  82. package/dist/mjs/esm-wrapper.js +10 -0
  83. package/dist/mjs/package.json +3 -0
  84. package/dist/package.json +3 -0
  85. package/dist/routes/ancillary.d.ts +4 -0
  86. package/dist/routes/ancillary.js +19 -0
  87. package/dist/routes/dirname.cjs +1 -0
  88. package/dist/routes/journey.d.ts +8 -0
  89. package/dist/routes/journey.js +130 -0
  90. package/dist/routes/static.d.ts +26 -0
  91. package/dist/routes/static.js +67 -0
  92. package/package.json +45 -86
  93. package/views/casa/components/checkboxes/template.njk +4 -1
  94. package/views/casa/components/date-input/template.njk +3 -3
  95. package/views/casa/components/journey-form/README.md +3 -1
  96. package/views/casa/components/journey-form/template.njk +1 -1
  97. package/views/casa/components/postal-address-object/template.njk +5 -5
  98. package/views/casa/components/radios/template.njk +1 -1
  99. package/views/casa/layouts/journey.njk +26 -9
  100. package/views/casa/layouts/main.njk +6 -19
  101. package/views/casa/partials/scripts.njk +8 -3
  102. package/views/casa/partials/styles.njk +2 -2
  103. package/casa.js +0 -208
  104. package/definitions/review-page.js +0 -60
  105. package/dist/casa/css/casa.css +0 -1
  106. package/dist/casa/js/casa.js +0 -1
  107. package/index.d.ts +0 -121
  108. package/lib/ConfigIngestor.js +0 -588
  109. package/lib/GatherModifier.js +0 -14
  110. package/lib/I18n.js +0 -160
  111. package/lib/JourneyContext.d.ts +0 -97
  112. package/lib/JourneyContext.js +0 -552
  113. package/lib/JourneyMap.js +0 -233
  114. package/lib/JourneyRoad.js +0 -330
  115. package/lib/Logger.js +0 -59
  116. package/lib/PageDictionary.d.ts +0 -11
  117. package/lib/PageDirectory.js +0 -77
  118. package/lib/Plan.js +0 -423
  119. package/lib/RoadConverter.js +0 -153
  120. package/lib/UserJourney.js +0 -8
  121. package/lib/Util.js +0 -227
  122. package/lib/Validation.js +0 -20
  123. package/lib/bootstrap/end-session.js +0 -44
  124. package/lib/bootstrap/load-definitions.js +0 -64
  125. package/lib/commonBodyParser.js +0 -15
  126. package/lib/enums.js +0 -6
  127. package/lib/gather-modifiers/index.js +0 -7
  128. package/lib/gather-modifiers/trimPostalAddressObject.js +0 -75
  129. package/lib/gather-modifiers/trimWhitespace.js +0 -16
  130. package/lib/utils/createGetRequest.d.ts +0 -5
  131. package/lib/utils/createGetRequest.js +0 -59
  132. package/lib/utils/index.js +0 -11
  133. package/lib/utils/parseRequest.d.ts +0 -5
  134. package/lib/utils/parseRequest.js +0 -72
  135. package/lib/utils/sanitise.js +0 -74
  136. package/lib/utils/validate.js +0 -32
  137. package/lib/validation/ArrayObjectField.js +0 -49
  138. package/lib/validation/ObjectField.js +0 -53
  139. package/lib/validation/SimpleField.d.ts +0 -11
  140. package/lib/validation/SimpleField.js +0 -46
  141. package/lib/validation/ValidationError.d.ts +0 -14
  142. package/lib/validation/ValidationError.js +0 -170
  143. package/lib/validation/ValidatorFactory.d.ts +0 -32
  144. package/lib/validation/ValidatorFactory.js +0 -91
  145. package/lib/validation/index.js +0 -22
  146. package/lib/validation/processor/flattenErrorArray.js +0 -24
  147. package/lib/validation/processor/queue.js +0 -214
  148. package/lib/validation/processor.js +0 -84
  149. package/lib/validation/rules/README.md +0 -3
  150. package/lib/validation/rules/ValidationRules.d.ts +0 -22
  151. package/lib/validation/rules/dateObject.js +0 -156
  152. package/lib/validation/rules/email.js +0 -44
  153. package/lib/validation/rules/inArray.js +0 -61
  154. package/lib/validation/rules/index.js +0 -23
  155. package/lib/validation/rules/nino.js +0 -48
  156. package/lib/validation/rules/optional.js +0 -14
  157. package/lib/validation/rules/postalAddressObject.js +0 -142
  158. package/lib/validation/rules/regex.js +0 -39
  159. package/lib/validation/rules/required.js +0 -57
  160. package/lib/validation/rules/strlen.js +0 -57
  161. package/lib/validation/rules/wordCount.js +0 -61
  162. package/lib/view-filters/formatDateObject.js +0 -35
  163. package/lib/view-filters/includes.js +0 -10
  164. package/lib/view-filters/index.js +0 -23
  165. package/lib/view-filters/mergeObjectsDeep.js +0 -21
  166. package/lib/view-filters/renderAsAttributes.js +0 -33
  167. package/middleware/errors/404.js +0 -12
  168. package/middleware/errors/catch-all.js +0 -27
  169. package/middleware/errors/index.js +0 -9
  170. package/middleware/headers/config-defaults.js +0 -57
  171. package/middleware/headers/headers.js +0 -40
  172. package/middleware/headers/index.js +0 -9
  173. package/middleware/i18n/i18n.js +0 -56
  174. package/middleware/i18n/index.js +0 -16
  175. package/middleware/index.js +0 -55
  176. package/middleware/mount/index.js +0 -9
  177. package/middleware/mount/mount.js +0 -10
  178. package/middleware/nunjucks/environment.js +0 -57
  179. package/middleware/nunjucks/index.js +0 -8
  180. package/middleware/page/csrf.js +0 -37
  181. package/middleware/page/edit-mode.js +0 -52
  182. package/middleware/page/gather.js +0 -75
  183. package/middleware/page/index.js +0 -103
  184. package/middleware/page/journey-continue.js +0 -157
  185. package/middleware/page/journey-rails.js +0 -102
  186. package/middleware/page/prepare-request.js +0 -77
  187. package/middleware/page/render.js +0 -75
  188. package/middleware/page/skip.js +0 -72
  189. package/middleware/page/utils.js +0 -206
  190. package/middleware/page/validate.js +0 -67
  191. package/middleware/session/expiry.js +0 -95
  192. package/middleware/session/genid.js +0 -18
  193. package/middleware/session/index.js +0 -18
  194. package/middleware/session/init.js +0 -25
  195. package/middleware/session/seed.js +0 -50
  196. package/middleware/session/timeout.js +0 -5
  197. package/middleware/static/asset-versions.js +0 -23
  198. package/middleware/static/index.js +0 -104
  199. package/middleware/static/prepare-assets.js +0 -51
  200. package/middleware/static/serve-assets.js +0 -58
  201. package/middleware/variables/index.js +0 -12
  202. package/middleware/variables/variables.js +0 -35
  203. package/src/browserconfig.xml +0 -5
  204. package/src/js/casa.js +0 -132
  205. package/src/scss/_casaElements.scss +0 -11
  206. package/src/scss/_casaGovukTemplateJinjaPolyfill.scss +0 -39
  207. package/src/scss/_casaMountUrl.scss +0 -8
  208. package/src/scss/casa-ie8.scss +0 -3
  209. package/src/scss/casa.scss +0 -14
  210. package/test/unit/templates/README.md +0 -5
  211. package/test/utils/BaseTestWaypoint.js +0 -106
  212. package/test/utils/concatWaypoints.js +0 -26
  213. package/test/utils/index.js +0 -6
  214. package/test/utils/testTraversal.js +0 -90
  215. package/views/casa/partials/cookie_message.njk +0 -3
  216. package/views/casa/partials/phase_banner_alpha.njk +0 -8
  217. package/views/casa/partials/phase_banner_beta.njk +0 -8
  218. package/views/casa/review/page-block.njk +0 -8
  219. package/views/casa/review/review.njk +0 -47
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ /* eslint-disable class-methods-use-this */
7
+ // const { isPlainObject } = require('../Util.js');
8
+ const lodash_1 = __importDefault(require("lodash"));
9
+ const { isPlainObject } = lodash_1.default; // CommonJS
10
+ class ValidatorFactory {
11
+ /**
12
+ * This is a convenience method that will bind and return this class'
13
+ * `validate()` function, so you can call it directly rather than calling the
14
+ * method. i.e.
15
+ *
16
+ * MyValidator.make()('value to validate')
17
+ * versus
18
+ * (new MyValidator()).validate('value to validate')
19
+ *
20
+ * It also attaches the `sanitise()` method as a static property to that
21
+ * function.
22
+ *
23
+ * @param {object} config Validator config
24
+ * @returns {object} Validator object
25
+ * @throws {TypeError} When configurarion is invalid.
26
+ */
27
+ static make(config = {}) {
28
+ if (!isPlainObject(config)) {
29
+ throw new TypeError('Configuration must be an object');
30
+ }
31
+ const validator = Reflect.construct(this, [config]);
32
+ /* eslint-disable-next-line sonarjs/prefer-object-literal */
33
+ const instance = {};
34
+ instance.name = validator.name || validator.constructor.name;
35
+ instance.config = config;
36
+ instance.validate = validator.validate.bind(instance);
37
+ instance.sanitise = validator.sanitise.bind(instance);
38
+ Object.freeze(instance);
39
+ return instance;
40
+ }
41
+ static coerceToValidatorObject(input) {
42
+ let validator = Object.create(null);
43
+ validator.validate = () => (Promise.reject(new Error('validate() method has not been defined')));
44
+ validator.sanitise = (val) => (val);
45
+ validator.config = Object.create(null);
46
+ // An uninstantied Validator subclass
47
+ if (typeof input === 'function' && Reflect.getPrototypeOf(input) === ValidatorFactory) {
48
+ validator = input.make();
49
+ }
50
+ else if (typeof input === 'function') {
51
+ // A plain function is assumed to be just the validation logic. We do not
52
+ // bind the function to `validator` here because it may already be bound to
53
+ // another context in userland.
54
+ validator.name = input.name || input.constructor.name || 'unknown';
55
+ validator.validate = input;
56
+ }
57
+ else if (isPlainObject(input)) {
58
+ // A plain object
59
+ validator = {
60
+ validate: input.validate || validator.validate,
61
+ sanitise: input.sanitise || validator.sanitise,
62
+ config: input.config || validator.config,
63
+ name: input.name || validator.name,
64
+ };
65
+ }
66
+ else {
67
+ // An unsupported scenario
68
+ throw new TypeError(`Cannot coerce input to a validator object (typeof = ${typeof input})`);
69
+ }
70
+ return validator;
71
+ }
72
+ constructor(config = {}) {
73
+ if (new.target === ValidatorFactory) {
74
+ throw new TypeError('Cannot instantiate the abstract class, ValidatorFactory');
75
+ }
76
+ this.config = config;
77
+ }
78
+ /* eslint-disable-next-line no-unused-vars */
79
+ validate(fieldValue, context) {
80
+ throw new Error('validate() method has not been implemented');
81
+ }
82
+ /* eslint-disable-next-line no-unused-vars */
83
+ sanitise(fieldValue) {
84
+ return fieldValue;
85
+ }
86
+ }
87
+ exports.default = ValidatorFactory;
@@ -0,0 +1,205 @@
1
+ /**
2
+ *
3
+ * @typedef {Object} SessionOptions
4
+ * @property {string} [name=casasession] Session name
5
+ * @property {string} [secret=secret] Encryption secret
6
+ * @property {number} [ttl=3600] Session ttl (seconds)
7
+ * @property {boolean} [secure=false] Whether to use secure session cookies
8
+ * @property {boolean|string} [cookieSameSite=true] SameSite setting (true (Strict), false (no value), Strict, Lax, None, default true)
9
+ * @property {object} [store] Session store (default MemoryStore)
10
+ */
11
+ /**
12
+ * @typedef {Object} GlobalHook Hook configuration
13
+ * @property {string} hook Hook name in format `<router>.<hook>`
14
+ * @property {function} middleware Middleware function to insert at the hook point
15
+ * @property {string|RegExp} [path=undefined] Hook only triggered if route path matches this string/regexp (optional, default /./)
16
+ */
17
+ /**
18
+ * @typedef {Object} PageHook (extends GlobalHook)
19
+ * @property {string} hook Hook name (without a scope prefix)
20
+ * @property {function} middleware Middleware function to insert at the hook point
21
+ */
22
+ /**
23
+ * @typedef {Object} Page Page configuration. A Page is the interactive representation of a waypoint
24
+ * @property {string} waypoint The waypoint with which this page is associated
25
+ * @property {string} view Template path
26
+ * @property {PageHook[]} hooks Page-specific hooks
27
+ * @property {PageField[]} fields Fields to be managed on this page
28
+ */
29
+ /**
30
+ * @typedef {Object} ConfigureResult Result of a call to configure() function
31
+ * @property {nunjucks.Environment} nunjucksEnv Nunjucks environment to attach to other ExpressJS app instances
32
+ * @property {MutableRouter} staticRouter ExpressJS Router instance which serves all static assets
33
+ * @property {MutableRouter} ancillaryRouter ExpressJS app instance which serves all ancillary routes
34
+ * @property {MutableRouter} journeyRouter ExpressJS app instance which serves page routes for each waypoint in a user's journey
35
+ * @property {express.RequestHandler[]} preMiddleware Middleware mounted before anything else
36
+ * @property {express.RequestHandler[]} postMiddleware Middleware mounted after everything else
37
+ * @property {express.RequestHandler[]} csrfMiddleware Collection of middleware useful for pages containing forms
38
+ * @property {express.RequestHandler} sessionMiddleware Session middleware
39
+ * @property {express.RequestHandler[]} cookieParserMiddleware Cookie-parsing middleware (configured to match that of session middleware)
40
+ * @property {express.RequestHandler[]} i18nMiddleware I18n preparation middleware
41
+ * @property {express.RequestHandler} bodyParserMiddleware Body parsing middleware
42
+ * @property {express.RequestHandler[]} csrfMiddleware CSRF middleware
43
+ * @property {function} mount Function used to mount all CASA artifacts onto an ExpressJS app instance
44
+ */
45
+ /**
46
+ * Configure some middleware for use in creating a new CASA app.
47
+ *
48
+ * `mountUrl` is used to ensure the CSS content uses the correct reference to
49
+ * static assets in the `govuk-frontend` module.
50
+ *
51
+ * @param {Object} config Configuration options
52
+ * @param {string} [config.mountUrl=/] URL path used to serve static assets from and generate links (optional, default /)
53
+ * @param {string} [config.serviceName=common.serviceName] Service name (i18n key) to display in header (optional, default common:serviceName)
54
+ * @param {string[]} [config.views=[]] Array of directories in which Nunjucks templates will be searched for (optional, default [])
55
+ * @param {SessionOptions} [config.session] Session configuration
56
+ * @param {Page[]} [config.pages=[]] Pages the represent waypoints in the Plan (optional, default [])
57
+ * @param {GlobalHook[]} [config.hooks=[]] Hooks to apply (optional, default [])
58
+ * @param {Object[]} [config.plugins=[]] Plugins(optional, default [])
59
+ * @param {I18nOptions[]} [config.i18n] I18n configuration
60
+ * @param {Plan} config.plan CASA Plan
61
+ * @param {ContextEvent[]} [config.events=[]] Handlers for JourneyContext events
62
+ * @returns {ConfigureResult} Result
63
+ */
64
+ export default function configure(config?: {
65
+ mountUrl?: string | undefined;
66
+ serviceName?: string | undefined;
67
+ views?: string[] | undefined;
68
+ session?: SessionOptions | undefined;
69
+ pages?: Page[] | undefined;
70
+ hooks?: GlobalHook[] | undefined;
71
+ plugins?: Object[] | undefined;
72
+ i18n?: any[] | undefined;
73
+ plan: any;
74
+ events?: any[] | undefined;
75
+ }): ConfigureResult;
76
+ export type SessionOptions = {
77
+ /**
78
+ * Session name
79
+ */
80
+ name?: string | undefined;
81
+ /**
82
+ * Encryption secret
83
+ */
84
+ secret?: string | undefined;
85
+ /**
86
+ * Session ttl (seconds)
87
+ */
88
+ ttl?: number | undefined;
89
+ /**
90
+ * Whether to use secure session cookies
91
+ */
92
+ secure?: boolean | undefined;
93
+ /**
94
+ * SameSite setting (true (Strict), false (no value), Strict, Lax, None, default true)
95
+ */
96
+ cookieSameSite?: string | boolean | undefined;
97
+ /**
98
+ * Session store (default MemoryStore)
99
+ */
100
+ store?: object | undefined;
101
+ };
102
+ /**
103
+ * Hook configuration
104
+ */
105
+ export type GlobalHook = {
106
+ /**
107
+ * Hook name in format `<router>.<hook>`
108
+ */
109
+ hook: string;
110
+ /**
111
+ * Middleware function to insert at the hook point
112
+ */
113
+ middleware: Function;
114
+ /**
115
+ * Hook only triggered if route path matches this string/regexp (optional, default /./)
116
+ */
117
+ path?: string | RegExp | undefined;
118
+ };
119
+ /**
120
+ * (extends GlobalHook)
121
+ */
122
+ export type PageHook = {
123
+ /**
124
+ * Hook name (without a scope prefix)
125
+ */
126
+ hook: string;
127
+ /**
128
+ * Middleware function to insert at the hook point
129
+ */
130
+ middleware: Function;
131
+ };
132
+ /**
133
+ * Page configuration. A Page is the interactive representation of a waypoint
134
+ */
135
+ export type Page = {
136
+ /**
137
+ * The waypoint with which this page is associated
138
+ */
139
+ waypoint: string;
140
+ /**
141
+ * Template path
142
+ */
143
+ view: string;
144
+ /**
145
+ * Page-specific hooks
146
+ */
147
+ hooks: PageHook[];
148
+ /**
149
+ * Fields to be managed on this page
150
+ */
151
+ fields: any[];
152
+ };
153
+ /**
154
+ * Result of a call to configure() function
155
+ */
156
+ export type ConfigureResult = {
157
+ /**
158
+ * Nunjucks environment to attach to other ExpressJS app instances
159
+ */
160
+ nunjucksEnv: any;
161
+ /**
162
+ * ExpressJS Router instance which serves all static assets
163
+ */
164
+ staticRouter: any;
165
+ /**
166
+ * ExpressJS app instance which serves all ancillary routes
167
+ */
168
+ ancillaryRouter: any;
169
+ /**
170
+ * ExpressJS app instance which serves page routes for each waypoint in a user's journey
171
+ */
172
+ journeyRouter: any;
173
+ /**
174
+ * Middleware mounted before anything else
175
+ */
176
+ preMiddleware: any[];
177
+ /**
178
+ * Middleware mounted after everything else
179
+ */
180
+ postMiddleware: any[];
181
+ /**
182
+ * Collection of middleware useful for pages containing forms
183
+ */
184
+ csrfMiddleware: any[];
185
+ /**
186
+ * Session middleware
187
+ */
188
+ sessionMiddleware: any;
189
+ /**
190
+ * Cookie-parsing middleware (configured to match that of session middleware)
191
+ */
192
+ cookieParserMiddleware: any[];
193
+ /**
194
+ * I18n preparation middleware
195
+ */
196
+ i18nMiddleware: any[];
197
+ /**
198
+ * Body parsing middleware
199
+ */
200
+ bodyParserMiddleware: any;
201
+ /**
202
+ * Function used to mount all CASA artifacts onto an ExpressJS app instance
203
+ */
204
+ mount: Function;
205
+ };
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const dirname_cjs_1 = __importDefault(require("./dirname.cjs"));
7
+ const express_session_1 = require("express-session");
8
+ const path_1 = require("path");
9
+ const module_1 = require("module");
10
+ const nunjucks_js_1 = __importDefault(require("./nunjucks.js"));
11
+ const static_js_1 = __importDefault(require("../routes/static.js"));
12
+ const ancillary_js_1 = __importDefault(require("../routes/ancillary.js"));
13
+ const journey_js_1 = __importDefault(require("../routes/journey.js"));
14
+ const pre_js_1 = __importDefault(require("../middleware/pre.js"));
15
+ const post_js_1 = __importDefault(require("../middleware/post.js"));
16
+ const session_js_1 = __importDefault(require("../middleware/session.js"));
17
+ const cookie_parser_1 = __importDefault(require("cookie-parser"));
18
+ const i18n_js_1 = __importDefault(require("../middleware/i18n.js"));
19
+ const data_js_1 = __importDefault(require("../middleware/data.js"));
20
+ const body_parser_js_1 = __importDefault(require("../middleware/body-parser.js"));
21
+ const csrf_js_1 = __importDefault(require("../middleware/csrf.js"));
22
+ /**
23
+ *
24
+ * @typedef {Object} SessionOptions
25
+ * @property {string} [name=casasession] Session name
26
+ * @property {string} [secret=secret] Encryption secret
27
+ * @property {number} [ttl=3600] Session ttl (seconds)
28
+ * @property {boolean} [secure=false] Whether to use secure session cookies
29
+ * @property {boolean|string} [cookieSameSite=true] SameSite setting (true (Strict), false (no value), Strict, Lax, None, default true)
30
+ * @property {object} [store] Session store (default MemoryStore)
31
+ */
32
+ /**
33
+ * @typedef {Object} GlobalHook Hook configuration
34
+ * @property {string} hook Hook name in format `<router>.<hook>`
35
+ * @property {function} middleware Middleware function to insert at the hook point
36
+ * @property {string|RegExp} [path=undefined] Hook only triggered if route path matches this string/regexp (optional, default /./)
37
+ */
38
+ /**
39
+ * @typedef {Object} PageHook (extends GlobalHook)
40
+ * @property {string} hook Hook name (without a scope prefix)
41
+ * @property {function} middleware Middleware function to insert at the hook point
42
+ */
43
+ /**
44
+ * @typedef {Object} Page Page configuration. A Page is the interactive representation of a waypoint
45
+ * @property {string} waypoint The waypoint with which this page is associated
46
+ * @property {string} view Template path
47
+ * @property {PageHook[]} hooks Page-specific hooks
48
+ * @property {PageField[]} fields Fields to be managed on this page
49
+ */
50
+ /**
51
+ * @typedef {Object} ConfigureResult Result of a call to configure() function
52
+ * @property {nunjucks.Environment} nunjucksEnv Nunjucks environment to attach to other ExpressJS app instances
53
+ * @property {MutableRouter} staticRouter ExpressJS Router instance which serves all static assets
54
+ * @property {MutableRouter} ancillaryRouter ExpressJS app instance which serves all ancillary routes
55
+ * @property {MutableRouter} journeyRouter ExpressJS app instance which serves page routes for each waypoint in a user's journey
56
+ * @property {express.RequestHandler[]} preMiddleware Middleware mounted before anything else
57
+ * @property {express.RequestHandler[]} postMiddleware Middleware mounted after everything else
58
+ * @property {express.RequestHandler[]} csrfMiddleware Collection of middleware useful for pages containing forms
59
+ * @property {express.RequestHandler} sessionMiddleware Session middleware
60
+ * @property {express.RequestHandler[]} cookieParserMiddleware Cookie-parsing middleware (configured to match that of session middleware)
61
+ * @property {express.RequestHandler[]} i18nMiddleware I18n preparation middleware
62
+ * @property {express.RequestHandler} bodyParserMiddleware Body parsing middleware
63
+ * @property {express.RequestHandler[]} csrfMiddleware CSRF middleware
64
+ * @property {function} mount Function used to mount all CASA artifacts onto an ExpressJS app instance
65
+ */
66
+ /**
67
+ * Configure some middleware for use in creating a new CASA app.
68
+ *
69
+ * `mountUrl` is used to ensure the CSS content uses the correct reference to
70
+ * static assets in the `govuk-frontend` module.
71
+ *
72
+ * @param {Object} config Configuration options
73
+ * @param {string} [config.mountUrl=/] URL path used to serve static assets from and generate links (optional, default /)
74
+ * @param {string} [config.serviceName=common.serviceName] Service name (i18n key) to display in header (optional, default common:serviceName)
75
+ * @param {string[]} [config.views=[]] Array of directories in which Nunjucks templates will be searched for (optional, default [])
76
+ * @param {SessionOptions} [config.session] Session configuration
77
+ * @param {Page[]} [config.pages=[]] Pages the represent waypoints in the Plan (optional, default [])
78
+ * @param {GlobalHook[]} [config.hooks=[]] Hooks to apply (optional, default [])
79
+ * @param {Object[]} [config.plugins=[]] Plugins(optional, default [])
80
+ * @param {I18nOptions[]} [config.i18n] I18n configuration
81
+ * @param {Plan} config.plan CASA Plan
82
+ * @param {ContextEvent[]} [config.events=[]] Handlers for JourneyContext events
83
+ * @returns {ConfigureResult} Result
84
+ */
85
+ function configure(config = {}) {
86
+ var _a, _b;
87
+ // Pass the raw config through each plugin's configure phase so they can
88
+ // optionally modify it
89
+ ((_a = config.plugins) !== null && _a !== void 0 ? _a : []).forEach((plugin) => {
90
+ plugin.configure(config);
91
+ });
92
+ // Extract config
93
+ // TODO: Validate/sanitise and deep-freeze object
94
+ const { mountUrl = '/', serviceName = 'common:serviceName', views = [], session = {
95
+ secret: 'secret',
96
+ name: 'casasession',
97
+ secure: false,
98
+ ttl: 60 * 60,
99
+ cookieSameSite: true,
100
+ store: undefined,
101
+ }, pages = [], plan = null, hooks = [], plugins = [], events = [], i18n = {
102
+ dirs: [],
103
+ locales: ['en', 'cy'],
104
+ }, } = config;
105
+ // Prepare a Nunjucks environment for rendering all templates.
106
+ // Resolve priority: userland templates > CASA templates > GOVUK templates > Plugin templates
107
+ const nunjucksEnv = (0, nunjucks_js_1.default)({
108
+ mountUrl,
109
+ views: [
110
+ ...views,
111
+ (0, path_1.resolve)(dirname_cjs_1.default, '../../views'),
112
+ (0, path_1.resolve)((0, module_1.createRequire)(dirname_cjs_1.default).resolve('govuk-frontend'), '../../'),
113
+ ],
114
+ });
115
+ // Prepare mandatory middleware
116
+ // These _must_ be added to the ExpressJS application at the start and end
117
+ // of all other middleware respectively.
118
+ const preMiddleware = (0, pre_js_1.default)();
119
+ const postMiddleware = (0, post_js_1.default)();
120
+ // Prepare common middleware mounted prior to the ancillaryRouter
121
+ const cookieParserMiddleware = (0, cookie_parser_1.default)(session.secret);
122
+ const sessionMiddleware = (0, session_js_1.default)({
123
+ cookieParserMiddleware,
124
+ secure: session.secure,
125
+ secret: session.secret,
126
+ name: session.name,
127
+ ttl: session.ttl,
128
+ cookieSameSite: session.cookieSameSite,
129
+ mountUrl,
130
+ store: (_b = session.store) !== null && _b !== void 0 ? _b : new express_session_1.MemoryStore(),
131
+ });
132
+ const i18nMiddleware = (0, i18n_js_1.default)({
133
+ directories: [
134
+ // Order is important; latter directories take precedence
135
+ (0, path_1.resolve)(dirname_cjs_1.default, '../../locales/'),
136
+ ...i18n.dirs,
137
+ ],
138
+ languages: i18n.locales,
139
+ });
140
+ const dataMiddleware = (0, data_js_1.default)({
141
+ plan,
142
+ mountUrl,
143
+ serviceName,
144
+ events,
145
+ });
146
+ // Prepare form middleware and its constiuent parts
147
+ // These are used for any forms, including waypoint page forms.
148
+ const bodyParserMiddleware = (0, body_parser_js_1.default)();
149
+ const csrfMiddleware = (0, csrf_js_1.default)();
150
+ // Setup router to serve up bundled static assets
151
+ const staticRouter = (0, static_js_1.default)({
152
+ mountUrl,
153
+ });
154
+ // Setup ancillary router default stand-alone pages.
155
+ const ancillaryRouter = (0, ancillary_js_1.default)({
156
+ sessionTtl: session.ttl,
157
+ });
158
+ // Setup waypoint router, which includes routes for every defined waypoint
159
+ const journeyRouter = (0, journey_js_1.default)({
160
+ globalHooks: hooks,
161
+ pages,
162
+ plan,
163
+ csrfMiddleware,
164
+ mountUrl,
165
+ });
166
+ // Mount function
167
+ // This will mount all of these routes and middleware in the correct order on
168
+ // the given ExpressJS app.
169
+ // Once this is called, you will not be able to modify any of the routers as
170
+ // they will be "sealed".
171
+ const mount = (app) => {
172
+ nunjucksEnv.express(app);
173
+ app.set('view engine', 'njk');
174
+ app.use(preMiddleware);
175
+ app.use(staticRouter.seal());
176
+ app.use(sessionMiddleware); // A session is useful to all pages, so assume it is always mounted after static routes
177
+ app.use(i18nMiddleware);
178
+ app.use(bodyParserMiddleware);
179
+ app.use(dataMiddleware);
180
+ app.use(ancillaryRouter.seal());
181
+ app.use(journeyRouter.seal());
182
+ app.use(postMiddleware);
183
+ return app;
184
+ };
185
+ // Prepare configuration result
186
+ const configOutput = {
187
+ // Nunjucks environment, so it can be attached to other ExpressJS instances
188
+ // using `nunjucksEnv.express(myApp); myApp.set('view engine', 'njk');`.
189
+ nunjucksEnv,
190
+ // Mandatory middleware. User must add these to their ExpressJS app.
191
+ preMiddleware,
192
+ postMiddleware,
193
+ // Mandatory routers that consumer must mount onto their own ExpressJS parent app
194
+ staticRouter,
195
+ ancillaryRouter,
196
+ journeyRouter,
197
+ // Form middleware. Should be used wherever form pages are built.
198
+ csrfMiddleware,
199
+ // Other middleware
200
+ // These may be used by the application author to build other custom routes
201
+ cookieParserMiddleware,
202
+ sessionMiddleware,
203
+ bodyParserMiddleware,
204
+ csrfMiddleware,
205
+ i18nMiddleware,
206
+ dataMiddleware,
207
+ // Mount function
208
+ mount,
209
+ };
210
+ // Bootstrap all plugins
211
+ plugins.filter(p => p.bootstrap).forEach((plugin) => plugin === null || plugin === void 0 ? void 0 : plugin.bootstrap(configOutput));
212
+ // Finished configuration
213
+ return configOutput;
214
+ }
215
+ exports.default = configure;
@@ -0,0 +1 @@
1
+ module.exports = __dirname;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * A convenience for ending the current session, but retaining some data in it,
3
+ * like the current language.
4
+ *
5
+ * Note: this will not remove the session from server-side storage, which will
6
+ * instead be left up to the storage mechanism to clean up.
7
+ *
8
+ * @param {Object} req HTTP request
9
+ * @param {Function} next Chain
10
+ * @returns {void}
11
+ */
12
+ export default function endSession(req: Object, next: Function): void;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * A convenience for ending the current session, but retaining some data in it,
5
+ * like the current language.
6
+ *
7
+ * Note: this will not remove the session from server-side storage, which will
8
+ * instead be left up to the storage mechanism to clean up.
9
+ *
10
+ * @param {Object} req HTTP request
11
+ * @param {Function} next Chain
12
+ * @returns {void}
13
+ */
14
+ function endSession(req, next) {
15
+ const language = req.session.language;
16
+ req.session.regenerate((err) => {
17
+ if (err) {
18
+ return next(err);
19
+ }
20
+ req.session.language = language;
21
+ req.session.save(next);
22
+ });
23
+ }
24
+ exports.default = endSession;
@@ -0,0 +1,79 @@
1
+ export default function field(...args: any[]): PageField;
2
+ export class PageField {
3
+ constructor(name: any, { optional, persist }?: any);
4
+ /**
5
+ * For complex fields, we need may need to drill into an object to extract the
6
+ * value.
7
+ *
8
+ * @param {Object} obj Object from which to extract the value
9
+ * @returns {any}
10
+ */
11
+ getValue(obj?: Object): any;
12
+ putValue(obj: any, value: any): PageField;
13
+ get name(): string;
14
+ get meta(): Object;
15
+ validators(...items: any[]): PageField | Validator[];
16
+ processors(...items: any[]): Function[] | PageField;
17
+ conditions(...items: any[]): Function[] | PageField;
18
+ /**
19
+ * Run all validators and return array of errors, if applicable.
20
+ *
21
+ * @param {any} value Value to validate
22
+ * @param {ValidateContext} context Contextual information
23
+ * @returns {ValidationError[]} Errors, or an empty array if all valid
24
+ */
25
+ runValidators(value: any, context: ValidateContext): any[];
26
+ applyProcessors(value: any): any;
27
+ /**
28
+ * Apply all conditions to get the resulting boolean
29
+ *
30
+ * @param {Object} params Parameters
31
+ * @param {string} fieldValue Field value
32
+ * @param {string} waypoint Waypoint
33
+ * @param {object} journeyContext JourneyContext
34
+ * @returns {boolean}
35
+ */
36
+ testConditions({ fieldValue, waypoint, journeyContext }: Object): boolean;
37
+ validator(...args: any[]): PageField | Validator[];
38
+ processor(...args: any[]): Function[] | PageField;
39
+ condition(...args: any[]): Function[] | PageField;
40
+ if(...args: any[]): Function[] | PageField;
41
+ #private;
42
+ }
43
+ export type Validator = {
44
+ /**
45
+ * Validation function
46
+ */
47
+ validate: ValidateFunction;
48
+ /**
49
+ * Sanitise a given value to make it suitable for this validator
50
+ */
51
+ sanitise: SanitiseFunction;
52
+ /**
53
+ * Configuration
54
+ */
55
+ config: Object;
56
+ /**
57
+ * Validator name
58
+ */
59
+ name: string;
60
+ };
61
+ export type ValidateFunction = (value: any, context: ValidateContext) => any[];
62
+ export type SanitiseFunction = (value: any) => any;
63
+ /**
64
+ * Context passed to validate function
65
+ */
66
+ export type ValidateContext = {
67
+ /**
68
+ * Journey context
69
+ */
70
+ journeyContext: any;
71
+ /**
72
+ * Waypoint
73
+ */
74
+ waypoint: string;
75
+ /**
76
+ * Name of field being processed
77
+ */
78
+ fieldName: string;
79
+ };