Skip to content

Commit e962f61

Browse files
scvnathanDangoDev
authored andcommitted
Optional wrapper (#33)
* made wrapper optional by passing false * update test name * change wrapper option to boolean if false * added nowrapper option; docs
1 parent 1ac34a8 commit e962f61

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace API"
5656
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare module '@api'"
5757
```
5858

59+
By default, wrapper is `declare namespace OpenAPI2`. You can skip exposing types via a wrapper by adding the `--nowrapper` flag:
60+
61+
```bash
62+
npx @manifoldco/swagger-to-ts schema.yaml --nowrapper
63+
```
64+
5965
As mentioned before, this uses [Prettier][prettier] to clean up output, so
6066
extra spaces are generally OK here. Prettier also will err on cleanup if you
6167
specify invalid TypeScript, letting you know on generation if anything went
@@ -102,6 +108,7 @@ also use the Node API (below).
102108
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
103109
| `--swagger [version]` | `-s` | `2` | Which Swagger version to use. Currently only supports `2`. |
104110
| `--camelcase` | `-c` | `false` | Convert `snake_case` properties to `camelCase`? |
111+
| `--nowrapper` | `-nw` | `false` | Disables rendering a wrapper |
105112

106113
### Node
107114

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

129136
#### Node Options
130137

131-
| Name | Type | Default | Description |
132-
| :---------- | :-------: | :--------------------------: | :--------------------------------------------------------- |
133-
| `wrapper` | `string` | `declare namespace OpenAPI2` | How should this export the types? |
134-
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
135-
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |
138+
| Name | Type | Default | Description |
139+
| :---------- | :---------------: | :--------------------------: | :-------------------------------------------------------------------------- |
140+
| `wrapper` | `string \| false` | `declare namespace OpenAPI2` | How should this export the types? Pass false to disable rendering a wrapper |
141+
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
142+
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |
136143

137144
[glob]: https://www.npmjs.com/package/glob
138145
[js-yaml]: https://www.npmjs.com/package/js-yaml

bin/cli.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Options
1919
--output, -o specify output file
2020
--camelcase, -c convert snake_case properties to camelCase (default: off)
2121
--swagger, -s specify Swagger version (default: 2)
22+
--nowrapper -nw disables rendering the wrapper
2223
`,
2324
{
2425
flags: {
@@ -48,6 +49,10 @@ Options
4849
type: 'boolean',
4950
alias: 'e',
5051
},
52+
nowrapper: {
53+
type: 'boolean',
54+
alias: 'nw',
55+
},
5156
},
5257
}
5358
);
@@ -94,6 +99,10 @@ try {
9499
);
95100
}
96101

102+
if (cli.flags.nowrapper) {
103+
cli.flags.wrapper = false;
104+
}
105+
97106
const result = swaggerToTS(spec, cli.flags);
98107

99108
// Write to file if specifying output

src/swagger-2.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface Swagger2 {
2222

2323
export interface Swagger2Options {
2424
camelcase?: boolean;
25-
wrapper?: string;
25+
wrapper?: string | false;
2626
}
2727

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

4949
function parse(spec: Swagger2, options: Swagger2Options = {}): string {
50-
const wrapper = options.wrapper || 'declare namespace OpenAPI2';
50+
const shouldUseWrapper = options.wrapper !== false;
51+
const wrapper =
52+
typeof options.wrapper === 'string' && options.wrapper
53+
? options.wrapper
54+
: 'declare namespace OpenAPI2';
5155
const shouldCamelCase = options.camelcase || false;
5256

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

5559
const output: string[] = [];
56-
output.push(`${wrapper} {`);
60+
61+
if (wrapper && shouldUseWrapper) {
62+
output.push(`${wrapper} {`);
63+
}
5764

5865
const { definitions } = spec;
5966

@@ -197,7 +204,9 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
197204
buildNextInterface();
198205
}
199206

200-
output.push('}'); // Close namespace
207+
if (wrapper && shouldUseWrapper) {
208+
output.push('}'); // Close namespace
209+
}
201210

202211
return prettier.format(output.join('\n'), { parser: 'typescript', singleQuote: true });
203212
}

tests/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ describe('swaggerToTS', () => {
1313
const options: Options = { swagger: 1 };
1414
expect(() => swaggerToTS(spec, options)).toThrowError();
1515
});
16+
17+
it('should not render a wrapper when passing false', () => {
18+
const spec = { definitions: {} };
19+
const options: Options = { swagger: 2, wrapper: false };
20+
expect(swaggerToTS(spec, options)).toBe('');
21+
});
1622
});

0 commit comments

Comments
 (0)