Skip to content

Optional wrapper #33

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
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 13 additions & 4 deletions src/swagger-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface Swagger2 {

export interface Swagger2Options {
camelcase?: boolean;
wrapper?: string;
wrapper?: string | false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than having this be a union type, maybe we should just have const shouldUseWrapper = options.wrapper !== undefined ? And we can just make this property optional.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry—brainfart. It is nice having declare namespace OpenAPI be the default. I’m fine with this as-is.

}

// Primitives only!
Expand All @@ -47,13 +47,20 @@ function sanitize(name: string): string {
}

function parse(spec: Swagger2, options: Swagger2Options = {}): string {
const wrapper = options.wrapper || 'declare namespace OpenAPI2';
const shouldUseWrapper = options.wrapper !== false;
const wrapper =
typeof options.wrapper === 'string' && options.wrapper
? options.wrapper
: 'declare namespace OpenAPI2';
const shouldCamelCase = options.camelcase || false;

const queue: [string, Swagger2Definition][] = [];

const output: string[] = [];
output.push(`${wrapper} {`);

if (wrapper && shouldUseWrapper) {
output.push(`${wrapper} {`);
}

const { definitions } = spec;

Expand Down Expand Up @@ -197,7 +204,9 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
buildNextInterface();
}

output.push('}'); // Close namespace
if (wrapper && shouldUseWrapper) {
output.push('}'); // Close namespace
}

return prettier.format(output.join('\n'), { parser: 'typescript', singleQuote: true });
}
Expand Down
6 changes: 6 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ describe('swaggerToTS', () => {
const options: Options = { swagger: 1 };
expect(() => swaggerToTS(spec, options)).toThrowError();
});

it('should not render a wrapper when passing false', () => {
const spec = { definitions: {} };
const options: Options = { swagger: 2, wrapper: false };
expect(swaggerToTS(spec, options)).toBe('');
});
});