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

Commit

Permalink
WIP: Add forms api to runner
Browse files Browse the repository at this point in the history
  • Loading branch information
danielburnley committed Jan 24, 2022
1 parent 42089e8 commit 2720d63
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 83 deletions.
2 changes: 2 additions & 0 deletions runner/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const schema = Joi.object({
serviceName: Joi.string().optional(),
documentUploadApiUrl: Joi.string().default(DEFAULT_DOCUMENT_UPLOAD_API_URL),
previewMode: Joi.boolean().optional(),
formsApiUrl: Joi.string().optional(),
sslKey: Joi.string().optional(),
sslCert: Joi.string().optional(),
sessionTimeout: Joi.number().default(DEFAULT_SESSION_TTL),
Expand Down Expand Up @@ -126,6 +127,7 @@ export function buildConfig() {
serviceName: process.env.SERVICE_NAME,
documentUploadApiUrl: process.env.DOCUMENT_UPLOAD_API_URL,
previewMode: process.env.PREVIEW_MODE === "true",
formsApiUrl: process.env.FORM_API_URL,
sslKey: process.env.SSL_KEY,
sslCert: process.env.SSL_CERT,
sessionTimeout: process.env.SESSION_TIMEOUT,
Expand Down
8 changes: 7 additions & 1 deletion runner/src/server/plugins/engine/configureEnginePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ConfigureEnginePlugin = (
id: string;
}[];
previewMode: boolean;
formsApiUrl: string;
};
};

Expand Down Expand Up @@ -56,6 +57,11 @@ export const configureEnginePlugin: ConfigureEnginePlugin = (

return {
plugin,
options: { modelOptions, configs, previewMode: config.previewMode },
options: {
modelOptions,
configs,
previewMode: config.previewMode,
formsApiUrl: config.formsApiUrl,
},
};
};
1 change: 1 addition & 0 deletions runner/src/server/plugins/engine/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function redirectTo(
return h.redirect(targetUrl);
}

console.log("QUACK????", targetUrl);
const url = redirectUrl(request, targetUrl, params);
return h.redirect(url);
}
Expand Down
194 changes: 112 additions & 82 deletions runner/src/server/plugins/engine/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { HapiRequest, HapiResponseToolkit, HapiServer } from "server/types";
import { FormModel } from "./models";
import Boom from "boom";
import { PluginSpecificConfiguration } from "@hapi/hapi";
import Wreck from "@hapi/wreck";
import { FormPayload } from "./types";
import { shouldLogin } from "server/plugins/auth";
import config from "src/server/config";

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

async function getForm(
id: string,
formsApiUrl: string,
modelOptions,
basePath
): Promise<FormModel> {
const { payload } = await Wreck.get(`${formsApiUrl}/published/${id}`);
var configuration = JSON.parse(payload.toString()).values;
return new FormModel(configuration, { ...modelOptions, basePath });
}

function getStartPageRedirect(
request: HapiRequest,
h: HapiResponseToolkit,
Expand All @@ -47,6 +60,7 @@ type PluginOptions = {
modelOptions: any;
configs: any[];
previewMode: boolean;
formsApiUrl: string;
};

export const plugin = {
Expand All @@ -64,93 +78,98 @@ export const plugin = {
});
});

if (previewMode) {
/**
* The following endpoints are used from the designer for operating in 'preview' mode.
* I.E. Designs saved in the designer can be accessed in the runner for viewing.
* The designer also uses these endpoints as a persistence mechanism for storing and retrieving data
* for it's own purposes so if you're changing these endpoints you likely need to go and amend
* the designer too!
*/
server.route({
method: "post",
path: "/publish",
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
const payload = request.payload as FormPayload;
const { id, configuration } = payload;

const parsedConfiguration =
typeof configuration === "string"
? JSON.parse(configuration)
: configuration;
forms[id] = new FormModel(parsedConfiguration, {
...modelOptions,
basePath: id,
});
return h.response({}).code(204);
},
});
// if (previewMode) {
// /**
// * The following endpoints are used from the designer for operating in 'preview' mode.
// * I.E. Designs saved in the designer can be accessed in the runner for viewing.
// * The designer also uses these endpoints as a persistence mechanism for storing and retrieving data
// * for it's own purposes so if you're changing these endpoints you likely need to go and amend
// * the designer too!
// */
// server.route({
// method: "post",
// path: "/publish",
// handler: (request: HapiRequest, h: HapiResponseToolkit) => {
// const payload = request.payload as FormPayload;
// const { id, configuration } = payload;

server.route({
method: "get",
path: "/published/{id}",
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
const { id } = request.params;
if (forms[id]) {
const { values } = forms[id];
return h.response(JSON.stringify({ id, values })).code(200);
} else {
return h.response({}).code(204);
}
},
});
// const parsedConfiguration =
// typeof configuration === "string"
// ? JSON.parse(configuration)
// : configuration;
// forms[id] = new FormModel(parsedConfiguration, {
// ...modelOptions,
// basePath: id,
// });
// return h.response({}).code(204);
// },
// });

server.route({
method: "get",
path: "/published",
handler: (_request: HapiRequest, h: HapiResponseToolkit) => {
return h
.response(
JSON.stringify(
Object.keys(forms).map(
(key) =>
new FormConfiguration(
key,
forms[key].name,
undefined,
forms[key].def.feedback?.feedbackForm
)
)
)
)
.code(200);
},
});
}
// server.route({
// method: "get",
// path: "/published/{id}",
// handler: (request: HapiRequest, h: HapiResponseToolkit) => {
// const { id } = request.params;
// if (forms[id]) {
// const { values } = forms[id];
// return h.response(JSON.stringify({ id, values })).code(200);
// } else {
// return h.response({}).code(204);
// }
// },
// });

server.route({
method: "get",
path: "/",
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
const keys = Object.keys(forms);
let id = "";
if (keys.length === 1) {
id = keys[0];
}
const model = forms[id];
if (model) {
return getStartPageRedirect(request, h, id, model);
}
throw Boom.notFound("No default form found");
},
});
// server.route({
// method: "get",
// path: "/published",
// handler: (_request: HapiRequest, h: HapiResponseToolkit) => {
// return h
// .response(
// JSON.stringify(
// Object.keys(forms).map(
// (key) =>
// new FormConfiguration(
// key,
// forms[key].name,
// undefined,
// forms[key].def.feedback?.feedbackForm
// )
// )
// )
// )
// .code(200);
// },
// });
// }

// server.route({
// method: "get",
// path: "/",
// handler: (request: HapiRequest, h: HapiResponseToolkit) => {
// const keys = Object.keys(forms);
// let id = "";
// if (keys.length === 1) {
// id = keys[0];
// }
// const model = forms[id];
// if (model) {
// return getStartPageRedirect(request, h, id, model);
// }
// throw Boom.notFound("No default form found");
// },
// });

server.route({
method: "get",
path: "/{id}",
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
handler: async (request: HapiRequest, h: HapiResponseToolkit) => {
const { id } = request.params;
const model = forms[id];
const model = await getForm(
id,
options.formsApiUrl,
modelOptions,
config.id
);
if (model) {
return getStartPageRedirect(request, h, id, model);
}
Expand All @@ -161,9 +180,14 @@ export const plugin = {
server.route({
method: "get",
path: "/{id}/{path*}",
handler: (request: HapiRequest, h: HapiResponseToolkit) => {
handler: async (request: HapiRequest, h: HapiResponseToolkit) => {
const { path, id } = request.params;
const model = forms[id];
const model = await getForm(
id,
options.formsApiUrl,
modelOptions,
config.id
);
const page = model?.pages.find(
(page) => normalisePath(page.path) === normalisePath(path)
);
Expand Down Expand Up @@ -196,7 +220,13 @@ export const plugin = {
h: HapiResponseToolkit
) => {
const { path, id } = request.params;
const model = forms[id];
const model = await getForm(
id,
options.formsApiUrl,
modelOptions,
config.id
);
console.log("WOOF??", id);

if (model) {
const page = model.pages.find(
Expand Down

0 comments on commit 2720d63

Please sign in to comment.