@defra/forms-engine-plugin 0.1.9 → 0.1.11
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/.public/stylesheets/application.min.css +1 -1
- package/.public/stylesheets/application.min.css.map +1 -1
- package/.server/client/stylesheets/application.scss +10 -0
- package/.server/config/index.js +3 -14
- package/.server/config/index.js.map +1 -1
- package/.server/server/devserver/dxt-devtool-baselayout.html +71 -0
- package/.server/server/forms/register-as-a-unicorn-breeder.json +393 -0
- package/.server/server/forms/register-as-a-unicorn-breeder.yaml +251 -0
- package/.server/server/index.js +11 -16
- package/.server/server/index.js.map +1 -1
- package/.server/server/plugins/engine/configureEnginePlugin.js +16 -2
- package/.server/server/plugins/engine/configureEnginePlugin.js.map +1 -1
- package/.server/server/plugins/engine/plugin.js +27 -16
- package/.server/server/plugins/engine/plugin.js.map +1 -1
- package/.server/server/plugins/engine/services/formsService.js +15 -29
- package/.server/server/plugins/engine/services/formsService.js.map +1 -1
- package/.server/server/plugins/engine/services/localFormsService.js +52 -0
- package/.server/server/plugins/engine/services/localFormsService.js.map +1 -0
- package/.server/server/plugins/engine/views/confirmation.html +1 -1
- package/.server/server/plugins/engine/views/file-upload.html +1 -1
- package/.server/server/plugins/engine/views/index.html +1 -1
- package/.server/server/plugins/engine/views/item-delete.html +1 -1
- package/.server/server/plugins/engine/views/repeat-list-summary.html +1 -1
- package/.server/server/plugins/engine/views/summary.html +1 -1
- package/.server/server/plugins/errorPages.js +4 -26
- package/.server/server/plugins/errorPages.js.map +1 -1
- package/.server/server/plugins/nunjucks/context.js +37 -28
- package/.server/server/plugins/nunjucks/context.js.map +1 -1
- package/.server/server/plugins/nunjucks/context.test.js +23 -28
- package/.server/server/plugins/nunjucks/context.test.js.map +1 -1
- package/.server/server/plugins/nunjucks/types.js +3 -4
- package/.server/server/plugins/nunjucks/types.js.map +1 -1
- package/.server/server/routes/index.js +0 -1
- package/.server/server/routes/index.js.map +1 -1
- package/.server/server/utils/file-form-service.js +134 -0
- package/.server/server/utils/file-form-service.js.map +1 -0
- package/.server/typings/hapi/index.d.js.map +1 -1
- package/README.md +3 -1
- package/package.json +7 -3
- package/src/client/stylesheets/application.scss +10 -0
- package/src/config/index.ts +4 -17
- package/src/server/devserver/dxt-devtool-baselayout.html +71 -0
- package/src/server/forms/register-as-a-unicorn-breeder.json +393 -0
- package/src/server/forms/register-as-a-unicorn-breeder.yaml +251 -0
- package/src/server/index.test.ts +4 -37
- package/src/server/index.ts +13 -16
- package/src/server/plugins/engine/components/AutocompleteField.test.ts +5 -5
- package/src/server/plugins/engine/components/CheckboxesField.test.ts +7 -7
- package/src/server/plugins/engine/components/List.test.ts +3 -0
- package/src/server/plugins/engine/components/RadiosField.test.ts +5 -5
- package/src/server/plugins/engine/components/SelectField.test.ts +5 -5
- package/src/server/plugins/engine/configureEnginePlugin.ts +19 -1
- package/src/server/plugins/engine/pageControllers/QuestionPageController.test.ts +4 -0
- package/src/server/plugins/engine/plugin.ts +43 -17
- package/src/server/plugins/engine/services/formsService.js +17 -35
- package/src/server/plugins/engine/services/localFormsService.js +49 -0
- package/src/server/plugins/engine/views/confirmation.html +1 -1
- package/src/server/plugins/engine/views/file-upload.html +1 -1
- package/src/server/plugins/engine/views/index.html +1 -1
- package/src/server/plugins/engine/views/item-delete.html +1 -1
- package/src/server/plugins/engine/views/repeat-list-summary.html +1 -1
- package/src/server/plugins/engine/views/summary.html +1 -1
- package/src/server/plugins/errorPages.ts +4 -26
- package/src/server/plugins/nunjucks/context.js +41 -31
- package/src/server/plugins/nunjucks/context.test.js +24 -27
- package/src/server/plugins/nunjucks/types.js +3 -4
- package/src/server/routes/index.ts +0 -1
- package/src/server/utils/file-form-service.js +144 -0
- package/src/typings/hapi/index.d.ts +3 -9
- package/.server/common/cookies.js +0 -55
- package/.server/common/cookies.js.map +0 -1
- package/.server/common/cookies.test.js +0 -15
- package/.server/common/cookies.test.js.map +0 -1
- package/.server/common/types.js +0 -6
- package/.server/common/types.js.map +0 -1
- package/.server/server/forms/README.md +0 -10
- package/.server/server/forms/report-a-terrorist.json +0 -270
- package/.server/server/forms/runner-components-test.json +0 -365
- package/.server/server/forms/test.json +0 -581
- package/.server/server/plugins/blankie.js +0 -29
- package/.server/server/plugins/blankie.js.map +0 -1
- package/.server/server/plugins/engine/services/formsService.test.js +0 -71
- package/.server/server/plugins/engine/services/formsService.test.js.map +0 -1
- package/.server/server/plugins/engine/views/layout.html +0 -199
- package/.server/server/plugins/router.js +0 -169
- package/.server/server/plugins/router.js.map +0 -1
- package/.server/server/routes/health.js +0 -15
- package/.server/server/routes/health.js.map +0 -1
- package/.server/server/routes/health.test.js +0 -32
- package/.server/server/routes/health.test.js.map +0 -1
- package/.server/server/views/404.html +0 -16
- package/.server/server/views/500.html +0 -19
- package/.server/server/views/help/accessibility-statement.html +0 -58
- package/.server/server/views/help/cookie-preferences.html +0 -57
- package/.server/server/views/help/cookies.html +0 -71
- package/.server/server/views/help/get-support.html +0 -37
- package/.server/server/views/help/privacy-notice.html +0 -68
- package/.server/server/views/help/terms-and-conditions.html +0 -83
- package/src/common/cookies.js +0 -58
- package/src/common/cookies.test.js +0 -23
- package/src/common/types.js +0 -5
- package/src/server/forms/README.md +0 -10
- package/src/server/forms/report-a-terrorist.json +0 -270
- package/src/server/forms/runner-components-test.json +0 -365
- package/src/server/forms/test.json +0 -581
- package/src/server/plugins/blankie.test.ts +0 -73
- package/src/server/plugins/blankie.ts +0 -48
- package/src/server/plugins/engine/services/formsService.test.js +0 -90
- package/src/server/plugins/engine/views/layout.html +0 -199
- package/src/server/plugins/router.ts +0 -201
- package/src/server/routes/health.js +0 -13
- package/src/server/routes/health.test.js +0 -35
- package/src/server/routes/index.test.ts +0 -125
- package/src/server/views/404.html +0 -16
- package/src/server/views/500.html +0 -19
- package/src/server/views/help/accessibility-statement.html +0 -58
- package/src/server/views/help/cookie-preferences.html +0 -57
- package/src/server/views/help/cookies.html +0 -71
- package/src/server/views/help/get-support.html +0 -37
- package/src/server/views/help/privacy-notice.html +0 -68
- package/src/server/views/help/terms-and-conditions.html +0 -83
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
type ResponseToolkit,
|
|
4
4
|
type ServerRegisterPluginObject
|
|
5
5
|
} from '@hapi/hapi'
|
|
6
|
-
import { StatusCodes } from 'http-status-codes'
|
|
7
6
|
|
|
8
7
|
/*
|
|
9
8
|
* Add an `onPreResponse` listener to return error pages
|
|
@@ -20,36 +19,15 @@ export default {
|
|
|
20
19
|
// processing the request
|
|
21
20
|
const statusCode = response.output.statusCode
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
// and use it to set the correct service name
|
|
25
|
-
// and start page path. In the event of a error
|
|
26
|
-
// happening inside a "form" level request, the header
|
|
27
|
-
// then displays the contextual form text and href
|
|
28
|
-
const model = request.app.model
|
|
29
|
-
const viewModel = model
|
|
30
|
-
? {
|
|
31
|
-
name: model.name,
|
|
32
|
-
serviceUrl: `/${model.basePath}`
|
|
33
|
-
}
|
|
34
|
-
: undefined
|
|
35
|
-
|
|
36
|
-
// In the event of 404
|
|
37
|
-
// return the `404` view
|
|
38
|
-
if (statusCode === StatusCodes.NOT_FOUND.valueOf()) {
|
|
39
|
-
return h.view('404', viewModel).code(statusCode)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
request.log('error', {
|
|
22
|
+
const error = {
|
|
43
23
|
statusCode,
|
|
44
|
-
data: response.data,
|
|
45
24
|
message: response.message,
|
|
46
25
|
stack: response.stack
|
|
47
|
-
}
|
|
26
|
+
}
|
|
48
27
|
|
|
49
|
-
request.
|
|
28
|
+
request.log('error', error)
|
|
50
29
|
|
|
51
|
-
|
|
52
|
-
return h.view('500', viewModel).code(statusCode)
|
|
30
|
+
return h.response(error).code(statusCode)
|
|
53
31
|
}
|
|
54
32
|
return h.continue
|
|
55
33
|
})
|
|
@@ -5,7 +5,6 @@ import Boom from '@hapi/boom'
|
|
|
5
5
|
import { StatusCodes } from 'http-status-codes'
|
|
6
6
|
|
|
7
7
|
import pkg from '~/package.json' with { type: 'json' }
|
|
8
|
-
import { parseCookieConsent } from '~/src/common/cookies.js'
|
|
9
8
|
import { config } from '~/src/config/index.js'
|
|
10
9
|
import { createLogger } from '~/src/server/common/helpers/logging/logger.js'
|
|
11
10
|
import { PREVIEW_PATH_PREFIX } from '~/src/server/constants.js'
|
|
@@ -23,20 +22,8 @@ let webpackManifest
|
|
|
23
22
|
* @param {FormRequest | FormRequestPayload | null} request
|
|
24
23
|
*/
|
|
25
24
|
export function context(request) {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
if (!webpackManifest) {
|
|
29
|
-
try {
|
|
30
|
-
// eslint-disable-next-line -- Allow JSON type 'any'
|
|
31
|
-
webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
32
|
-
} catch {
|
|
33
|
-
logger.error(`Webpack ${basename(manifestPath)} not found`)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
25
|
+
const { params, path, response } = request ?? {}
|
|
36
26
|
|
|
37
|
-
const { params, path, query = {}, response, state } = request ?? {}
|
|
38
|
-
|
|
39
|
-
const isForceAccess = 'force' in query
|
|
40
27
|
const isPreviewMode = path?.startsWith(PREVIEW_PATH_PREFIX)
|
|
41
28
|
|
|
42
29
|
// Only add the slug in to the context if the response is OK.
|
|
@@ -44,45 +31,68 @@ export function context(request) {
|
|
|
44
31
|
const isResponseOK =
|
|
45
32
|
!Boom.isBoom(response) && response?.statusCode === StatusCodes.OK
|
|
46
33
|
|
|
34
|
+
const pluginStorage = request?.server.plugins['forms-engine-plugin']
|
|
35
|
+
let consumerViewContext = {}
|
|
36
|
+
|
|
37
|
+
if (!pluginStorage) {
|
|
38
|
+
throw Error('context called before plugin registered')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!pluginStorage.baseLayoutPath) {
|
|
42
|
+
throw Error('Missing baseLayoutPath in plugin.options.nunjucks')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if ('viewContext' in pluginStorage) {
|
|
46
|
+
consumerViewContext = pluginStorage.viewContext(request)
|
|
47
|
+
}
|
|
48
|
+
|
|
47
49
|
/** @type {ViewContext} */
|
|
48
50
|
const ctx = {
|
|
51
|
+
// take consumers props first so we can override it
|
|
52
|
+
...consumerViewContext,
|
|
53
|
+
baseLayoutPath: pluginStorage.baseLayoutPath,
|
|
49
54
|
appVersion: pkg.version,
|
|
50
|
-
assetPath: '/assets',
|
|
51
55
|
config: {
|
|
52
56
|
cdpEnvironment: config.get('cdpEnvironment'),
|
|
53
57
|
designerUrl: config.get('designerUrl'),
|
|
54
58
|
feedbackLink: encodeUrl(config.get('feedbackLink')),
|
|
55
59
|
phaseTag: config.get('phaseTag'),
|
|
56
|
-
serviceBannerText: config.get('serviceBannerText'),
|
|
57
60
|
serviceName: config.get('serviceName'),
|
|
58
61
|
serviceVersion: config.get('serviceVersion')
|
|
59
62
|
},
|
|
60
63
|
crumb: safeGenerateCrumb(request),
|
|
61
|
-
|
|
62
|
-
currentPath: request ? `${request.path}${request.url.search}` : undefined,
|
|
64
|
+
currentPath: `${request.path}${request.url.search}`,
|
|
63
65
|
previewMode: isPreviewMode ? params?.state : undefined,
|
|
64
|
-
slug: isResponseOK ? params?.slug : undefined
|
|
65
|
-
|
|
66
|
-
getAssetPath: (asset = '') => {
|
|
67
|
-
return `/${webpackManifest?.[asset] ?? asset}`
|
|
68
|
-
}
|
|
66
|
+
slug: isResponseOK ? params?.slug : undefined
|
|
69
67
|
}
|
|
70
68
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
return ctx
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Returns the context for the devtool. Consumers won't have access to this.
|
|
74
|
+
*/
|
|
75
|
+
export function devtoolContext() {
|
|
76
|
+
const manifestPath = join(config.get('publicDir'), 'assets-manifest.json')
|
|
75
77
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
+
if (!webpackManifest) {
|
|
79
|
+
try {
|
|
80
|
+
// eslint-disable-next-line -- Allow JSON type 'any'
|
|
81
|
+
webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
|
|
82
|
+
} catch {
|
|
83
|
+
logger.error(`Webpack ${basename(manifestPath)} not found`)
|
|
78
84
|
}
|
|
79
85
|
}
|
|
80
86
|
|
|
81
|
-
return
|
|
87
|
+
return {
|
|
88
|
+
assetPath: '/assets',
|
|
89
|
+
getDxtAssetPath: (asset = '') => {
|
|
90
|
+
return `/${webpackManifest?.[asset] ?? asset}`
|
|
91
|
+
}
|
|
92
|
+
}
|
|
82
93
|
}
|
|
83
94
|
|
|
84
95
|
/**
|
|
85
|
-
* @import { CookieConsent } from '~/src/common/types.js'
|
|
86
96
|
* @import { ViewContext } from '~/src/server/plugins/nunjucks/types.js'
|
|
87
97
|
* @import { FormRequest, FormRequestPayload } from '~/src/server/routes/types.js'
|
|
88
98
|
*/
|
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
import { tmpdir } from 'node:os'
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import {
|
|
4
|
+
context,
|
|
5
|
+
devtoolContext
|
|
6
|
+
} from '~/src/server/plugins/nunjucks/context.js'
|
|
6
7
|
|
|
7
8
|
describe('Nunjucks context', () => {
|
|
8
9
|
beforeEach(() => jest.resetModules())
|
|
9
10
|
|
|
10
11
|
describe('Asset path', () => {
|
|
11
12
|
it("should include 'assetPath' for GOV.UK Frontend icons", () => {
|
|
12
|
-
const { assetPath } =
|
|
13
|
+
const { assetPath } = devtoolContext()
|
|
13
14
|
expect(assetPath).toBe('/assets')
|
|
14
15
|
})
|
|
15
16
|
})
|
|
16
17
|
|
|
17
18
|
describe('Asset helper', () => {
|
|
18
19
|
it("should locate 'assets-manifest.json' assets", () => {
|
|
19
|
-
const {
|
|
20
|
+
const { getDxtAssetPath } = devtoolContext()
|
|
20
21
|
|
|
21
|
-
expect(
|
|
22
|
+
expect(getDxtAssetPath('example.scss')).toBe(
|
|
22
23
|
'/stylesheets/example.xxxxxxx.min.css'
|
|
23
24
|
)
|
|
24
25
|
|
|
25
|
-
expect(
|
|
26
|
+
expect(getDxtAssetPath('example.mjs')).toBe(
|
|
26
27
|
'/javascripts/example.xxxxxxx.min.js'
|
|
27
28
|
)
|
|
28
29
|
})
|
|
@@ -32,43 +33,33 @@ describe('Nunjucks context', () => {
|
|
|
32
33
|
const { config } = await import('~/src/config/index.js')
|
|
33
34
|
|
|
34
35
|
// Import when isolated to avoid cache
|
|
35
|
-
const {
|
|
36
|
+
const { devtoolContext } = await import(
|
|
36
37
|
'~/src/server/plugins/nunjucks/context.js'
|
|
37
38
|
)
|
|
38
39
|
|
|
39
40
|
// Update config for missing manifest
|
|
40
41
|
config.set('publicDir', tmpdir())
|
|
41
|
-
const {
|
|
42
|
+
const { getDxtAssetPath } = devtoolContext()
|
|
42
43
|
|
|
43
44
|
// Uses original paths when missing
|
|
44
|
-
expect(
|
|
45
|
-
expect(
|
|
45
|
+
expect(getDxtAssetPath('example.scss')).toBe('/example.scss')
|
|
46
|
+
expect(getDxtAssetPath('example.mjs')).toBe('/example.mjs')
|
|
46
47
|
})
|
|
47
48
|
})
|
|
48
49
|
|
|
49
50
|
it('should return path to unknown assets', () => {
|
|
50
|
-
const {
|
|
51
|
+
const { getDxtAssetPath } = devtoolContext()
|
|
51
52
|
|
|
52
|
-
expect(
|
|
53
|
-
expect(
|
|
54
|
-
expect(
|
|
53
|
+
expect(getDxtAssetPath()).toBe('/')
|
|
54
|
+
expect(getDxtAssetPath('example.jpg')).toBe('/example.jpg')
|
|
55
|
+
expect(getDxtAssetPath('example.gif')).toBe('/example.gif')
|
|
55
56
|
})
|
|
56
57
|
})
|
|
57
58
|
|
|
58
59
|
describe('Config', () => {
|
|
59
60
|
it('should include environment, phase tag and service info', () => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
expect(ctx.config).toEqual(
|
|
63
|
-
expect.objectContaining({
|
|
64
|
-
cdpEnvironment: config.get('cdpEnvironment'),
|
|
65
|
-
feedbackLink: encodeUrl(config.get('feedbackLink')),
|
|
66
|
-
googleAnalyticsTrackingId: config.get('googleAnalyticsTrackingId'),
|
|
67
|
-
phaseTag: config.get('phaseTag'),
|
|
68
|
-
serviceBannerText: config.get('serviceBannerText'),
|
|
69
|
-
serviceName: config.get('serviceName'),
|
|
70
|
-
serviceVersion: config.get('serviceVersion')
|
|
71
|
-
})
|
|
61
|
+
expect(() => context(null)).toThrow(
|
|
62
|
+
'context called before plugin registered'
|
|
72
63
|
)
|
|
73
64
|
})
|
|
74
65
|
})
|
|
@@ -83,6 +74,9 @@ describe('Nunjucks context', () => {
|
|
|
83
74
|
plugins: {
|
|
84
75
|
crumb: {
|
|
85
76
|
generate: jest.fn()
|
|
77
|
+
},
|
|
78
|
+
'forms-engine-plugin': {
|
|
79
|
+
baseLayoutPath: 'randomValue'
|
|
86
80
|
}
|
|
87
81
|
}
|
|
88
82
|
},
|
|
@@ -113,6 +107,9 @@ describe('Nunjucks context', () => {
|
|
|
113
107
|
plugins: {
|
|
114
108
|
crumb: {
|
|
115
109
|
generate: jest.fn().mockReturnValue(mockCrumb)
|
|
110
|
+
},
|
|
111
|
+
'forms-engine-plugin': {
|
|
112
|
+
baseLayoutPath: 'randomValue'
|
|
116
113
|
}
|
|
117
114
|
}
|
|
118
115
|
},
|
|
@@ -12,16 +12,15 @@
|
|
|
12
12
|
/**
|
|
13
13
|
* @typedef {object} ViewContext - Nunjucks view context
|
|
14
14
|
* @property {string} appVersion - Application version
|
|
15
|
-
* @property {string}
|
|
15
|
+
* @property {string} [baseLayoutPath] - Base layout path
|
|
16
16
|
* @property {Partial<Config>} config - Application config properties
|
|
17
|
-
* @property {CookieConsent} [cookieConsent] - Cookie consent preferences
|
|
18
17
|
* @property {string} [crumb] - Cross-Site Request Forgery (CSRF) token
|
|
19
18
|
* @property {string} [cspNonce] - Content Security Policy (CSP) nonce
|
|
20
19
|
* @property {string} [currentPath] - Current path
|
|
21
20
|
* @property {string} [previewMode] - Preview mode
|
|
22
21
|
* @property {string} [slug] - Form slug
|
|
23
|
-
* @property {(asset?: string) => string} getAssetPath - Asset path resolver
|
|
24
22
|
* @property {FormContext} [context] - the current form context
|
|
23
|
+
* @property {PluginOptions['viewContext']} [injectedViewContext] - the current form context
|
|
25
24
|
*/
|
|
26
25
|
|
|
27
26
|
/**
|
|
@@ -34,7 +33,7 @@
|
|
|
34
33
|
*/
|
|
35
34
|
|
|
36
35
|
/**
|
|
37
|
-
* @import { CookieConsent } from '~/src/common/types.js'
|
|
38
36
|
* @import { config } from '~/src/config/index.js'
|
|
39
37
|
* @import { FormContext } from '~/src/server/plugins/engine/types.js'
|
|
38
|
+
* @import { PluginOptions } from '~/src/server/plugins/engine/plugin.js'
|
|
40
39
|
*/
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import fs from 'fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import YAML from 'yaml'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* FileFormService class
|
|
8
|
+
*/
|
|
9
|
+
export class FileFormService {
|
|
10
|
+
/**
|
|
11
|
+
* The map of form metadatas by slug
|
|
12
|
+
* @type {Map<string, FormMetadata>}
|
|
13
|
+
*/
|
|
14
|
+
#metadata = new Map()
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The map of form definitions by id
|
|
18
|
+
* @type {Map<string, FormDefinition>}
|
|
19
|
+
*/
|
|
20
|
+
#definition = new Map()
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Add form from a file
|
|
24
|
+
* @param {string} filepath - the file path
|
|
25
|
+
* @param {FormMetadata} metadata - the metadata to use for this form
|
|
26
|
+
* @returns {Promise<FormDefinition>}
|
|
27
|
+
*/
|
|
28
|
+
async addForm(filepath, metadata) {
|
|
29
|
+
const definition = await this.readForm(filepath)
|
|
30
|
+
|
|
31
|
+
this.#metadata.set(metadata.slug, metadata)
|
|
32
|
+
this.#definition.set(metadata.id, definition)
|
|
33
|
+
|
|
34
|
+
return definition
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Read the form definition from file
|
|
39
|
+
* @param {string} filepath - the file path
|
|
40
|
+
* @returns {Promise<FormDefinition>}
|
|
41
|
+
*/
|
|
42
|
+
async readForm(filepath) {
|
|
43
|
+
const ext = path.extname(filepath).toLowerCase()
|
|
44
|
+
|
|
45
|
+
switch (ext) {
|
|
46
|
+
case '.json':
|
|
47
|
+
return this.readJsonForm(filepath)
|
|
48
|
+
case '.yaml':
|
|
49
|
+
return this.readYamlForm(filepath)
|
|
50
|
+
default:
|
|
51
|
+
throw new Error(`Invalid file extension '${ext}'`)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Read the form definition from a json file
|
|
57
|
+
* @param {string} filepath - the file path
|
|
58
|
+
* @returns {Promise<FormDefinition>}
|
|
59
|
+
*/
|
|
60
|
+
async readJsonForm(filepath) {
|
|
61
|
+
/**
|
|
62
|
+
* @type {FormDefinition}
|
|
63
|
+
*/
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
65
|
+
const definition = JSON.parse(await fs.readFile(filepath, 'utf8'))
|
|
66
|
+
|
|
67
|
+
return definition
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Read the form definition from a yaml file
|
|
72
|
+
* @param {string} filepath - the file path
|
|
73
|
+
* @returns {Promise<FormDefinition>}
|
|
74
|
+
*/
|
|
75
|
+
async readYamlForm(filepath) {
|
|
76
|
+
/**
|
|
77
|
+
* @type {FormDefinition}
|
|
78
|
+
*/
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
80
|
+
const definition = YAML.parse(await fs.readFile(filepath, 'utf8'))
|
|
81
|
+
|
|
82
|
+
return definition
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get the form metadata by slug
|
|
87
|
+
* @param {string} slug - the form slug
|
|
88
|
+
* @returns {FormMetadata}
|
|
89
|
+
*/
|
|
90
|
+
getFormMetadata(slug) {
|
|
91
|
+
const metadata = this.#metadata.get(slug)
|
|
92
|
+
|
|
93
|
+
if (!metadata) {
|
|
94
|
+
throw new Error(`Form metadata '${slug}' not found`)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return metadata
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Get the form defintion by id
|
|
102
|
+
* @param {string} id - the form id
|
|
103
|
+
* @returns {FormDefinition}
|
|
104
|
+
*/
|
|
105
|
+
getFormDefinition(id) {
|
|
106
|
+
const definition = this.#definition.get(id)
|
|
107
|
+
|
|
108
|
+
if (!definition) {
|
|
109
|
+
throw new Error(`Form definition '${id}' not found`)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return definition
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns a FormsService compliant interface
|
|
117
|
+
* @returns {import('~/src/server/types.js').FormsService}
|
|
118
|
+
*/
|
|
119
|
+
toFormsService() {
|
|
120
|
+
return {
|
|
121
|
+
/**
|
|
122
|
+
* Get the form metadata by slug
|
|
123
|
+
* @param {string} slug
|
|
124
|
+
* @returns {Promise<FormMetadata>}
|
|
125
|
+
*/
|
|
126
|
+
getFormMetadata: (slug) => {
|
|
127
|
+
return Promise.resolve(this.getFormMetadata(slug))
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Get the form defintion by id
|
|
132
|
+
* @param {string} id
|
|
133
|
+
* @returns {Promise<FormDefinition>}
|
|
134
|
+
*/
|
|
135
|
+
getFormDefinition: (id) => {
|
|
136
|
+
return Promise.resolve(this.getFormDefinition(id))
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @import { FormMetadata, FormDefinition } from '@defra/forms-model'
|
|
144
|
+
*/
|
|
@@ -5,6 +5,7 @@ import { type ServerYar, type Yar } from '@hapi/yar'
|
|
|
5
5
|
import { type Logger } from 'pino'
|
|
6
6
|
|
|
7
7
|
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
|
|
8
|
+
import { type context } from '~/src/server/plugins/engine/nunjucks.js'
|
|
8
9
|
import {
|
|
9
10
|
type FormRequest,
|
|
10
11
|
type FormRequestPayload
|
|
@@ -19,16 +20,9 @@ declare module '@hapi/hapi' {
|
|
|
19
20
|
generate?: (request: Request | FormRequest | FormRequestPayload) => string
|
|
20
21
|
}
|
|
21
22
|
'forms-engine-plugin': {
|
|
23
|
+
baseLayoutPath: string
|
|
22
24
|
cacheService: CacheService
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface PluginsStates {
|
|
27
|
-
blankie?: {
|
|
28
|
-
nonces?: {
|
|
29
|
-
script?: string
|
|
30
|
-
style?: string
|
|
31
|
-
}
|
|
25
|
+
viewContext: context
|
|
32
26
|
}
|
|
33
27
|
}
|
|
34
28
|
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
@type {CookieConsent}
|
|
3
|
-
*/
|
|
4
|
-
export const defaultConsent = {
|
|
5
|
-
analytics: null,
|
|
6
|
-
dismissed: false
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Parses the cookie consent policy
|
|
11
|
-
* @param {string} value
|
|
12
|
-
*/
|
|
13
|
-
export function parseCookieConsent(value) {
|
|
14
|
-
/** @type {CookieConsent} */
|
|
15
|
-
let cookieConsent;
|
|
16
|
-
try {
|
|
17
|
-
const encodedValue = decodeURIComponent(value);
|
|
18
|
-
|
|
19
|
-
// eslint-disable-next-line -- Allow JSON type 'any'
|
|
20
|
-
const decodedValue = JSON.parse(encodedValue);
|
|
21
|
-
if (isValidConsent(decodedValue)) {
|
|
22
|
-
cookieConsent = decodedValue;
|
|
23
|
-
} else {
|
|
24
|
-
cookieConsent = defaultConsent;
|
|
25
|
-
}
|
|
26
|
-
} catch {
|
|
27
|
-
cookieConsent = defaultConsent;
|
|
28
|
-
}
|
|
29
|
-
return cookieConsent;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Serialises the cookie consent policy
|
|
34
|
-
* @param {CookieConsent} consent
|
|
35
|
-
* @returns {string} cookie value
|
|
36
|
-
*/
|
|
37
|
-
export function serialiseCookieConsent(consent) {
|
|
38
|
-
return encodeURIComponent(JSON.stringify(consent));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @param {unknown} consent
|
|
43
|
-
* @returns {consent is CookieConsent}
|
|
44
|
-
*/
|
|
45
|
-
function isValidConsent(consent) {
|
|
46
|
-
if (consent === null || Array.isArray(consent)) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
return typeof consent === 'object' && 'analytics' in consent;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @import {CookieConsent} from '~/src/common/types.js'
|
|
54
|
-
*/
|
|
55
|
-
//# sourceMappingURL=cookies.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.js","names":["defaultConsent","analytics","dismissed","parseCookieConsent","value","cookieConsent","encodedValue","decodeURIComponent","decodedValue","JSON","parse","isValidConsent","serialiseCookieConsent","consent","encodeURIComponent","stringify","Array","isArray"],"sources":["../../src/common/cookies.js"],"sourcesContent":["/**\n @type {CookieConsent}\n */\nexport const defaultConsent = {\n analytics: null,\n dismissed: false\n}\n\n/**\n * Parses the cookie consent policy\n * @param {string} value\n */\nexport function parseCookieConsent(value) {\n /** @type {CookieConsent} */\n let cookieConsent\n\n try {\n const encodedValue = decodeURIComponent(value)\n\n // eslint-disable-next-line -- Allow JSON type 'any'\n const decodedValue = JSON.parse(encodedValue)\n\n if (isValidConsent(decodedValue)) {\n cookieConsent = decodedValue\n } else {\n cookieConsent = defaultConsent\n }\n } catch {\n cookieConsent = defaultConsent\n }\n\n return cookieConsent\n}\n\n/**\n * Serialises the cookie consent policy\n * @param {CookieConsent} consent\n * @returns {string} cookie value\n */\nexport function serialiseCookieConsent(consent) {\n return encodeURIComponent(JSON.stringify(consent))\n}\n\n/**\n * @param {unknown} consent\n * @returns {consent is CookieConsent}\n */\nfunction isValidConsent(consent) {\n if (consent === null || Array.isArray(consent)) {\n return false\n }\n\n return typeof consent === 'object' && 'analytics' in consent\n}\n\n/**\n * @import {CookieConsent} from '~/src/common/types.js'\n */\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAO,MAAMA,cAAc,GAAG;EAC5BC,SAAS,EAAE,IAAI;EACfC,SAAS,EAAE;AACb,CAAC;;AAED;AACA;AACA;AACA;AACA,OAAO,SAASC,kBAAkBA,CAACC,KAAK,EAAE;EACxC;EACA,IAAIC,aAAa;EAEjB,IAAI;IACF,MAAMC,YAAY,GAAGC,kBAAkB,CAACH,KAAK,CAAC;;IAE9C;IACA,MAAMI,YAAY,GAAGC,IAAI,CAACC,KAAK,CAACJ,YAAY,CAAC;IAE7C,IAAIK,cAAc,CAACH,YAAY,CAAC,EAAE;MAChCH,aAAa,GAAGG,YAAY;IAC9B,CAAC,MAAM;MACLH,aAAa,GAAGL,cAAc;IAChC;EACF,CAAC,CAAC,MAAM;IACNK,aAAa,GAAGL,cAAc;EAChC;EAEA,OAAOK,aAAa;AACtB;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASO,sBAAsBA,CAACC,OAAO,EAAE;EAC9C,OAAOC,kBAAkB,CAACL,IAAI,CAACM,SAAS,CAACF,OAAO,CAAC,CAAC;AACpD;;AAEA;AACA;AACA;AACA;AACA,SAASF,cAAcA,CAACE,OAAO,EAAE;EAC/B,IAAIA,OAAO,KAAK,IAAI,IAAIG,KAAK,CAACC,OAAO,CAACJ,OAAO,CAAC,EAAE;IAC9C,OAAO,KAAK;EACd;EAEA,OAAO,OAAOA,OAAO,KAAK,QAAQ,IAAI,WAAW,IAAIA,OAAO;AAC9D;;AAEA;AACA;AACA","ignoreList":[]}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { parseCookieConsent } from "./cookies.js";
|
|
2
|
-
describe('cookies', () => {
|
|
3
|
-
it('parses a valid policy', () => {
|
|
4
|
-
expect(parseCookieConsent('{"analytics":true}')).toEqual({
|
|
5
|
-
analytics: true
|
|
6
|
-
});
|
|
7
|
-
});
|
|
8
|
-
it.each(["['not', 'an', 'object']", '{{ not: "an object" }}', '{ additional: AAA }', '{ marketing: 100 }', '', 'null'])('converts a malformed policy to the default', value => {
|
|
9
|
-
expect(parseCookieConsent(value)).toEqual({
|
|
10
|
-
analytics: null,
|
|
11
|
-
dismissed: false
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
//# sourceMappingURL=cookies.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.test.js","names":["parseCookieConsent","describe","it","expect","toEqual","analytics","each","value","dismissed"],"sources":["../../src/common/cookies.test.js"],"sourcesContent":["import { parseCookieConsent } from '~/src/common/cookies.js'\n\ndescribe('cookies', () => {\n it('parses a valid policy', () => {\n expect(parseCookieConsent('{\"analytics\":true}')).toEqual({\n analytics: true\n })\n })\n\n it.each([\n \"['not', 'an', 'object']\",\n '{{ not: \"an object\" }}',\n '{ additional: AAA }',\n '{ marketing: 100 }',\n '',\n 'null'\n ])('converts a malformed policy to the default', (value) => {\n expect(parseCookieConsent(value)).toEqual({\n analytics: null,\n dismissed: false\n })\n })\n})\n"],"mappings":"AAAA,SAASA,kBAAkB;AAE3BC,QAAQ,CAAC,SAAS,EAAE,MAAM;EACxBC,EAAE,CAAC,uBAAuB,EAAE,MAAM;IAChCC,MAAM,CAACH,kBAAkB,CAAC,oBAAoB,CAAC,CAAC,CAACI,OAAO,CAAC;MACvDC,SAAS,EAAE;IACb,CAAC,CAAC;EACJ,CAAC,CAAC;EAEFH,EAAE,CAACI,IAAI,CAAC,CACN,yBAAyB,EACzB,wBAAwB,EACxB,qBAAqB,EACrB,oBAAoB,EACpB,EAAE,EACF,MAAM,CACP,CAAC,CAAC,4CAA4C,EAAGC,KAAK,IAAK;IAC1DJ,MAAM,CAACH,kBAAkB,CAACO,KAAK,CAAC,CAAC,CAACH,OAAO,CAAC;MACxCC,SAAS,EAAE,IAAI;MACfG,SAAS,EAAE;IACb,CAAC,CAAC;EACJ,CAAC,CAAC;AACJ,CAAC,CAAC","ignoreList":[]}
|
package/.server/common/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","names":[],"sources":["../../src/common/types.js"],"sourcesContent":["/**\n * @typedef CookieConsent\n * @property {boolean | null} analytics - whether analytics cookies are allowed\n * @property {boolean} dismissed - whether cookie banner has been dismissed\n */\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA","ignoreList":[]}
|