Skip to content

Commit 2dc048d

Browse files
authored
Add wrapper option (#14)
* Add wrapper option * Fix namespace error
1 parent 924081d commit 2dc048d

File tree

8 files changed

+810
-778
lines changed

8 files changed

+810
-778
lines changed

README.md

+43-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To compare actual generated output, see the [example](./example) folder.
2525
### CLI
2626

2727
```bash
28-
npx @manifoldco/swagger-to-ts schema.yaml --namespace OpenAPI --output schema.ts
28+
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace OpenAPI" --output schema.d.ts
2929

3030
# 🚀 schema.yaml -> schema.ts [2ms]
3131
```
@@ -35,13 +35,42 @@ This will save a `schema.ts` file in the current folder under the TypeScript
3535
collision among specs is highly likely). The CLI can accept YAML or JSON for
3636
the input file.
3737

38+
#### Wrapper option
39+
40+
There are many different ways to expose types in TypeScript. To name a few:
41+
42+
```ts
43+
namespace MyNamespace {}
44+
export namespace MyNamespace {}
45+
declare namespace MyNamespace {}
46+
declare module MyModule {}
47+
```
48+
49+
The `--wrapper` flag lets you specify any of the above with a string (omit
50+
the `{}`):
51+
52+
```bash
53+
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "namespace API"
54+
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace API"
55+
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace API"
56+
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare module '@api'"
57+
```
58+
59+
As mentioned before, this uses [Prettier][prettier] to clean up output, so
60+
extra spaces are generally OK here. Prettier also will err on cleanup if you
61+
specify invalid TypeScript, letting you know on generation if anything went
62+
wrong.
63+
64+
_Note: previous versions of the CLI tool used `--namespace` and `--export`.
65+
These have both been deprecated in favor of `--wrapper`._
66+
3867
#### CamelCasing properties
3968

4069
Within interfaces, you may want to convert `snake_case` properties to
4170
`camelCase` by adding the `--camelcase` flag:
4271

4372
```bash
44-
npx @manifoldco/swagger-to-ts schema.yaml --camelcase --namespace OpenAPI --output schema.ts
73+
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace OpenAPI" --output schema.d.ts
4574

4675
# 🚀 schema.yaml -> schema.ts [2ms]
4776
```
@@ -67,13 +96,12 @@ also use the Node API (below).
6796

6897
#### CLI Options
6998

70-
| Option | Alias | Default | Description |
71-
| :-------------------- | :---- | :--------: | :--------------------------------------------------------------------------------------------------- |
72-
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
73-
| `--namespace [name]` | `-n` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
74-
| `--swagger [version]` | `-s` | `2` | Which Swagger version to use. Currently only supports `2`. |
75-
| `--camelcase` | `-c` | `false` | Convert `snake_case` properties to `camelCase`? |
76-
| `--export` | `-e` | `false` | Exports the namespace |
99+
| Option | Alias | Default | Description |
100+
| :-------------------- | :---- | :--------------------------: | :--------------------------------------------------------- |
101+
| `--wrapper` | `-w` | `declare namespace OpenAPI2` | How should this export the types? |
102+
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
103+
| `--swagger [version]` | `-s` | `2` | Which Swagger version to use. Currently only supports `2`. |
104+
| `--camelcase` | `-c` | `false` | Convert `snake_case` properties to `camelCase`? |
77105

78106
### Node
79107

@@ -86,7 +114,7 @@ const { readFileSync } = require('fs');
86114
const swaggerToTS = require('@manifoldco/swagger-to-ts');
87115

88116
const input = JSON.parse(readFileSync('spec.json', 'utf8')); // Input be any JS object (OpenAPI format)
89-
const output = swaggerToTS(input, { namespace: 'MySpec' }); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
117+
const output = swaggerToTS(input, { wrapper: 'declare namespace MyAPI' }); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
90118
```
91119

92120
The Node API is a bit more flexible: it will only take a JS object as input
@@ -103,12 +131,11 @@ in handy.
103131

104132
#### Node Options
105133

106-
| Name | Type | Default | Description |
107-
| :---------- | :-------: | :--------: | :--------------------------------------------------------------------------------------------------- |
108-
| `namespace` | `string` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
109-
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
110-
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |
111-
| `export` | `boolean` | `false` | Exports the namespace |
134+
| Name | Type | Default | Description |
135+
| :---------- | :-------: | :--------------------------: | :--------------------------------------------------------- |
136+
| `--wrapper` | `string` | `declare namespace OpenAPI2` | How should this export the types? |
137+
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
138+
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |
112139

113140
[glob]: https://www.npmjs.com/package/glob
114141
[js-yaml]: https://www.npmjs.com/package/js-yaml

bin/cli.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ Usage
1414
$ swagger-to-ts [input] [options]
1515
1616
Options
17-
--namespace, -n specify namespace to prefix types
1817
--help display this
18+
--wrapper, -w specify wrapper (default: "declare namespace OpenAPI2")
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-
--export, -e exports the namespace (default: false)
2322
`,
2423
{
2524
flags: {
@@ -28,9 +27,10 @@ Options
2827
default: false,
2928
alias: 'c',
3029
},
31-
namespace: {
30+
wrapper: {
3231
type: 'string',
33-
alias: 'n',
32+
default: 'declare namespace OpenAPI2',
33+
alias: 'w',
3434
},
3535
output: {
3636
type: 'string',
@@ -40,9 +40,12 @@ Options
4040
type: 'number',
4141
alias: 's',
4242
},
43+
namespace: {
44+
type: 'string',
45+
alias: 'n',
46+
},
4347
export: {
4448
type: 'boolean',
45-
default: false,
4649
alias: 'e',
4750
},
4851
},
@@ -51,6 +54,16 @@ Options
5154

5255
let spec = cli.input[0];
5356

57+
if (typeof cli.flags.namespace === 'string' && cli.flags.namespace.length > 0) {
58+
console.error(chalk.red('--namespace option is deprecated. Please use --wrapper instead.'));
59+
return;
60+
}
61+
62+
if (cli.flags.export === true) {
63+
console.error(chalk.red('--export option is deprecated. Please use --wrapper instead.'));
64+
return;
65+
}
66+
5467
// If input is a file, load it
5568
const pathname = resolve(process.cwd(), spec);
5669
if (existsSync(pathname)) {

example/output.ts renamed to example/output.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace OpenAPI2 {
1+
declare namespace OpenAPI2 {
22
export interface ValueProp {
33
// Heading of a value proposition.
44
header: string;

0 commit comments

Comments
 (0)