@@ -2,6 +2,7 @@ const { byValues, byString } = require('sort-es');
2
2
3
3
const express = require ( 'express' ) ;
4
4
const nocache = require ( 'nocache' ) ;
5
+ const registry = require ( '../registry' ) ;
5
6
6
7
/**
7
8
* Map a Component to a displayable (api/ui) item.
@@ -23,9 +24,9 @@ function mapComponentToItem(key, component) {
23
24
* @param listFunction
24
25
* @returns {{id: string}[] }
25
26
*/
26
- function mapComponentsToList ( listFunction ) {
27
- return Object . keys ( listFunction ( ) )
28
- . map ( ( key ) => mapComponentToItem ( key , listFunction ( ) [ key ] ) )
27
+ function mapComponentsToList ( components ) {
28
+ return Object . keys ( components )
29
+ . map ( ( key ) => mapComponentToItem ( key , components [ key ] ) )
29
30
. sort ( byValues ( [
30
31
[ ( x ) => x . type , byString ( ) ] ,
31
32
[ ( x ) => x . name , byString ( ) ] ,
@@ -37,8 +38,8 @@ function mapComponentsToList(listFunction) {
37
38
* @param req
38
39
* @param res
39
40
*/
40
- function getAll ( req , res , listFunction ) {
41
- res . status ( 200 ) . json ( mapComponentsToList ( listFunction ) ) ;
41
+ function getAll ( req , res , kind ) {
42
+ res . status ( 200 ) . json ( mapComponentsToList ( registry . getState ( ) [ kind ] ) ) ;
42
43
}
43
44
44
45
/**
@@ -47,9 +48,15 @@ function getAll(req, res, listFunction) {
47
48
* @param res
48
49
* @param listFunction
49
50
*/
50
- function getById ( req , res , listFunction ) {
51
- const { id } = req . params ;
52
- const component = listFunction ( ) [ id ] ;
51
+ function getById ( req , res , kind ) {
52
+ const { type, name } = req . params ;
53
+ let id = `${ kind } .${ type } .${ name } ` ;
54
+
55
+ // Hack for registries because id and name are equivalent
56
+ if ( kind === 'registry' ) {
57
+ id = `${ name } ` ;
58
+ }
59
+ const component = registry . getState ( ) [ kind ] [ id ] ;
53
60
if ( component ) {
54
61
res . status ( 200 ) . json ( mapComponentToItem ( id , component ) ) ;
55
62
} else {
@@ -59,18 +66,19 @@ function getById(req, res, listFunction) {
59
66
60
67
/**
61
68
* Init the component router.
62
- * @param listFunction
69
+ * @param kind
63
70
* @returns {*|Router }
64
71
*/
65
- function init ( listFunction ) {
72
+ function init ( kind ) {
66
73
const router = express . Router ( ) ;
67
74
router . use ( nocache ( ) ) ;
68
- router . get ( '/' , ( req , res ) => getAll ( req , res , listFunction ) ) ;
69
- router . get ( '/:id ' , ( req , res ) => getById ( req , res , listFunction ) ) ;
75
+ router . get ( '/' , ( req , res ) => getAll ( req , res , kind ) ) ;
76
+ router . get ( '/:type/:name ' , ( req , res ) => getById ( req , res , kind ) ) ;
70
77
return router ;
71
78
}
72
79
73
80
module . exports = {
74
81
init,
75
82
mapComponentsToList,
83
+ getById,
76
84
} ;
0 commit comments