@dwp/govuk-casa 7.0.8 → 7.0.9
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 +7 -0
- package/middleware/page/journey-continue.js +5 -0
- package/package.json +15 -15
- package/dist/assets/css/casa-ie8.css +0 -1
- package/dist/assets/css/casa.css +0 -1
- package/dist/casa.d.ts +0 -11
- package/dist/casa.js +0 -46
- package/dist/lib/CasaTemplateLoader.d.ts +0 -29
- package/dist/lib/CasaTemplateLoader.js +0 -74
- package/dist/lib/JourneyContext.d.ts +0 -297
- package/dist/lib/JourneyContext.js +0 -581
- package/dist/lib/MutableRouter.d.ts +0 -155
- package/dist/lib/MutableRouter.js +0 -277
- package/dist/lib/Plan.d.ts +0 -154
- package/dist/lib/Plan.js +0 -442
- package/dist/lib/ValidationError.d.ts +0 -74
- package/dist/lib/ValidationError.js +0 -159
- package/dist/lib/ValidatorFactory.d.ts +0 -83
- package/dist/lib/ValidatorFactory.js +0 -106
- package/dist/lib/configuration-ingestor.d.ts +0 -262
- package/dist/lib/configuration-ingestor.js +0 -490
- package/dist/lib/configure.d.ts +0 -90
- package/dist/lib/configure.js +0 -192
- package/dist/lib/dirname.cjs +0 -1
- package/dist/lib/dirname.d.cts +0 -2
- package/dist/lib/end-session.d.ts +0 -13
- package/dist/lib/end-session.js +0 -43
- package/dist/lib/field.d.ts +0 -77
- package/dist/lib/field.js +0 -255
- package/dist/lib/index.d.ts +0 -14
- package/dist/lib/index.js +0 -54
- package/dist/lib/logger.d.ts +0 -9
- package/dist/lib/logger.js +0 -18
- package/dist/lib/nunjucks-filters.d.ts +0 -26
- package/dist/lib/nunjucks-filters.js +0 -90
- package/dist/lib/nunjucks.d.ts +0 -23
- package/dist/lib/nunjucks.js +0 -49
- package/dist/lib/utils.d.ts +0 -48
- package/dist/lib/utils.js +0 -111
- package/dist/lib/validators/dateObject.d.ts +0 -4
- package/dist/lib/validators/dateObject.js +0 -135
- package/dist/lib/validators/email.d.ts +0 -4
- package/dist/lib/validators/email.js +0 -46
- package/dist/lib/validators/inArray.d.ts +0 -4
- package/dist/lib/validators/inArray.js +0 -60
- package/dist/lib/validators/index.d.ts +0 -21
- package/dist/lib/validators/index.js +0 -47
- package/dist/lib/validators/nino.d.ts +0 -4
- package/dist/lib/validators/nino.js +0 -46
- package/dist/lib/validators/postalAddressObject.d.ts +0 -4
- package/dist/lib/validators/postalAddressObject.js +0 -123
- package/dist/lib/validators/regex.d.ts +0 -4
- package/dist/lib/validators/regex.js +0 -40
- package/dist/lib/validators/required.d.ts +0 -4
- package/dist/lib/validators/required.js +0 -56
- package/dist/lib/validators/strlen.d.ts +0 -4
- package/dist/lib/validators/strlen.js +0 -51
- package/dist/lib/validators/wordCount.d.ts +0 -5
- package/dist/lib/validators/wordCount.js +0 -54
- package/dist/lib/waypoint-url.d.ts +0 -23
- package/dist/lib/waypoint-url.js +0 -52
- package/dist/middleware/body-parser.d.ts +0 -1
- package/dist/middleware/body-parser.js +0 -24
- package/dist/middleware/csrf.d.ts +0 -1
- package/dist/middleware/csrf.js +0 -31
- package/dist/middleware/data.d.ts +0 -5
- package/dist/middleware/data.js +0 -53
- package/dist/middleware/dirname.cjs +0 -1
- package/dist/middleware/dirname.d.cts +0 -2
- package/dist/middleware/gather-fields.d.ts +0 -6
- package/dist/middleware/gather-fields.js +0 -48
- package/dist/middleware/i18n.d.ts +0 -4
- package/dist/middleware/i18n.js +0 -88
- package/dist/middleware/post.d.ts +0 -3
- package/dist/middleware/post.js +0 -57
- package/dist/middleware/pre.d.ts +0 -3
- package/dist/middleware/pre.js +0 -51
- package/dist/middleware/progress-journey.d.ts +0 -6
- package/dist/middleware/progress-journey.js +0 -80
- package/dist/middleware/sanitise-fields.d.ts +0 -5
- package/dist/middleware/sanitise-fields.js +0 -53
- package/dist/middleware/session.d.ts +0 -11
- package/dist/middleware/session.js +0 -121
- package/dist/middleware/skip-waypoint.d.ts +0 -5
- package/dist/middleware/skip-waypoint.js +0 -43
- package/dist/middleware/steer-journey.d.ts +0 -7
- package/dist/middleware/steer-journey.js +0 -62
- package/dist/middleware/validate-fields.d.ts +0 -7
- package/dist/middleware/validate-fields.js +0 -67
- package/dist/mjs/esm-wrapper.js +0 -11
- package/dist/mjs/package.json +0 -3
- package/dist/package.json +0 -3
- package/dist/routes/ancillary.d.ts +0 -11
- package/dist/routes/ancillary.js +0 -27
- package/dist/routes/dirname.cjs +0 -1
- package/dist/routes/dirname.d.cts +0 -2
- package/dist/routes/journey.d.ts +0 -8
- package/dist/routes/journey.js +0 -127
- package/dist/routes/static.d.ts +0 -26
- package/dist/routes/static.js +0 -68
package/dist/lib/configure.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
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 express_session_1 = require("express-session");
|
|
7
|
-
const path_1 = require("path");
|
|
8
|
-
const module_1 = require("module");
|
|
9
|
-
const cookie_parser_1 = __importDefault(require("cookie-parser"));
|
|
10
|
-
const dirname_cjs_1 = __importDefault(require("./dirname.cjs"));
|
|
11
|
-
const configuration_ingestor_js_1 = __importDefault(require("./configuration-ingestor.js"));
|
|
12
|
-
const nunjucks_js_1 = __importDefault(require("./nunjucks.js"));
|
|
13
|
-
const static_js_1 = __importDefault(require("../routes/static.js"));
|
|
14
|
-
const ancillary_js_1 = __importDefault(require("../routes/ancillary.js"));
|
|
15
|
-
const journey_js_1 = __importDefault(require("../routes/journey.js"));
|
|
16
|
-
const pre_js_1 = __importDefault(require("../middleware/pre.js"));
|
|
17
|
-
const post_js_1 = __importDefault(require("../middleware/post.js"));
|
|
18
|
-
const session_js_1 = __importDefault(require("../middleware/session.js"));
|
|
19
|
-
const i18n_js_1 = __importDefault(require("../middleware/i18n.js"));
|
|
20
|
-
const data_js_1 = __importDefault(require("../middleware/data.js"));
|
|
21
|
-
const body_parser_js_1 = __importDefault(require("../middleware/body-parser.js"));
|
|
22
|
-
const csrf_js_1 = __importDefault(require("../middleware/csrf.js"));
|
|
23
|
-
/**
|
|
24
|
-
* @typedef {import('express').RequestHandler} ExpressRequestHandler
|
|
25
|
-
*/
|
|
26
|
-
/**
|
|
27
|
-
* @typedef {import('./index').MutableRouter} MutableRouter
|
|
28
|
-
*/
|
|
29
|
-
/**
|
|
30
|
-
* @typedef {import('./configuration-ingestor').ConfigurationOptions} ConfigurationOptions
|
|
31
|
-
*/
|
|
32
|
-
/**
|
|
33
|
-
* @typedef {object} ConfigureResult Result of a call to configure() function
|
|
34
|
-
* @property {nunjucks.Environment} nunjucksEnv Nunjucks environment
|
|
35
|
-
* @property {MutableRouter} staticRouter Router handling all static assets
|
|
36
|
-
* @property {MutableRouter} ancillaryRouter Router handling ancillary routes
|
|
37
|
-
* @property {MutableRouter} journeyRouter Router handling all waypoint requests
|
|
38
|
-
* @property {ExpressRequestHandler[]} preMiddleware Middleware mounted before anything else
|
|
39
|
-
* @property {ExpressRequestHandler[]} postMiddleware Middleware mounted after everything else
|
|
40
|
-
* @property {ExpressRequestHandler[]} csrfMiddleware CSRF get/set middleware (useful for forms)
|
|
41
|
-
* @property {ExpressRequestHandler} sessionMiddleware Session middleware
|
|
42
|
-
* @property {ExpressRequestHandler[]} cookieParserMiddleware Cookie-parsing middleware
|
|
43
|
-
* @property {ExpressRequestHandler[]} i18nMiddleware I18n preparation middleware
|
|
44
|
-
* @property {ExpressRequestHandler} bodyParserMiddleware Body parsing middleware
|
|
45
|
-
* @property {Function} mount Function used to mount all CASA artifacts onto an ExpressJS app
|
|
46
|
-
*/
|
|
47
|
-
/**
|
|
48
|
-
* Configure some middleware for use in creating a new CASA app.
|
|
49
|
-
*
|
|
50
|
-
* `mountUrl` is used to ensure the CSS content uses the correct reference to
|
|
51
|
-
* static assets in the `govuk-frontend` module.
|
|
52
|
-
*
|
|
53
|
-
* @param {ConfigurationOptions} config Configuration options
|
|
54
|
-
* @returns {ConfigureResult} Result
|
|
55
|
-
*/
|
|
56
|
-
function configure(config = {}) {
|
|
57
|
-
var _a, _b;
|
|
58
|
-
// Pass the raw config through each plugin's configure phase so they can
|
|
59
|
-
// optionally modify it
|
|
60
|
-
((_a = config.plugins) !== null && _a !== void 0 ? _a : []).forEach((plugin) => {
|
|
61
|
-
plugin.configure(config);
|
|
62
|
-
});
|
|
63
|
-
// Extract config
|
|
64
|
-
// TODO: Validate/sanitise and deep-freeze object
|
|
65
|
-
const { mountUrl = '/', views = [], session = {
|
|
66
|
-
secret: 'secret',
|
|
67
|
-
name: 'casasession',
|
|
68
|
-
secure: false,
|
|
69
|
-
ttl: 60 * 60,
|
|
70
|
-
cookieSameSite: true,
|
|
71
|
-
cookiePath: '/',
|
|
72
|
-
store: undefined,
|
|
73
|
-
}, pages = [], plan = null, hooks = [], plugins = [], events = [], i18n = {
|
|
74
|
-
dirs: [],
|
|
75
|
-
locales: ['en', 'cy'],
|
|
76
|
-
}, } = (0, configuration_ingestor_js_1.default)(config);
|
|
77
|
-
// Prepare all page hooks so they are prefixed with the `journey.` scope.
|
|
78
|
-
pages.forEach((page) => {
|
|
79
|
-
var _a;
|
|
80
|
-
/* eslint-disable-next-line no-param-reassign,no-return-assign */
|
|
81
|
-
((_a = page === null || page === void 0 ? void 0 : page.hooks) !== null && _a !== void 0 ? _a : []).forEach((h) => h.hook = `journey.${h.hook}`);
|
|
82
|
-
});
|
|
83
|
-
// Prepare a Nunjucks environment for rendering all templates.
|
|
84
|
-
// Resolve priority: userland templates > CASA templates > GOVUK templates > Plugin templates
|
|
85
|
-
const nunjucksEnv = (0, nunjucks_js_1.default)({
|
|
86
|
-
mountUrl,
|
|
87
|
-
views: [
|
|
88
|
-
...views,
|
|
89
|
-
(0, path_1.resolve)(dirname_cjs_1.default, '../../views'),
|
|
90
|
-
(0, path_1.resolve)((0, module_1.createRequire)(dirname_cjs_1.default).resolve('govuk-frontend'), '../../'),
|
|
91
|
-
],
|
|
92
|
-
});
|
|
93
|
-
// Prepare mandatory middleware
|
|
94
|
-
// These _must_ be added to the ExpressJS application at the start and end
|
|
95
|
-
// of all other middleware respectively.
|
|
96
|
-
const preMiddleware = (0, pre_js_1.default)();
|
|
97
|
-
const postMiddleware = (0, post_js_1.default)({ mountUrl });
|
|
98
|
-
// Prepare common middleware mounted prior to the ancillaryRouter
|
|
99
|
-
const cookieParserMiddleware = (0, cookie_parser_1.default)(session.secret);
|
|
100
|
-
const sessionMiddleware = (0, session_js_1.default)({
|
|
101
|
-
cookieParserMiddleware,
|
|
102
|
-
secure: session.secure,
|
|
103
|
-
secret: session.secret,
|
|
104
|
-
name: session.name,
|
|
105
|
-
ttl: session.ttl,
|
|
106
|
-
cookieSameSite: session.cookieSameSite,
|
|
107
|
-
cookiePath: session.cookiePath,
|
|
108
|
-
mountUrl,
|
|
109
|
-
store: (_b = session.store) !== null && _b !== void 0 ? _b : new express_session_1.MemoryStore(),
|
|
110
|
-
});
|
|
111
|
-
const i18nMiddleware = (0, i18n_js_1.default)({
|
|
112
|
-
directories: [
|
|
113
|
-
// Order is important; latter directories take precedence
|
|
114
|
-
(0, path_1.resolve)(dirname_cjs_1.default, '../../locales/'),
|
|
115
|
-
...i18n.dirs,
|
|
116
|
-
],
|
|
117
|
-
languages: i18n.locales,
|
|
118
|
-
});
|
|
119
|
-
const dataMiddleware = (0, data_js_1.default)({
|
|
120
|
-
plan,
|
|
121
|
-
mountUrl,
|
|
122
|
-
events,
|
|
123
|
-
});
|
|
124
|
-
// Prepare form middleware and its constiuent parts
|
|
125
|
-
// These are used for any forms, including waypoint page forms.
|
|
126
|
-
const bodyParserMiddleware = (0, body_parser_js_1.default)();
|
|
127
|
-
const csrfMiddleware = (0, csrf_js_1.default)();
|
|
128
|
-
// Setup router to serve up bundled static assets
|
|
129
|
-
const staticRouter = (0, static_js_1.default)({
|
|
130
|
-
mountUrl,
|
|
131
|
-
});
|
|
132
|
-
// Setup ancillary router default stand-alone pages.
|
|
133
|
-
const ancillaryRouter = (0, ancillary_js_1.default)({
|
|
134
|
-
sessionTtl: session.ttl,
|
|
135
|
-
});
|
|
136
|
-
// Setup waypoint router, which includes routes for every defined waypoint
|
|
137
|
-
const journeyRouter = (0, journey_js_1.default)({
|
|
138
|
-
globalHooks: hooks,
|
|
139
|
-
pages,
|
|
140
|
-
plan,
|
|
141
|
-
csrfMiddleware,
|
|
142
|
-
mountUrl,
|
|
143
|
-
});
|
|
144
|
-
// Mount function
|
|
145
|
-
// This will mount all of these routes and middleware in the correct order on
|
|
146
|
-
// the given ExpressJS app.
|
|
147
|
-
// Once this is called, you will not be able to modify any of the routers as
|
|
148
|
-
// they will be "sealed".
|
|
149
|
-
const mount = (app) => {
|
|
150
|
-
nunjucksEnv.express(app);
|
|
151
|
-
app.set('view engine', 'njk');
|
|
152
|
-
app.use(preMiddleware);
|
|
153
|
-
app.use(staticRouter.seal());
|
|
154
|
-
app.use(sessionMiddleware); // A session is useful to all pages, so always mounted
|
|
155
|
-
app.use(i18nMiddleware);
|
|
156
|
-
app.use(bodyParserMiddleware);
|
|
157
|
-
app.use(dataMiddleware);
|
|
158
|
-
app.use(ancillaryRouter.seal());
|
|
159
|
-
app.use(journeyRouter.seal());
|
|
160
|
-
app.use(postMiddleware);
|
|
161
|
-
return app;
|
|
162
|
-
};
|
|
163
|
-
// Prepare configuration result
|
|
164
|
-
const configOutput = {
|
|
165
|
-
// Nunjucks environment, so it can be attached to other ExpressJS instances
|
|
166
|
-
// using `nunjucksEnv.express(myApp); myApp.set('view engine', 'njk');`.
|
|
167
|
-
nunjucksEnv,
|
|
168
|
-
// Mandatory middleware. User must add these to their ExpressJS app.
|
|
169
|
-
preMiddleware,
|
|
170
|
-
postMiddleware,
|
|
171
|
-
// Mandatory routers that consumer must mount onto their own ExpressJS parent app
|
|
172
|
-
staticRouter,
|
|
173
|
-
ancillaryRouter,
|
|
174
|
-
journeyRouter,
|
|
175
|
-
// CSRF middleware. Should be used wherever form pages are built.
|
|
176
|
-
csrfMiddleware,
|
|
177
|
-
// Other middleware
|
|
178
|
-
// These may be used by the application author to build other custom routes
|
|
179
|
-
cookieParserMiddleware,
|
|
180
|
-
sessionMiddleware,
|
|
181
|
-
bodyParserMiddleware,
|
|
182
|
-
i18nMiddleware,
|
|
183
|
-
dataMiddleware,
|
|
184
|
-
// Mount function
|
|
185
|
-
mount,
|
|
186
|
-
};
|
|
187
|
-
// Bootstrap all plugins
|
|
188
|
-
plugins.filter((p) => p.bootstrap).forEach((plugin) => plugin === null || plugin === void 0 ? void 0 : plugin.bootstrap(configOutput));
|
|
189
|
-
// Finished configuration
|
|
190
|
-
return configOutput;
|
|
191
|
-
}
|
|
192
|
-
exports.default = configure;
|
package/dist/lib/dirname.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = __dirname;
|
package/dist/lib/dirname.d.cts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A convenience for ending the current session, but retaining some data in it,
|
|
3
|
-
* like the current language. It persists an empty session before regenerating
|
|
4
|
-
* a new ID.
|
|
5
|
-
*
|
|
6
|
-
* Note: this will not remove the session from server-side storage, which will
|
|
7
|
-
* instead be left up to the storage mechanism to clean up.
|
|
8
|
-
*
|
|
9
|
-
* @param {object} req HTTP request
|
|
10
|
-
* @param {Function} next Chain
|
|
11
|
-
* @returns {void}
|
|
12
|
-
*/
|
|
13
|
-
export default function endSession(req: object, next: Function): void;
|
package/dist/lib/end-session.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
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 logger_js_1 = __importDefault(require("./logger.js"));
|
|
7
|
-
const log = (0, logger_js_1.default)('function:end-session');
|
|
8
|
-
/**
|
|
9
|
-
* A convenience for ending the current session, but retaining some data in it,
|
|
10
|
-
* like the current language. It persists an empty session before regenerating
|
|
11
|
-
* a new ID.
|
|
12
|
-
*
|
|
13
|
-
* Note: this will not remove the session from server-side storage, which will
|
|
14
|
-
* instead be left up to the storage mechanism to clean up.
|
|
15
|
-
*
|
|
16
|
-
* @param {object} req HTTP request
|
|
17
|
-
* @param {Function} next Chain
|
|
18
|
-
* @returns {void}
|
|
19
|
-
*/
|
|
20
|
-
function endSession(req, next) {
|
|
21
|
-
const { language } = req.session;
|
|
22
|
-
Object.entries(req.session).forEach(([k]) => {
|
|
23
|
-
if (!['cookie'].includes(k)) {
|
|
24
|
-
req.session[k] = null;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
req.session.save((saveErr) => {
|
|
28
|
-
if (saveErr) {
|
|
29
|
-
log.error(saveErr);
|
|
30
|
-
}
|
|
31
|
-
req.session.regenerate((err) => {
|
|
32
|
-
if (err) {
|
|
33
|
-
log.error(err);
|
|
34
|
-
next(err);
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
req.session.language = language;
|
|
38
|
-
req.session.save(next);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
exports.default = endSession;
|
package/dist/lib/field.d.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
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} Value extracted from object
|
|
10
|
-
* @throws {Error} When run on a complex field
|
|
11
|
-
*/
|
|
12
|
-
getValue(obj?: object): any;
|
|
13
|
-
putValue(obj: any, value: any): PageField;
|
|
14
|
-
get name(): string;
|
|
15
|
-
get meta(): object;
|
|
16
|
-
/**
|
|
17
|
-
* Add/get value validators
|
|
18
|
-
* Some validators will include a `sanitise()` method which will be run at the
|
|
19
|
-
* same time as other "processors".
|
|
20
|
-
*
|
|
21
|
-
* @param {ValidateFunction[]} items Validation functions
|
|
22
|
-
* @returns {PageField | ValidateFunction[]} Chain or return all validators
|
|
23
|
-
*/
|
|
24
|
-
validators(items?: ValidateFunction[]): PageField | ValidateFunction[];
|
|
25
|
-
/**
|
|
26
|
-
* Add/get value pre-processors
|
|
27
|
-
* This is most often used to sanitise values to a particular data type.
|
|
28
|
-
*
|
|
29
|
-
* @param {ProcessorFunction[]} items Processor functions
|
|
30
|
-
* @returns {PageField | ProcessorFunction[]} Chain or return all processors
|
|
31
|
-
*/
|
|
32
|
-
processors(items?: ProcessorFunction[]): PageField | ProcessorFunction[];
|
|
33
|
-
/**
|
|
34
|
-
* Add/get conditions
|
|
35
|
-
* All conditions must be met in order for this field to be considered
|
|
36
|
-
* "actionable".
|
|
37
|
-
*
|
|
38
|
-
* @param {ConditionFunction[]} items Condition functions
|
|
39
|
-
* @returns {PageField | ConditionFunction[]} Chain or return all conditions
|
|
40
|
-
*/
|
|
41
|
-
conditions(items?: ConditionFunction[]): PageField | ConditionFunction[];
|
|
42
|
-
/**
|
|
43
|
-
* Run all validators and return array of errors, if applicable.
|
|
44
|
-
*
|
|
45
|
-
* @param {any} value Value to validate
|
|
46
|
-
* @param {ValidateContext} context Contextual information
|
|
47
|
-
* @returns {ValidationError[]} Errors, or an empty array if all valid
|
|
48
|
-
*/
|
|
49
|
-
runValidators(value: any, context: any): ValidationError[];
|
|
50
|
-
applyProcessors(value: any): any;
|
|
51
|
-
/**
|
|
52
|
-
* Apply all conditions to get the resulting boolean
|
|
53
|
-
*
|
|
54
|
-
* @param {object} params Parameters
|
|
55
|
-
* @param {string} params.fieldValue Field value
|
|
56
|
-
* @param {string} params.waypoint Waypoint
|
|
57
|
-
* @param {object} params.journeyContext JourneyContext
|
|
58
|
-
* @returns {boolean} True if all conditions pass
|
|
59
|
-
*/
|
|
60
|
-
testConditions({ fieldValue, waypoint, journeyContext }: {
|
|
61
|
-
fieldValue: string;
|
|
62
|
-
waypoint: string;
|
|
63
|
-
journeyContext: object;
|
|
64
|
-
}): boolean;
|
|
65
|
-
validator(validator: any): any[] | PageField;
|
|
66
|
-
processor(processor: any): PageField | ProcessorFunction[];
|
|
67
|
-
condition(condition: any): PageField | ConditionFunction[];
|
|
68
|
-
if(...args: any[]): PageField | ConditionFunction[];
|
|
69
|
-
#private;
|
|
70
|
-
}
|
|
71
|
-
export type JourneyContext = import('./index').JourneyContext;
|
|
72
|
-
export type Validator = import('./index').Validator;
|
|
73
|
-
export type ValidateFunction = import('./index').ValidateFunction;
|
|
74
|
-
export type ValidateContext = import('./index').ValidateContext;
|
|
75
|
-
export type ValidationError = import('./index').ValidationError;
|
|
76
|
-
export type ProcessorFunction = (value: any) => any;
|
|
77
|
-
export type ConditionFunction = (context: Object, fieldName: string, fieldValue: any, waypoint: string, waypointId: string, journeyContext: JourneyContext) => boolean;
|
package/dist/lib/field.js
DELETED
|
@@ -1,255 +0,0 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
|
-
var _PageField_name, _PageField_processors, _PageField_validators, _PageField_conditions, _PageField_meta;
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.PageField = void 0;
|
|
19
|
-
const lodash_1 = __importDefault(require("lodash"));
|
|
20
|
-
const utils_js_1 = require("./utils.js");
|
|
21
|
-
const { isFunction } = lodash_1.default;
|
|
22
|
-
/**
|
|
23
|
-
* @typedef {import('./index').JourneyContext} JourneyContext
|
|
24
|
-
*/
|
|
25
|
-
/**
|
|
26
|
-
* @typedef {import('./index').Validator} Validator
|
|
27
|
-
*/
|
|
28
|
-
/**
|
|
29
|
-
* @typedef {import('./index').ValidateFunction} ValidateFunction
|
|
30
|
-
*/
|
|
31
|
-
/**
|
|
32
|
-
* @typedef {import('./index').ValidateContext} ValidateContext
|
|
33
|
-
*/
|
|
34
|
-
/**
|
|
35
|
-
* @typedef {import('./index').ValidationError} ValidationError
|
|
36
|
-
*/
|
|
37
|
-
/**
|
|
38
|
-
* @callback ProcessorFunction
|
|
39
|
-
* @param {any} value Value to be processed
|
|
40
|
-
* @returns {any}
|
|
41
|
-
*/
|
|
42
|
-
/**
|
|
43
|
-
* @callback ConditionFunction
|
|
44
|
-
* @param {Object} context Value to be processed
|
|
45
|
-
* @param {string} context.fieldName Field name
|
|
46
|
-
* @param {any} context.fieldValue Field value
|
|
47
|
-
* @param {string} context.waypoint Waypoint
|
|
48
|
-
* @param {string} context.waypointId [DEPRECATED] Waypoint (for backwards compatibility with v7)
|
|
49
|
-
* @param {JourneyContext} journeyContext Journey Context
|
|
50
|
-
* @returns {boolean} True if the validators should be run
|
|
51
|
-
*/
|
|
52
|
-
// Quick check to see if the field name corresponds to a non-primitive complex
|
|
53
|
-
// type. For example, `my_field[nested]`.
|
|
54
|
-
const reComplexType = /\[/;
|
|
55
|
-
const reInvalidName = /[^a-z0-9_.\-[\]]/i;
|
|
56
|
-
// This is never exposed via a public API, and instead users are encouraged to
|
|
57
|
-
// use the `field()` factory instead
|
|
58
|
-
class PageField {
|
|
59
|
-
constructor(name, { optional = false, persist = true } = Object.create(null)) {
|
|
60
|
-
/*
|
|
61
|
-
* @type {string}
|
|
62
|
-
*/
|
|
63
|
-
_PageField_name.set(this, void 0);
|
|
64
|
-
/**
|
|
65
|
-
* @type {ProcessorFunction[]}
|
|
66
|
-
*/
|
|
67
|
-
_PageField_processors.set(this, void 0);
|
|
68
|
-
/**
|
|
69
|
-
* @type {ValidateFunction[]}
|
|
70
|
-
*/
|
|
71
|
-
_PageField_validators.set(this, void 0);
|
|
72
|
-
/**
|
|
73
|
-
* @type {ConditionFunction[]}
|
|
74
|
-
*/
|
|
75
|
-
_PageField_conditions.set(this, void 0);
|
|
76
|
-
/**
|
|
77
|
-
* @type {object}
|
|
78
|
-
*/
|
|
79
|
-
_PageField_meta.set(this, void 0);
|
|
80
|
-
if (!name) {
|
|
81
|
-
throw new SyntaxError('A name for this field is required, i.e. "field(\'myField\')".');
|
|
82
|
-
}
|
|
83
|
-
else if (reInvalidName.test(String(name))) {
|
|
84
|
-
throw new SyntaxError(`Field '${String(name)}' name contains invalid characters.`);
|
|
85
|
-
}
|
|
86
|
-
__classPrivateFieldSet(this, _PageField_name, String(name), "f");
|
|
87
|
-
__classPrivateFieldSet(this, _PageField_validators, [], "f");
|
|
88
|
-
__classPrivateFieldSet(this, _PageField_processors, [], "f");
|
|
89
|
-
__classPrivateFieldSet(this, _PageField_conditions, [], "f");
|
|
90
|
-
__classPrivateFieldSet(this, _PageField_meta, {
|
|
91
|
-
optional,
|
|
92
|
-
persist,
|
|
93
|
-
complex: reComplexType.test(name),
|
|
94
|
-
}, "f");
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* For complex fields, we need may need to drill into an object to extract the
|
|
98
|
-
* value.
|
|
99
|
-
*
|
|
100
|
-
* @param {object} obj Object from which to extract the value
|
|
101
|
-
* @returns {any} Value extracted from object
|
|
102
|
-
* @throws {Error} When run on a complex field
|
|
103
|
-
*/
|
|
104
|
-
getValue(obj = Object.create(null)) {
|
|
105
|
-
if (!__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
|
|
106
|
-
return obj[__classPrivateFieldGet(this, _PageField_name, "f")];
|
|
107
|
-
}
|
|
108
|
-
throw new Error('Not yet supporting complex field types');
|
|
109
|
-
}
|
|
110
|
-
putValue(obj = Object.create(null), value) {
|
|
111
|
-
if (!__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
|
|
112
|
-
/* eslint-disable-next-line no-param-reassign */
|
|
113
|
-
obj[__classPrivateFieldGet(this, _PageField_name, "f")] = value;
|
|
114
|
-
return this;
|
|
115
|
-
}
|
|
116
|
-
throw new Error('Not yet supporting complex field types');
|
|
117
|
-
}
|
|
118
|
-
/* -------------------------------------------------------------- configure */
|
|
119
|
-
get name() {
|
|
120
|
-
return __classPrivateFieldGet(this, _PageField_name, "f");
|
|
121
|
-
}
|
|
122
|
-
get meta() {
|
|
123
|
-
return __classPrivateFieldGet(this, _PageField_meta, "f");
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Add/get value validators
|
|
127
|
-
* Some validators will include a `sanitise()` method which will be run at the
|
|
128
|
-
* same time as other "processors".
|
|
129
|
-
*
|
|
130
|
-
* @param {ValidateFunction[]} items Validation functions
|
|
131
|
-
* @returns {PageField | ValidateFunction[]} Chain or return all validators
|
|
132
|
-
*/
|
|
133
|
-
validators(items = []) {
|
|
134
|
-
if (!items.length) {
|
|
135
|
-
return __classPrivateFieldGet(this, _PageField_validators, "f");
|
|
136
|
-
}
|
|
137
|
-
__classPrivateFieldSet(this, _PageField_validators, [...__classPrivateFieldGet(this, _PageField_validators, "f"), ...(items.flat())], "f");
|
|
138
|
-
return this;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Add/get value pre-processors
|
|
142
|
-
* This is most often used to sanitise values to a particular data type.
|
|
143
|
-
*
|
|
144
|
-
* @param {ProcessorFunction[]} items Processor functions
|
|
145
|
-
* @returns {PageField | ProcessorFunction[]} Chain or return all processors
|
|
146
|
-
*/
|
|
147
|
-
processors(items = []) {
|
|
148
|
-
if (!items.length) {
|
|
149
|
-
return __classPrivateFieldGet(this, _PageField_processors, "f");
|
|
150
|
-
}
|
|
151
|
-
__classPrivateFieldSet(this, _PageField_processors, [...__classPrivateFieldGet(this, _PageField_processors, "f"), ...(items.flat())], "f");
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Add/get conditions
|
|
156
|
-
* All conditions must be met in order for this field to be considered
|
|
157
|
-
* "actionable".
|
|
158
|
-
*
|
|
159
|
-
* @param {ConditionFunction[]} items Condition functions
|
|
160
|
-
* @returns {PageField | ConditionFunction[]} Chain or return all conditions
|
|
161
|
-
*/
|
|
162
|
-
conditions(items = []) {
|
|
163
|
-
if (!items.length) {
|
|
164
|
-
return __classPrivateFieldGet(this, _PageField_conditions, "f");
|
|
165
|
-
}
|
|
166
|
-
__classPrivateFieldSet(this, _PageField_conditions, [...__classPrivateFieldGet(this, _PageField_conditions, "f"), ...(items.flat())], "f");
|
|
167
|
-
return this;
|
|
168
|
-
}
|
|
169
|
-
/* ---------------------------------------------------------------- execute */
|
|
170
|
-
/**
|
|
171
|
-
* Run all validators and return array of errors, if applicable.
|
|
172
|
-
*
|
|
173
|
-
* @param {any} value Value to validate
|
|
174
|
-
* @param {ValidateContext} context Contextual information
|
|
175
|
-
* @returns {ValidationError[]} Errors, or an empty array if all valid
|
|
176
|
-
*/
|
|
177
|
-
runValidators(value, context) {
|
|
178
|
-
// Skip validation if the field is empty and optional
|
|
179
|
-
if (__classPrivateFieldGet(this, _PageField_meta, "f").optional && (0, utils_js_1.isEmpty)(value)) {
|
|
180
|
-
return [];
|
|
181
|
-
}
|
|
182
|
-
let errors = [];
|
|
183
|
-
for (let i = 0, l = __classPrivateFieldGet(this, _PageField_validators, "f").length; i < l; i++) {
|
|
184
|
-
const fieldErrors = __classPrivateFieldGet(this, _PageField_validators, "f")[i].validate(value, context).map((e) => e.withContext(Object.assign(Object.assign({}, context), { validator: __classPrivateFieldGet(this, _PageField_validators, "f")[i].name })));
|
|
185
|
-
errors = [
|
|
186
|
-
...errors,
|
|
187
|
-
...(fieldErrors !== null && fieldErrors !== void 0 ? fieldErrors : []),
|
|
188
|
-
];
|
|
189
|
-
}
|
|
190
|
-
return errors;
|
|
191
|
-
}
|
|
192
|
-
/*
|
|
193
|
-
* Apply all the processors to the given value.
|
|
194
|
-
*
|
|
195
|
-
* @param {any} value Value to process
|
|
196
|
-
* @returns {any} Processed value
|
|
197
|
-
*/
|
|
198
|
-
applyProcessors(value) {
|
|
199
|
-
let processedValue = value;
|
|
200
|
-
// Some of the validators may have their own "sanitise()" methods. These
|
|
201
|
-
// should be run before any other processors
|
|
202
|
-
for (let i = 0, l = __classPrivateFieldGet(this, _PageField_validators, "f").length; i < l; i++) {
|
|
203
|
-
if (isFunction(__classPrivateFieldGet(this, _PageField_validators, "f")[i].sanitise)) {
|
|
204
|
-
processedValue = __classPrivateFieldGet(this, _PageField_validators, "f")[i].sanitise(processedValue);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
for (let i = 0, l = __classPrivateFieldGet(this, _PageField_processors, "f").length; i < l; i++) {
|
|
208
|
-
processedValue = __classPrivateFieldGet(this, _PageField_processors, "f")[i](processedValue);
|
|
209
|
-
}
|
|
210
|
-
return processedValue;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Apply all conditions to get the resulting boolean
|
|
214
|
-
*
|
|
215
|
-
* @param {object} params Parameters
|
|
216
|
-
* @param {string} params.fieldValue Field value
|
|
217
|
-
* @param {string} params.waypoint Waypoint
|
|
218
|
-
* @param {object} params.journeyContext JourneyContext
|
|
219
|
-
* @returns {boolean} True if all conditions pass
|
|
220
|
-
*/
|
|
221
|
-
testConditions({ fieldValue, waypoint, journeyContext }) {
|
|
222
|
-
const context = {
|
|
223
|
-
fieldName: __classPrivateFieldGet(this, _PageField_name, "f"),
|
|
224
|
-
fieldValue,
|
|
225
|
-
waypoint,
|
|
226
|
-
waypointId: waypoint,
|
|
227
|
-
journeyContext,
|
|
228
|
-
};
|
|
229
|
-
let result = true;
|
|
230
|
-
for (let i = 0, l = __classPrivateFieldGet(this, _PageField_conditions, "f").length; i < l; i++) {
|
|
231
|
-
result = result && __classPrivateFieldGet(this, _PageField_conditions, "f")[i](context);
|
|
232
|
-
}
|
|
233
|
-
return result;
|
|
234
|
-
}
|
|
235
|
-
/* ---------------------------------------------------------------- aliases */
|
|
236
|
-
validator(validator) {
|
|
237
|
-
return this.validators([validator]);
|
|
238
|
-
}
|
|
239
|
-
processor(processor) {
|
|
240
|
-
return this.processors([processor]);
|
|
241
|
-
}
|
|
242
|
-
condition(condition) {
|
|
243
|
-
return this.conditions([condition]);
|
|
244
|
-
}
|
|
245
|
-
if(...args) {
|
|
246
|
-
return this.conditions(...args);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
exports.PageField = PageField;
|
|
250
|
-
_PageField_name = new WeakMap(), _PageField_processors = new WeakMap(), _PageField_validators = new WeakMap(), _PageField_conditions = new WeakMap(), _PageField_meta = new WeakMap();
|
|
251
|
-
// Factory for convenience
|
|
252
|
-
function field(...args) {
|
|
253
|
-
return new PageField(...args);
|
|
254
|
-
}
|
|
255
|
-
exports.default = field;
|
package/dist/lib/index.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import CasaTemplateLoader from "./CasaTemplateLoader.js";
|
|
2
|
-
import configure from "./configure.js";
|
|
3
|
-
import configurationIngestor from "./configuration-ingestor.js";
|
|
4
|
-
import endSession from "./end-session.js";
|
|
5
|
-
import field from "./field.js";
|
|
6
|
-
import { PageField } from "./field.js";
|
|
7
|
-
import JourneyContext from "./JourneyContext.js";
|
|
8
|
-
import MutableRouter from "./MutableRouter.js";
|
|
9
|
-
import Plan from "./Plan.js";
|
|
10
|
-
import * as utils from "./utils.js";
|
|
11
|
-
import ValidationError from "./ValidationError.js";
|
|
12
|
-
import ValidatorFactory from "./ValidatorFactory.js";
|
|
13
|
-
import waypointUrl from "./waypoint-url.js";
|
|
14
|
-
export { CasaTemplateLoader, configure, configurationIngestor, endSession, field, PageField, JourneyContext, MutableRouter, Plan, utils, ValidationError, ValidatorFactory, waypointUrl };
|
package/dist/lib/index.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Just used to collate type information for intellisense. This should not get
|
|
4
|
-
* imported anywhere in code, other than JSDoc references.
|
|
5
|
-
*/
|
|
6
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
-
if (k2 === undefined) k2 = k;
|
|
8
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.waypointUrl = exports.ValidatorFactory = exports.ValidationError = exports.utils = exports.Plan = exports.MutableRouter = exports.JourneyContext = exports.PageField = exports.field = exports.endSession = exports.configurationIngestor = exports.configure = exports.CasaTemplateLoader = void 0;
|
|
30
|
-
const CasaTemplateLoader_js_1 = __importDefault(require("./CasaTemplateLoader.js"));
|
|
31
|
-
exports.CasaTemplateLoader = CasaTemplateLoader_js_1.default;
|
|
32
|
-
const configure_js_1 = __importDefault(require("./configure.js"));
|
|
33
|
-
exports.configure = configure_js_1.default;
|
|
34
|
-
const configuration_ingestor_js_1 = __importDefault(require("./configuration-ingestor.js"));
|
|
35
|
-
exports.configurationIngestor = configuration_ingestor_js_1.default;
|
|
36
|
-
const end_session_js_1 = __importDefault(require("./end-session.js"));
|
|
37
|
-
exports.endSession = end_session_js_1.default;
|
|
38
|
-
const field_js_1 = __importStar(require("./field.js"));
|
|
39
|
-
exports.field = field_js_1.default;
|
|
40
|
-
Object.defineProperty(exports, "PageField", { enumerable: true, get: function () { return field_js_1.PageField; } });
|
|
41
|
-
const JourneyContext_js_1 = __importDefault(require("./JourneyContext.js"));
|
|
42
|
-
exports.JourneyContext = JourneyContext_js_1.default;
|
|
43
|
-
const MutableRouter_js_1 = __importDefault(require("./MutableRouter.js"));
|
|
44
|
-
exports.MutableRouter = MutableRouter_js_1.default;
|
|
45
|
-
const Plan_js_1 = __importDefault(require("./Plan.js"));
|
|
46
|
-
exports.Plan = Plan_js_1.default;
|
|
47
|
-
const ValidationError_js_1 = __importDefault(require("./ValidationError.js"));
|
|
48
|
-
exports.ValidationError = ValidationError_js_1.default;
|
|
49
|
-
const ValidatorFactory_js_1 = __importDefault(require("./ValidatorFactory.js"));
|
|
50
|
-
exports.ValidatorFactory = ValidatorFactory_js_1.default;
|
|
51
|
-
const waypoint_url_js_1 = __importDefault(require("./waypoint-url.js"));
|
|
52
|
-
exports.waypointUrl = waypoint_url_js_1.default;
|
|
53
|
-
const utils = __importStar(require("./utils.js"));
|
|
54
|
-
exports.utils = utils;
|