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 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
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace API"
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare module '@api'"
```

By default, wrapper is `declare namespace OpenAPI2`. You can skip exposing types via a wrapper by adding the `--nowrapper` flag:

```bash
npx @manifoldco/swagger-to-ts schema.yaml --nowrapper
```

As mentioned before, this uses [Prettier][prettier] to clean up output, so
extra spaces are generally OK here. Prettier also will err on cleanup if you
specify invalid TypeScript, letting you know on generation if anything went
Expand Down Expand Up @@ -102,6 +108,7 @@ also use the Node API (below).
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
| `--swagger [version]` | `-s` | `2` | Which Swagger version to use. Currently only supports `2`. |
| `--camelcase` | `-c` | `false` | Convert `snake_case` properties to `camelCase`? |
| `--nowrapper` | `-nw` | `false` | Disables rendering a wrapper |

### Node

Expand All @@ -128,11 +135,11 @@ specs, [glob][glob] may also come in handy.

#### Node Options

| Name | Type | Default | Description |
| :---------- | :-------: | :--------------------------: | :--------------------------------------------------------- |
| `wrapper` | `string` | `declare namespace OpenAPI2` | How should this export the types? |
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |
| Name | Type | Default | Description |
| :---------- | :---------------: | :--------------------------: | :-------------------------------------------------------------------------- |
| `wrapper` | `string \| false` | `declare namespace OpenAPI2` | How should this export the types? Pass false to disable rendering a wrapper |
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |

[glob]: https://www.npmjs.com/package/glob
[js-yaml]: https://www.npmjs.com/package/js-yaml
Expand Down
9 changes: 9 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)
--nowrapper -nw disables rendering the wrapper
`,
{
flags: {
Expand Down Expand Up @@ -48,6 +49,10 @@ Options
type: 'boolean',
alias: 'e',
},
nowrapper: {
type: 'boolean',
alias: 'nw',
},
},
}
);
Expand Down Expand Up @@ -94,6 +99,10 @@ try {
);
}

if (cli.flags.nowrapper) {
cli.flags.wrapper = false;
}

const result = swaggerToTS(spec, cli.flags);

// Write to file if specifying output
Expand Down
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('');
});
});