@@ -48,7 +48,7 @@ npx @manifoldco/swagger-to-ts schema.yaml --camelcase --namespace OpenAPI --outp
48
48
#### Generating multiple schemas
49
49
50
50
Say you have multiple schemas you need to parse. I’ve found the simplest way
51
- to do that is just to use npm scripts. In your ` package.json ` , you can do
51
+ to do that is to use npm scripts. In your ` package.json ` , you can do
52
52
something like the following:
53
53
54
54
``` json
@@ -61,8 +61,8 @@ something like the following:
61
61
62
62
Rinse and repeat for more specs.
63
63
64
- For anything more complicated or for generating specs more dynamically, you
65
- can also use the Node API (below).
64
+ For anything more complicated, or for generating specs dynamically, you can
65
+ also use the Node API (below).
66
66
67
67
### Node
68
68
@@ -71,13 +71,18 @@ npm i --save-dev @manifoldco/swagger-to-ts
71
71
```
72
72
73
73
``` js
74
+ const { readFileSync } = require (' fs' );
74
75
const swaggerToTS = require (' @manifoldco/swagger-to-ts' );
75
76
76
- swaggerToTS (spec, [options]);
77
+ const spec = JSON .parse (readFileSync (' spec.json' , ' utf8' )); // Can be any JS object, so long as it’s in OpenAPI format
78
+ const options = { output: ' types.ts' }; // Optional
79
+ swaggerToTS (spec, options);
77
80
```
78
81
79
- ` spec ` must be in JSON format. For an example of converting YAML to JSON, see
80
- the [ generate.js] ( ./scripts/generate.js ) script.
82
+ Although the CLI can handle YAML and JSON, the Node API only understands JS
83
+ objects. A library such as [ js-yaml] [ js-yaml ] makes it trivial to convert
84
+ YAML to JS. If you’re batching large folders of specs, [ glob] [ glob ] may also
85
+ come in handy.
81
86
82
87
### Options
83
88
@@ -88,5 +93,7 @@ the [generate.js](./scripts/generate.js) script.
88
93
| ` camelcase ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` |
89
94
| ` swagger ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
90
95
96
+ [ glob ] : https://www.npmjs.com/package/glob
97
+ [ js-yaml ] : https://www.npmjs.com/package/js-yaml
91
98
[ namespace ] : https://www.typescriptlang.org/docs/handbook/namespaces.html
92
99
[ prettier ] : https://npmjs.com/prettier
0 commit comments