Skip to content

Commit b4ee891

Browse files
committed
Update Node section in readme
1 parent 7ce0a42 commit b4ee891

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ npx @manifoldco/swagger-to-ts schema.yaml --camelcase --namespace OpenAPI --outp
4848
#### Generating multiple schemas
4949

5050
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
5252
something like the following:
5353

5454
```json
@@ -61,8 +61,8 @@ something like the following:
6161

6262
Rinse and repeat for more specs.
6363

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).
6666

6767
### Node
6868

@@ -71,13 +71,18 @@ npm i --save-dev @manifoldco/swagger-to-ts
7171
```
7272

7373
```js
74+
const { readFileSync } = require('fs');
7475
const swaggerToTS = require('@manifoldco/swagger-to-ts');
7576

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);
7780
```
7881

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.
8186

8287
### Options
8388

@@ -88,5 +93,7 @@ the [generate.js](./scripts/generate.js) script.
8893
| `camelcase` | `false` | Convert `snake_case` properties to `camelCase` |
8994
| `swagger` | `2` | Which Swagger version to use. Currently only supports `2`. |
9095

96+
[glob]: https://www.npmjs.com/package/glob
97+
[js-yaml]: https://www.npmjs.com/package/js-yaml
9198
[namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html
9299
[prettier]: https://npmjs.com/prettier

0 commit comments

Comments
 (0)