Skip to content

Commit f77c3e8

Browse files
committed
FLS-1452 - Refactoring RegisterFormsApi for readability
This is a non-functional change, a pure refactor. We now inline consistently in calls to server.route() (instead of creating variables only used in one place), group middleware functions and place them above route functions, and reduce whitespace.
1 parent 8fe84ba commit f77c3e8

File tree

1 file changed

+66
-122
lines changed

1 file changed

+66
-122
lines changed
Lines changed: 66 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {RegisterApi} from "./RegisterApi";
22
import {HapiRequest, HapiResponseToolkit, HapiServer} from "../../../types";
3-
import {FormPayload} from "../../../../../../digital-form-builder/runner/src/server/plugins/engine/types";
43
// @ts-ignore
54
import Boom from "boom";
65
import {PluginUtil} from "../util/PluginUtil";
@@ -13,51 +12,24 @@ import {config} from "../../utils/AdapterConfigurationSchema";
1312

1413
export class RegisterFormsApi implements RegisterApi {
1514
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([]);
3416

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) => {
3919
const {query} = request;
4020
const {id} = request.params;
4121
const {adapterCacheService} = request.services([]);
4222
const model = await adapterCacheService.getFormAdapterModel(id, request);
4323
if (!model) {
4424
throw Boom.notFound("No form found for id");
4525
}
46-
4726
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) {
5228
return h.continue;
5329
}
5430
// @ts-ignore
5531
const state = await adapterCacheService.getState(request);
56-
const newValues = getValidStateFromQueryParameters(
57-
prePopFields,
58-
query,
59-
state
60-
);
32+
const newValues = getValidStateFromQueryParameters(prePopFields, query, state);
6133
// @ts-ignore
6234
await adapterCacheService.mergeState(request, newValues);
6335
if (Object.keys(newValues).length > 0) {
@@ -66,43 +38,56 @@ export class RegisterFormsApi implements RegisterApi {
6638
return h.continue;
6739
};
6840

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) => {
7843
const {adapterCacheService} = request.services([]);
79-
8044
// @ts-ignore
8145
const state = await adapterCacheService.getState(request);
82-
8346
if (config.copilotEnv == "prod" && !state.callback) {
84-
// if you are here the session likely dropped
47+
// If you are here the session likely dropped
8548
request.logger.error(["checkUserSession"], `Session expired ${request.yar.id}`);
86-
8749
throw Boom.clientTimeout("Session expired");
8850
}
89-
9051
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+
});
9284

9385
server.route({
9486
method: "get",
9587
path: "/{id}",
9688
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}],
10691
},
10792
handler: async (request: HapiRequest, h: HapiResponseToolkit) => {
10893
const {id} = request.params;
@@ -115,19 +100,13 @@ export class RegisterFormsApi implements RegisterApi {
115100
}
116101
});
117102

118-
const getOptions: any = {
103+
server.route({
119104
method: "get",
120105
path: "/{id}/{path*}",
121106
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,
131110
},
132111
handler: async (request: HapiRequest, h: HapiResponseToolkit) => {
133112
const {path, id} = request.params;
@@ -144,55 +123,13 @@ export class RegisterFormsApi implements RegisterApi {
144123
}
145124
throw Boom.notFound("No form or page found");
146125
}
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+
});
190127

191-
let postConfig: any = {
128+
server.route({
192129
method: "post",
193130
path: "/{id}/{path*}",
194131
options: {
195-
description: "See API-README.md file in the runner/src/server/plugins/engine/api",
132+
description: "Form submission",
196133
plugins: <PluginSpecificConfiguration>{
197134
"hapi-rate-limit": {
198135
userPathLimit: 10
@@ -210,15 +147,22 @@ export class RegisterFormsApi implements RegisterApi {
210147
}
211148
},
212149
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+
});
221167
}
222-
223-
224168
}

0 commit comments

Comments
 (0)