@@ -7,8 +7,10 @@ import { HapiRequest, HapiResponseToolkit, HapiServer } from "server/types";
7
7
import { FormModel } from "./models" ;
8
8
import Boom from "boom" ;
9
9
import { PluginSpecificConfiguration } from "@hapi/hapi" ;
10
+ import Wreck from "@hapi/wreck" ;
10
11
import { FormPayload } from "./types" ;
11
12
import { shouldLogin } from "server/plugins/auth" ;
13
+ import config from "src/server/config" ;
12
14
13
15
configure ( [
14
16
// Configure Nunjucks to allow rendering of content that is revealed conditionally.
@@ -24,6 +26,17 @@ function normalisePath(path: string) {
24
26
return path . replace ( / ^ \/ / , "" ) . replace ( / \/ $ / , "" ) ;
25
27
}
26
28
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
+
27
40
function getStartPageRedirect (
28
41
request : HapiRequest ,
29
42
h : HapiResponseToolkit ,
@@ -47,6 +60,7 @@ type PluginOptions = {
47
60
modelOptions : any ;
48
61
configs : any [ ] ;
49
62
previewMode : boolean ;
63
+ formsApiUrl : string ;
50
64
} ;
51
65
52
66
export const plugin = {
@@ -64,93 +78,93 @@ export const plugin = {
64
78
} ) ;
65
79
} ) ;
66
80
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;
93
95
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
+ // });
107
107
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
+ // });
130
121
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
+ // });
147
161
148
162
server . route ( {
149
163
method : "get" ,
150
164
path : "/{id}" ,
151
- handler : ( request : HapiRequest , h : HapiResponseToolkit ) => {
165
+ handler : async ( request : HapiRequest , h : HapiResponseToolkit ) => {
152
166
const { id } = request . params ;
153
- const model = forms [ id ] ;
167
+ const model = await getForm ( id , options . formsApiUrl , modelOptions , id ) ;
154
168
if ( model ) {
155
169
return getStartPageRedirect ( request , h , id , model ) ;
156
170
}
@@ -161,9 +175,9 @@ export const plugin = {
161
175
server . route ( {
162
176
method : "get" ,
163
177
path : "/{id}/{path*}" ,
164
- handler : ( request : HapiRequest , h : HapiResponseToolkit ) => {
178
+ handler : async ( request : HapiRequest , h : HapiResponseToolkit ) => {
165
179
const { path, id } = request . params ;
166
- const model = forms [ id ] ;
180
+ const model = await getForm ( id , options . formsApiUrl , modelOptions , id ) ;
167
181
const page = model ?. pages . find (
168
182
( page ) => normalisePath ( page . path ) === normalisePath ( path )
169
183
) ;
@@ -196,7 +210,8 @@ export const plugin = {
196
210
h : HapiResponseToolkit
197
211
) => {
198
212
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 ) ;
200
215
201
216
if ( model ) {
202
217
const page = model . pages . find (
0 commit comments