@defra/forms-engine-plugin 1.0.1 → 1.0.3

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 (47) hide show
  1. package/.server/server/plugins/engine/configureEnginePlugin.d.ts +2 -1
  2. package/.server/server/plugins/engine/configureEnginePlugin.js +1 -1
  3. package/.server/server/plugins/engine/configureEnginePlugin.js.map +1 -1
  4. package/.server/server/plugins/engine/options.d.ts +7 -0
  5. package/.server/server/plugins/engine/options.js +36 -0
  6. package/.server/server/plugins/engine/options.js.map +1 -0
  7. package/.server/server/plugins/engine/options.test.js +33 -0
  8. package/.server/server/plugins/engine/options.test.js.map +1 -0
  9. package/.server/server/plugins/engine/plugin.d.ts +2 -30
  10. package/.server/server/plugins/engine/plugin.js +12 -596
  11. package/.server/server/plugins/engine/plugin.js.map +1 -1
  12. package/.server/server/plugins/engine/registrationOptions.d.ts +1 -0
  13. package/.server/server/plugins/engine/registrationOptions.js +2 -0
  14. package/.server/server/plugins/engine/registrationOptions.js.map +1 -0
  15. package/.server/server/plugins/engine/routes/file-upload.d.ts +4 -0
  16. package/.server/server/plugins/engine/routes/file-upload.js +41 -0
  17. package/.server/server/plugins/engine/routes/file-upload.js.map +1 -0
  18. package/.server/server/plugins/engine/routes/index.d.ts +7 -0
  19. package/.server/server/plugins/engine/routes/index.js +141 -0
  20. package/.server/server/plugins/engine/routes/index.js.map +1 -0
  21. package/.server/server/plugins/engine/routes/questions.d.ts +3 -0
  22. package/.server/server/plugins/engine/routes/questions.js +168 -0
  23. package/.server/server/plugins/engine/routes/questions.js.map +1 -0
  24. package/.server/server/plugins/engine/routes/repeaters/item-delete.d.ts +3 -0
  25. package/.server/server/plugins/engine/routes/repeaters/item-delete.js +106 -0
  26. package/.server/server/plugins/engine/routes/repeaters/item-delete.js.map +1 -0
  27. package/.server/server/plugins/engine/routes/repeaters/summary.d.ts +3 -0
  28. package/.server/server/plugins/engine/routes/repeaters/summary.js +98 -0
  29. package/.server/server/plugins/engine/routes/repeaters/summary.js.map +1 -0
  30. package/.server/server/plugins/engine/types.d.ts +19 -1
  31. package/.server/server/plugins/engine/types.js.map +1 -1
  32. package/.server/server/plugins/engine/vision.d.ts +12 -0
  33. package/.server/server/plugins/engine/vision.js +55 -0
  34. package/.server/server/plugins/engine/vision.js.map +1 -0
  35. package/package.json +1 -1
  36. package/src/server/plugins/engine/configureEnginePlugin.ts +3 -5
  37. package/src/server/plugins/engine/options.js +37 -0
  38. package/src/server/plugins/engine/options.test.js +34 -0
  39. package/src/server/plugins/engine/plugin.ts +30 -772
  40. package/src/server/plugins/engine/registrationOptions.ts +0 -0
  41. package/src/server/plugins/engine/routes/file-upload.ts +54 -0
  42. package/src/server/plugins/engine/routes/index.ts +187 -0
  43. package/src/server/plugins/engine/routes/questions.ts +208 -0
  44. package/src/server/plugins/engine/routes/repeaters/item-delete.ts +157 -0
  45. package/src/server/plugins/engine/routes/repeaters/summary.ts +137 -0
  46. package/src/server/plugins/engine/types.ts +26 -1
  47. package/src/server/plugins/engine/vision.ts +95 -0
@@ -0,0 +1,106 @@
1
+ import { slugSchema } from '@defra/forms-model';
2
+ import Boom from '@hapi/boom';
3
+ import Joi from 'joi';
4
+ import { FileUploadPageController } from "../../pageControllers/FileUploadPageController.js";
5
+ import { RepeatPageController } from "../../pageControllers/RepeatPageController.js";
6
+ import { redirectOrMakeHandler } from "../index.js";
7
+ import { actionSchema, confirmSchema, crumbSchema, itemIdSchema, pathSchema, stateSchema } from "../../../../schemas/index.js";
8
+
9
+ // Item delete GET route
10
+ function getHandler(request, h) {
11
+ const {
12
+ params
13
+ } = request;
14
+ return redirectOrMakeHandler(request, h, (page, context) => {
15
+ if (!(page instanceof RepeatPageController || page instanceof FileUploadPageController)) {
16
+ throw Boom.notFound(`No page found for /${params.path}`);
17
+ }
18
+ return page.makeGetItemDeleteRouteHandler()(request, context, h);
19
+ });
20
+ }
21
+ function postHandler(request, h) {
22
+ const {
23
+ params
24
+ } = request;
25
+ return redirectOrMakeHandler(request, h, (page, context) => {
26
+ const {
27
+ isForceAccess
28
+ } = context;
29
+ if (isForceAccess || !(page instanceof RepeatPageController || page instanceof FileUploadPageController)) {
30
+ throw Boom.notFound(`No page found for /${params.path}`);
31
+ }
32
+ return page.makePostItemDeleteRouteHandler()(request, context, h);
33
+ });
34
+ }
35
+ export function getRoutes(getRouteOptions, postRouteOptions) {
36
+ return [{
37
+ method: 'get',
38
+ path: '/{slug}/{path}/{itemId}/confirm-delete',
39
+ handler: getHandler,
40
+ options: {
41
+ ...getRouteOptions,
42
+ validate: {
43
+ params: Joi.object().keys({
44
+ slug: slugSchema,
45
+ path: pathSchema,
46
+ itemId: itemIdSchema
47
+ })
48
+ }
49
+ }
50
+ }, {
51
+ method: 'get',
52
+ path: '/preview/{state}/{slug}/{path}/{itemId}/confirm-delete',
53
+ handler: getHandler,
54
+ options: {
55
+ ...getRouteOptions,
56
+ validate: {
57
+ params: Joi.object().keys({
58
+ state: stateSchema,
59
+ slug: slugSchema,
60
+ path: pathSchema,
61
+ itemId: itemIdSchema
62
+ })
63
+ }
64
+ }
65
+ }, {
66
+ method: 'post',
67
+ path: '/{slug}/{path}/{itemId}/confirm-delete',
68
+ handler: postHandler,
69
+ options: {
70
+ ...postRouteOptions,
71
+ validate: {
72
+ params: Joi.object().keys({
73
+ slug: slugSchema,
74
+ path: pathSchema,
75
+ itemId: itemIdSchema
76
+ }),
77
+ payload: Joi.object().keys({
78
+ crumb: crumbSchema,
79
+ action: actionSchema,
80
+ confirm: confirmSchema
81
+ }).required()
82
+ }
83
+ }
84
+ }, {
85
+ method: 'post',
86
+ path: '/preview/{state}/{slug}/{path}/{itemId}/confirm-delete',
87
+ handler: postHandler,
88
+ options: {
89
+ ...postRouteOptions,
90
+ validate: {
91
+ params: Joi.object().keys({
92
+ state: stateSchema,
93
+ slug: slugSchema,
94
+ path: pathSchema,
95
+ itemId: itemIdSchema
96
+ }),
97
+ payload: Joi.object().keys({
98
+ crumb: crumbSchema,
99
+ action: actionSchema,
100
+ confirm: confirmSchema
101
+ }).required()
102
+ }
103
+ }
104
+ }];
105
+ }
106
+ //# sourceMappingURL=item-delete.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"item-delete.js","names":["slugSchema","Boom","Joi","FileUploadPageController","RepeatPageController","redirectOrMakeHandler","actionSchema","confirmSchema","crumbSchema","itemIdSchema","pathSchema","stateSchema","getHandler","request","h","params","page","context","notFound","path","makeGetItemDeleteRouteHandler","postHandler","isForceAccess","makePostItemDeleteRouteHandler","getRoutes","getRouteOptions","postRouteOptions","method","handler","options","validate","object","keys","slug","itemId","state","payload","crumb","action","confirm","required"],"sources":["../../../../../../src/server/plugins/engine/routes/repeaters/item-delete.ts"],"sourcesContent":["import { slugSchema } from '@defra/forms-model'\nimport Boom from '@hapi/boom'\nimport {\n type ResponseToolkit,\n type RouteOptions,\n type ServerRoute\n} from '@hapi/hapi'\nimport Joi from 'joi'\n\nimport { FileUploadPageController } from '~/src/server/plugins/engine/pageControllers/FileUploadPageController.js'\nimport { RepeatPageController } from '~/src/server/plugins/engine/pageControllers/RepeatPageController.js'\nimport { redirectOrMakeHandler } from '~/src/server/plugins/engine/routes/index.js'\nimport {\n type FormRequest,\n type FormRequestPayload,\n type FormRequestPayloadRefs,\n type FormRequestRefs\n} from '~/src/server/routes/types.js'\nimport {\n actionSchema,\n confirmSchema,\n crumbSchema,\n itemIdSchema,\n pathSchema,\n stateSchema\n} from '~/src/server/schemas/index.js'\n\n// Item delete GET route\nfunction getHandler(\n request: FormRequest,\n h: Pick<ResponseToolkit, 'redirect' | 'view'>\n) {\n const { params } = request\n\n return redirectOrMakeHandler(request, h, (page, context) => {\n if (\n !(\n page instanceof RepeatPageController ||\n page instanceof FileUploadPageController\n )\n ) {\n throw Boom.notFound(`No page found for /${params.path}`)\n }\n\n return page.makeGetItemDeleteRouteHandler()(request, context, h)\n })\n}\n\nfunction postHandler(\n request: FormRequestPayload,\n h: Pick<ResponseToolkit, 'redirect' | 'view'>\n) {\n const { params } = request\n\n return redirectOrMakeHandler(request, h, (page, context) => {\n const { isForceAccess } = context\n\n if (\n isForceAccess ||\n !(\n page instanceof RepeatPageController ||\n page instanceof FileUploadPageController\n )\n ) {\n throw Boom.notFound(`No page found for /${params.path}`)\n }\n\n return page.makePostItemDeleteRouteHandler()(request, context, h)\n })\n}\n\nexport function getRoutes(\n getRouteOptions: RouteOptions<FormRequestRefs>,\n postRouteOptions: RouteOptions<FormRequestPayloadRefs>\n): (ServerRoute<FormRequestRefs> | ServerRoute<FormRequestPayloadRefs>)[] {\n return [\n {\n method: 'get',\n path: '/{slug}/{path}/{itemId}/confirm-delete',\n handler: getHandler,\n options: {\n ...getRouteOptions,\n validate: {\n params: Joi.object().keys({\n slug: slugSchema,\n path: pathSchema,\n itemId: itemIdSchema\n })\n }\n }\n },\n\n {\n method: 'get',\n path: '/preview/{state}/{slug}/{path}/{itemId}/confirm-delete',\n handler: getHandler,\n options: {\n ...getRouteOptions,\n validate: {\n params: Joi.object().keys({\n state: stateSchema,\n slug: slugSchema,\n path: pathSchema,\n itemId: itemIdSchema\n })\n }\n }\n },\n\n {\n method: 'post',\n path: '/{slug}/{path}/{itemId}/confirm-delete',\n handler: postHandler,\n options: {\n ...postRouteOptions,\n validate: {\n params: Joi.object().keys({\n slug: slugSchema,\n path: pathSchema,\n itemId: itemIdSchema\n }),\n payload: Joi.object()\n .keys({\n crumb: crumbSchema,\n action: actionSchema,\n confirm: confirmSchema\n })\n .required()\n }\n }\n },\n\n {\n method: 'post',\n path: '/preview/{state}/{slug}/{path}/{itemId}/confirm-delete',\n handler: postHandler,\n options: {\n ...postRouteOptions,\n validate: {\n params: Joi.object().keys({\n state: stateSchema,\n slug: slugSchema,\n path: pathSchema,\n itemId: itemIdSchema\n }),\n payload: Joi.object()\n .keys({\n crumb: crumbSchema,\n action: actionSchema,\n confirm: confirmSchema\n })\n .required()\n }\n }\n }\n ]\n}\n"],"mappings":"AAAA,SAASA,UAAU,QAAQ,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EACF,CAAC,EAED;IACEb,MAAM,EAAE,MAAM;IACdR,IAAI,EAAE,wDAAwD;IAC9DS,OAAO,EAAEP,WAAW;IACpBQ,OAAO,EAAE;MACP,GAAGH,gBAAgB;MACnBI,QAAQ,EAAE;QACRf,MAAM,EAAEb,GAAG,CAAC6B,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC;UACxBG,KAAK,EAAExB,WAAW;UAClBsB,IAAI,EAAEjC,UAAU;UAChBmB,IAAI,EAAET,UAAU;UAChBwB,MAAM,EAAEzB;QACV,CAAC,CAAC;QACF2B,OAAO,EAAElC,GAAG,CAAC6B,MAAM,CAAC,CAAC,CAClBC,IAAI,CAAC;UACJK,KAAK,EAAE7B,WAAW;UAClB8B,MAAM,EAAEhC,YAAY;UACpBiC,OAAO,EAAEhC;QACX,CAAC,CAAC,CACDiC,QAAQ,CAAC;MACd;IACF;EACF,CAAC,CACF;AACH","ignoreList":[]}
@@ -0,0 +1,3 @@
1
+ import { type RouteOptions, type ServerRoute } from '@hapi/hapi';
2
+ import { type FormRequestPayloadRefs, type FormRequestRefs } from '~/src/server/routes/types.js';
3
+ export declare function getRoutes(getRouteOptions: RouteOptions<FormRequestRefs>, postRouteOptions: RouteOptions<FormRequestPayloadRefs>): (ServerRoute<FormRequestRefs> | ServerRoute<FormRequestPayloadRefs>)[];
@@ -0,0 +1,98 @@
1
+ // List summary GET route
2
+ import { slugSchema } from '@defra/forms-model';
3
+ import Boom from '@hapi/boom';
4
+ import Joi from 'joi';
5
+ import { RepeatPageController } from "../../pageControllers/RepeatPageController.js";
6
+ import { redirectOrMakeHandler } from "../index.js";
7
+ import { actionSchema, crumbSchema, pathSchema, stateSchema } from "../../../../schemas/index.js";
8
+ function getHandler(request, h) {
9
+ const {
10
+ params
11
+ } = request;
12
+ return redirectOrMakeHandler(request, h, (page, context) => {
13
+ if (!(page instanceof RepeatPageController)) {
14
+ throw Boom.notFound(`No repeater page found for /${params.path}`);
15
+ }
16
+ return page.makeGetListSummaryRouteHandler()(request, context, h);
17
+ });
18
+ }
19
+ function postHandler(request, h) {
20
+ const {
21
+ params
22
+ } = request;
23
+ return redirectOrMakeHandler(request, h, (page, context) => {
24
+ const {
25
+ isForceAccess
26
+ } = context;
27
+ if (isForceAccess || !(page instanceof RepeatPageController)) {
28
+ throw Boom.notFound(`No repeater page found for /${params.path}`);
29
+ }
30
+ return page.makePostListSummaryRouteHandler()(request, context, h);
31
+ });
32
+ }
33
+ export function getRoutes(getRouteOptions, postRouteOptions) {
34
+ return [{
35
+ method: 'get',
36
+ path: '/{slug}/{path}/summary',
37
+ handler: getHandler,
38
+ options: {
39
+ ...getRouteOptions,
40
+ validate: {
41
+ params: Joi.object().keys({
42
+ slug: slugSchema,
43
+ path: pathSchema
44
+ })
45
+ }
46
+ }
47
+ }, {
48
+ method: 'get',
49
+ path: '/preview/{state}/{slug}/{path}/summary',
50
+ handler: getHandler,
51
+ options: {
52
+ ...getRouteOptions,
53
+ validate: {
54
+ params: Joi.object().keys({
55
+ state: stateSchema,
56
+ slug: slugSchema,
57
+ path: pathSchema
58
+ })
59
+ }
60
+ }
61
+ }, {
62
+ method: 'post',
63
+ path: '/{slug}/{path}/summary',
64
+ handler: postHandler,
65
+ options: {
66
+ ...postRouteOptions,
67
+ validate: {
68
+ params: Joi.object().keys({
69
+ slug: slugSchema,
70
+ path: pathSchema
71
+ }),
72
+ payload: Joi.object().keys({
73
+ crumb: crumbSchema,
74
+ action: actionSchema
75
+ }).required()
76
+ }
77
+ }
78
+ }, {
79
+ method: 'post',
80
+ path: '/preview/{state}/{slug}/{path}/summary',
81
+ handler: postHandler,
82
+ options: {
83
+ ...postRouteOptions,
84
+ validate: {
85
+ params: Joi.object().keys({
86
+ state: stateSchema,
87
+ slug: slugSchema,
88
+ path: pathSchema
89
+ }),
90
+ payload: Joi.object().keys({
91
+ crumb: crumbSchema,
92
+ action: actionSchema
93
+ }).required()
94
+ }
95
+ }
96
+ }];
97
+ }
98
+ //# sourceMappingURL=summary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"summary.js","names":["slugSchema","Boom","Joi","RepeatPageController","redirectOrMakeHandler","actionSchema","crumbSchema","pathSchema","stateSchema","getHandler","request","h","params","page","context","notFound","path","makeGetListSummaryRouteHandler","postHandler","isForceAccess","makePostListSummaryRouteHandler","getRoutes","getRouteOptions","postRouteOptions","method","handler","options","validate","object","keys","slug","state","payload","crumb","action","required"],"sources":["../../../../../../src/server/plugins/engine/routes/repeaters/summary.ts"],"sourcesContent":["// List summary GET route\nimport { slugSchema } from '@defra/forms-model'\nimport Boom from '@hapi/boom'\nimport {\n type ResponseToolkit,\n type RouteOptions,\n type ServerRoute\n} from '@hapi/hapi'\nimport Joi from 'joi'\n\nimport { RepeatPageController } from '~/src/server/plugins/engine/pageControllers/RepeatPageController.js'\nimport { redirectOrMakeHandler } from '~/src/server/plugins/engine/routes/index.js'\nimport {\n type FormRequest,\n type FormRequestPayload,\n type FormRequestPayloadRefs,\n type FormRequestRefs\n} from '~/src/server/routes/types.js'\nimport {\n actionSchema,\n crumbSchema,\n pathSchema,\n stateSchema\n} from '~/src/server/schemas/index.js'\n\nfunction getHandler(\n request: FormRequest,\n h: Pick<ResponseToolkit, 'redirect' | 'view'>\n) {\n const { params } = request\n\n return redirectOrMakeHandler(request, h, (page, context) => {\n if (!(page instanceof RepeatPageController)) {\n throw Boom.notFound(`No repeater page found for /${params.path}`)\n }\n\n return page.makeGetListSummaryRouteHandler()(request, context, h)\n })\n}\n\nfunction postHandler(\n request: FormRequestPayload,\n h: Pick<ResponseToolkit, 'redirect' | 'view'>\n) {\n const { params } = request\n\n return redirectOrMakeHandler(request, h, (page, context) => {\n const { isForceAccess } = context\n\n if (isForceAccess || !(page instanceof RepeatPageController)) {\n throw Boom.notFound(`No repeater page found for /${params.path}`)\n }\n\n return page.makePostListSummaryRouteHandler()(request, context, h)\n })\n}\n\nexport function getRoutes(\n getRouteOptions: RouteOptions<FormRequestRefs>,\n postRouteOptions: RouteOptions<FormRequestPayloadRefs>\n): (ServerRoute<FormRequestRefs> | ServerRoute<FormRequestPayloadRefs>)[] {\n return [\n {\n method: 'get',\n path: '/{slug}/{path}/summary',\n handler: getHandler,\n options: {\n ...getRouteOptions,\n validate: {\n params: Joi.object().keys({\n slug: slugSchema,\n path: pathSchema\n })\n }\n }\n },\n\n {\n method: 'get',\n path: '/preview/{state}/{slug}/{path}/summary',\n handler: getHandler,\n options: {\n ...getRouteOptions,\n validate: {\n params: Joi.object().keys({\n state: stateSchema,\n slug: slugSchema,\n path: pathSchema\n })\n }\n }\n },\n\n {\n method: 'post',\n path: '/{slug}/{path}/summary',\n handler: postHandler,\n options: {\n ...postRouteOptions,\n validate: {\n params: Joi.object().keys({\n slug: slugSchema,\n path: pathSchema\n }),\n payload: Joi.object()\n .keys({\n crumb: crumbSchema,\n action: actionSchema\n })\n .required()\n }\n }\n },\n\n {\n method: 'post',\n path: '/preview/{state}/{slug}/{path}/summary',\n handler: postHandler,\n options: {\n ...postRouteOptions,\n validate: {\n params: Joi.object().keys({\n state: stateSchema,\n slug: slugSchema,\n path: pathSchema\n }),\n payload: Joi.object()\n .keys({\n crumb: crumbSchema,\n action: actionSchema\n })\n .required()\n }\n }\n }\n ]\n}\n"],"mappings":"AAAA;AACA,SAASA,UAAU,QAAQ,oBAAoB;AAC/C,OAAOC,IAAI,MAAM,YAAY;AAM7B,OAAOC,GAAG,MAAM,KAAK;AAErB,SAASC,oBAAoB;AAC7B,SAASC,qBAAqB;AAO9B,SACEC,YAAY,EACZC,WAAW,EACXC,UAAU,EACVC,WAAW;AAGb,SAASC,UAAUA,CACjBC,OAAoB,EACpBC,CAA6C,EAC7C;EACA,MAAM;IAAEC;EAAO,CAAC,GAAGF,OAAO;EAE1B,OAAON,qBAAqB,CAACM,OAAO,EAAEC,CAAC,EAAE,CAACE,IAAI,EAAEC,OAAO,KAAK;IAC1D,IAAI,EAAED,IAAI,YAAYV,oBAAoB,CAAC,EAAE;MAC3C,MAAMF,IAAI,CAACc,Q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,CAACC,IAAI,CAAC;UACxBE,KAAK,EAAEvB,WAAW;UAClBsB,IAAI,EAAE9B,UAAU;UAChBgB,IAAI,EAAET;QACR,CAAC;MACH;IACF;EACF,CAAC,EAED;IACEiB,MAAM,EAAE,MAAM;IACdR,IAAI,EAAE,wBAAwB;IAC9BS,OAAO,EAAEP,WAAW;IACpBQ,OAAO,EAAE;MACP,GAAGH,gBAAgB;MACnBI,QAAQ,EAAE;QACRf,MAAM,EAAEV,GAAG,CAAC0B,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC;UACxBC,IAAI,EAAE9B,UAAU;UAChBgB,IAAI,EAAET;QACR,CAAC,CAAC;QACFyB,OAAO,EAAE9B,GAAG,CAAC0B,MAAM,CAAC,CAAC,CAClBC,IAAI,CAAC;UACJI,KAAK,EAAE3B,WAAW;UAClB4B,MAAM,EAAE7B;QACV,CAAC,CAAC,CACD8B,QAAQ,CAAC;MACd;IACF;EACF,CAAC,EAED;IACEX,MAAM,EAAE,MAAM;IACdR,IAAI,EAAE,wCAAwC;IAC9CS,OAAO,EAAEP,WAAW;IACpBQ,OAAO,EAAE;MACP,GAAGH,gBAAgB;MACnBI,QAAQ,EAAE;QACRf,MAAM,EAAEV,GAAG,CAAC0B,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC;UACxBE,KAAK,EAAEvB,WAAW;UAClBsB,IAAI,EAAE9B,UAAU;UAChBgB,IAAI,EAAET;QACR,CAAC,CAAC;QACFyB,OAAO,EAAE9B,GAAG,CAAC0B,MAAM,CAAC,CAAC,CAClBC,IAAI,CAAC;UACJI,KAAK,EAAE3B,WAAW;UAClB4B,MAAM,EAAE7B;QACV,CAAC,CAAC,CACD8B,QAAQ,CAAC;MACd;IACF;EACF,CAAC,CACF;AACH","ignoreList":[]}
@@ -1,11 +1,14 @@
1
1
  import { type ComponentDef, type Item, type List, type Page } from '@defra/forms-model';
2
+ import { type PluginProperties, type Request } from '@hapi/hapi';
2
3
  import { type JoiExpression, type ValidationErrorItem } from 'joi';
3
4
  import { type Component } from '~/src/server/plugins/engine/components/helpers.js';
4
5
  import { type BackLink, type ComponentText, type ComponentViewModel } from '~/src/server/plugins/engine/components/types.js';
6
+ import { type FormModel } from '~/src/server/plugins/engine/models/index.js';
5
7
  import { type PageController } from '~/src/server/plugins/engine/pageControllers/PageController.js';
6
8
  import { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers.js';
7
9
  import { type ViewContext } from '~/src/server/plugins/nunjucks/types.js';
8
- import { type FormAction, type FormRequest } from '~/src/server/routes/types.js';
10
+ import { type FormAction, type FormRequest, type FormRequestPayload } from '~/src/server/routes/types.js';
11
+ import { type Services } from '~/src/server/types.js';
9
12
  /**
10
13
  * Form submission state stores the following in Redis:
11
14
  * Props containing user's submitted values as `{ [inputId]: value }` or as `{ [sectionName]: { [inputName]: value } }`
@@ -254,3 +257,18 @@ export interface ErrorMessageTemplateList {
254
257
  baseErrors: ErrorMessageTemplate[];
255
258
  advancedSettingsErrors: ErrorMessageTemplate[];
256
259
  }
260
+ export interface PluginOptions {
261
+ model?: FormModel;
262
+ services?: Services;
263
+ controllers?: Record<string, typeof PageController>;
264
+ cacheName?: string;
265
+ filters?: Record<string, FilterFunction>;
266
+ keyGenerator?: (request: Request | FormRequest | FormRequestPayload) => string;
267
+ sessionHydrator?: (request: Request | FormRequest | FormRequestPayload) => Promise<FormSubmissionState>;
268
+ pluginPath?: string;
269
+ nunjucks: {
270
+ baseLayoutPath: string;
271
+ paths: string[];
272
+ };
273
+ viewContext: PluginProperties['forms-engine-plugin']['viewContext'];
274
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":["UploadStatus","FileStatus"],"sources":["../../../../src/server/plugins/engine/types.ts"],"sourcesContent":["import {\n type ComponentDef,\n type Item,\n type List,\n type Page\n} from '@defra/forms-model'\nimport { type JoiExpression, type ValidationErrorItem } from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { type Component } from '~/src/server/plugins/engine/components/helpers.js'\nimport {\n type BackLink,\n type ComponentText,\n type ComponentViewModel\n} from '~/src/server/plugins/engine/components/types.js'\nimport { type PageController } from '~/src/server/plugins/engine/pageControllers/PageController.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers.js'\nimport { type ViewContext } from '~/src/server/plugins/nunjucks/types.js'\nimport { type FormAction, type FormRequest } from '~/src/server/routes/types.js'\n\n/**\n * Form submission state stores the following in Redis:\n * Props containing user's submitted values as `{ [inputId]: value }` or as `{ [sectionName]: { [inputName]: value } }`\n * a) . e.g:\n * ```ts\n * {\n * _C9PRHmsgt: 'Ben',\n * WfLk9McjzX: 'Music',\n * IK7jkUFCBL: 'Royal Academy of Music'\n * }\n * ```\n *\n * b)\n * ```ts\n * {\n * checkBeforeYouStart: { ukPassport: true },\n * applicantDetails: {\n * numberOfApplicants: 1,\n * phoneNumber: '77777777',\n * emailAddress: 'aaa@aaa.com'\n * },\n * applicantOneDetails: {\n * firstName: 'a',\n * middleName: 'a',\n * lastName: 'a',\n * address: { addressLine1: 'a', addressLine2: 'a', town: 'a', postcode: 'a' }\n * }\n * }\n * ```\n */\n\n/**\n * Form submission state\n */\nexport type FormSubmissionState = {\n upload?: Record<string, TempFileState>\n} & FormState\n\nexport interface FormSubmissionError\n extends Pick<ValidationErrorItem, 'context' | 'path'> {\n href: string // e.g: '#dateField__day'\n name: string // e.g: 'dateField__day'\n text: string // e.g: 'Date field must be a real date'\n}\n\nexport interface FormParams {\n action?: FormAction\n confirm?: true\n crumb?: string\n itemId?: string\n}\n\n/**\n * Form POST for question pages\n * (after Joi has converted value types)\n */\nexport type FormPayload = FormParams & Partial<Record<string, FormValue>>\n\nexport type FormValue =\n | Item['value']\n | Item['value'][]\n | UploadState\n | RepeatListState\n | undefined\n\nexport type FormState = Partial<Record<string, FormStateValue>>\nexport type FormStateValue = Exclude<FormValue, undefined> | null\n\nexport interface FormValidationResult<\n ValueType extends FormPayload | FormSubmissionState\n> {\n value: ValueType\n errors: FormSubmissionError[] | undefined\n}\n\nexport interface FormContext {\n /**\n * Evaluation form state only (filtered by visited paths),\n * with values formatted for condition evaluation using\n * {@link FormComponent.getContextValueFromState}\n */\n evaluationState: FormState\n\n /**\n * Relevant form state only (filtered by visited paths)\n */\n relevantState: FormState\n\n /**\n * Relevant pages only (filtered by visited paths)\n */\n relevantPages: PageControllerClass[]\n\n /**\n * Form submission payload (single page)\n */\n payload: FormPayload\n\n /**\n * Form submission state (entire form)\n */\n state: FormSubmissionState\n\n /**\n * Validation errors (entire form)\n */\n errors?: FormSubmissionError[]\n\n /**\n * Visited paths evaluated from form state\n */\n paths: string[]\n\n /**\n * Preview URL direct access is allowed\n */\n isForceAccess: boolean\n\n /**\n * Miscellaneous extra data from event responses\n */\n data: object\n\n pageDefMap: Map<string, Page>\n listDefMap: Map<string, List>\n componentDefMap: Map<string, ComponentDef>\n pageMap: Map<string, PageControllerClass>\n componentMap: Map<string, Component>\n referenceNumber: string\n}\n\nexport type FormContextRequest = (\n | {\n method: 'get'\n payload?: undefined\n }\n | {\n method: 'post'\n payload: FormPayload\n }\n | {\n method: FormRequest['method']\n payload?: object | undefined\n }\n) &\n Pick<FormRequest, 'app' | 'method' | 'params' | 'path' | 'query' | 'url'>\n\nexport interface UploadInitiateResponse {\n uploadId: string\n uploadUrl: string\n statusUrl: string\n}\n\nexport enum UploadStatus {\n initiated = 'initiated',\n pending = 'pending',\n ready = 'ready'\n}\n\nexport enum FileStatus {\n complete = 'complete',\n rejected = 'rejected',\n pending = 'pending'\n}\n\nexport type UploadState = FileState[]\n\nexport type FileUpload = {\n fileId: string\n filename: string\n contentLength: number\n} & (\n | {\n fileStatus: FileStatus.complete | FileStatus.rejected | FileStatus.pending\n errorMessage?: string\n }\n | {\n fileStatus: FileStatus.complete\n errorMessage?: undefined\n }\n)\n\nexport interface FileUploadMetadata {\n retrievalKey: string\n}\n\nexport type UploadStatusResponse =\n | {\n uploadStatus: UploadStatus.initiated\n metadata: FileUploadMetadata\n form: { file?: undefined }\n }\n | {\n uploadStatus: UploadStatus.pending | UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles?: number\n }\n | {\n uploadStatus: UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles: 0\n }\n\nexport type UploadStatusFileResponse = Exclude<\n UploadStatusResponse,\n { uploadStatus: UploadStatus.initiated }\n>\n\nexport interface FileState {\n uploadId: string\n status: UploadStatusFileResponse\n}\n\nexport interface TempFileState {\n upload?: UploadInitiateResponse\n files: UploadState\n}\n\nexport interface RepeatItemState extends FormPayload {\n itemId: string\n}\n\nexport type RepeatListState = RepeatItemState[]\n\nexport interface CheckAnswers {\n title?: ComponentText\n summaryList: SummaryList\n}\n\nexport interface SummaryList {\n classes?: string\n rows: SummaryListRow[]\n}\n\nexport interface SummaryListRow {\n key: ComponentText\n value: ComponentText\n actions?: { items: SummaryListAction[] }\n}\n\nexport type SummaryListAction = ComponentText & {\n href: string\n visuallyHiddenText: string\n}\n\nexport interface PageViewModelBase extends Partial<ViewContext> {\n page: PageController\n name?: string\n pageTitle: string\n sectionTitle?: string\n showTitle: boolean\n isStartPage: boolean\n backLink?: BackLink\n feedbackLink?: string\n serviceUrl: string\n phaseTag?: string\n}\n\nexport interface ItemDeletePageViewModel extends PageViewModelBase {\n context: FormContext\n itemTitle: string\n confirmation?: ComponentText\n buttonConfirm: ComponentText\n buttonCancel: ComponentText\n}\n\nexport interface FormPageViewModel extends PageViewModelBase {\n components: ComponentViewModel[]\n context: FormContext\n errors?: FormSubmissionError[]\n hasMissingNotificationEmail?: boolean\n}\n\nexport interface RepeaterSummaryPageViewModel extends PageViewModelBase {\n context: FormContext\n errors?: FormSubmissionError[]\n checkAnswers: CheckAnswers[]\n repeatTitle: string\n}\n\nexport interface FeaturedFormPageViewModel extends FormPageViewModel {\n formAction?: string\n formComponent: ComponentViewModel\n componentsBefore: ComponentViewModel[]\n uploadId: string | undefined\n proxyUrl: string | null\n}\n\nexport type PageViewModel =\n | PageViewModelBase\n | ItemDeletePageViewModel\n | FormPageViewModel\n | RepeaterSummaryPageViewModel\n | FeaturedFormPageViewModel\n\nexport type FilterFunction = (value: unknown) => unknown\nexport interface ErrorMessageTemplate {\n type: string\n template: JoiExpression\n}\n\nexport interface ErrorMessageTemplateList {\n baseErrors: ErrorMessageTemplate[]\n advancedSettingsErrors: ErrorMessageTemplate[]\n}\n"],"mappings":"AAoBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAmBA;AACA;AACA;AACA;;AAkGA,WAAYA,YAAY,0BAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAA,OAAZA,YAAY;AAAA;AAMxB,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA","ignoreList":[]}
1
+ {"version":3,"file":"types.js","names":["UploadStatus","FileStatus"],"sources":["../../../../src/server/plugins/engine/types.ts"],"sourcesContent":["import {\n type ComponentDef,\n type Item,\n type List,\n type Page\n} from '@defra/forms-model'\nimport { type PluginProperties, type Request } from '@hapi/hapi'\nimport { type JoiExpression, type ValidationErrorItem } from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { type Component } from '~/src/server/plugins/engine/components/helpers.js'\nimport {\n type BackLink,\n type ComponentText,\n type ComponentViewModel\n} from '~/src/server/plugins/engine/components/types.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { type PageController } from '~/src/server/plugins/engine/pageControllers/PageController.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers.js'\nimport { type ViewContext } from '~/src/server/plugins/nunjucks/types.js'\nimport {\n type FormAction,\n type FormRequest,\n type FormRequestPayload\n} from '~/src/server/routes/types.js'\nimport { type Services } from '~/src/server/types.js'\n\n/**\n * Form submission state stores the following in Redis:\n * Props containing user's submitted values as `{ [inputId]: value }` or as `{ [sectionName]: { [inputName]: value } }`\n * a) . e.g:\n * ```ts\n * {\n * _C9PRHmsgt: 'Ben',\n * WfLk9McjzX: 'Music',\n * IK7jkUFCBL: 'Royal Academy of Music'\n * }\n * ```\n *\n * b)\n * ```ts\n * {\n * checkBeforeYouStart: { ukPassport: true },\n * applicantDetails: {\n * numberOfApplicants: 1,\n * phoneNumber: '77777777',\n * emailAddress: 'aaa@aaa.com'\n * },\n * applicantOneDetails: {\n * firstName: 'a',\n * middleName: 'a',\n * lastName: 'a',\n * address: { addressLine1: 'a', addressLine2: 'a', town: 'a', postcode: 'a' }\n * }\n * }\n * ```\n */\n\n/**\n * Form submission state\n */\nexport type FormSubmissionState = {\n upload?: Record<string, TempFileState>\n} & FormState\n\nexport interface FormSubmissionError\n extends Pick<ValidationErrorItem, 'context' | 'path'> {\n href: string // e.g: '#dateField__day'\n name: string // e.g: 'dateField__day'\n text: string // e.g: 'Date field must be a real date'\n}\n\nexport interface FormParams {\n action?: FormAction\n confirm?: true\n crumb?: string\n itemId?: string\n}\n\n/**\n * Form POST for question pages\n * (after Joi has converted value types)\n */\nexport type FormPayload = FormParams & Partial<Record<string, FormValue>>\n\nexport type FormValue =\n | Item['value']\n | Item['value'][]\n | UploadState\n | RepeatListState\n | undefined\n\nexport type FormState = Partial<Record<string, FormStateValue>>\nexport type FormStateValue = Exclude<FormValue, undefined> | null\n\nexport interface FormValidationResult<\n ValueType extends FormPayload | FormSubmissionState\n> {\n value: ValueType\n errors: FormSubmissionError[] | undefined\n}\n\nexport interface FormContext {\n /**\n * Evaluation form state only (filtered by visited paths),\n * with values formatted for condition evaluation using\n * {@link FormComponent.getContextValueFromState}\n */\n evaluationState: FormState\n\n /**\n * Relevant form state only (filtered by visited paths)\n */\n relevantState: FormState\n\n /**\n * Relevant pages only (filtered by visited paths)\n */\n relevantPages: PageControllerClass[]\n\n /**\n * Form submission payload (single page)\n */\n payload: FormPayload\n\n /**\n * Form submission state (entire form)\n */\n state: FormSubmissionState\n\n /**\n * Validation errors (entire form)\n */\n errors?: FormSubmissionError[]\n\n /**\n * Visited paths evaluated from form state\n */\n paths: string[]\n\n /**\n * Preview URL direct access is allowed\n */\n isForceAccess: boolean\n\n /**\n * Miscellaneous extra data from event responses\n */\n data: object\n\n pageDefMap: Map<string, Page>\n listDefMap: Map<string, List>\n componentDefMap: Map<string, ComponentDef>\n pageMap: Map<string, PageControllerClass>\n componentMap: Map<string, Component>\n referenceNumber: string\n}\n\nexport type FormContextRequest = (\n | {\n method: 'get'\n payload?: undefined\n }\n | {\n method: 'post'\n payload: FormPayload\n }\n | {\n method: FormRequest['method']\n payload?: object | undefined\n }\n) &\n Pick<FormRequest, 'app' | 'method' | 'params' | 'path' | 'query' | 'url'>\n\nexport interface UploadInitiateResponse {\n uploadId: string\n uploadUrl: string\n statusUrl: string\n}\n\nexport enum UploadStatus {\n initiated = 'initiated',\n pending = 'pending',\n ready = 'ready'\n}\n\nexport enum FileStatus {\n complete = 'complete',\n rejected = 'rejected',\n pending = 'pending'\n}\n\nexport type UploadState = FileState[]\n\nexport type FileUpload = {\n fileId: string\n filename: string\n contentLength: number\n} & (\n | {\n fileStatus: FileStatus.complete | FileStatus.rejected | FileStatus.pending\n errorMessage?: string\n }\n | {\n fileStatus: FileStatus.complete\n errorMessage?: undefined\n }\n)\n\nexport interface FileUploadMetadata {\n retrievalKey: string\n}\n\nexport type UploadStatusResponse =\n | {\n uploadStatus: UploadStatus.initiated\n metadata: FileUploadMetadata\n form: { file?: undefined }\n }\n | {\n uploadStatus: UploadStatus.pending | UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles?: number\n }\n | {\n uploadStatus: UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles: 0\n }\n\nexport type UploadStatusFileResponse = Exclude<\n UploadStatusResponse,\n { uploadStatus: UploadStatus.initiated }\n>\n\nexport interface FileState {\n uploadId: string\n status: UploadStatusFileResponse\n}\n\nexport interface TempFileState {\n upload?: UploadInitiateResponse\n files: UploadState\n}\n\nexport interface RepeatItemState extends FormPayload {\n itemId: string\n}\n\nexport type RepeatListState = RepeatItemState[]\n\nexport interface CheckAnswers {\n title?: ComponentText\n summaryList: SummaryList\n}\n\nexport interface SummaryList {\n classes?: string\n rows: SummaryListRow[]\n}\n\nexport interface SummaryListRow {\n key: ComponentText\n value: ComponentText\n actions?: { items: SummaryListAction[] }\n}\n\nexport type SummaryListAction = ComponentText & {\n href: string\n visuallyHiddenText: string\n}\n\nexport interface PageViewModelBase extends Partial<ViewContext> {\n page: PageController\n name?: string\n pageTitle: string\n sectionTitle?: string\n showTitle: boolean\n isStartPage: boolean\n backLink?: BackLink\n feedbackLink?: string\n serviceUrl: string\n phaseTag?: string\n}\n\nexport interface ItemDeletePageViewModel extends PageViewModelBase {\n context: FormContext\n itemTitle: string\n confirmation?: ComponentText\n buttonConfirm: ComponentText\n buttonCancel: ComponentText\n}\n\nexport interface FormPageViewModel extends PageViewModelBase {\n components: ComponentViewModel[]\n context: FormContext\n errors?: FormSubmissionError[]\n hasMissingNotificationEmail?: boolean\n}\n\nexport interface RepeaterSummaryPageViewModel extends PageViewModelBase {\n context: FormContext\n errors?: FormSubmissionError[]\n checkAnswers: CheckAnswers[]\n repeatTitle: string\n}\n\nexport interface FeaturedFormPageViewModel extends FormPageViewModel {\n formAction?: string\n formComponent: ComponentViewModel\n componentsBefore: ComponentViewModel[]\n uploadId: string | undefined\n proxyUrl: string | null\n}\n\nexport type PageViewModel =\n | PageViewModelBase\n | ItemDeletePageViewModel\n | FormPageViewModel\n | RepeaterSummaryPageViewModel\n | FeaturedFormPageViewModel\n\nexport type FilterFunction = (value: unknown) => unknown\nexport interface ErrorMessageTemplate {\n type: string\n template: JoiExpression\n}\n\nexport interface ErrorMessageTemplateList {\n baseErrors: ErrorMessageTemplate[]\n advancedSettingsErrors: ErrorMessageTemplate[]\n}\n\nexport interface PluginOptions {\n model?: FormModel\n services?: Services\n controllers?: Record<string, typeof PageController>\n cacheName?: string\n filters?: Record<string, FilterFunction>\n keyGenerator?: (request: Request | FormRequest | FormRequestPayload) => string\n sessionHydrator?: (\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<FormSubmissionState>\n pluginPath?: string\n nunjucks: {\n baseLayoutPath: string\n paths: string[]\n }\n viewContext: PluginProperties['forms-engine-plugin']['viewContext']\n}\n"],"mappings":"AA2BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAmBA;AACA;AACA;AACA;;AAkGA,WAAYA,YAAY,0BAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAA,OAAZA,YAAY;AAAA;AAMxB,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA","ignoreList":[]}
@@ -0,0 +1,12 @@
1
+ import { type Server } from '@hapi/hapi';
2
+ import { type Environment } from 'nunjucks';
3
+ import { type PluginOptions } from '~/src/server/plugins/engine/types.js';
4
+ export declare function registerVision(server: Server, pluginOptions: PluginOptions): Promise<void>;
5
+ interface CompileOptions {
6
+ environment: Environment;
7
+ }
8
+ export interface EngineConfigurationObject {
9
+ compileOptions: CompileOptions;
10
+ }
11
+ export declare function findPackageRoot(): string;
12
+ export {};
@@ -0,0 +1,55 @@
1
+ import { existsSync } from 'fs';
2
+ import { dirname, join } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import vision from '@hapi/vision';
5
+ import nunjucks from 'nunjucks';
6
+ import resolvePkg from 'resolve';
7
+ import { VIEW_PATH, context, prepareNunjucksEnvironment } from "./index.js";
8
+ export async function registerVision(server, pluginOptions) {
9
+ const packageRoot = findPackageRoot();
10
+ const govukFrontendPath = dirname(resolvePkg.sync('govuk-frontend/package.json'));
11
+ const viewPathResolved = join(packageRoot, VIEW_PATH);
12
+ const paths = [...pluginOptions.nunjucks.paths, viewPathResolved, join(govukFrontendPath, 'dist')];
13
+ await server.register({
14
+ plugin: vision,
15
+ options: {
16
+ engines: {
17
+ html: {
18
+ compile: (path, compileOptions) => {
19
+ const template = nunjucks.compile(path, compileOptions.environment);
20
+ return context => {
21
+ return template.render(context);
22
+ };
23
+ },
24
+ prepare: (options, next) => {
25
+ // Nunjucks also needs an additional path configuration
26
+ // to use the templates and macros from `govuk-frontend`
27
+ const environment = nunjucks.configure(paths);
28
+
29
+ // Applies custom filters and globals for nunjucks
30
+ // that are required by the `forms-engine-plugin`
31
+ prepareNunjucksEnvironment(environment, pluginOptions.filters);
32
+ options.compileOptions.environment = environment;
33
+ next();
34
+ }
35
+ }
36
+ },
37
+ path: paths,
38
+ // Provides global context used with all templates
39
+ context
40
+ }
41
+ });
42
+ }
43
+ export function findPackageRoot() {
44
+ const currentFileName = fileURLToPath(import.meta.url);
45
+ const currentDirectoryName = dirname(currentFileName);
46
+ let dir = currentDirectoryName;
47
+ while (dir !== '/') {
48
+ if (existsSync(join(dir, 'package.json'))) {
49
+ return dir;
50
+ }
51
+ dir = dirname(dir);
52
+ }
53
+ throw new Error('package.json not found in parent directories');
54
+ }
55
+ //# sourceMappingURL=vision.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vision.js","names":["existsSync","dirname","join","fileURLToPath","vision","nunjucks","resolvePkg","VIEW_PATH","context","prepareNunjucksEnvironment","registerVision","server","pluginOptions","packageRoot","findPackageRoot","govukFrontendPath","sync","viewPathResolved","paths","register","plugin","options","engines","html","compile","path","compileOptions","template","environment","render","prepare","next","configure","filters","currentFileName","import","meta","url","currentDirectoryName","dir","Error"],"sources":["../../../../src/server/plugins/engine/vision.ts"],"sourcesContent":["import { existsSync } from 'fs'\nimport { dirname, join } from 'path'\nimport { fileURLToPath } from 'url'\n\nimport { type Server } from '@hapi/hapi'\nimport vision from '@hapi/vision'\nimport nunjucks, { type Environment } from 'nunjucks'\nimport resolvePkg from 'resolve'\n\nimport {\n VIEW_PATH,\n context,\n prepareNunjucksEnvironment\n} from '~/src/server/plugins/engine/index.js'\nimport { type PluginOptions } from '~/src/server/plugins/engine/types.js'\n\nexport async function registerVision(\n server: Server,\n pluginOptions: PluginOptions\n) {\n const packageRoot = findPackageRoot()\n const govukFrontendPath = dirname(\n resolvePkg.sync('govuk-frontend/package.json')\n )\n\n const viewPathResolved = join(packageRoot, VIEW_PATH)\n\n const paths = [\n ...pluginOptions.nunjucks.paths,\n viewPathResolved,\n join(govukFrontendPath, 'dist')\n ]\n\n await server.register({\n plugin: vision,\n options: {\n engines: {\n html: {\n compile: (\n path: string,\n compileOptions: { environment: Environment }\n ) => {\n const template = nunjucks.compile(path, compileOptions.environment)\n\n return (context: object | undefined) => {\n return template.render(context)\n }\n },\n prepare: (\n options: EngineConfigurationObject,\n next: (err?: Error) => void\n ) => {\n // Nunjucks also needs an additional path configuration\n // to use the templates and macros from `govuk-frontend`\n const environment = nunjucks.configure(paths)\n\n // Applies custom filters and globals for nunjucks\n // that are required by the `forms-engine-plugin`\n prepareNunjucksEnvironment(environment, pluginOptions.filters)\n\n options.compileOptions.environment = environment\n\n next()\n }\n }\n },\n path: paths,\n // Provides global context used with all templates\n context\n }\n })\n}\n\ninterface CompileOptions {\n environment: Environment\n}\n\nexport interface EngineConfigurationObject {\n compileOptions: CompileOptions\n}\n\nexport function findPackageRoot() {\n const currentFileName = fileURLToPath(import.meta.url)\n const currentDirectoryName = dirname(currentFileName)\n\n let dir = currentDirectoryName\n while (dir !== '/') {\n if (existsSync(join(dir, 'package.json'))) {\n return dir\n }\n dir = dirname(dir)\n }\n\n throw new Error('package.json not found in parent directories')\n}\n"],"mappings":"AAAA,SA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ACK,cAAc,CAACE,WAAW,GAAGA,WAAW;YAEhDG,IAAI,CAAC,CAAC;UACR;QACF;MACF,CAAC;MACDN,IAAI,EAAEP,KAAK;MACX;MACAV;IACF;EACF,CAAC,CAAC;AACJ;AAUA,OAAO,SAASM,eAAeA,CAAA,EAAG;EAChC,MAAMoB,eAAe,GAAG/B,aAAa,CAACgC,MAAM,CAACC,IAAI,CAACC,GAAG,CAAC;EACtD,MAAMC,oBAAoB,GAAGrC,OAAO,CAACiC,eAAe,CAAC;EAErD,IAAIK,GAAG,GAAGD,oBAAoB;EAC9B,OAAOC,GAAG,KAAK,GAAG,EAAE;IAClB,IAAIvC,UAAU,CAACE,IAAI,CAACqC,GAAG,EAAE,cAAc,CAAC,CAAC,EAAE;MACzC,OAAOA,GAAG;IACZ;IACAA,GAAG,GAAGtC,OAAO,CAACsC,GAAG,CAAC;EACpB;EAEA,MAAM,IAAIC,KAAK,CAAC,8CAA8C,CAAC;AACjE","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/forms-engine-plugin",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Defra forms engine",
5
5
  "type": "module",
6
6
  "files": [
@@ -4,13 +4,11 @@ import { type FormDefinition } from '@defra/forms-model'
4
4
 
5
5
  import { FORM_PREFIX } from '~/src/server/constants.js'
6
6
  import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
7
- import {
8
- plugin,
9
- type PluginOptions
10
- } from '~/src/server/plugins/engine/plugin.js'
11
- import { findPackageRoot } from '~/src/server/plugins/engine/plugin.js'
7
+ import { plugin } from '~/src/server/plugins/engine/plugin.js'
12
8
  import * as defaultServices from '~/src/server/plugins/engine/services/index.js'
13
9
  import { formsService } from '~/src/server/plugins/engine/services/localFormsService.js'
10
+ import { type PluginOptions } from '~/src/server/plugins/engine/types.js'
11
+ import { findPackageRoot } from '~/src/server/plugins/engine/vision.js'
14
12
  import { devtoolContext } from '~/src/server/plugins/nunjucks/context.js'
15
13
  import { type RouteConfig } from '~/src/server/types.js'
16
14
 
@@ -0,0 +1,37 @@
1
+ import Joi from 'joi'
2
+
3
+ const pluginRegistrationOptionsSchema = Joi.object({
4
+ model: Joi.object().optional(),
5
+ services: Joi.object().optional(),
6
+ controllers: Joi.object().pattern(Joi.string(), Joi.any()).optional(),
7
+ cacheName: Joi.string().optional(),
8
+ filters: Joi.object().pattern(Joi.string(), Joi.any()).optional(),
9
+ pluginPath: Joi.string().optional(),
10
+ nunjucks: Joi.object({
11
+ baseLayoutPath: Joi.string().required(),
12
+ paths: Joi.array().items(Joi.string()).required()
13
+ }).required(),
14
+ viewContext: Joi.function().required()
15
+ })
16
+
17
+ /**
18
+ * Validates the plugin options against the schema and returns the validated value.
19
+ * @param {PluginOptions} options
20
+ * @returns {PluginOptions}
21
+ */
22
+ export function validatePluginOptions(options) {
23
+ const result = pluginRegistrationOptionsSchema.validate(options, {
24
+ abortEarly: false
25
+ })
26
+
27
+ if (result.error) {
28
+ throw new Error('Invalid plugin options', result.error)
29
+ }
30
+
31
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
32
+ return result.value
33
+ }
34
+
35
+ /**
36
+ * @import { PluginOptions } from '~/src/server/plugins/engine/types.js'
37
+ */
@@ -0,0 +1,34 @@
1
+ import { validatePluginOptions } from '~/src/server/plugins/engine/options.js'
2
+
3
+ describe('validatePluginOptions', () => {
4
+ it('returns the validated value for valid options', () => {
5
+ const validOptions = {
6
+ nunjucks: {
7
+ baseLayoutPath: 'dxt-devtool-baselayout.html',
8
+ paths: ['src/server/devserver'] // custom layout to make it really clear this is not the same as the runner
9
+ },
10
+ viewContext: () => {
11
+ return { hello: 'world' }
12
+ }
13
+ }
14
+
15
+ expect(validatePluginOptions(validOptions)).toEqual(validOptions)
16
+ })
17
+
18
+ /**
19
+ * tsc would usually check compliance with the type, but given a user might be using plain JS we still want a test
20
+ */
21
+ it('fails if a required attribute is missing', () => {
22
+ const invalidOptions = {
23
+ nunjucks: {
24
+ baseLayoutPath: 'dxt-devtool-baselayout.html',
25
+ paths: ['src/server/devserver'] // custom layout to make it really clear this is not the same as the runner
26
+ }
27
+ }
28
+
29
+ // @ts-expect-error -- add a test for JS users
30
+ expect(() => validatePluginOptions(invalidOptions)).toThrow(
31
+ 'Invalid plugin options'
32
+ )
33
+ })
34
+ })