@@ -362,7 +362,6 @@ router.post('/', async (req, res) => {
362
362
return ;
363
363
}
364
364
365
- let { license, contributors, changelogs, keywords } = req . body ;
366
365
const { pluginJson, icon, readme, changelogsFile } = await exploreZip ( pluginZip . data ) ;
367
366
368
367
const pluginId = pluginJson . id . toLowerCase ( ) ;
@@ -387,16 +386,7 @@ router.post('/', async (req, res) => {
387
386
return ;
388
387
}
389
388
390
- const {
391
- name,
392
- price,
393
- version,
394
- minVersionCode,
395
- license : licenseFromPlugin ,
396
- keywords : keywordsFromPlugin ,
397
- changelogs : changelogsFromPlugin ,
398
- contributors : contributorsFromPlugin ,
399
- } = pluginJson ;
389
+ const { name, price, version, minVersionCode } = pluginJson ;
400
390
401
391
if ( ! VERSION_REGEX . test ( version ) ) {
402
392
res . status ( 400 ) . send ( { error : 'Invalid version number, version should be in the format x.x.x' } ) ;
@@ -417,66 +407,41 @@ router.post('/', async (req, res) => {
417
407
await registerSKU ( name , pluginId , price ) ;
418
408
}
419
409
420
- if ( ! license && licenseFromPlugin ) {
421
- if ( ! validLicenses . includes ( licenseFromPlugin ) ) {
422
- res . status ( 400 ) . send ( { error : 'Invalid license' } ) ;
423
- return ;
424
- }
425
-
426
- license = licenseFromPlugin ;
410
+ const { changelogs } = req . body ;
411
+ if ( ! pluginJson . changelogs && ( changelogsFile || changelogs ) ) {
412
+ pluginJson . changelogs = changelogsFile || changelogs ;
427
413
}
428
414
429
- if ( ! keywords && keywordsFromPlugin ) {
430
- if ( Array . isArray ( keywordsFromPlugin ) ) {
431
- const invalidKeywords = keywordsFromPlugin . filter ( ( keyword ) => typeof keyword !== 'string' ) ;
432
- if ( invalidKeywords . length ) {
433
- res . status ( 400 ) . send ( { error : 'Keywords should be an array of strings' } ) ;
434
- return ;
435
- }
415
+ validatePluginData ( pluginJson ) ;
436
416
437
- keywords = keywordsFromPlugin . join ( ', ' ) ;
438
- }
417
+ const insert = [
418
+ [ Plugin . ID , pluginId ] ,
419
+ [ Plugin . NAME , name ] ,
420
+ [ Plugin . PRICE , price ] ,
421
+ [ Plugin . VERSION , version ] ,
422
+ [ Plugin . USER_ID , user . id ] ,
423
+ [ Plugin . DESCRIPTION , readme ] ,
424
+ [ Plugin . SKU , getPluginSKU ( pluginId ) ] ,
425
+ [ Plugin . MIN_VERSION_CODE , minVersionCode ] ,
426
+ ] ;
439
427
440
- keywords = keywordsFromPlugin ;
428
+ if ( pluginJson . license ) {
429
+ insert . push ( [ Plugin . LICENSE , pluginJson . license ] ) ;
441
430
}
442
431
443
- if ( ! contributors && contributorsFromPlugin ) {
444
- if ( Array . isArray ( contributorsFromPlugin ) ) {
445
- const invalidContributors = contributorsFromPlugin . filter ( ( contributor ) => typeof contributor !== 'string' ) ;
446
- if ( invalidContributors . length ) {
447
- res . status ( 400 ) . send ( { error : 'Contributors should be an array of strings' } ) ;
448
- return ;
449
- }
450
-
451
- contributors = contributorsFromPlugin . join ( ', ' ) ;
452
- }
453
-
454
- contributors = contributorsFromPlugin ;
432
+ if ( pluginJson . contributors ) {
433
+ insert . push ( [ Plugin . CONTRIBUTORS , JSON . stringify ( pluginJson . contributors ) ] ) ;
455
434
}
456
435
457
- if ( ! changelogs && ( changelogsFromPlugin || changelogsFile ) ) {
458
- if ( changelogsFromPlugin && typeof changelogsFromPlugin !== 'string' ) {
459
- res . status ( 400 ) . send ( { error : 'Changelogs should be a string' } ) ;
460
- return ;
461
- }
436
+ if ( pluginJson . keywords ) {
437
+ insert . push ( [ Plugin . KEYWORDS , JSON . stringify ( pluginJson . keywords ) ] ) ;
438
+ }
462
439
463
- changelogs = changelogsFromPlugin || changelogsFile ;
440
+ if ( pluginJson . changelogs ) {
441
+ insert . push ( [ Plugin . CHANGELOGS , pluginJson . changelogs ] ) ;
464
442
}
465
443
466
- await Plugin . insert (
467
- [ Plugin . ID , pluginId ] ,
468
- [ Plugin . SKU , getPluginSKU ( pluginId ) ] ,
469
- [ Plugin . NAME , name ] ,
470
- [ Plugin . PRICE , price ] ,
471
- [ Plugin . VERSION , version ] ,
472
- [ Plugin . USER_ID , user . id ] ,
473
- [ Plugin . DESCRIPTION , readme ] ,
474
- [ Plugin . LICENSE , license ] ,
475
- [ Plugin . CONTRIBUTORS , contributors ] ,
476
- [ Plugin . CHANGELOGS , changelogs || changelogsFile || changelogsFromPlugin ] ,
477
- [ Plugin . KEYWORDS , keywords ] ,
478
- [ Plugin . MIN_VERSION_CODE , minVersionCode ] ,
479
- ) ;
444
+ await Plugin . insert ( ...insert ) ;
480
445
481
446
savePlugin ( pluginId , pluginZip , icon ) ;
482
447
res . send ( { message : 'Plugin uploaded successfully' } ) ;
@@ -513,7 +478,6 @@ router.put('/', async (req, res) => {
513
478
return ;
514
479
}
515
480
516
- let { license, contributors, changelogs, keywords } = req . body ;
517
481
const { pluginJson, icon, readme, changelogs : changelogsFile } = await exploreZip ( pluginZip . data ) ;
518
482
519
483
const errorMessage = validatePlugin ( pluginJson , icon , readme ) ;
@@ -522,16 +486,7 @@ router.put('/', async (req, res) => {
522
486
return ;
523
487
}
524
488
525
- const {
526
- name,
527
- price,
528
- version,
529
- id : pluginId ,
530
- license : licenseFromPlugin ,
531
- keywords : keywordsFromPlugin ,
532
- changelogs : changelogsFromPlugin ,
533
- contributors : contributorsFromPlugin ,
534
- } = pluginJson ;
489
+ const { name, price, version, id : pluginId } = pluginJson ;
535
490
536
491
if ( ! VERSION_REGEX . test ( version ) ) {
537
492
res . status ( 400 ) . send ( { error : 'Invalid version number, version should be in the format x.x.x' } ) ;
@@ -544,59 +499,30 @@ router.put('/', async (req, res) => {
544
499
return ;
545
500
}
546
501
547
- if ( ! license && licenseFromPlugin ) {
548
- if ( ! validLicenses . includes ( licenseFromPlugin ) ) {
549
- res . status ( 400 ) . send ( { error : 'Invalid license' } ) ;
550
- return ;
551
- }
552
-
553
- license = licenseFromPlugin ;
502
+ const { changelogs } = req . body ;
503
+ if ( ! pluginJson . changelogs && ( changelogsFile || changelogs ) ) {
504
+ pluginJson . changelogs = changelogsFile || changelogs ;
554
505
}
555
506
556
- if ( ! keywords && keywordsFromPlugin ) {
557
- if ( Array . isArray ( keywordsFromPlugin ) ) {
558
- const invalidKeywords = keywordsFromPlugin . filter ( ( keyword ) => typeof keyword !== 'string' ) ;
559
- if ( invalidKeywords . length ) {
560
- res . status ( 400 ) . send ( { error : 'Keywords should be an array of strings' } ) ;
561
- return ;
562
- }
507
+ validatePluginData ( pluginJson ) ;
563
508
564
- keywords = keywordsFromPlugin . join ( ', ' ) ;
565
- }
509
+ const updates = [ [ Plugin . DESCRIPTION , readme ] ] ;
566
510
567
- keywords = keywordsFromPlugin ;
511
+ if ( pluginJson . license ) {
512
+ updates . push ( [ Plugin . LICENSE , pluginJson . license ] ) ;
568
513
}
569
514
570
- if ( ! contributors && contributorsFromPlugin ) {
571
- if ( Array . isArray ( contributorsFromPlugin ) ) {
572
- const invalidContributors = contributorsFromPlugin . filter ( ( contributor ) => typeof contributor !== 'string' ) ;
573
- if ( invalidContributors . length ) {
574
- res . status ( 400 ) . send ( { error : 'Contributors should be an array of strings' } ) ;
575
- return ;
576
- }
577
-
578
- contributors = contributorsFromPlugin . join ( ', ' ) ;
579
- }
580
-
581
- contributors = contributorsFromPlugin ;
515
+ if ( pluginJson . contributors ) {
516
+ updates . push ( [ Plugin . CONTRIBUTORS , JSON . stringify ( pluginJson . contributors ) ] ) ;
582
517
}
583
518
584
- if ( ! changelogs && ( changelogsFromPlugin || changelogsFile ) ) {
585
- if ( changelogsFromPlugin && typeof changelogsFromPlugin !== 'string' ) {
586
- res . status ( 400 ) . send ( { error : 'Changelogs should be a string' } ) ;
587
- return ;
588
- }
589
-
590
- changelogs = changelogsFromPlugin || changelogsFile ;
519
+ if ( pluginJson . keywords ) {
520
+ updates . push ( [ Plugin . KEYWORDS , JSON . stringify ( pluginJson . keywords ) ] ) ;
591
521
}
592
522
593
- const updates = [
594
- [ Plugin . DESCRIPTION , readme ] ,
595
- [ Plugin . LICENSE , license ] ,
596
- [ Plugin . CONTRIBUTORS , contributors ] ,
597
- [ Plugin . CHANGELOGS , changelogs ] ,
598
- [ Plugin . KEYWORDS , keywords ] ,
599
- ] ;
523
+ if ( pluginJson . changelogs ) {
524
+ updates . push ( [ Plugin . CHANGELOGS , pluginJson . changelogs ] ) ;
525
+ }
600
526
601
527
if ( version !== row . version ) {
602
528
if ( ! isVersionGreater ( version , row . version ) ) {
@@ -845,4 +771,54 @@ function isVersionGreater(newV, oldV) {
845
771
return false ;
846
772
}
847
773
774
+ /**
775
+ * Validate plugin data
776
+ * @param {object } arg
777
+ * @param {string } arg.license
778
+ * @param {object[] } arg.contributors
779
+ * @param {string[] } arg.keywords
780
+ * @param {string } arg.changelogs
781
+ */
782
+ function validatePluginData ( { license, contributors, keywords, changelogs } ) {
783
+ if ( license && ! validLicenses . includes ( license ) ) {
784
+ throw new Error ( 'Invalid license' ) ;
785
+ }
786
+
787
+ if ( contributors ) {
788
+ const error = new Error ( 'Contributors should be an array of {name, role, github}' ) ;
789
+ if ( ! Array . isArray ( contributors ) ) {
790
+ throw error ;
791
+ }
792
+
793
+ const invalidContributors = contributors . filter ( ( contributor ) => {
794
+ for ( const key in contributor ) {
795
+ if ( ! [ 'role' , 'github' , 'name' ] . includes ( key ) ) {
796
+ return true ;
797
+ }
798
+ }
799
+ return false ;
800
+ } ) ;
801
+
802
+ if ( invalidContributors . length ) {
803
+ throw error ;
804
+ }
805
+ }
806
+
807
+ if ( keywords ) {
808
+ const error = new Error ( 'Keywords should be an array of string' ) ;
809
+ if ( ! Array . isArray ( keywords ) ) {
810
+ throw error ;
811
+ }
812
+
813
+ const invalidKeywords = keywords . filter ( ( keyword ) => typeof keyword !== 'string' ) ;
814
+ if ( invalidKeywords . length ) {
815
+ throw error ;
816
+ }
817
+ }
818
+
819
+ if ( changelogs && typeof changelogs !== 'string' ) {
820
+ throw new Error ( 'Changelogs should be a string' ) ;
821
+ }
822
+ }
823
+
848
824
module . exports = router ;
0 commit comments