@@ -22,7 +22,7 @@ To compare actual generated output, see the [example](./example) folder.
22
22
23
23
## Usage
24
24
25
- #### Basic example ( CLI)
25
+ ### CLI
26
26
27
27
``` bash
28
28
npx @manifoldco/swagger-to-ts schema.yaml --namespace OpenAPI --output schema.ts
@@ -37,8 +37,8 @@ the input file.
37
37
38
38
#### CamelCasing properties
39
39
40
- You can also convert ` snake_case ` keys to ` camelCase ` by adding the
41
- ` --camelcase ` flag:
40
+ Within interfaces, you may want to convert ` snake_case ` properties to
41
+ ` camelCase ` by adding the ` --camelcase ` flag:
42
42
43
43
``` bash
44
44
npx @manifoldco/swagger-to-ts schema.yaml --camelcase --namespace OpenAPI --output schema.ts
@@ -65,6 +65,15 @@ Rinse and repeat for more specs.
65
65
For anything more complicated, or for generating specs dynamically, you can
66
66
also use the Node API (below).
67
67
68
+ #### CLI Options
69
+
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
+
68
77
### Node
69
78
70
79
``` bash
@@ -75,24 +84,29 @@ npm i --save-dev @manifoldco/swagger-to-ts
75
84
const { readFileSync } = require (' fs' );
76
85
const swaggerToTS = require (' @manifoldco/swagger-to-ts' );
77
86
78
- const spec = JSON .parse (readFileSync (' spec.json' , ' utf8' )); // Can be any JS object, so long as it’s in OpenAPI format
79
- const options = { output: ' types.ts' }; // Optional
80
- swaggerToTS (spec, options);
87
+ const input = JSON .parse (readFileSync (' spec.json' , ' utf8' )); // Input be any JS object (OpenAPI format)
88
+ const output = swaggerToTS (input, { namespace: ' MySpec' }); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
81
89
```
82
90
83
- Although the CLI can handle YAML and JSON, the Node API only understands JS
84
- objects. A library such as [ js-yaml] [ js-yaml ] makes it trivial to convert
85
- YAML to JS. If you’re batching large folders of specs, [ glob] [ glob ] may also
86
- come in handy.
87
-
88
- ### Options
89
-
90
- | Name | Default | Description |
91
- | :---------- | :--------- | :--------------------------------------------------------------------------------------------------- |
92
- | ` output ` | (stdout) | Where should the output file be saved? |
93
- | ` namespace ` | ` OpenAPI2 ` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
94
- | ` camelcase ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` |
95
- | ` swagger ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
91
+ The Node API is a bit more flexible: it will only take a JS object as input
92
+ (OpenAPI format), and return a string of TS definitions. This lets you pull
93
+ from any source (a Swagger server, local files, etc.), and similarly lets put
94
+ the output anywhere. It even allows for some post-processing in-between if
95
+ desired.
96
+
97
+ If you are working with local files, you’ll have to read/write files
98
+ yourself. Also, if your specs are in YAML, you’ll have to convert them to JS
99
+ objects. A library such as [ js-yaml] [ js-yaml ] does make this trivial, though!
100
+ Lastly, if you’re batching large folders of specs, [ glob] [ glob ] may also come
101
+ in handy.
102
+
103
+ #### Node Options
104
+
105
+ | Name | Type | Default | Description |
106
+ | :---------- | :-------: | :--------: | :--------------------------------------------------------------------------------------------------- |
107
+ | ` namespace ` | ` string ` | ` OpenAPI2 ` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
108
+ | ` swagger ` | ` number ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
109
+ | ` camelcase ` | ` boolean ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` |
96
110
97
111
[ glob ] : https://www.npmjs.com/package/glob
98
112
[ js-yaml ] : https://www.npmjs.com/package/js-yaml
0 commit comments