@@ -2,29 +2,32 @@ const path = require("path");
2
2
const version = require ( "../package.json" ) . version ;
3
3
const EXPORT_FORM_CONFIG = require ( "./forms/export-form.json" ) ;
4
4
const mockAuthMiddleware = require ( "./mockAuthMiddleware" ) ;
5
+ const express = require ( "express" ) ;
6
+ const bodyParser = require ( "body-parser" ) ;
7
+ const cors = require ( "cors" ) ;
5
8
6
9
// Taken from:
7
10
// http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
8
11
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 ] ;
12
+ for ( let i = array . length - 1 ; i > 0 ; i -- ) {
13
+ const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
14
+ const temp = array [ i ] ;
12
15
array [ i ] = array [ j ] ;
13
16
array [ j ] = temp ;
14
17
}
15
18
return array ;
16
19
}
17
20
18
21
const ERROR = JSON . stringify ( {
19
- message : "Could not process the request" ,
22
+ message : "Could not process the request"
20
23
} ) ;
21
24
22
25
const LONG_DELAY = 500 ;
23
26
const SHORT_DELAY = 300 ;
24
27
const NO_DELAY = 10 ;
25
28
26
29
// Simulate API
27
- module . exports = function ( app , port ) {
30
+ function mountApi ( app ) {
28
31
/*
29
32
QUERIES
30
33
*/
@@ -78,7 +81,7 @@ module.exports = function (app, port) {
78
81
id : 1 ,
79
82
status : "DONE" ,
80
83
numberOfResults : 5 ,
81
- resultUrl : `/api/results/results.csv` ,
84
+ resultUrl : `/api/results/results.csv`
82
85
} )
83
86
) ;
84
87
} , LONG_DELAY ) ;
@@ -97,8 +100,8 @@ module.exports = function (app, port) {
97
100
{ id : "empty-set" , label : "Empty Dataset" } ,
98
101
{
99
102
id : "another-empty-set" ,
100
- label : "Another empty dataset with a long name" ,
101
- } ,
103
+ label : "Another empty dataset with a long name"
104
+ }
102
105
] )
103
106
) ;
104
107
} ) ;
@@ -149,10 +152,10 @@ module.exports = function (app, port) {
149
152
"group 1" ,
150
153
"important" ,
151
154
"jk" ,
152
- "interesting" ,
155
+ "interesting"
153
156
] ;
154
157
155
- for ( var i = 25600 ; i < 35600 ; i ++ ) {
158
+ for ( let i = 25600 ; i < 35600 ; i ++ ) {
156
159
const notExecuted = Math . random ( ) < 0.1 ;
157
160
158
161
ids . push ( {
@@ -168,7 +171,7 @@ module.exports = function (app, port) {
168
171
own : Math . random ( ) < 0.1 ,
169
172
shared : Math . random ( ) < 0.8 ,
170
173
resultUrl : notExecuted ? null : `/api/results/results.csv` ,
171
- ownerName : "System" ,
174
+ ownerName : "System"
172
175
} ) ;
173
176
}
174
177
@@ -229,7 +232,7 @@ module.exports = function (app, port) {
229
232
res . send (
230
233
JSON . stringify ( {
231
234
successful : 1 + Math . floor ( Math . random ( ) * 200 ) ,
232
- unsuccessful : 586 ,
235
+ unsuccessful : 586
233
236
} )
234
237
) ;
235
238
} , LONG_DELAY ) ;
@@ -258,16 +261,16 @@ module.exports = function (app, port) {
258
261
"1000326535" ,
259
262
"1014150881" ,
260
263
"1017126347" ,
261
- "1008445564" ,
264
+ "1008445564"
262
265
] ;
263
266
264
267
const suggestions = storedValues
265
268
. map ( ( v , id ) => ( {
266
269
label : v ,
267
270
value : id ,
268
- templateValues : { company : "Columbia Pictures Corporation" } ,
271
+ templateValues : { company : "Columbia Pictures Corporation" }
269
272
} ) )
270
- . filter ( ( v ) => v . label . toLowerCase ( ) . startsWith ( text ) ) ;
273
+ . filter ( v => v . label . toLowerCase ( ) . startsWith ( text ) ) ;
271
274
272
275
res . send ( JSON . stringify ( suggestions ) ) ;
273
276
} , LONG_DELAY ) ;
@@ -285,7 +288,7 @@ module.exports = function (app, port) {
285
288
286
289
res . send ( {
287
290
unknownCodes : concepts . slice ( 5 ) ,
288
- resolvedConcepts : concepts . slice ( 1 ) ,
291
+ resolvedConcepts : concepts . slice ( 1 )
289
292
} ) ;
290
293
} , LONG_DELAY ) ;
291
294
}
@@ -299,7 +302,7 @@ module.exports = function (app, port) {
299
302
300
303
res . send ( {
301
304
version : version ,
302
- isDevelopment : process . env . NODE_ENV !== "production" ,
305
+ isDevelopment : process . env . NODE_ENV !== "production"
303
306
} ) ;
304
307
} ) ;
305
308
@@ -318,16 +321,16 @@ module.exports = function (app, port) {
318
321
if ( req . params . filterId !== "production_country" ) return null ;
319
322
320
323
const countries = require ( "./autocomplete/countries" ) ;
321
- const unknownCodes = values . filter ( ( val ) => ! countries . includes ( val ) ) ;
322
- const resolvedValues = values . filter ( ( val ) => countries . includes ( val ) ) ;
324
+ const unknownCodes = values . filter ( val => ! countries . includes ( val ) ) ;
325
+ const resolvedValues = values . filter ( val => countries . includes ( val ) ) ;
323
326
324
327
res . send ( {
325
328
unknownCodes : unknownCodes ,
326
329
resolvedFilter : {
327
330
tableId : req . params . tableId ,
328
331
filterId : req . params . filterId ,
329
- value : resolvedValues . map ( ( val ) => ( { label : val , value : val } ) ) ,
330
- } ,
332
+ value : resolvedValues . map ( val => ( { label : val , value : val } ) )
333
+ }
331
334
} ) ;
332
335
} , LONG_DELAY ) ;
333
336
}
@@ -351,13 +354,13 @@ module.exports = function (app, port) {
351
354
352
355
if ( user === "test" && password === "test" ) {
353
356
res . send ( {
354
- access_token : "VALID" ,
357
+ access_token : "VALID"
355
358
} ) ;
356
359
} else {
357
360
res . status ( 422 ) ;
358
361
res . send (
359
362
JSON . stringify ( {
360
- message : "Login failed" ,
363
+ message : "Login failed"
361
364
} )
362
365
) ;
363
366
}
@@ -374,10 +377,10 @@ module.exports = function (app, port) {
374
377
domains : [ "datasets" ] ,
375
378
abilities : [ "read" , "download" , "preserve_id" ] ,
376
379
targets : [ "imdb" ] ,
377
- creationTime : "2020-01-23T09:52:31.3318485" ,
378
- } ,
380
+ creationTime : "2020-01-23T09:52:31.3318485"
381
+ }
379
382
] ,
380
- groups : [ ] ,
383
+ groups : [ ]
381
384
} ) ;
382
385
} ) ;
383
386
@@ -390,7 +393,7 @@ module.exports = function (app, port) {
390
393
res . status ( 201 ) ;
391
394
res . send (
392
395
JSON . stringify ( {
393
- id : 56000 + Math . floor ( Math . random ( ) * 200 ) ,
396
+ id : 56000 + Math . floor ( Math . random ( ) * 200 )
394
397
} )
395
398
) ;
396
399
} , LONG_DELAY ) ;
@@ -409,12 +412,12 @@ module.exports = function (app, port) {
409
412
if ( dice < 0.5 ) {
410
413
return {
411
414
formType : "EXPORT_FORM" ,
412
- values : { } ,
415
+ values : { }
413
416
} ;
414
417
} else {
415
418
return {
416
419
formType : "Other form" ,
417
- values : { } ,
420
+ values : { }
418
421
} ;
419
422
}
420
423
}
@@ -428,10 +431,10 @@ module.exports = function (app, port) {
428
431
"group 1" ,
429
432
"important" ,
430
433
"jk" ,
431
- "interesting" ,
434
+ "interesting"
432
435
] ;
433
436
434
- for ( var i = 55600 ; i < 85600 ; i ++ ) {
437
+ for ( let i = 55600 ; i < 85600 ; i ++ ) {
435
438
configs . push ( {
436
439
id : i ,
437
440
label : "Saved Config" ,
@@ -442,7 +445,7 @@ module.exports = function (app, port) {
442
445
own : Math . random ( ) < 0.1 ,
443
446
shared : Math . random ( ) < 0.8 ,
444
447
ownerName : "System" ,
445
- ...getFormConfigAttributes ( ) ,
448
+ ...getFormConfigAttributes ( )
446
449
} ) ;
447
450
}
448
451
@@ -481,4 +484,16 @@ module.exports = function (app, port) {
481
484
} , LONG_DELAY ) ;
482
485
}
483
486
) ;
487
+ }
488
+
489
+ const createApi = ( ) => {
490
+ const app = express ( ) ;
491
+ app . use ( cors ( ) ) ;
492
+ // body parser must be set up before routes are attached
493
+ app . use ( bodyParser . json ( ) ) ;
494
+
495
+ mountApi ( app ) ;
496
+ return app ;
484
497
} ;
498
+
499
+ module . exports = createApi ;
0 commit comments