This repository was archived by the owner on Dec 10, 2021. It is now read-only.
forked from temando/serverless-openapi-documentation
-
Notifications
You must be signed in to change notification settings - Fork 25
Add file ref support #7
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ee05a6b
Type improvements
Deadarius a341ed3
Remove unused file
Deadarius cc41213
Tidyup dependencies
Deadarius efb0b85
Support for referencing external json schemas
Deadarius 2f50e96
Add example models for external references
Deadarius a9d6181
Fixing definitions that were no longer included
Deadarius 354ff10
Fixing tests
Deadarius 9544c11
Post-rebase fix
Deadarius 0dd338b
Post-rebase fix for lock files
Deadarius 5e0eaca
Lint fixes
Deadarius 8f11f74
1.1.0-alpha
Deadarius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,58 @@ | ||
import chalk from 'chalk'; | ||
import * as fs from 'fs'; | ||
import * as YAML from 'js-yaml'; | ||
import _ = require('lodash'); | ||
import * as Serverless from 'serverless'; | ||
import { inspect } from 'util'; | ||
|
||
import { DefinitionGenerator } from './DefinitionGenerator'; | ||
import { IDefinitionType, ILog } from './types'; | ||
import { merge } from './utils'; | ||
import { Format, IDefinitionConfig, IDefinitionType, ILog } from './types'; | ||
|
||
interface IOptions { | ||
indent: number; | ||
format: Format; | ||
output: string; | ||
} | ||
|
||
interface IProcessedInput { | ||
options: IOptions; | ||
} | ||
|
||
interface ICustomVars { | ||
documentation: IDefinitionConfig; | ||
} | ||
|
||
interface IService { | ||
custom: ICustomVars; | ||
} | ||
|
||
interface IVariables { | ||
service: IService; | ||
} | ||
|
||
interface IFullServerless extends Serverless { | ||
variables: IVariables; | ||
processedInput: IProcessedInput; | ||
} | ||
|
||
export class ServerlessOpenApiDocumentation { | ||
public hooks; | ||
public commands; | ||
/** Serverless Instance */ | ||
private serverless; | ||
/** CLI options */ | ||
// private options; | ||
private serverless: IFullServerless; | ||
|
||
/** Serverless Service Custom vars */ | ||
private customVars; | ||
private customVars: ICustomVars; | ||
|
||
/** | ||
* Constructor | ||
* @param serverless | ||
* @param options | ||
*/ | ||
constructor (serverless, options) { | ||
constructor (serverless: IFullServerless, options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how come There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This project is not configured(yet?) to use |
||
|
||
// pull the serverless instance into our class vars | ||
this.serverless = serverless; | ||
// pull the CLI options into our class vars | ||
// this.options = options; | ||
// Serverless service custom variables | ||
this.customVars = this.serverless.variables.service.custom; | ||
|
||
|
@@ -67,51 +94,20 @@ export class ServerlessOpenApiDocumentation { | |
process.stdout.write(str.join(' ')); | ||
} | ||
|
||
/** | ||
* Processes CLI input by reading the input from serverless | ||
* @returns config IConfigType | ||
*/ | ||
private processCliInput (): IDefinitionType { | ||
const config: IDefinitionType = { | ||
format: 'yaml', | ||
file: 'openapi.yml', | ||
indent: 2, | ||
}; | ||
|
||
config.indent = this.serverless.processedInput.options.indent || 2; | ||
config.format = this.serverless.processedInput.options.format || 'yaml'; | ||
|
||
if (['yaml', 'json'].indexOf(config.format.toLowerCase()) < 0) { | ||
throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"'); | ||
} | ||
|
||
config.file = this.serverless.processedInput.options.output || | ||
((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json'); | ||
|
||
this.log( | ||
`${chalk.bold.green('[OPTIONS]')}`, | ||
`format: "${chalk.bold.red(config.format)}",`, | ||
`output file: "${chalk.bold.red(config.file)}",`, | ||
`indentation: "${chalk.bold.red(String(config.indent))}"\n\n`, | ||
); | ||
|
||
return config; | ||
} | ||
|
||
/** | ||
* Generates OpenAPI Documentation based on serverless configuration and functions | ||
*/ | ||
private generate () { | ||
public async generate () { | ||
this.log(chalk.bold.underline('OpenAPI v3 Documentation Generator\n\n')); | ||
// Instantiate DocumentGenerator | ||
const generator = new DefinitionGenerator(this.customVars.documentation); | ||
const generator = new DefinitionGenerator(this.customVars.documentation, this.serverless.config.servicePath); | ||
|
||
generator.parse(); | ||
await generator.parse(); | ||
|
||
// Map function configurations | ||
const funcConfigs = this.serverless.service.getAllFunctions().map((functionName) => { | ||
const func = this.serverless.service.getFunction(functionName); | ||
return merge({ _functionName: functionName }, func); | ||
return _.merge({ _functionName: functionName }, func); | ||
}); | ||
|
||
// Add Paths to OpenAPI Output from Function Configuration | ||
|
@@ -162,4 +158,35 @@ export class ServerlessOpenApiDocumentation { | |
|
||
this.log(`${chalk.bold.green('[OUTPUT]')} To "${chalk.bold.red(config.file)}"\n`); | ||
} | ||
|
||
/** | ||
* Processes CLI input by reading the input from serverless | ||
* @returns config IConfigType | ||
*/ | ||
private processCliInput (): IDefinitionType { | ||
const config: IDefinitionType = { | ||
format: Format.yaml, | ||
file: 'openapi.yml', | ||
indent: 2, | ||
}; | ||
|
||
config.indent = this.serverless.processedInput.options.indent || 2; | ||
config.format = this.serverless.processedInput.options.format || Format.yaml; | ||
|
||
if ([Format.yaml, Format.json].indexOf(config.format) < 0) { | ||
throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"'); | ||
} | ||
|
||
config.file = this.serverless.processedInput.options.output || | ||
((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json'); | ||
|
||
this.log( | ||
`${chalk.bold.green('[OPTIONS]')}`, | ||
`format: "${chalk.bold.red(config.format)}",`, | ||
`output file: "${chalk.bold.red(config.file)}",`, | ||
`indentation: "${chalk.bold.red(String(config.indent))}"\n\n`, | ||
); | ||
|
||
return config; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this syntax looks weird. how does it work? I've never seen it before
also, we normally use ramda 🙈 But I can see how much it makes sense to just replace the existing utils with lodash here 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an open source project, I would prefer to keep it easy for people to contribute -
lodash
is more aligned with this idea thanramda