Skip to content

Commit f079c6e

Browse files
committed
Separate Node & CLI options in readme
1 parent d4a54c2 commit f079c6e

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

README.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To compare actual generated output, see the [example](./example) folder.
2222

2323
## Usage
2424

25-
#### Basic example (CLI)
25+
### CLI
2626

2727
```bash
2828
npx @manifoldco/swagger-to-ts schema.yaml --namespace OpenAPI --output schema.ts
@@ -37,8 +37,8 @@ the input file.
3737

3838
#### CamelCasing properties
3939

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:
4242

4343
```bash
4444
npx @manifoldco/swagger-to-ts schema.yaml --camelcase --namespace OpenAPI --output schema.ts
@@ -65,6 +65,15 @@ Rinse and repeat for more specs.
6565
For anything more complicated, or for generating specs dynamically, you can
6666
also use the Node API (below).
6767

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+
6877
### Node
6978

7079
```bash
@@ -75,24 +84,29 @@ npm i --save-dev @manifoldco/swagger-to-ts
7584
const { readFileSync } = require('fs');
7685
const swaggerToTS = require('@manifoldco/swagger-to-ts');
7786

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)
8189
```
8290

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` |
96110

97111
[glob]: https://www.npmjs.com/package/glob
98112
[js-yaml]: https://www.npmjs.com/package/js-yaml

0 commit comments

Comments
 (0)