1
1
import { RegisterApi } from "./RegisterApi" ;
2
2
import { HapiRequest , HapiResponseToolkit , HapiServer } from "../../../types" ;
3
- import { FormPayload } from "../../../../../../digital-form-builder/runner/src/server/plugins/engine/types" ;
4
3
// @ts -ignore
5
4
import Boom from "boom" ;
6
5
import { PluginUtil } from "../util/PluginUtil" ;
@@ -13,51 +12,24 @@ import {config} from "../../utils/AdapterConfigurationSchema";
13
12
14
13
export class RegisterFormsApi implements RegisterApi {
15
14
register ( server : HapiServer ) {
16
- server . route ( {
17
- method : "get" ,
18
- path : "/" ,
19
- options : {
20
- description : "See API-README.md file in the runner/src/server/plugins/engine/api" ,
21
- } ,
22
- handler : async ( request : HapiRequest , h : HapiResponseToolkit ) => {
23
- const { adapterCacheService} = request . services ( [ ] ) ;
24
- const model = await adapterCacheService . getFormAdapterModel ( "components" , request ) ;
25
- if ( model ) {
26
- return PluginUtil . getStartPageRedirect ( request , h , "components" , model ) ;
27
- }
28
- if ( config . serviceStartPage ) {
29
- return h . redirect ( config . serviceStartPage ) ;
30
- }
31
- throw Boom . notFound ( "No default form found" ) ;
32
- }
33
- } ) ;
15
+ const { s3UploadService} = server . services ( [ ] ) ;
34
16
35
- const queryParamPreHandler = async (
36
- request : HapiRequest ,
37
- h : HapiResponseToolkit
38
- ) => {
17
+ // Middleware to prepopulate fields from query parameters
18
+ const queryParamPreHandler = async ( request : HapiRequest , h : HapiResponseToolkit ) => {
39
19
const { query} = request ;
40
20
const { id} = request . params ;
41
21
const { adapterCacheService} = request . services ( [ ] ) ;
42
22
const model = await adapterCacheService . getFormAdapterModel ( id , request ) ;
43
23
if ( ! model ) {
44
24
throw Boom . notFound ( "No form found for id" ) ;
45
25
}
46
-
47
26
const prePopFields = model . fieldsForPrePopulation ;
48
- if (
49
- Object . keys ( query ) . length === 0 ||
50
- Object . keys ( prePopFields ) . length === 0
51
- ) {
27
+ if ( Object . keys ( query ) . length === 0 || Object . keys ( prePopFields ) . length === 0 ) {
52
28
return h . continue ;
53
29
}
54
30
// @ts -ignore
55
31
const state = await adapterCacheService . getState ( request ) ;
56
- const newValues = getValidStateFromQueryParameters (
57
- prePopFields ,
58
- query ,
59
- state
60
- ) ;
32
+ const newValues = getValidStateFromQueryParameters ( prePopFields , query , state ) ;
61
33
// @ts -ignore
62
34
await adapterCacheService . mergeState ( request , newValues ) ;
63
35
if ( Object . keys ( newValues ) . length > 0 ) {
@@ -66,43 +38,56 @@ export class RegisterFormsApi implements RegisterApi {
66
38
return h . continue ;
67
39
} ;
68
40
69
- /**
70
- * Middleware to check if the user session is still valid.
71
- *
72
- * If the session is dropped, it will throw a client timeout error
73
- */
74
- const checkUserSession = async (
75
- request : HapiRequest ,
76
- h : HapiResponseToolkit
77
- ) => {
41
+ // Middleware to check if the user session is still valid
42
+ const checkUserSession = async ( request : HapiRequest , h : HapiResponseToolkit ) => {
78
43
const { adapterCacheService} = request . services ( [ ] ) ;
79
-
80
44
// @ts -ignore
81
45
const state = await adapterCacheService . getState ( request ) ;
82
-
83
46
if ( config . copilotEnv == "prod" && ! state . callback ) {
84
- // if you are here the session likely dropped
47
+ // If you are here the session likely dropped
85
48
request . logger . error ( [ "checkUserSession" ] , `Session expired ${ request . yar . id } ` ) ;
86
-
87
49
throw Boom . clientTimeout ( "Session expired" ) ;
88
50
}
89
-
90
51
return h . continue ;
91
- }
52
+ } ;
53
+
54
+ // Middleware to handle file uploads
55
+ const handleFiles = async ( request : HapiRequest , h : HapiResponseToolkit ) => {
56
+ const { path, id} = request . params ;
57
+ const { adapterCacheService} = request . services ( [ ] ) ;
58
+ const model = await adapterCacheService . getFormAdapterModel ( id , request ) ;
59
+ const page = model ?. pages . find (
60
+ ( page ) => PluginUtil . normalisePath ( page . path ) === PluginUtil . normalisePath ( path )
61
+ ) ;
62
+ // @ts -ignore
63
+ return s3UploadService . handleUploadRequest ( request , h , page . pageDef ) ;
64
+ } ;
65
+
66
+ server . route ( {
67
+ method : "get" ,
68
+ path : "/" ,
69
+ options : {
70
+ description : "Default route - redirects to a default form if configured" ,
71
+ } ,
72
+ handler : async ( request : HapiRequest , h : HapiResponseToolkit ) => {
73
+ const { adapterCacheService} = request . services ( [ ] ) ;
74
+ const model = await adapterCacheService . getFormAdapterModel ( "components" , request ) ;
75
+ if ( model ) {
76
+ return PluginUtil . getStartPageRedirect ( request , h , "components" , model ) ;
77
+ }
78
+ if ( config . serviceStartPage ) {
79
+ return h . redirect ( config . serviceStartPage ) ;
80
+ }
81
+ throw Boom . notFound ( "No default form found" ) ;
82
+ }
83
+ } ) ;
92
84
93
85
server . route ( {
94
86
method : "get" ,
95
87
path : "/{id}" ,
96
88
options : {
97
- description : "See API-README.md file in the runner/src/server/plugins/engine/api" ,
98
- pre : [
99
- {
100
- method : queryParamPreHandler
101
- } ,
102
- {
103
- method : checkUserSession
104
- }
105
- ]
89
+ description : "Form start page" ,
90
+ pre : [ { method : queryParamPreHandler } , { method : checkUserSession } ] ,
106
91
} ,
107
92
handler : async ( request : HapiRequest , h : HapiResponseToolkit ) => {
108
93
const { id} = request . params ;
@@ -115,19 +100,13 @@ export class RegisterFormsApi implements RegisterApi {
115
100
}
116
101
} ) ;
117
102
118
- const getOptions : any = {
103
+ server . route ( {
119
104
method : "get" ,
120
105
path : "/{id}/{path*}" ,
121
106
options : {
122
- description : "See API-README.md file in the runner/src/server/plugins/engine/api" ,
123
- pre : [
124
- {
125
- method : queryParamPreHandler
126
- } ,
127
- {
128
- method : checkUserSession
129
- }
130
- ] ,
107
+ description : "Form page" ,
108
+ pre : [ { method : queryParamPreHandler } , { method : checkUserSession } ] ,
109
+ auth : config . jwtAuthEnabled === "true" ? jwtAuthStrategyName : false ,
131
110
} ,
132
111
handler : async ( request : HapiRequest , h : HapiResponseToolkit ) => {
133
112
const { path, id} = request . params ;
@@ -144,55 +123,13 @@ export class RegisterFormsApi implements RegisterApi {
144
123
}
145
124
throw Boom . notFound ( "No form or page found" ) ;
146
125
}
147
- }
148
-
149
- // TODO: Stop being naughty! Conditionally disabling auth for pre-prod envs is a temporary measure for getting
150
- // FAB into production
151
- if ( config . jwtAuthEnabled && config . jwtAuthEnabled === "true" ) {
152
- getOptions . options . auth = jwtAuthStrategyName
153
- }
154
-
155
- server . route ( getOptions ) ;
156
-
157
- const { s3UploadService} = server . services ( [ ] ) ;
158
-
159
- const handleFiles = async ( request : HapiRequest , h : HapiResponseToolkit ) => {
160
- const { path, id} = request . params ;
161
- const { adapterCacheService} = request . services ( [ ] ) ;
162
- const model = await adapterCacheService . getFormAdapterModel ( id , request ) ;
163
- const page = model ?. pages . find (
164
- ( page ) => PluginUtil . normalisePath ( page . path ) === PluginUtil . normalisePath ( path )
165
- ) ;
166
- // @ts -ignore
167
- return s3UploadService . handleUploadRequest ( request , h , page . pageDef ) ;
168
- } ;
169
-
170
- const postHandler = async (
171
- request : HapiRequest ,
172
- h : HapiResponseToolkit
173
- ) => {
174
- const { path, id} = request . params ;
175
- const { adapterCacheService} = request . services ( [ ] ) ;
176
- const model = await adapterCacheService . getFormAdapterModel ( id , request ) ;
177
-
178
- if ( model ) {
179
- const page = model . pages . find (
180
- ( page ) => page . path . replace ( / ^ \/ / , "" ) === path . replace ( / ^ \/ / , "" )
181
- ) ;
182
-
183
- if ( page ) {
184
- return page . makePostRouteHandler ( ) ( request , h ) ;
185
- }
186
- }
187
-
188
- throw Boom . notFound ( "No form of path found" ) ;
189
- } ;
126
+ } ) ;
190
127
191
- let postConfig : any = {
128
+ server . route ( {
192
129
method : "post" ,
193
130
path : "/{id}/{path*}" ,
194
131
options : {
195
- description : "See API-README.md file in the runner/src/server/plugins/engine/api " ,
132
+ description : "Form submission " ,
196
133
plugins : < PluginSpecificConfiguration > {
197
134
"hapi-rate-limit" : {
198
135
userPathLimit : 10
@@ -210,15 +147,22 @@ export class RegisterFormsApi implements RegisterApi {
210
147
}
211
148
} ,
212
149
pre : [ { method : handleFiles } ] ,
213
- handler : postHandler ,
214
- }
215
- }
216
- if ( config . jwtAuthEnabled && config . jwtAuthEnabled === "true" ) {
217
- postConfig . options . auth = jwtAuthStrategyName
218
- }
219
- server . route ( postConfig ) ;
220
-
150
+ auth : config . jwtAuthEnabled === "true" ? jwtAuthStrategyName : false ,
151
+ } ,
152
+ handler : async ( request : HapiRequest , h : HapiResponseToolkit ) => {
153
+ const { path, id} = request . params ;
154
+ const { adapterCacheService} = request . services ( [ ] ) ;
155
+ const model = await adapterCacheService . getFormAdapterModel ( id , request ) ;
156
+ if ( model ) {
157
+ const page = model . pages . find (
158
+ ( page ) => page . path . replace ( / ^ \/ / , "" ) === path . replace ( / ^ \/ / , "" )
159
+ ) ;
160
+ if ( page ) {
161
+ return page . makePostRouteHandler ( ) ( request , h ) ;
162
+ }
163
+ }
164
+ throw Boom . notFound ( "No form or path found" ) ;
165
+ } ,
166
+ } ) ;
221
167
}
222
-
223
-
224
168
}
0 commit comments