Skip to content

Add an option to export the namespace #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ also use the Node API (below).
| `--namespace [name]` | `-n` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
| `--swagger [version]` | `-s` | `2` | Which Swagger version to use. Currently only supports `2`. |
| `--camelcase` | `-c` | `false` | Convert `snake_case` properties to `camelCase`? |
| `--export` | `-e` | `false` | Exports the namespace |

### Node

Expand Down Expand Up @@ -107,6 +108,7 @@ in handy.
| `namespace` | `string` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |
| `export` | `boolean` | `false` | Exports the namespace |

[glob]: https://www.npmjs.com/package/glob
[js-yaml]: https://www.npmjs.com/package/js-yaml
Expand Down
6 changes: 6 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Options
--output, -o specify output file
--camelcase, -c convert snake_case properties to camelCase (default: off)
--swagger, -s specify Swagger version (default: 2)
--export, -e exports the namespace (default: false)
`,
{
flags: {
Expand All @@ -39,6 +40,11 @@ Options
type: 'number',
alias: 's',
},
export: {
type: 'boolean',
default: false,
alias: 'e',
},
},
}
);
Expand Down
6 changes: 5 additions & 1 deletion src/swagger-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Swagger2 {
export interface Swagger2Options {
camelcase?: boolean;
namespace?: string;
export?: boolean;
}

// Primitives only!
Expand All @@ -43,9 +44,12 @@ function camelCase(name: string): string {
function parse(spec: Swagger2, options: Swagger2Options = {}): string {
const namespace = options.namespace || 'OpenAPI2';
const shouldCamelCase = options.camelcase || false;
const shouldExport = options.export || false;

const queue: [string, Swagger2Definition][] = [];
const output: string[] = [`namespace ${namespace} {`];

const output: string[] = shouldExport ? ['export '] : [];
output.push(`namespace ${namespace} {`);

const { definitions } = spec;

Expand Down