@hitchy/plugin-odem-rest 0.4.9 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/coverage/tmp/coverage-17048-1649848422081-0.json +1 -0
- package/index.js +60 -81
- package/package.json +11 -9
package/index.js
CHANGED
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* (c) 2018 cepharum GmbH, Berlin, http://cepharum.de
|
|
3
|
-
*
|
|
4
|
-
* The MIT License (MIT)
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2018 cepharum GmbH
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
* @author: cepharum
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
1
|
"use strict";
|
|
30
2
|
|
|
31
3
|
const { posix: { resolve } } = require( "path" );
|
|
@@ -34,6 +6,9 @@ module.exports = function() {
|
|
|
34
6
|
const api = this;
|
|
35
7
|
const { runtime: { services: Services, models: Models }, utility: { case: Case } } = api;
|
|
36
8
|
|
|
9
|
+
const logDebug = api.log( "hitchy:odem:rest:debug" );
|
|
10
|
+
const logError = api.log( "hitchy:odem:rest:error" );
|
|
11
|
+
|
|
37
12
|
return {
|
|
38
13
|
policies() {
|
|
39
14
|
const { config: { model: modelConfig = {} } } = api;
|
|
@@ -67,8 +42,8 @@ module.exports = function() {
|
|
|
67
42
|
/**
|
|
68
43
|
* Responds on failure in case of not having handled request before.
|
|
69
44
|
*
|
|
70
|
-
* @param {
|
|
71
|
-
* @param {
|
|
45
|
+
* @param {Hitchy.Core.IncomingMessage} req request descriptor
|
|
46
|
+
* @param {Hitchy.Core.ServerResponse} res response manager
|
|
72
47
|
* @param {function(error:?Error):void} next callback to invoke when finished
|
|
73
48
|
* @returns {void}
|
|
74
49
|
*/
|
|
@@ -122,8 +97,8 @@ module.exports = function() {
|
|
|
122
97
|
/**
|
|
123
98
|
* Handles request for listing schemata of all available models.
|
|
124
99
|
*
|
|
125
|
-
* @param {
|
|
126
|
-
* @param {
|
|
100
|
+
* @param {Hitchy.Core.IncomingMessage} req request descriptor
|
|
101
|
+
* @param {Hitchy.Core.ServerResponse} res response manager
|
|
127
102
|
* @returns {void}
|
|
128
103
|
*/
|
|
129
104
|
function reqFetchSchemata( req, res ) {
|
|
@@ -201,8 +176,8 @@ module.exports = function() {
|
|
|
201
176
|
/**
|
|
202
177
|
* Responds on success.
|
|
203
178
|
*
|
|
204
|
-
* @param {
|
|
205
|
-
* @param {
|
|
179
|
+
* @param {Hitchy.Core.IncomingMessage} req request descriptor
|
|
180
|
+
* @param {Hitchy.Core.ServerResponse} res response manager
|
|
206
181
|
* @returns {void}
|
|
207
182
|
*/
|
|
208
183
|
function reqSuccess( req, res ) {
|
|
@@ -229,12 +204,12 @@ module.exports = function() {
|
|
|
229
204
|
/**
|
|
230
205
|
* Handles request for fetching schema of selected model.
|
|
231
206
|
*
|
|
232
|
-
* @param {
|
|
233
|
-
* @param {
|
|
207
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
208
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
234
209
|
* @returns {void}
|
|
235
210
|
*/
|
|
236
211
|
function reqFetchSchema( req, res ) {
|
|
237
|
-
|
|
212
|
+
logDebug( "got request fetching schema" );
|
|
238
213
|
|
|
239
214
|
if ( !Services.OdemRestSchema.mayBeExposed( req, Model ) ) {
|
|
240
215
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -248,12 +223,12 @@ module.exports = function() {
|
|
|
248
223
|
* Handles request for checking whether some selected item of model exists
|
|
249
224
|
* or not.
|
|
250
225
|
*
|
|
251
|
-
* @param {
|
|
252
|
-
* @param {
|
|
226
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
227
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
253
228
|
* @returns {Promise|undefined} promises request processed successfully
|
|
254
229
|
*/
|
|
255
230
|
function reqCheckItem( req, res ) {
|
|
256
|
-
|
|
231
|
+
logDebug( "got request checking if some item exists" );
|
|
257
232
|
|
|
258
233
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
259
234
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -273,7 +248,7 @@ module.exports = function() {
|
|
|
273
248
|
res.status( exists ? 200 : 404 ).send();
|
|
274
249
|
} )
|
|
275
250
|
.catch( error => {
|
|
276
|
-
|
|
251
|
+
logError( "checking %s:", routeName, error );
|
|
277
252
|
res.status( 500 ).json( { error: error.message } );
|
|
278
253
|
} );
|
|
279
254
|
}
|
|
@@ -281,12 +256,12 @@ module.exports = function() {
|
|
|
281
256
|
/**
|
|
282
257
|
* Handles request for fetching data of selected item.
|
|
283
258
|
*
|
|
284
|
-
* @param {
|
|
285
|
-
* @param {
|
|
259
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
260
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
286
261
|
* @returns {Promise} promises request processed successfully
|
|
287
262
|
*/
|
|
288
263
|
function reqFetchItem( req, res ) {
|
|
289
|
-
|
|
264
|
+
logDebug( "got request fetching some item" );
|
|
290
265
|
|
|
291
266
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
292
267
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -302,9 +277,10 @@ module.exports = function() {
|
|
|
302
277
|
const item = new Model( uuid ); // eslint-disable-line new-cap
|
|
303
278
|
|
|
304
279
|
return item.load()
|
|
305
|
-
.then( loaded => res.json( loaded.toObject() ) )
|
|
280
|
+
.then( loaded => res.json( loaded.toObject( { serialized: true } ) ) )
|
|
306
281
|
.catch( error => {
|
|
307
|
-
|
|
282
|
+
logError( "fetching %s:", routeName, error );
|
|
283
|
+
|
|
308
284
|
switch ( error.code ) {
|
|
309
285
|
case "ENOENT" : {
|
|
310
286
|
res.status( 404 ).json( { error: "selected item not found" } );
|
|
@@ -321,12 +297,12 @@ module.exports = function() {
|
|
|
321
297
|
* Fetches items of a collection optionally required to match some provided
|
|
322
298
|
* query.
|
|
323
299
|
*
|
|
324
|
-
* @param {
|
|
325
|
-
* @param {
|
|
300
|
+
* @param {Hitchy.Core.IncomingMessage} req incoming request
|
|
301
|
+
* @param {Hitchy.Core.ServerResponse} res response controller
|
|
326
302
|
* @returns {Promise} promises response sent
|
|
327
303
|
*/
|
|
328
304
|
function reqFetchItems( req, res ) {
|
|
329
|
-
|
|
305
|
+
logDebug( "got request fetching items" );
|
|
330
306
|
|
|
331
307
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
332
308
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -376,12 +352,12 @@ module.exports = function() {
|
|
|
376
352
|
/**
|
|
377
353
|
* Handles request for listing all items of model.
|
|
378
354
|
*
|
|
379
|
-
* @param {
|
|
380
|
-
* @param {
|
|
355
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
356
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
381
357
|
* @returns {Promise|undefined} promises request processed successfully
|
|
382
358
|
*/
|
|
383
359
|
function reqListMatches( req, res ) {
|
|
384
|
-
|
|
360
|
+
logDebug( "got request listing matching items" );
|
|
385
361
|
|
|
386
362
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
387
363
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -409,7 +385,7 @@ module.exports = function() {
|
|
|
409
385
|
} )
|
|
410
386
|
.then( matches => {
|
|
411
387
|
const result = {
|
|
412
|
-
items: matches.map( m => m.toObject() ),
|
|
388
|
+
items: matches.map( m => m.toObject( { serialized: true } ) ),
|
|
413
389
|
};
|
|
414
390
|
|
|
415
391
|
if ( meta ) {
|
|
@@ -420,7 +396,8 @@ module.exports = function() {
|
|
|
420
396
|
res.json( result );
|
|
421
397
|
} )
|
|
422
398
|
.catch( error => {
|
|
423
|
-
|
|
399
|
+
logError( "querying %s:", routeName, error );
|
|
400
|
+
|
|
424
401
|
res.status( 500 ).json( { error: error.message } );
|
|
425
402
|
} );
|
|
426
403
|
}
|
|
@@ -429,12 +406,12 @@ module.exports = function() {
|
|
|
429
406
|
* Handles request for listing all items of model matching single given
|
|
430
407
|
* condition.
|
|
431
408
|
*
|
|
432
|
-
* @param {
|
|
433
|
-
* @param {
|
|
409
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
410
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
434
411
|
* @returns {Promise|undefined} promises request processed successfully
|
|
435
412
|
*/
|
|
436
413
|
function reqListAll( req, res ) {
|
|
437
|
-
|
|
414
|
+
logDebug( "got request listing all items" );
|
|
438
415
|
|
|
439
416
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
440
417
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -451,7 +428,7 @@ module.exports = function() {
|
|
|
451
428
|
}, { loadRecords, metaCollector: meta } )
|
|
452
429
|
.then( matches => {
|
|
453
430
|
const result = {
|
|
454
|
-
items: matches.map( m => m.toObject() ),
|
|
431
|
+
items: matches.map( m => m.toObject( { serialized: true } ) ),
|
|
455
432
|
};
|
|
456
433
|
|
|
457
434
|
if ( meta ) {
|
|
@@ -462,7 +439,8 @@ module.exports = function() {
|
|
|
462
439
|
res.json( result );
|
|
463
440
|
} )
|
|
464
441
|
.catch( error => {
|
|
465
|
-
|
|
442
|
+
logError( "listing %s:", routeName, error );
|
|
443
|
+
|
|
466
444
|
res.status( 500 ).json( { error: error.message } );
|
|
467
445
|
} );
|
|
468
446
|
}
|
|
@@ -470,12 +448,12 @@ module.exports = function() {
|
|
|
470
448
|
/**
|
|
471
449
|
* Handles request for adding new item.
|
|
472
450
|
*
|
|
473
|
-
* @param {
|
|
474
|
-
* @param {
|
|
451
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
452
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
475
453
|
* @returns {Promise|undefined} promises request processed successfully
|
|
476
454
|
*/
|
|
477
455
|
function reqCreateItem( req, res ) {
|
|
478
|
-
|
|
456
|
+
logDebug( "got request creating item" );
|
|
479
457
|
|
|
480
458
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
481
459
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -487,7 +465,7 @@ module.exports = function() {
|
|
|
487
465
|
return ( req.method === "GET" ? Promise.resolve( req.query ) : req.fetchBody() )
|
|
488
466
|
.then( record => {
|
|
489
467
|
if ( record.uuid ) {
|
|
490
|
-
|
|
468
|
+
logDebug( "creating %s:", routeName, "new entry can not be created with uuid" );
|
|
491
469
|
res.status( 400 ).json( { error: "new entry can not be created with uuid" } );
|
|
492
470
|
return undefined;
|
|
493
471
|
}
|
|
@@ -511,12 +489,12 @@ module.exports = function() {
|
|
|
511
489
|
}
|
|
512
490
|
|
|
513
491
|
return item.save().then( saved => {
|
|
514
|
-
|
|
492
|
+
logDebug( "created %s with %s", routeName, saved.uuid );
|
|
515
493
|
res.status( 201 ).json( { uuid: saved.uuid } );
|
|
516
494
|
} );
|
|
517
495
|
} )
|
|
518
496
|
.catch( error => {
|
|
519
|
-
|
|
497
|
+
logError( "creating %s:", routeName, error );
|
|
520
498
|
res.status( 500 ).json( { error: error.message } );
|
|
521
499
|
} );
|
|
522
500
|
}
|
|
@@ -524,12 +502,12 @@ module.exports = function() {
|
|
|
524
502
|
/**
|
|
525
503
|
* Handles request for updating properties of a selected item.
|
|
526
504
|
*
|
|
527
|
-
* @param {
|
|
528
|
-
* @param {
|
|
505
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
506
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
529
507
|
* @returns {Promise|undefined} promises request processed successfully
|
|
530
508
|
*/
|
|
531
509
|
function reqModifyItem( req, res ) {
|
|
532
|
-
|
|
510
|
+
logDebug( "got request to modify some item" );
|
|
533
511
|
|
|
534
512
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
535
513
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -557,6 +535,7 @@ module.exports = function() {
|
|
|
557
535
|
] )
|
|
558
536
|
.then( ( [ loaded, record ] ) => {
|
|
559
537
|
if ( record ) {
|
|
538
|
+
// FIXME consider using Odem's Model.fromObject() method here
|
|
560
539
|
const names = Object.keys( record );
|
|
561
540
|
const numNames = names.length;
|
|
562
541
|
|
|
@@ -567,21 +546,21 @@ module.exports = function() {
|
|
|
567
546
|
const name = names[i];
|
|
568
547
|
|
|
569
548
|
if ( definedProps[name] ) {
|
|
570
|
-
loaded.$properties[name] = record[name];
|
|
549
|
+
loaded.$properties[name] = record[name]; // eslint-disable-line no-param-reassign
|
|
571
550
|
} else if ( definedComputed[name] ) {
|
|
572
|
-
loaded[name] = record[name];
|
|
551
|
+
loaded[name] = record[name]; // eslint-disable-line no-param-reassign
|
|
573
552
|
}
|
|
574
553
|
}
|
|
575
554
|
}
|
|
576
555
|
|
|
577
556
|
return loaded.save()
|
|
578
557
|
.then( saved => {
|
|
579
|
-
res.json( saved.toObject() );
|
|
558
|
+
res.json( saved.toObject( { serialized: true } ) );
|
|
580
559
|
} );
|
|
581
560
|
} );
|
|
582
561
|
} )
|
|
583
562
|
.catch( error => {
|
|
584
|
-
|
|
563
|
+
logError( "updating %s:", routeName, error );
|
|
585
564
|
res.status( 500 ).json( { error: error.message } );
|
|
586
565
|
} );
|
|
587
566
|
}
|
|
@@ -590,12 +569,12 @@ module.exports = function() {
|
|
|
590
569
|
/**
|
|
591
570
|
* Handles request for updating properties of a selected item.
|
|
592
571
|
*
|
|
593
|
-
* @param {
|
|
594
|
-
* @param {
|
|
572
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
573
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
595
574
|
* @returns {Promise|undefined} promises request processed successfully
|
|
596
575
|
*/
|
|
597
576
|
function reqReplaceItem( req, res ) {
|
|
598
|
-
|
|
577
|
+
logDebug( "got request replacing some item" );
|
|
599
578
|
|
|
600
579
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
601
580
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -651,7 +630,7 @@ module.exports = function() {
|
|
|
651
630
|
res.json( { uuid: saved.uuid } );
|
|
652
631
|
} )
|
|
653
632
|
.catch( error => {
|
|
654
|
-
|
|
633
|
+
logError( "updating %s:", routeName, error );
|
|
655
634
|
res.status( 500 ).json( { error: error.message } );
|
|
656
635
|
} );
|
|
657
636
|
}
|
|
@@ -659,12 +638,12 @@ module.exports = function() {
|
|
|
659
638
|
/**
|
|
660
639
|
* Handles request for removing selected item.
|
|
661
640
|
*
|
|
662
|
-
* @param {
|
|
663
|
-
* @param {
|
|
641
|
+
* @param {Hitchy.Core.IncomingMessage} req description of request
|
|
642
|
+
* @param {Hitchy.Core.ServerResponse} res API for creating response
|
|
664
643
|
* @returns {Promise|undefined} promises request processed successfully
|
|
665
644
|
*/
|
|
666
645
|
function reqRemoveItem( req, res ) {
|
|
667
|
-
|
|
646
|
+
logDebug( "got request removing some item" );
|
|
668
647
|
|
|
669
648
|
if ( !Schema.mayBeExposed( req, Model ) ) {
|
|
670
649
|
res.status( 403 ).json( { error: "access forbidden by model" } );
|
|
@@ -689,12 +668,12 @@ module.exports = function() {
|
|
|
689
668
|
action: "remove"
|
|
690
669
|
} ) )
|
|
691
670
|
.catch( error => {
|
|
692
|
-
|
|
671
|
+
logError( "removing %s:", routeName, error );
|
|
693
672
|
res.status( 500 ).json( { error: error.message } );
|
|
694
673
|
} );
|
|
695
674
|
}
|
|
696
675
|
|
|
697
|
-
|
|
676
|
+
logError( "request for removing missing %s ignored", routeName );
|
|
698
677
|
res.status( 404 ).json( { error: "no such entry" } );
|
|
699
678
|
|
|
700
679
|
return undefined;
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hitchy/plugin-odem-rest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "HTTP REST API for Hitchy's ODM",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"lint": "eslint .",
|
|
8
|
-
"test": "hitchy-pm odem --exec mocha --ui=tdd test/**/*.js"
|
|
8
|
+
"test": "hitchy-pm odem --exec mocha --ui=tdd 'test/scripts/**/*.js'",
|
|
9
|
+
"coverage": "hitchy-pm odem --exec c8 mocha --ui=tdd 'test/scripts/**/*.js'"
|
|
9
10
|
},
|
|
10
11
|
"repository": "https://gitlab.com/hitchy/plugin-odem-rest.git",
|
|
11
12
|
"keywords": [
|
|
@@ -17,15 +18,16 @@
|
|
|
17
18
|
"bugs": "https://gitlab.com/hitchy/plugin-odem-rest/-/issues",
|
|
18
19
|
"homepage": "https://gitlab.com/hitchy/plugin-odem-rest#plugin-odem-rest",
|
|
19
20
|
"peerDependencies": {
|
|
20
|
-
"@hitchy/core": "^0.6.
|
|
21
|
-
"@hitchy/plugin-odem": "^0.
|
|
21
|
+
"@hitchy/core": "^0.6.18",
|
|
22
|
+
"@hitchy/plugin-odem": "^0.7.0"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"eslint
|
|
27
|
-
"
|
|
28
|
-
"
|
|
25
|
+
"@hitchy/server-dev-tools": "^0.4.2",
|
|
26
|
+
"c8": "^7.11.0",
|
|
27
|
+
"eslint": "^8.13.0",
|
|
28
|
+
"eslint-config-cepharum": "^1.0.12",
|
|
29
|
+
"eslint-plugin-promise": "^6.0.0",
|
|
30
|
+
"mocha": "^9.2.2",
|
|
29
31
|
"should": "^13.2.3",
|
|
30
32
|
"should-http": "^0.1.1"
|
|
31
33
|
}
|