@@ -18,161 +18,186 @@ program
18
18
. version ( require ( '../package.json' ) . version )
19
19
. usage ( '<OAS JSON file path(s) and/or remote url(s)> [options]' )
20
20
. arguments ( '<path(s) and/or url(s)>' )
21
- . option ( '-p, --port <port>' , 'select the port where the server will start' , parseInt )
21
+ . option (
22
+ '-p, --port <port>' ,
23
+ 'select the port where the server will start' ,
24
+ parseInt
25
+ )
22
26
. option ( '-u, --url <url>' , 'select the base url which paths will be built on' )
23
- . option ( '-s, --strict' , 'throw an error if OASGraph cannot run without compensating for errors or missing data in the OAS' )
24
- . option ( '-f, --fillEmptyResponses' , 'create placeholder schemas for operations with HTTP status code 204 (no response) rather than ignore them' )
25
- . option ( '-o, --operationIdFieldNames' , 'create field names based on the operationId' )
27
+ . option (
28
+ '-s, --strict' ,
29
+ 'throw an error if OASGraph cannot run without compensating for errors or missing data in the OAS'
30
+ )
31
+ . option (
32
+ '-f, --fillEmptyResponses' ,
33
+ 'create placeholder schemas for operations with HTTP status code 204 (no response) rather than ignore them'
34
+ )
35
+ . option (
36
+ '-o, --operationIdFieldNames' ,
37
+ 'create field names based on the operationId'
38
+ )
26
39
. option ( '--cors' , 'enable Cross-origin resource sharing (CORS)' )
27
- . option ( '--no-viewer' , 'do not create GraphQL viewer objects for passing authentication credentials' )
28
- . option ( '--no-extensions' , 'do not add extentions, containing information about failed REST calls, to the GraphQL errors objects' )
40
+ . option (
41
+ '--no-viewer' ,
42
+ 'do not create GraphQL viewer objects for passing authentication credentials'
43
+ )
44
+ . option (
45
+ '--no-extensions' ,
46
+ 'do not add extentions, containing information about failed REST calls, to the GraphQL errors objects'
47
+ )
29
48
. option ( '--save <file path>' , 'save schema to path and do not start server' )
30
49
. parse ( process . argv )
31
50
32
51
// Select the port on which to host the GraphQL server
33
- const portNumber : number | string = program . port ?
34
- program . port :
35
- 3000
52
+ const portNumber : number | string = program . port ? program . port : 3000
36
53
37
54
const filePaths = program . args
38
55
39
56
if ( typeof filePaths === 'undefined' || filePaths . length === 0 ) {
40
57
console . error ( 'No path(s) provided' )
41
- console . error ( 'Please refer to the help manual (oasgraph -h) for more information' )
58
+ console . error (
59
+ 'Please refer to the help manual (oasgraph -h) for more information'
60
+ )
42
61
process . exit ( 1 )
43
62
}
44
63
45
64
// Load the OASs based off of the provided paths
46
- Promise . all ( filePaths . map ( filePath => {
47
- return new Promise ( ( resolve , reject ) => {
48
- // Check if the file exists
49
- if ( fs . existsSync ( path . resolve ( filePath ) ) ) {
50
- try {
51
- resolve ( readFile ( path . resolve ( filePath ) ) )
52
- } catch ( error ) {
53
- console . error ( error )
65
+ Promise . all (
66
+ filePaths . map ( filePath => {
67
+ return new Promise ( ( resolve , reject ) => {
68
+ // Check if the file exists
69
+ if ( fs . existsSync ( path . resolve ( filePath ) ) ) {
70
+ try {
71
+ resolve ( readFile ( path . resolve ( filePath ) ) )
72
+ } catch ( error ) {
73
+ console . error ( error )
74
+ reject ( filePath )
75
+ }
76
+
77
+ // Check if file is in a remote location
78
+ } else if ( filePath . match ( / ^ h t t p s ? / g) ) {
79
+ getRemoteFileSpec ( filePath )
80
+ . then ( remoteContent => {
81
+ resolve ( remoteContent )
82
+ } )
83
+ . catch ( error => {
84
+ console . error ( error )
85
+ reject ( filePath )
86
+ } )
87
+
88
+ // Cannot determine location of file
89
+ } else {
54
90
reject ( filePath )
55
91
}
56
-
57
- // Check if file is in a remote location
58
- } else if ( filePath . match ( / ^ h t t p s ? / g) ) {
59
- getRemoteFileSpec ( filePath )
60
- . then ( ( remoteContent ) => {
61
- resolve ( remoteContent )
62
- } )
63
- . catch ( ( error ) => {
64
- console . error ( error )
65
- reject ( filePath )
66
- } )
67
-
68
- // Cannot determine location of file
69
- } else {
70
- reject ( filePath )
71
- }
92
+ } )
93
+ } )
94
+ )
95
+ . then ( oass => {
96
+ startGraphQLServer ( oass , portNumber )
97
+ } )
98
+ . catch ( filePath => {
99
+ console . error (
100
+ `OASGraph cannot read file. File "${ filePath } " does not exist.`
101
+ )
102
+ process . exit ( 1 )
72
103
} )
73
- } ) )
74
- . then ( oass => {
75
- startGraphQLServer ( oass , portNumber )
76
- } )
77
- . catch ( filePath => {
78
- console . error ( `OASGraph cannot read file. File "${ filePath } " does not exist.` )
79
- process . exit ( 1 )
80
- } )
81
-
82
104
83
105
/**
84
- * Returns content of read JSON/YAML file.
85
- *
86
- * @param {string } path Path to file to read
87
- * @return {object } Content of read file
88
- */
89
- function readFile ( path ) {
106
+ * Returns content of read JSON/YAML file.
107
+ *
108
+ * @param {string } path Path to file to read
109
+ * @return {object } Content of read file
110
+ */
111
+ function readFile ( path ) {
90
112
try {
91
- const doc = / j s o n $ / . test ( path ) ?
92
- JSON . parse ( fs . readFileSync ( path , 'utf8' ) ) :
93
- yaml . safeLoad ( fs . readFileSync ( path , 'utf8' ) )
113
+ const doc = / j s o n $ / . test ( path )
114
+ ? JSON . parse ( fs . readFileSync ( path , 'utf8' ) )
115
+ : yaml . safeLoad ( fs . readFileSync ( path , 'utf8' ) )
94
116
return doc
95
117
} catch ( e ) {
96
118
console . error ( 'Error: failed to parse YAML/JSON' )
97
119
return null
98
120
}
99
121
}
100
122
101
-
102
123
/**
103
124
* reads a remote file content using http protocol
104
125
* @param {string } url specifies a valid URL path including the port number
105
126
*/
106
- function getRemoteFileSpec ( uri ) {
127
+ function getRemoteFileSpec ( uri ) {
107
128
return new Promise ( ( resolve , reject ) => {
108
- request ( {
109
- uri,
110
- json : true
111
- } , ( err , res , body ) => {
112
- if ( err ) {
113
- reject ( err )
114
- } else if ( res . statusCode !== 200 ) {
115
- reject ( new Error ( `Error: ${ JSON . stringify ( body ) } ` ) )
116
- } else {
117
- resolve ( body )
129
+ request (
130
+ {
131
+ uri,
132
+ json : true
133
+ } ,
134
+ ( err , res , body ) => {
135
+ if ( err ) {
136
+ reject ( err )
137
+ } else if ( res . statusCode !== 200 ) {
138
+ reject ( new Error ( `Error: ${ JSON . stringify ( body ) } ` ) )
139
+ } else {
140
+ resolve ( body )
141
+ }
118
142
}
119
- } )
143
+ )
120
144
} )
121
145
}
122
146
123
-
124
147
/**
125
148
* generates a GraphQL schema and starts the GraphQL server on the specified port
126
149
* @param {object } oas the OAS specification file
127
150
* @param {number } port the port number to listen on on this server
128
151
*/
129
152
function startGraphQLServer ( oas , port ) {
130
153
// Create GraphQL interface
131
- createGraphQlSchema ( oas , {
154
+ createGraphQlSchema ( oas , {
132
155
strict : program . strict ,
133
156
viewer : program . viewer ,
134
157
fillEmptyResponses : program . fillEmptyResponses ,
135
158
baseUrl : program . url ,
136
159
operationIdFieldNames : program . operationIdFieldNames ,
137
160
provideErrorExtensions : program . extensions
138
161
} )
139
- . then ( ( { schema, report} ) => {
140
- console . log ( JSON . stringify ( report , null , 2 ) )
141
-
142
- // save local file if required
143
- if ( program . save ) {
144
- writeSchema ( schema ) ;
145
- } else {
146
- // Enable CORS
147
- if ( program . cors ) {
148
- app . use ( cors ( ) ) ;
149
- }
162
+ . then ( ( { schema, report } ) => {
163
+ console . log ( JSON . stringify ( report , null , 2 ) )
150
164
151
- // mounting graphql endpoint using the middleware express-graphql
152
- app . use ( '/graphql' , graphqlHTTP ( {
153
- schema : schema ,
154
- graphiql : true
155
- } ) )
156
-
157
- // initiating the server on the port specified by user or the default one
158
- app . listen ( port , ( ) => {
159
- console . log ( `GraphQL accessible at: http://localhost:${ port } /graphql` )
160
- } )
161
- }
162
- } )
163
- . catch ( err => {
164
- console . log ( 'OASGraph creation event error: ' , err . message )
165
- } )
165
+ // save local file if required
166
+ if ( program . save ) {
167
+ writeSchema ( schema )
168
+ } else {
169
+ // Enable CORS
170
+ if ( program . cors ) {
171
+ app . use ( cors ( ) )
172
+ }
173
+
174
+ // mounting graphql endpoint using the middleware express-graphql
175
+ app . use (
176
+ '/graphql' ,
177
+ graphqlHTTP ( {
178
+ schema : schema ,
179
+ graphiql : true
180
+ } )
181
+ )
182
+
183
+ // initiating the server on the port specified by user or the default one
184
+ app . listen ( port , ( ) => {
185
+ console . log ( `GraphQL accessible at: http://localhost:${ port } /graphql` )
186
+ } )
187
+ }
188
+ } )
189
+ . catch ( err => {
190
+ console . log ( 'OASGraph creation event error: ' , err . message )
191
+ } )
166
192
}
167
193
168
-
169
194
/**
170
195
* saves a grahpQL schema generated by OASGraph to a file
171
196
* @param {createGraphQlSchema } schema
172
197
*/
173
- function writeSchema ( schema ) {
174
- fs . writeFile ( program . save , printSchema ( schema ) , ( err ) => {
198
+ function writeSchema ( schema ) {
199
+ fs . writeFile ( program . save , printSchema ( schema ) , err => {
175
200
if ( err ) throw err
176
201
console . log ( `OASGraph successfully saved your schema at ${ program . save } ` )
177
202
} )
178
- }
203
+ }
0 commit comments