@@ -30,10 +30,9 @@ npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace OpenAPI"
30
30
# 🚀 schema.yaml -> schema.d.ts [2ms]
31
31
```
32
32
33
- This will save a ` schema.ts ` file in the current folder under the TypeScript
34
- [ namespace] [ namespace ] ` OpenAPI ` (namespaces are required because chances of
35
- collision among specs is highly likely). The CLI can accept YAML or JSON for
36
- the input file.
33
+ This will save a ` schema.ts ` file in the current folder under the TypeScript [ namespace] [ namespace ]
34
+ ` OpenAPI ` (namespaces are required because chances of collision among specs is highly likely). The
35
+ CLI can accept YAML or JSON for the input file.
37
36
38
37
#### Wrapper option
39
38
@@ -46,8 +45,7 @@ declare namespace MyNamespace {}
46
45
declare module MyModule {}
47
46
```
48
47
49
- The ` --wrapper ` flag lets you specify any of the above with a string (omit
50
- the ` {} ` ):
48
+ The ` --wrapper ` flag lets you specify any of the above with a string (omit the ` {} ` ):
51
49
52
50
``` bash
53
51
npx @manifoldco/swagger-to-ts schema.yaml --wrapper " namespace API"
@@ -56,24 +54,24 @@ npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace API"
56
54
npx @manifoldco/swagger-to-ts schema.yaml --wrapper " declare module '@api'"
57
55
```
58
56
59
- By default, wrapper is ` declare namespace OpenAPI2 ` . You can skip exposing types via a wrapper by adding the ` --nowrapper ` flag:
57
+ By default, wrapper is ` declare namespace OpenAPI2 ` . You can skip exposing types via a wrapper by
58
+ adding the ` --nowrapper ` flag:
60
59
61
60
``` bash
62
61
npx @manifoldco/swagger-to-ts schema.yaml --nowrapper
63
62
```
64
63
65
- As mentioned before, this uses [ Prettier] [ prettier ] to clean up output, so
66
- extra spaces are generally OK here. Prettier also will err on cleanup if you
67
- specify invalid TypeScript, letting you know on generation if anything went
68
- wrong.
64
+ As mentioned before, this uses [ Prettier] [ prettier ] to clean up output, so extra spaces are
65
+ generally OK here. Prettier also will err on cleanup if you specify invalid TypeScript, letting you
66
+ know on generation if anything went wrong.
69
67
70
- _ Note: previous versions of the CLI tool used ` --namespace ` and ` --export ` .
71
- These have both been deprecated in favor of ` --wrapper ` ._
68
+ _ Note: previous versions of the CLI tool used ` --namespace ` and ` --export ` . These have both been
69
+ deprecated in favor of ` --wrapper ` ._
72
70
73
71
#### CamelCasing properties
74
72
75
- Within interfaces, you may want to convert ` snake_case ` properties to
76
- ` camelCase ` by adding the ` --camelcase ` flag:
73
+ Within interfaces, you may want to convert ` snake_case ` properties to ` camelCase ` by adding the
74
+ ` --camelcase ` flag:
77
75
78
76
``` bash
79
77
npx @manifoldco/swagger-to-ts schema.yaml --camelcase --wrapper " declare namespace OpenAPI" --output schema.d.ts
@@ -83,9 +81,8 @@ npx @manifoldco/swagger-to-ts schema.yaml --camelcase --wrapper "declare namespa
83
81
84
82
#### Generating multiple schemas
85
83
86
- Say you have multiple schemas you need to parse. I’ve found the simplest way
87
- to do that is to use npm scripts. In your ` package.json ` , you can do
88
- something like the following:
84
+ Say you have multiple schemas you need to parse. I’ve found the simplest way to do that is to use
85
+ npm scripts. In your ` package.json ` , you can do something like the following:
89
86
90
87
``` json
91
88
"scripts" : {
@@ -97,18 +94,19 @@ something like the following:
97
94
98
95
Rinse and repeat for more specs.
99
96
100
- For anything more complicated, or for generating specs dynamically, you can
101
- also use the Node API (below).
97
+ For anything more complicated, or for generating specs dynamically, you can also use the Node API
98
+ (below).
102
99
103
100
#### CLI Options
104
101
105
- | Option | Alias | Default | Description |
106
- | :-------------------- | :---- | :--------------------------: | :--------------------------------------------------------- |
107
- | ` --wrapper ` | ` -w ` | ` declare namespace OpenAPI2 ` | How should this export the types? |
108
- | ` --output [location] ` | ` -o ` | (stdout) | Where should the output file be saved? |
109
- | ` --swagger [version] ` | ` -s ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
110
- | ` --camelcase ` | ` -c ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` ? |
111
- | ` --nowrapper ` | ` -nw ` | ` false ` | Disables rendering a wrapper |
102
+ | Option | Alias | Default | Description |
103
+ | :-------------------- | :---- | :--------------------------: | :----------------------------------------------------------------------- |
104
+ | ` --wrapper ` | ` -w ` | ` declare namespace OpenAPI2 ` | How should this export the types? |
105
+ | ` --output [location] ` | ` -o ` | (stdout) | Where should the output file be saved? |
106
+ | ` --swagger [version] ` | ` -s ` | ` 2 ` | specify Swagger version (currently only supports ` 2 ` ) |
107
+ | ` --camelcase ` | ` -c ` | ` false ` | convert ` snake_case ` properties to ` camelCase ` |
108
+ | ` --injectWarning ` | ` -iw ` | ` false ` | injects an “autogenerated file” warning at the top of the generated file |
109
+ | ` --nowrapper ` | ` -nw ` | ` false ` | disables rendering a wrapper |
112
110
113
111
### Node
114
112
@@ -124,14 +122,12 @@ const input = JSON.parse(readFileSync('spec.json', 'utf8')); // Input can be any
124
122
const output = swaggerToTS (input, { wrapper: ' declare namespace MyAPI' }); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
125
123
```
126
124
127
- The Node API is a bit more flexible: it will only take a JS object as input
128
- (OpenAPI format), and return a string of TS definitions. This lets you pull
129
- from any source (a Swagger server, local files, etc.), and similarly lets you
130
- parse, post-process, and save the output anywhere.
125
+ The Node API is a bit more flexible: it will only take a JS object as input (OpenAPI format), and
126
+ return a string of TS definitions. This lets you pull from any source (a Swagger server, local
127
+ files, etc.), and similarly lets you parse, post-process, and save the output anywhere.
131
128
132
- If your specs are in YAML, you’ll have to convert them to JS objects using a
133
- library such as [ js-yaml] [ js-yaml ] . If you’re batching large folders of
134
- specs, [ glob] [ glob ] may also come in handy.
129
+ If your specs are in YAML, you’ll have to convert them to JS objects using a library such as
130
+ [ js-yaml] [ js-yaml ] . If you’re batching large folders of specs, [ glob] [ glob ] may also come in handy.
135
131
136
132
#### Node Options
137
133
0 commit comments