Skip to content

Commit 12fd011

Browse files
Anthony GuimardErikWittern
authored andcommitted
Add cli option to enable CORS
Update oasgraph-cli package in order to add ability to enable cors if needed by adding `--cors` to the cli. Signed-off-by: Anthony Guimard <[email protected]>
1 parent 9c2c949 commit 12fd011

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

packages/oasgraph-cli/lib/oasgraph.js

100644100755
Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph-cli/lib/oasgraph.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
"dependencies": {
4545
"commander": "^2.19.0",
46+
"cors": "^2.8.5",
4647
"express": "^4.16.4",
4748
"express-graphql": "^0.7.1",
4849
"graphql": "^14.0.2",

packages/oasgraph-cli/src/oasgraph.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as express from 'express'
44
import * as graphqlHTTP from 'express-graphql'
5+
import * as cors from 'cors'
56
import { createGraphQlSchema } from 'oasgraph'
67
import * as path from 'path'
78
import * as request from 'request'
@@ -25,6 +26,7 @@ program
2526
.option('-s, --strict', 'throw an error if OASGraph cannot run without compensating for errors or missing data in the OAS')
2627
.option('-a, --addSubOperations', 'nest operations based on path hierarchy')
2728
.option('-f, --fillEmptyResponses', 'create placeholder schemas for operations with HTTP status code 204 (no response) rather than ignore them')
29+
.option('--cors', 'enable Cross-origin resource sharing (CORS)')
2830
.option('--no-viewer', 'do not create GraphQL viewer objects for passing authentication credentials')
2931
.option('--save <file path>', 'save schema to path and do not start server')
3032
.action(function (path) {
@@ -42,16 +44,16 @@ if (program.port) {
4244
portNumber = program.port
4345
}
4446

45-
// check if the file exists
47+
// check if the file exists
4648
if (fs.existsSync(path.resolve(filePath))) {
4749
try {
4850
let oas = readFile(path.resolve(filePath))
4951
startGraphQLServer(oas, portNumber)
5052
} catch (e) {
5153
console.error(e)
5254
}
53-
54-
} else {
55+
56+
} else {
5557
// falls back to a remote location
5658
if (filePath.match(/^https?/g)) {
5759
getRemoteFileSpec(filePath).then(remoteContent=> {
@@ -105,7 +107,7 @@ function getRemoteFileSpec (uri) {
105107
}
106108

107109
/**
108-
* generates a GraphQL schema and starts the GraphQL server on the specified port
110+
* generates a GraphQL schema and starts the GraphQL server on the specified port
109111
* @param {Object} oas the OAS specification file
110112
* @param {number} port the port number to listen on on this server
111113
*/
@@ -117,14 +119,19 @@ function startGraphQLServer(oas, port) {
117119
addSubOperations: program.addSubOperations,
118120
fillEmptyResponses: program.fillEmptyResponses,
119121
baseUrl: program.url
120-
})
122+
})
121123
.then(({schema, report}) => {
122124
console.log(JSON.stringify(report, null, 2))
123125

124-
// save local file if required
126+
// save local file if required
125127
if (program.save) {
126128
writeSchema(schema);
127129
} else {
130+
// Enable CORS
131+
if (program.cors) {
132+
app.use(cors());
133+
}
134+
128135
// mounting graphql endpoint using the middleware express-graphql
129136
app.use('/graphql', graphqlHTTP({
130137
schema: schema,
@@ -144,7 +151,7 @@ function startGraphQLServer(oas, port) {
144151

145152
/**
146153
* saves a grahpQL schema generated by OASGraph to a file
147-
* @param {createGraphQlSchema} schema
154+
* @param {createGraphQlSchema} schema
148155
*/
149156
function writeSchema(schema){
150157
fs.writeFile(program.save, printSchema(schema), (err) => {

0 commit comments

Comments
 (0)