Skip to content

Commit

Permalink
Merge pull request #34 from JaredCE/create-tests-for-postman-generation
Browse files Browse the repository at this point in the history
Create tests for postman generation
  • Loading branch information
JaredCE authored Jul 28, 2022
2 parents 4554b6f + 9e80797 commit acaff29
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-openapi-documenter",
"version": "0.0.18",
"version": "0.0.19",
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
"main": "index.js",
"keywords": [
Expand Down
61 changes: 32 additions & 29 deletions src/openAPIGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class OpenAPIGenerator {

async generate() {
this.log(this.defaultLog, chalk.bold.underline('OpenAPI v3 Document Generation'))
const config = this.processCliInput()
this.processCliInput()
const generator = new DefinitionGenerator(this.serverless);

await generator.parse()
Expand All @@ -124,49 +124,53 @@ class OpenAPIGenerator {
if (valid)
this.log('success', 'OpenAPI v3 Documentation Successfully Generated')

if (config.postmanCollection) {
const postmanGeneration = (err, result) => {
if (err) {
this.log('error', `ERROR: An error was thrown when generating the postman collection`)
throw new this.serverless.classes.Error(err)
}

this.log('success', 'postman collection v2 Documentation Successfully Generated')
try {
fs.writeFileSync(config.postmanCollection, JSON.stringify(result.output[0].data))
this.log('success', 'postman collection v2 Documentation Successfully Written')
} catch (err) {
this.log('error', `ERROR: An error was thrown whilst writing the postman collection`)
throw new this.serverless.classes.Error(err)
}
}

const postmanCollection = PostmanGenerator.convert(
{type: 'json', data: JSON.parse(JSON.stringify(generator.openAPI))},
{},
postmanGeneration
)
if (this.config.postmanCollection) {
this.createPostman(generator.openAPI)
}

let output
switch (config.format.toLowerCase()) {
switch (this.config.format.toLowerCase()) {
case 'json':
output = JSON.stringify(generator.openAPI, null, config.indent);
output = JSON.stringify(generator.openAPI, null, this.config.indent);
break;
case 'yaml':
default:
output = yaml.dump(generator.openAPI, { indent: config.indent });
output = yaml.dump(generator.openAPI, { indent: this.config.indent });
break;
}
try {
fs.writeFileSync(config.file, output);
fs.writeFileSync(this.config.file, output);
this.log('success', 'OpenAPI v3 Documentation Successfully Written')
} catch (err) {
this.log('error', `ERROR: An error was thrown whilst writing the openAPI Documentation`)
throw new this.serverless.classes.Error(err)
}
}

createPostman(openAPI) {
const postmanGeneration = (err, result) => {
if (err) {
this.log('error', `ERROR: An error was thrown when generating the postman collection`)
throw new this.serverless.classes.Error(err)
}

this.log('success', 'postman collection v2 Documentation Successfully Generated')
try {
fs.writeFileSync(this.config.postmanCollection, JSON.stringify(result.output[0].data))
this.log('success', 'postman collection v2 Documentation Successfully Written')
} catch (err) {
this.log('error', `ERROR: An error was thrown whilst writing the postman collection`)
throw new this.serverless.classes.Error(err)
}
}

PostmanGenerator.convert(
{type: 'json', data: JSON.parse(JSON.stringify(openAPI))},
{},
postmanGeneration
)
}

processCliInput () {
const config = {
format: 'json',
Expand All @@ -182,7 +186,6 @@ class OpenAPIGenerator {
config.postmanCollection = this.serverless.processedInput.options.postmanCollection || null

if (['yaml', 'json'].indexOf(config.format.toLowerCase()) < 0) {
// throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"');
throw new this.serverless.classes.Error('Invalid Output Format Specified - must be one of "yaml" or "json"')
}

Expand All @@ -199,7 +202,7 @@ class OpenAPIGenerator {
${config.postmanCollection ? `postman collection: ${chalk.bold.green(config.postmanCollection)}`: `\n\n`}`
)

return config
this.config = config
}

validateDetails(validation) {
Expand Down
Loading

0 comments on commit acaff29

Please sign in to comment.