Skip to content
This repository was archived by the owner on Dec 13, 2022. It is now read-only.

Commit 6740ea7

Browse files
committed
Add forms api to runner
1 parent 42089e8 commit 6740ea7

File tree

5 files changed

+109
-83
lines changed

5 files changed

+109
-83
lines changed

runner/src/server/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const schema = Joi.object({
5454
serviceName: Joi.string().optional(),
5555
documentUploadApiUrl: Joi.string().default(DEFAULT_DOCUMENT_UPLOAD_API_URL),
5656
previewMode: Joi.boolean().optional(),
57+
formsApiUrl: Joi.string().optional(),
5758
sslKey: Joi.string().optional(),
5859
sslCert: Joi.string().optional(),
5960
sessionTimeout: Joi.number().default(DEFAULT_SESSION_TTL),
@@ -126,6 +127,7 @@ export function buildConfig() {
126127
serviceName: process.env.SERVICE_NAME,
127128
documentUploadApiUrl: process.env.DOCUMENT_UPLOAD_API_URL,
128129
previewMode: process.env.PREVIEW_MODE === "true",
130+
formsApiUrl: process.env.FORM_API_URL,
129131
sslKey: process.env.SSL_KEY,
130132
sslCert: process.env.SSL_CERT,
131133
sessionTimeout: process.env.SESSION_TIMEOUT,

runner/src/server/plugins/engine/configureEnginePlugin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type ConfigureEnginePlugin = (
2323
id: string;
2424
}[];
2525
previewMode: boolean;
26+
formsApiUrl: string;
2627
};
2728
};
2829

@@ -56,6 +57,11 @@ export const configureEnginePlugin: ConfigureEnginePlugin = (
5657

5758
return {
5859
plugin,
59-
options: { modelOptions, configs, previewMode: config.previewMode },
60+
options: {
61+
modelOptions,
62+
configs,
63+
previewMode: config.previewMode,
64+
formsApiUrl: config.formsApiUrl,
65+
},
6066
};
6167
};

runner/src/server/plugins/engine/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export function redirectTo(
7272
return h.redirect(targetUrl);
7373
}
7474

75+
console.log("QUACK????", targetUrl);
7576
const url = redirectUrl(request, targetUrl, params);
7677
return h.redirect(url);
7778
}

runner/src/server/plugins/engine/pageControllers/PageControllerBase.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ export class PageControllerBase {
217217
defaultLink = link;
218218
return false;
219219
});
220+
console.log("Next link: ", nextLink);
221+
console.log("Default link: ", defaultLink);
220222
return nextLink?.page ?? defaultLink?.page;
221223
}
222224

runner/src/server/plugins/engine/plugin.ts

Lines changed: 97 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { HapiRequest, HapiResponseToolkit, HapiServer } from "server/types";
77
import { FormModel } from "./models";
88
import Boom from "boom";
99
import { PluginSpecificConfiguration } from "@hapi/hapi";
10+
import Wreck from "@hapi/wreck";
1011
import { FormPayload } from "./types";
1112
import { shouldLogin } from "server/plugins/auth";
13+
import config from "src/server/config";
1214

1315
configure([
1416
// Configure Nunjucks to allow rendering of content that is revealed conditionally.
@@ -24,6 +26,17 @@ function normalisePath(path: string) {
2426
return path.replace(/^\//, "").replace(/\/$/, "");
2527
}
2628

29+
async function getForm(
30+
id: string,
31+
formsApiUrl: string,
32+
modelOptions,
33+
basePath: string
34+
): Promise<FormModel> {
35+
const { payload } = await Wreck.get(`${formsApiUrl}/published/${id}`);
36+
var configuration = JSON.parse(payload.toString()).values;
37+
return new FormModel(configuration, { ...modelOptions, basePath });
38+
}
39+
2740
function getStartPageRedirect(
2841
request: HapiRequest,
2942
h: HapiResponseToolkit,
@@ -47,6 +60,7 @@ type PluginOptions = {
4760
modelOptions: any;
4861
configs: any[];
4962
previewMode: boolean;
63+
formsApiUrl: string;
5064
};
5165

5266
export const plugin = {
@@ -64,93 +78,93 @@ export const plugin = {
6478
});
6579
});
6680

67-
if (previewMode) {
68-
/**
69-
* The following endpoints are used from the designer for operating in 'preview' mode.
70-
* I.E. Designs saved in the designer can be accessed in the runner for viewing.
71-
* The designer also uses these endpoints as a persistence mechanism for storing and retrieving data
72-
* for it's own purposes so if you're changing these endpoints you likely need to go and amend
73-
* the designer too!
74-
*/
75-
server.route({
76-
method: "post",
77-
path: "/publish",
78-
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
79-
const payload = request.payload as FormPayload;
80-
const { id, configuration } = payload;
81-
82-
const parsedConfiguration =
83-
typeof configuration === "string"
84-
? JSON.parse(configuration)
85-
: configuration;
86-
forms[id] = new FormModel(parsedConfiguration, {
87-
...modelOptions,
88-
basePath: id,
89-
});
90-
return h.response({}).code(204);
91-
},
92-
});
81+
// if (previewMode) {
82+
// /**
83+
// * The following endpoints are used from the designer for operating in 'preview' mode.
84+
// * I.E. Designs saved in the designer can be accessed in the runner for viewing.
85+
// * The designer also uses these endpoints as a persistence mechanism for storing and retrieving data
86+
// * for it's own purposes so if you're changing these endpoints you likely need to go and amend
87+
// * the designer too!
88+
// */
89+
// server.route({
90+
// method: "post",
91+
// path: "/publish",
92+
// handler: (request: HapiRequest, h: HapiResponseToolkit) => {
93+
// const payload = request.payload as FormPayload;
94+
// const { id, configuration } = payload;
9395

94-
server.route({
95-
method: "get",
96-
path: "/published/{id}",
97-
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
98-
const { id } = request.params;
99-
if (forms[id]) {
100-
const { values } = forms[id];
101-
return h.response(JSON.stringify({ id, values })).code(200);
102-
} else {
103-
return h.response({}).code(204);
104-
}
105-
},
106-
});
96+
// const parsedConfiguration =
97+
// typeof configuration === "string"
98+
// ? JSON.parse(configuration)
99+
// : configuration;
100+
// forms[id] = new FormModel(parsedConfiguration, {
101+
// ...modelOptions,
102+
// basePath: id,
103+
// });
104+
// return h.response({}).code(204);
105+
// },
106+
// });
107107

108-
server.route({
109-
method: "get",
110-
path: "/published",
111-
handler: (_request: HapiRequest, h: HapiResponseToolkit) => {
112-
return h
113-
.response(
114-
JSON.stringify(
115-
Object.keys(forms).map(
116-
(key) =>
117-
new FormConfiguration(
118-
key,
119-
forms[key].name,
120-
undefined,
121-
forms[key].def.feedback?.feedbackForm
122-
)
123-
)
124-
)
125-
)
126-
.code(200);
127-
},
128-
});
129-
}
108+
// server.route({
109+
// method: "get",
110+
// path: "/published/{id}",
111+
// handler: (request: HapiRequest, h: HapiResponseToolkit) => {
112+
// const { id } = request.params;
113+
// if (forms[id]) {
114+
// const { values } = forms[id];
115+
// return h.response(JSON.stringify({ id, values })).code(200);
116+
// } else {
117+
// return h.response({}).code(204);
118+
// }
119+
// },
120+
// });
130121

131-
server.route({
132-
method: "get",
133-
path: "/",
134-
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
135-
const keys = Object.keys(forms);
136-
let id = "";
137-
if (keys.length === 1) {
138-
id = keys[0];
139-
}
140-
const model = forms[id];
141-
if (model) {
142-
return getStartPageRedirect(request, h, id, model);
143-
}
144-
throw Boom.notFound("No default form found");
145-
},
146-
});
122+
// server.route({
123+
// method: "get",
124+
// path: "/published",
125+
// handler: (_request: HapiRequest, h: HapiResponseToolkit) => {
126+
// return h
127+
// .response(
128+
// JSON.stringify(
129+
// Object.keys(forms).map(
130+
// (key) =>
131+
// new FormConfiguration(
132+
// key,
133+
// forms[key].name,
134+
// undefined,
135+
// forms[key].def.feedback?.feedbackForm
136+
// )
137+
// )
138+
// )
139+
// )
140+
// .code(200);
141+
// },
142+
// });
143+
// }
144+
145+
// server.route({
146+
// method: "get",
147+
// path: "/",
148+
// handler: (request: HapiRequest, h: HapiResponseToolkit) => {
149+
// const keys = Object.keys(forms);
150+
// let id = "";
151+
// if (keys.length === 1) {
152+
// id = keys[0];
153+
// }
154+
// const model = forms[id];
155+
// if (model) {
156+
// return getStartPageRedirect(request, h, id, model);
157+
// }
158+
// throw Boom.notFound("No default form found");
159+
// },
160+
// });
147161

148162
server.route({
149163
method: "get",
150164
path: "/{id}",
151-
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
165+
handler: async (request: HapiRequest, h: HapiResponseToolkit) => {
152166
const { id } = request.params;
153-
const model = forms[id];
167+
const model = await getForm(id, options.formsApiUrl, modelOptions, id);
154168
if (model) {
155169
return getStartPageRedirect(request, h, id, model);
156170
}
@@ -161,9 +175,9 @@ export const plugin = {
161175
server.route({
162176
method: "get",
163177
path: "/{id}/{path*}",
164-
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
178+
handler: async (request: HapiRequest, h: HapiResponseToolkit) => {
165179
const { path, id } = request.params;
166-
const model = forms[id];
180+
const model = await getForm(id, options.formsApiUrl, modelOptions, id);
167181
const page = model?.pages.find(
168182
(page) => normalisePath(page.path) === normalisePath(path)
169183
);
@@ -196,7 +210,8 @@ export const plugin = {
196210
h: HapiResponseToolkit
197211
) => {
198212
const { path, id } = request.params;
199-
const model = forms[id];
213+
const model = await getForm(id, options.formsApiUrl, modelOptions, id);
214+
console.log("WOOF??", id);
200215

201216
if (model) {
202217
const page = model.pages.find(

0 commit comments

Comments
 (0)