1
1
export class API {
2
- constructor ( index , sendResponse , events ) {
3
- this . index = index
4
- this . sendResponse = sendResponse
2
+ constructor ( index , sendResponse , events , logResponse ) {
5
3
this . events = events
4
+ this . index = index
5
+ this . logResponse = logResponse
6
6
this . ready = false
7
+ this . sendResponse = sendResponse
7
8
events . on ( 'ready' , ( ) => ( this . ready = true ) )
8
9
}
9
10
@@ -19,10 +20,20 @@ export class API {
19
20
20
21
param = ( req , name ) => this . params ( req , name ) [ 0 ]
21
22
22
- sendJSONResponse = ( body , res ) => {
23
+ sendJSONResponse = ( body , req , res , statusCode ) => {
23
24
res . setHeader ( 'Content-Type' , 'application/json; charset=utf-8' )
24
- res . writeHead ( 200 )
25
+ res . writeHead ( statusCode )
25
26
res . end ( JSON . stringify ( body , null , 2 ) )
27
+ this . logResponse ( statusCode , req . url )
28
+ }
29
+
30
+ internalServerError = ( e , req , res ) => {
31
+ this . sendJSONResponse (
32
+ { status : 500 , error : 'Internal Server Error' } ,
33
+ req ,
34
+ res ,
35
+ 500
36
+ )
26
37
}
27
38
28
39
/**
@@ -40,7 +51,7 @@ export class API {
40
51
ALL_DOCUMENTS = ( req , res ) =>
41
52
this . index
42
53
. ALL_DOCUMENTS ( + this . params ( req , 'LIMIT' ) || undefined )
43
- . then ( ad => this . sendJSONResponse ( ad , res ) )
54
+ . then ( ad => this . sendJSONResponse ( ad , req , res , 200 ) )
44
55
45
56
/**
46
57
* @openapi
@@ -57,7 +68,7 @@ export class API {
57
68
BUCKETS = ( req , res ) =>
58
69
this . index
59
70
. BUCKETS ( ...this . params ( req , 'TOKENSPACE' ) )
60
- . then ( b => this . sendJSONResponse ( b , res ) )
71
+ . then ( b => this . sendJSONResponse ( b , req , res , 200 ) )
61
72
62
73
/**
63
74
* @openapi
@@ -74,7 +85,7 @@ export class API {
74
85
DELETE = ( req , res ) =>
75
86
this . index
76
87
. DELETE ( ...this . params ( req , 'ID' ) )
77
- . then ( idxRes => this . sendJSONResponse ( idxRes , res ) )
88
+ . then ( idxRes => this . sendJSONResponse ( idxRes , req , res , 200 ) )
78
89
79
90
// TODO: DELETE_RAW?
80
91
@@ -98,7 +109,7 @@ export class API {
98
109
this . index
99
110
. DICTIONARY ( ...this . params ( req , 'TOKENSPACE' ) )
100
111
. then ( d => d . slice ( 0 , + this . params ( req , 'LIMIT' ) ) )
101
- . then ( d => this . sendJSONResponse ( d , res ) )
112
+ . then ( d => this . sendJSONResponse ( d , req , res , 200 ) )
102
113
103
114
/**
104
115
* @openapi
@@ -120,7 +131,7 @@ export class API {
120
131
this . index
121
132
. DISTINCT ( ...this . params ( req , 'TOKENSPACE' ) )
122
133
. then ( d => d . slice ( 0 , + this . params ( req , 'LIMIT' ) ) )
123
- . then ( d => this . sendJSONResponse ( d , res ) )
134
+ . then ( d => this . sendJSONResponse ( d , req , res , 200 ) )
124
135
125
136
/**
126
137
* @openapi
@@ -137,7 +148,7 @@ export class API {
137
148
DOCUMENTS = ( req , res ) =>
138
149
this . index
139
150
. DOCUMENTS ( ...this . params ( req , 'ID' ) )
140
- . then ( b => this . sendJSONResponse ( b , res ) )
151
+ . then ( b => this . sendJSONResponse ( b , req , res , 200 ) )
141
152
142
153
// TODO: DOCUMENT_VECTORS?
143
154
@@ -153,7 +164,7 @@ export class API {
153
164
* description: A dump of the index
154
165
*/
155
166
EXPORT = ( req , res ) =>
156
- this . index . EXPORT ( ) . then ( exp => this . sendJSONResponse ( exp , res ) )
167
+ this . index . EXPORT ( ) . then ( exp => this . sendJSONResponse ( exp , req , res , 200 ) )
157
168
158
169
/**
159
170
* @openapi
@@ -173,7 +184,7 @@ export class API {
173
184
FACETS = ( req , res ) =>
174
185
this . index
175
186
. FACETS ( ...this . params ( req , 'TOKENSPACE' ) )
176
- . then ( b => this . sendJSONResponse ( b , res ) )
187
+ . then ( b => this . sendJSONResponse ( b , req , res , 200 ) )
177
188
178
189
/**
179
190
* @openapi
@@ -186,7 +197,7 @@ export class API {
186
197
* description: An array of field names
187
198
*/
188
199
FIELDS = ( req , res ) =>
189
- this . index . FIELDS ( ) . then ( f => this . sendJSONResponse ( f , res ) )
200
+ this . index . FIELDS ( ) . then ( f => this . sendJSONResponse ( f , req , res , 200 ) )
190
201
191
202
/**
192
203
* @openapi
@@ -199,7 +210,9 @@ export class API {
199
210
* description: Successfully deleted
200
211
*/
201
212
FLUSH = ( req , res ) =>
202
- this . index . FLUSH ( ) . then ( idxRes => this . sendJSONResponse ( idxRes , res ) )
213
+ this . index
214
+ . FLUSH ( )
215
+ . then ( idxRes => this . sendJSONResponse ( idxRes , req , res , 200 ) )
203
216
204
217
/**
205
218
* @openapi
@@ -225,7 +238,7 @@ export class API {
225
238
req . on ( 'end' , ( ) =>
226
239
this . index
227
240
. IMPORT ( JSON . parse ( body ) )
228
- . then ( idxRes => this . sendJSONResponse ( idxRes , res ) )
241
+ . then ( idxRes => this . sendJSONResponse ( idxRes , req , res , 200 ) )
229
242
)
230
243
}
231
244
@@ -244,7 +257,7 @@ export class API {
244
257
MAX = ( req , res ) =>
245
258
this . index
246
259
. MAX ( ...this . params ( req , 'TOKENSPACE' ) )
247
- . then ( m => this . sendJSONResponse ( m , res ) )
260
+ . then ( m => this . sendJSONResponse ( m , req , res , 200 ) )
248
261
249
262
/**
250
263
* @openapi
@@ -261,7 +274,7 @@ export class API {
261
274
MIN = ( req , res ) =>
262
275
this . index
263
276
. MIN ( JSON . parse ( this . params ( req , 'TOKENSPACE' ) ) ) // TODO: is this right?
264
- . then ( m => this . sendJSONResponse ( m , res ) )
277
+ . then ( m => this . sendJSONResponse ( m , req , res , 200 ) )
265
278
266
279
/**
267
280
* @openapi
@@ -331,12 +344,14 @@ export class API {
331
344
332
345
// curl -H "Content-Type: application/json" --data @testdata.json http://localhost:8081/put
333
346
PUT = ( req , res ) => {
347
+ console . log ( 'I AM HEEEEEREEE IN PUT' )
334
348
let body = ''
335
349
req . on ( 'data' , d => ( body += d . toString ( ) ) )
336
350
req . on ( 'end' , ( ) =>
337
351
this . index
338
352
. PUT ( JSON . parse ( body ) )
339
- . then ( idxRes => this . sendJSONResponse ( idxRes , res ) )
353
+ . then ( idxRes => this . sendJSONResponse ( idxRes , req , res , 200 ) )
354
+ . catch ( e => this . internalServerError ( e , req , res ) )
340
355
)
341
356
}
342
357
@@ -366,7 +381,7 @@ export class API {
366
381
req . on ( 'end' , ( ) =>
367
382
this . index
368
383
. PUT_RAW ( JSON . parse ( body ) )
369
- . then ( idxRes => this . sendJSONResponse ( idxRes , res ) )
384
+ . then ( idxRes => this . sendJSONResponse ( idxRes , req , res , 200 ) )
370
385
)
371
386
}
372
387
@@ -462,7 +477,7 @@ export class API {
462
477
SCORE : this . param ( req , 'SCORE' ) ,
463
478
SORT : this . param ( req , 'SORT' )
464
479
} )
465
- . then ( r => this . sendJSONResponse ( r , res ) )
480
+ . then ( r => this . sendJSONResponse ( r , req , res , 200 ) )
466
481
467
482
/**
468
483
* @openapi
@@ -497,7 +512,7 @@ export class API {
497
512
. SEARCH ( this . param ( req , 'STRING' ) . trim ( ) . split ( / \s + / ) , {
498
513
PAGE : this . param ( req , 'PAGE' )
499
514
} )
500
- . then ( r => this . sendJSONResponse ( r , res ) )
515
+ . then ( r => this . sendJSONResponse ( r , req , res , 200 ) )
501
516
502
517
/**
503
518
* @openapi
@@ -523,7 +538,9 @@ export class API {
523
538
CREATED : new Date ( CREATED ) ,
524
539
LAST_UPDATED : new Date ( LAST_UPDATED )
525
540
} ,
526
- res
541
+ req ,
542
+ res ,
543
+ 200
527
544
)
528
545
)
529
546
@@ -541,8 +558,8 @@ export class API {
541
558
*/
542
559
READY = ( req , res ) =>
543
560
this . ready
544
- ? this . sendJSONResponse ( { READY : true } , res )
561
+ ? this . sendJSONResponse ( { READY : true } , req , res , 200 )
545
562
: this . events . on ( 'ready' , ( ) =>
546
- this . sendJSONResponse ( { READY : true } , res )
547
- )
563
+ this . sendJSONResponse ( { READY : true } , req , res , 200 )
564
+ )
548
565
}
0 commit comments