1
+ 'use strict' ;
2
+
3
+ function _asyncToGenerator ( fn ) { return function ( ) { var gen = fn . apply ( this , arguments ) ; return new Promise ( function ( resolve , reject ) { function step ( key , arg ) { try { var info = gen [ key ] ( arg ) ; var value = info . value ; } catch ( error ) { reject ( error ) ; return ; } if ( info . done ) { resolve ( value ) ; } else { return Promise . resolve ( value ) . then ( function ( value ) { step ( "next" , value ) ; } , function ( err ) { step ( "throw" , err ) ; } ) ; } } return step ( "next" ) ; } ) ; } ; }
4
+
5
+ /* eslint-disable arrow-body-style */
6
+
7
+ var graphqlHTTP = require ( 'express-graphql' ) ;
8
+
9
+ var _require = require ( 'graphql' ) ,
10
+ buildSchema = _require . buildSchema ;
11
+
12
+ var fetch = require ( 'node-fetch' ) ;
13
+
14
+ var schema = buildSchema ( '\n type Registry {\n href: String\n ocVersion: String\n type: String\n }\n\n type Component {\n name: String\n description: String\n version: String\n }\n\n type Query {\n registry: Registry\n component(name: String): Component\n components: [Component]\n }\n' ) ;
15
+
16
+ var fetchComponent = function ( ) {
17
+ var _ref = _asyncToGenerator ( regeneratorRuntime . mark ( function _callee ( url ) {
18
+ return regeneratorRuntime . wrap ( function _callee$ ( _context ) {
19
+ while ( 1 ) {
20
+ switch ( _context . prev = _context . next ) {
21
+ case 0 :
22
+ return _context . abrupt ( 'return' , fetch ( url ) . then ( function ( response ) {
23
+ return response . json ( ) ;
24
+ } ) . then ( function ( data ) {
25
+ return data ;
26
+ } ) ) ;
27
+
28
+ case 1 :
29
+ case 'end' :
30
+ return _context . stop ( ) ;
31
+ }
32
+ }
33
+ } , _callee , undefined ) ;
34
+ } ) ) ;
35
+
36
+ return function fetchComponent ( _x ) {
37
+ return _ref . apply ( this , arguments ) ;
38
+ } ;
39
+ } ( ) ;
40
+
41
+ var makeComponent = function ( ) {
42
+ var _ref2 = _asyncToGenerator ( regeneratorRuntime . mark ( function _callee2 ( baseUrl , name ) {
43
+ var url , info , copy ;
44
+ return regeneratorRuntime . wrap ( function _callee2$ ( _context2 ) {
45
+ while ( 1 ) {
46
+ switch ( _context2 . prev = _context2 . next ) {
47
+ case 0 :
48
+ url = '' + baseUrl + name + '/~info' ;
49
+ _context2 . next = 3 ;
50
+ return fetchComponent ( url ) ;
51
+
52
+ case 3 :
53
+ info = _context2 . sent ;
54
+ copy = Object . assign ( { } , info ) ;
55
+ return _context2 . abrupt ( 'return' , copy ) ;
56
+
57
+ case 6 :
58
+ case 'end' :
59
+ return _context2 . stop ( ) ;
60
+ }
61
+ }
62
+ } , _callee2 , undefined ) ;
63
+ } ) ) ;
64
+
65
+ return function makeComponent ( _x2 , _x3 ) {
66
+ return _ref2 . apply ( this , arguments ) ;
67
+ } ;
68
+ } ( ) ;
69
+
70
+ var root = function root ( options ) {
71
+ return {
72
+ registry : function ( ) {
73
+ var _ref3 = _asyncToGenerator ( regeneratorRuntime . mark ( function _callee3 ( ) {
74
+ return regeneratorRuntime . wrap ( function _callee3$ ( _context3 ) {
75
+ while ( 1 ) {
76
+ switch ( _context3 . prev = _context3 . next ) {
77
+ case 0 :
78
+ return _context3 . abrupt ( 'return' , fetch ( options . baseUrl ) . then ( function ( response ) {
79
+ return response . json ( ) ;
80
+ } ) . then ( function ( data ) {
81
+ return {
82
+ href : data . href ,
83
+ ocVersion : data . ocVersion ,
84
+ type : data . type
85
+ } ;
86
+ } ) ) ;
87
+
88
+ case 1 :
89
+ case 'end' :
90
+ return _context3 . stop ( ) ;
91
+ }
92
+ }
93
+ } , _callee3 , undefined ) ;
94
+ } ) ) ;
95
+
96
+ return function registry ( ) {
97
+ return _ref3 . apply ( this , arguments ) ;
98
+ } ;
99
+ } ( ) ,
100
+ components : function ( ) {
101
+ var _ref4 = _asyncToGenerator ( regeneratorRuntime . mark ( function _callee4 ( ) {
102
+ return regeneratorRuntime . wrap ( function _callee4$ ( _context4 ) {
103
+ while ( 1 ) {
104
+ switch ( _context4 . prev = _context4 . next ) {
105
+ case 0 :
106
+ return _context4 . abrupt ( 'return' , fetch ( options . baseUrl ) . then ( function ( response ) {
107
+ return response . json ( ) ;
108
+ } ) . then ( function ( data ) {
109
+ return data . components . map ( function ( component ) {
110
+ var name = component . replace ( options . baseUrl , '' ) ;
111
+ return makeComponent ( options . baseUrl , name ) ;
112
+ } ) ;
113
+ } ) ) ;
114
+
115
+ case 1 :
116
+ case 'end' :
117
+ return _context4 . stop ( ) ;
118
+ }
119
+ }
120
+ } , _callee4 , undefined ) ;
121
+ } ) ) ;
122
+
123
+ return function components ( ) {
124
+ return _ref4 . apply ( this , arguments ) ;
125
+ } ;
126
+ } ( ) ,
127
+ component : function ( ) {
128
+ var _ref5 = _asyncToGenerator ( regeneratorRuntime . mark ( function _callee5 ( _ref6 ) {
129
+ var name = _ref6 . name ;
130
+ return regeneratorRuntime . wrap ( function _callee5$ ( _context5 ) {
131
+ while ( 1 ) {
132
+ switch ( _context5 . prev = _context5 . next ) {
133
+ case 0 :
134
+ return _context5 . abrupt ( 'return' , makeComponent ( options . baseUrl , name ) ) ;
135
+
136
+ case 1 :
137
+ case 'end' :
138
+ return _context5 . stop ( ) ;
139
+ }
140
+ }
141
+ } , _callee5 , undefined ) ;
142
+ } ) ) ;
143
+
144
+ return function component ( _x4 ) {
145
+ return _ref5 . apply ( this , arguments ) ;
146
+ } ;
147
+ } ( )
148
+ } ;
149
+ } ;
150
+
151
+ var factory = function factory ( options ) {
152
+ return graphqlHTTP ( {
153
+ schema : schema ,
154
+ rootValue : root ( options ) ,
155
+ graphiql : options . graphiql
156
+ } ) ;
157
+ } ;
158
+
159
+ module . exports = factory ;
0 commit comments