1
1
const path = require ( "path" ) ;
2
2
const version = require ( "../package.json" ) . version ;
3
- const EXPORT_FORM_CONFIG = require ( "./forms/export-form.json" ) ;
3
+ const EXPORT_FORM_CONFIG = require ( "./functions/api/ forms/export-form.json" ) ;
4
4
const mockAuthMiddleware = require ( "./mockAuthMiddleware" ) ;
5
-
5
+ const express = require ( "express" ) ;
6
+ const bodyParser = require ( "body-parser" ) ;
7
+ const cors = require ( "cors" ) ;
6
8
// Taken from:
7
9
// http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
8
10
function shuffleArray ( array ) {
9
- for ( var i = array . length - 1 ; i > 0 ; i -- ) {
10
- var j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
11
- var temp = array [ i ] ;
11
+ for ( let i = array . length - 1 ; i > 0 ; i -- ) {
12
+ const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
13
+ const temp = array [ i ] ;
12
14
array [ i ] = array [ j ] ;
13
15
array [ j ] = temp ;
14
16
}
15
17
return array ;
16
18
}
17
19
18
20
const ERROR = JSON . stringify ( {
19
- message : "Could not process the request" ,
21
+ message : "Could not process the request"
20
22
} ) ;
21
23
22
24
const LONG_DELAY = 500 ;
23
25
const SHORT_DELAY = 300 ;
24
26
const NO_DELAY = 10 ;
25
27
26
28
// Simulate API
27
- module . exports = function ( app , port ) {
29
+ function mountApi ( app ) {
28
30
/*
29
31
QUERIES
30
32
*/
@@ -78,7 +80,7 @@ module.exports = function (app, port) {
78
80
id : 1 ,
79
81
status : "DONE" ,
80
82
numberOfResults : 5 ,
81
- resultUrl : `/api/results/results.csv` ,
83
+ resultUrl : `/api/results/results.csv`
82
84
} )
83
85
) ;
84
86
} , LONG_DELAY ) ;
@@ -97,8 +99,8 @@ module.exports = function (app, port) {
97
99
{ id : "empty-set" , label : "Empty Dataset" } ,
98
100
{
99
101
id : "another-empty-set" ,
100
- label : "Another empty dataset with a long name" ,
101
- } ,
102
+ label : "Another empty dataset with a long name"
103
+ }
102
104
] )
103
105
) ;
104
106
} ) ;
@@ -110,7 +112,9 @@ module.exports = function (app, port) {
110
112
req ,
111
113
res
112
114
) {
113
- res . sendFile ( path . join ( __dirname , `./results/${ req . params . filename } ` ) ) ;
115
+ res . sendFile (
116
+ path . join ( __dirname , "functions/api" , `./results/${ req . params . filename } ` )
117
+ ) ;
114
118
} ) ;
115
119
116
120
/*
@@ -120,14 +124,20 @@ module.exports = function (app, port) {
120
124
req ,
121
125
res
122
126
) {
123
- res . sendFile ( path . join ( __dirname , "./concepts.json" ) ) ;
127
+ res . sendFile ( path . join ( __dirname , "functions/api" , " ./concepts.json") ) ;
124
128
} ) ;
125
129
126
130
app . get (
127
131
"/api/datasets/:datasetId/concepts/:id" ,
128
132
mockAuthMiddleware ,
129
133
function response ( req , res ) {
130
- res . sendFile ( path . join ( __dirname , `./concepts/${ req . params . id } .json` ) ) ;
134
+ res . sendFile (
135
+ path . join (
136
+ __dirname ,
137
+ "functions/api" ,
138
+ `./concepts/${ req . params . id } .json`
139
+ )
140
+ ) ;
131
141
}
132
142
) ;
133
143
@@ -149,10 +159,10 @@ module.exports = function (app, port) {
149
159
"group 1" ,
150
160
"important" ,
151
161
"jk" ,
152
- "interesting" ,
162
+ "interesting"
153
163
] ;
154
164
155
- for ( var i = 25600 ; i < 35600 ; i ++ ) {
165
+ for ( let i = 25600 ; i < 35600 ; i ++ ) {
156
166
const notExecuted = Math . random ( ) < 0.1 ;
157
167
158
168
ids . push ( {
@@ -168,7 +178,7 @@ module.exports = function (app, port) {
168
178
own : Math . random ( ) < 0.1 ,
169
179
shared : Math . random ( ) < 0.8 ,
170
180
resultUrl : notExecuted ? null : `/api/results/results.csv` ,
171
- ownerName : "System" ,
181
+ ownerName : "System"
172
182
} ) ;
173
183
}
174
184
@@ -182,7 +192,9 @@ module.exports = function (app, port) {
182
192
mockAuthMiddleware ,
183
193
function response ( req , res ) {
184
194
setTimeout ( ( ) => {
185
- res . sendFile ( path . join ( __dirname , "./stored-queries/25.json" ) ) ;
195
+ res . sendFile (
196
+ path . join ( __dirname , "functions/api" , "./stored-queries/25.json" )
197
+ ) ;
186
198
} , LONG_DELAY ) ;
187
199
}
188
200
) ;
@@ -229,7 +241,7 @@ module.exports = function (app, port) {
229
241
res . send (
230
242
JSON . stringify ( {
231
243
successful : 1 + Math . floor ( Math . random ( ) * 200 ) ,
232
- unsuccessful : 586 ,
244
+ unsuccessful : 586
233
245
} )
234
246
) ;
235
247
} , LONG_DELAY ) ;
@@ -247,7 +259,7 @@ module.exports = function (app, port) {
247
259
const countriesRequested = req . params . filterId === "production_country" ;
248
260
249
261
const storedValues = countriesRequested
250
- ? require ( "./autocomplete/countries" )
262
+ ? require ( "./functions/api/ autocomplete/countries" )
251
263
: [
252
264
"1008508208" ,
253
265
"1015841172" ,
@@ -258,16 +270,16 @@ module.exports = function (app, port) {
258
270
"1000326535" ,
259
271
"1014150881" ,
260
272
"1017126347" ,
261
- "1008445564" ,
273
+ "1008445564"
262
274
] ;
263
275
264
276
const suggestions = storedValues
265
277
. map ( ( v , id ) => ( {
266
278
label : v ,
267
279
value : id ,
268
- templateValues : { company : "Columbia Pictures Corporation" } ,
280
+ templateValues : { company : "Columbia Pictures Corporation" }
269
281
} ) )
270
- . filter ( ( v ) => v . label . toLowerCase ( ) . startsWith ( text ) ) ;
282
+ . filter ( v => v . label . toLowerCase ( ) . startsWith ( text ) ) ;
271
283
272
284
res . send ( JSON . stringify ( suggestions ) ) ;
273
285
} , LONG_DELAY ) ;
@@ -285,7 +297,7 @@ module.exports = function (app, port) {
285
297
286
298
res . send ( {
287
299
unknownCodes : concepts . slice ( 5 ) ,
288
- resolvedConcepts : concepts . slice ( 1 ) ,
300
+ resolvedConcepts : concepts . slice ( 1 )
289
301
} ) ;
290
302
} , LONG_DELAY ) ;
291
303
}
@@ -299,7 +311,7 @@ module.exports = function (app, port) {
299
311
300
312
res . send ( {
301
313
version : version ,
302
- isDevelopment : process . env . NODE_ENV !== "production" ,
314
+ isDevelopment : process . env . NODE_ENV !== "production"
303
315
} ) ;
304
316
} ) ;
305
317
@@ -317,17 +329,17 @@ module.exports = function (app, port) {
317
329
318
330
if ( req . params . filterId !== "production_country" ) return null ;
319
331
320
- const countries = require ( "./autocomplete/countries" ) ;
321
- const unknownCodes = values . filter ( ( val ) => ! countries . includes ( val ) ) ;
322
- const resolvedValues = values . filter ( ( val ) => countries . includes ( val ) ) ;
332
+ const countries = require ( "./functions/api/ autocomplete/countries" ) ;
333
+ const unknownCodes = values . filter ( val => ! countries . includes ( val ) ) ;
334
+ const resolvedValues = values . filter ( val => countries . includes ( val ) ) ;
323
335
324
336
res . send ( {
325
337
unknownCodes : unknownCodes ,
326
338
resolvedFilter : {
327
339
tableId : req . params . tableId ,
328
340
filterId : req . params . filterId ,
329
- value : resolvedValues . map ( ( val ) => ( { label : val , value : val } ) ) ,
330
- } ,
341
+ value : resolvedValues . map ( val => ( { label : val , value : val } ) )
342
+ }
331
343
} ) ;
332
344
} , LONG_DELAY ) ;
333
345
}
@@ -336,7 +348,7 @@ module.exports = function (app, port) {
336
348
app . get ( "/api/config/frontend" , mockAuthMiddleware , ( req , res ) => {
337
349
res . setHeader ( "Content-Type" , "application/json" ) ;
338
350
339
- const config = require ( "./config.json" ) ;
351
+ const config = require ( "./functions/api/ config.json" ) ;
340
352
341
353
config . version = version ;
342
354
@@ -351,13 +363,13 @@ module.exports = function (app, port) {
351
363
352
364
if ( user === "test" && password === "test" ) {
353
365
res . send ( {
354
- access_token : "VALID" ,
366
+ access_token : "VALID"
355
367
} ) ;
356
368
} else {
357
369
res . status ( 422 ) ;
358
370
res . send (
359
371
JSON . stringify ( {
360
- message : "Login failed" ,
372
+ message : "Login failed"
361
373
} )
362
374
) ;
363
375
}
@@ -374,10 +386,10 @@ module.exports = function (app, port) {
374
386
domains : [ "datasets" ] ,
375
387
abilities : [ "read" , "download" , "preserve_id" ] ,
376
388
targets : [ "imdb" ] ,
377
- creationTime : "2020-01-23T09:52:31.3318485" ,
378
- } ,
389
+ creationTime : "2020-01-23T09:52:31.3318485"
390
+ }
379
391
] ,
380
- groups : [ ] ,
392
+ groups : [ ]
381
393
} ) ;
382
394
} ) ;
383
395
@@ -390,7 +402,7 @@ module.exports = function (app, port) {
390
402
res . status ( 201 ) ;
391
403
res . send (
392
404
JSON . stringify ( {
393
- id : 56000 + Math . floor ( Math . random ( ) * 200 ) ,
405
+ id : 56000 + Math . floor ( Math . random ( ) * 200 )
394
406
} )
395
407
) ;
396
408
} , LONG_DELAY ) ;
@@ -409,12 +421,12 @@ module.exports = function (app, port) {
409
421
if ( dice < 0.5 ) {
410
422
return {
411
423
formType : "EXPORT_FORM" ,
412
- values : { } ,
424
+ values : { }
413
425
} ;
414
426
} else {
415
427
return {
416
428
formType : "Other form" ,
417
- values : { } ,
429
+ values : { }
418
430
} ;
419
431
}
420
432
}
@@ -428,10 +440,10 @@ module.exports = function (app, port) {
428
440
"group 1" ,
429
441
"important" ,
430
442
"jk" ,
431
- "interesting" ,
443
+ "interesting"
432
444
] ;
433
445
434
- for ( var i = 55600 ; i < 85600 ; i ++ ) {
446
+ for ( let i = 55600 ; i < 58600 ; i ++ ) {
435
447
configs . push ( {
436
448
id : i ,
437
449
label : "Saved Config" ,
@@ -442,7 +454,7 @@ module.exports = function (app, port) {
442
454
own : Math . random ( ) < 0.1 ,
443
455
shared : Math . random ( ) < 0.8 ,
444
456
ownerName : "System" ,
445
- ...getFormConfigAttributes ( ) ,
457
+ ...getFormConfigAttributes ( )
446
458
} ) ;
447
459
}
448
460
@@ -456,7 +468,9 @@ module.exports = function (app, port) {
456
468
mockAuthMiddleware ,
457
469
function response ( req , res ) {
458
470
setTimeout ( ( ) => {
459
- res . sendFile ( path . join ( __dirname , "./form-configs/testconf.json" ) ) ;
471
+ res . sendFile (
472
+ path . join ( __dirname , "functions/api" , "./form-configs/testconf.json" )
473
+ ) ;
460
474
} , LONG_DELAY ) ;
461
475
}
462
476
) ;
@@ -481,4 +495,16 @@ module.exports = function (app, port) {
481
495
} , LONG_DELAY ) ;
482
496
}
483
497
) ;
498
+ }
499
+
500
+ const createApi = ( ) => {
501
+ const app = express ( ) ;
502
+ app . use ( cors ( ) ) ;
503
+ // body parser must be set up before routes are attached
504
+ app . use ( bodyParser . json ( ) ) ;
505
+
506
+ mountApi ( app ) ;
507
+ return app ;
484
508
} ;
509
+
510
+ module . exports = createApi ;
0 commit comments