@@ -25,7 +25,7 @@ To compare actual generated output, see the [example](./example) folder.
25
25
### CLI
26
26
27
27
``` 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
29
29
30
30
# 🚀 schema.yaml -> schema.ts [2ms]
31
31
```
@@ -35,13 +35,42 @@ This will save a `schema.ts` file in the current folder under the TypeScript
35
35
collision among specs is highly likely). The CLI can accept YAML or JSON for
36
36
the input file.
37
37
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
+
38
67
#### CamelCasing properties
39
68
40
69
Within interfaces, you may want to convert ` snake_case ` properties to
41
70
` camelCase ` by adding the ` --camelcase ` flag:
42
71
43
72
``` 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
45
74
46
75
# 🚀 schema.yaml -> schema.ts [2ms]
47
76
```
@@ -67,13 +96,12 @@ also use the Node API (below).
67
96
68
97
#### CLI Options
69
98
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 ` ? |
77
105
78
106
### Node
79
107
@@ -86,7 +114,7 @@ const { readFileSync } = require('fs');
86
114
const swaggerToTS = require (' @manifoldco/swagger-to-ts' );
87
115
88
116
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)
90
118
```
91
119
92
120
The Node API is a bit more flexible: it will only take a JS object as input
@@ -103,12 +131,11 @@ in handy.
103
131
104
132
#### Node Options
105
133
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 ` |
112
139
113
140
[ glob ] : https://www.npmjs.com/package/glob
114
141
[ js-yaml ] : https://www.npmjs.com/package/js-yaml
0 commit comments