@@ -73,7 +73,7 @@ module Resource = {
73
73
})
74
74
}
75
75
76
- let uniqueKeywords : array < string > => array < string > = % raw ( ` ( keywords ) => [ ... new Set (keywords)] ` )
76
+ let uniqueKeywords = arr => arr -> Set . fromArray -> Set . toArray
77
77
78
78
let isOfficial = (res : t ) => {
79
79
switch res {
@@ -340,24 +340,15 @@ module InfoSidebar = {
340
340
}
341
341
342
342
type props = {
343
- " packages" : array <npmPackage >,
344
- " urlResources" : array <urlResource >,
345
- " unmaintained" : array <npmPackage >,
343
+ packages : array <npmPackage >,
344
+ urlResources : array <urlResource >,
345
+ unmaintained : array <npmPackage >,
346
346
}
347
347
348
348
type state =
349
349
| All
350
350
| Filtered (string ) // search term
351
351
352
- let scrollToTop : unit => unit = %raw (` function () {
353
- window .scroll ({
354
- top: 0 ,
355
- left: 0 ,
356
- behavior: ' smooth'
357
- });
358
- }
359
- ` )
360
-
361
352
let default = (props : props ) => {
362
353
open Markdown
363
354
@@ -373,9 +364,9 @@ let default = (props: props) => {
373
364
})
374
365
375
366
let allResources = {
376
- let npms = props [ " packages" ] -> Array .map (pkg => Resource .Npm (pkg ))
377
- let urls = props [ " urlResources" ] -> Array .map (res => Resource .Url (res ))
378
- let outdated = props [ " unmaintained" ] -> Array .map (pkg => Resource .Outdated (pkg ))
367
+ let npms = props . packages -> Array .map (pkg => Resource .Npm (pkg ))
368
+ let urls = props . urlResources -> Array .map (res => Resource .Url (res ))
369
+ let outdated = props . unmaintained -> Array .map (pkg => Resource .Outdated (pkg ))
379
370
Belt .Array .concatMany ([npms , urls , outdated ])
380
371
}
381
372
@@ -420,7 +411,7 @@ let default = (props: props) => {
420
411
})
421
412
422
413
let onKeywordSelect = keyword => {
423
- scrollToTop ( )
414
+ WebAPI . Window . scrollTo ( window , ~ options = { left : 0.0 , top : 0.0 , behavior : Smooth } )
424
415
setState (_ => {
425
416
Filtered (keyword )
426
417
})
@@ -524,73 +515,99 @@ let default = (props: props) => {
524
515
</>
525
516
}
526
517
527
- type npmData = {
528
- "objects" : array <{
529
- "searchScore" : float ,
530
- "score" : {
531
- "final" : float ,
532
- "detail" : {"quality" : float , "popularity" : float , "maintenance" : float },
533
- },
534
- "package" : {
535
- "name" : string ,
536
- "keywords" : array <string >,
537
- "description" : option <string >,
538
- "version" : string ,
539
- "links" : {"npm" : string , "repository" : option <string >},
540
- },
541
- }>,
542
- }
543
-
544
- module Response = {
545
- type t
546
- @send external json : t => promise <npmData > = "json"
518
+ let parsePkgs = data => {
519
+ open JSON
520
+
521
+ switch data {
522
+ | Object (dict {"objects" : Array (arr )}) =>
523
+ arr -> Array .filterMap (pkg => {
524
+ switch pkg {
525
+ | Object (dict {
526
+ "searchScore" : Number (searchScore ),
527
+ "score" : Object (dict {"detail" : Object (dict {"maintenance" : Number (maintenanceScore )})}),
528
+ "package" : Object (dict {
529
+ "name" : String (name ),
530
+ "keywords" : Array (keywords ),
531
+ "version" : String (version ),
532
+ "description" : ?Some (String (description )),
533
+ "links" : Object (dict {
534
+ "npm" : String (npmHref ),
535
+ "repository" : ?Some (String (repositoryHref )),
536
+ }),
537
+ }),
538
+ }) =>
539
+ let keywords =
540
+ keywords
541
+ -> Array .filterMap (k => {
542
+ switch k {
543
+ | String (k ) => Some (k )
544
+ | _ => None
545
+ }
546
+ })
547
+ -> Resource .filterKeywords
548
+ -> Resource .uniqueKeywords
549
+
550
+ Some ({
551
+ name ,
552
+ version ,
553
+ keywords ,
554
+ description ,
555
+ repositoryHref : repositoryHref -> Null .make ,
556
+ npmHref ,
557
+ searchScore ,
558
+ maintenanceScore ,
559
+ })
560
+ | _ => None
561
+ }
562
+ })
563
+ | _ => []
564
+ }
547
565
}
548
566
549
- @val external fetchNpmPackages : string => promise <Response .t > = "fetch"
550
-
551
- let parsePkgs = data =>
552
- Array .map (data ["objects" ], item => {
553
- let pkg = item ["package" ]
554
- {
555
- name : pkg ["name" ],
556
- version : pkg ["version" ],
557
- keywords : Resource .filterKeywords (pkg ["keywords" ])-> Resource .uniqueKeywords ,
558
- description : Option .getOr (pkg ["description" ], "" ),
559
- repositoryHref : Null .fromOption (pkg ["links" ]["repository" ]),
560
- npmHref : pkg ["links" ]["npm" ],
561
- searchScore : item ["searchScore" ],
562
- maintenanceScore : item ["score" ]["detail" ]["maintenance" ],
563
- }
564
- })
565
-
566
567
let getStaticProps : Next .GetStaticProps .t <props , unit > = async _ctx => {
567
568
let baseUrl = "https://registry.npmjs.org/-/v1/search?text=keywords:rescript&size=250&maintenance=1.0&popularity=0.5&quality=0.9"
568
569
569
570
let (one , two , three ) = await Promise .all3 ((
570
- fetchNpmPackages (baseUrl ),
571
- fetchNpmPackages (baseUrl ++ "&from=250" ),
572
- fetchNpmPackages (baseUrl ++ "&from=500" ),
571
+ fetch (baseUrl ),
572
+ fetch (baseUrl ++ "&from=250" ),
573
+ fetch (baseUrl ++ "&from=500" ),
573
574
))
574
575
576
+ let responseToOption = async response => {
577
+ try {
578
+ let json = await response -> WebAPI .Response .json
579
+ Some (json )
580
+ } catch {
581
+ | _ =>
582
+ Console .error2 ("Failed to parse response" , response )
583
+ None
584
+ }
585
+ }
586
+
575
587
let (data1 , data2 , data3 ) = await Promise .all3 ((
576
- one -> Response . json ,
577
- two -> Response . json ,
578
- three -> Response . json ,
588
+ one -> responseToOption ,
589
+ two -> responseToOption ,
590
+ three -> responseToOption ,
579
591
))
580
592
581
593
let unmaintained = []
582
594
583
595
let pkges =
584
- parsePkgs (data1 )
585
- -> Array .concat (parsePkgs (data2 ))
586
- -> Array .concat (parsePkgs (data3 ))
596
+ [data1 , data2 , data3 ]
597
+ -> Array .filterMap (d =>
598
+ switch d {
599
+ | Some (d ) => Some (parsePkgs (d ))
600
+ | None => None
601
+ }
602
+ )
603
+ -> Array .flat
587
604
-> Array .filter (pkg => {
588
605
if packageAllowList -> Array .includes (pkg .name ) {
589
606
true
590
607
} else if pkg .name -> String .includes ("reason" ) {
591
608
false
592
609
} else if pkg .maintenanceScore < 0.3 {
593
- let _ = unmaintained -> Array .push (pkg )
610
+ unmaintained -> Array .push (pkg )
594
611
false
595
612
} else {
596
613
true
@@ -606,9 +623,9 @@ let getStaticProps: Next.GetStaticProps.t<props, unit> = async _ctx => {
606
623
607
624
{
608
625
"props" : {
609
- " packages" : pkges ,
610
- "unmaintained" : unmaintained ,
611
- "urlResources" : urlResources ,
626
+ packages : pkges ,
627
+ unmaintained ,
628
+ urlResources ,
612
629
},
613
630
}
614
631
}
0 commit comments