@@ -45,7 +45,7 @@ npx openapi-typescript "specs/**/*.yaml" --output schemas/
45
45
46
46
_ Note: if generating a single schema, ` --output ` must be a file (preferably ` *.ts ` ). If using globs, ` --output ` must be a directory._
47
47
48
- _ Thanks to [ @sharmarajdaksh ] ( https://github.com/sharmarajdaksh ) for the glob feature !_
48
+ _ Thanks, [ @sharmarajdaksh ] ( https://github.com/sharmarajdaksh ) !_
49
49
50
50
#### ☁️ Reading specs from remote resource
51
51
@@ -56,7 +56,7 @@ npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml --output
56
56
# 🚀 https://petstore3.swagger.io/api/v3/openapi.yaml -> petstore.d.ts [650ms]
57
57
```
58
58
59
- _ Thanks to [ @psmyrdek ] ( https://github.com/psmyrdek ) for the remote spec feature !_
59
+ _ Thanks, [ @psmyrdek ] ( https://github.com/psmyrdek ) !_
60
60
61
61
#### Using in TypeScript
62
62
@@ -80,7 +80,7 @@ import { operations } from "./generated-schema.ts";
80
80
type getUsersById = operations [" getUsersById" ];
81
81
```
82
82
83
- _ Thanks to [ @gr2m ] ( https://github.com/gr2m ) for the operations feature !_
83
+ _ Thanks, [ @gr2m ] ( https://github.com/gr2m ) !_
84
84
85
85
#### openapi-typescript-fetch
86
86
@@ -132,31 +132,96 @@ try {
132
132
133
133
#### Outputting to stdout
134
134
135
- Simply omit the ` --output ` flag to return to stdout:
135
+ Omit the ` --output ` flag to return to stdout:
136
136
137
137
``` bash
138
138
npx openapi-typescript schema.yaml
139
139
```
140
140
141
- #### CLI Options
142
-
143
- | Option | Alias | Default | Description |
144
- | :----------------------------- | :---- | :------: | :-------------------------------------------------------------------------------------------------------------------------------------- |
145
- | ` --output [location] ` | ` -o ` | (stdout) | Where should the output file be saved? |
146
- | ` --auth [token] ` | | | (optional) Provide an auth token to be passed along in the request (only if accessing a private schema) |
147
- | ` --header ` | ` -x ` | | (optional) Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the ` key: value ` pattern |
148
- | ` --headersObject ` | ` -h ` | | (optional) Provide a JSON object as string of HTTP headers for remote schema request. This will take priority over ` --header ` |
149
- | ` --httpMethod ` | ` -m ` | ` GET ` | (optional) Provide the HTTP Verb/Method for fetching a schema from a remote URL |
150
- | ` --immutable-types ` | ` -it ` | ` false ` | (optional) Generates immutable types (readonly properties and readonly array) |
151
- | ` --additional-properties ` | ` -ap ` | ` false ` | (optional) Allow arbitrary properties for all schema objects without ` additionalProperties: false ` |
152
- | ` --default-non-nullable ` | | ` false ` | (optional) Treat schema objects with default values as non-nullable |
153
- | ` --prettier-config [location] ` | ` -c ` | | (optional) Path to your custom Prettier configuration for output |
154
- | ` --export-type ` | | ` false ` | (optional) Export ` type ` instead of ` interface ` |
155
- | ` --support-array-length ` | | ` false ` | (optional) Generate tuples using array minItems / maxItems |
156
- | ` --make-paths-enum ` | ` -pe ` | ` false ` | (optional) Generate an enum of endpoint paths |
157
- | ` --path-params-as-types ` | | ` false ` | (optional) Substitute path parameter names with their respective types |
158
- | ` --alphabetize ` | | ` false ` | (optional) Sort types alphabetically |
159
- | ` --raw-schema ` | | ` false ` | Generate TS types from partial schema (e.g. having ` components.schema ` at the top level) |
141
+ ### Options
142
+
143
+ The following flags can be appended to the CLI command.
144
+
145
+ | Option | Alias | Default | Description |
146
+ | :----------------------------- | :---- | :------: | :--------------------------------------------------------------------------------------------------------------------------- |
147
+ | ` --output [location] ` | ` -o ` | (stdout) | Where should the output file be saved? |
148
+ | ` --auth [token] ` | | | Provide an auth token to be passed along in the request (only if accessing a private schema) |
149
+ | ` --header ` | ` -x ` | | Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the ` key: value ` pattern |
150
+ | ` --headersObject ` | ` -h ` | | Provide a JSON object as string of HTTP headers for remote schema request. This will take priority over ` --header ` |
151
+ | ` --httpMethod ` | ` -m ` | ` GET ` | Provide the HTTP Verb/Method for fetching a schema from a remote URL |
152
+ | ` --immutable-types ` | | ` false ` | Generates immutable types (readonly properties and readonly array) |
153
+ | ` --additional-properties ` | | ` false ` | Allow arbitrary properties for all schema objects without ` additionalProperties: false ` |
154
+ | ` --default-non-nullable ` | | ` false ` | Treat schema objects with default values as non-nullable |
155
+ | ` --export-type ` | ` -t ` | ` false ` | Export ` type ` instead of ` interface ` |
156
+ | ` --path-params-as-types ` | | ` false ` | Allow dynamic string lookups on the ` paths ` object |
157
+ | ` --support-array-length ` | | ` false ` | Generate tuples using array ` minItems ` / ` maxItems ` |
158
+ | ` --alphabetize ` | | ` false ` | Sort types alphabetically |
159
+
160
+ #### ` --path-params-as-types `
161
+
162
+ By default, your URLs are preserved exactly as-written in your schema:
163
+
164
+ ``` ts
165
+ export interface paths {
166
+ ' /user/{user_id}' : components [" schemas" ][" User" ];
167
+ }
168
+ ```
169
+
170
+ Which means your type lookups also have to match the exact URL:
171
+
172
+ ``` ts
173
+ import { paths } from ' ./my-schema' ;
174
+
175
+ const url = ` /user/${id } ` ;
176
+ type UserResponses = paths [' /user/{user_id}' ][' responses' ];
177
+ ```
178
+
179
+ But when ` --path-params-as-types ` is enabled, you can take advantage of dynamic lookups like so:
180
+
181
+ ``` ts
182
+ import { paths } from ' ./my-schema' ;
183
+
184
+ const url = ` /user/${id } ` ;
185
+ type UserResponses = paths [url ][' responses' ]; // automatically matches `paths['/user/{user_id}']`
186
+ ```
187
+
188
+ Though this is a contrived example, you could use this feature to automatically infer typing based on the URL in a fetch client or in some other useful place in your application.
189
+
190
+ _ Thanks, [ @Powell-v2 ] ( https://github.com/Powell-v2 ) !_
191
+
192
+ #### ` --support-array-length `
193
+
194
+ This option is useful for generating tuples if an array type specifies ` minItems ` or ` maxItems ` .
195
+
196
+ For example, given the following schema:
197
+
198
+ ``` yaml
199
+ components :
200
+ schemas :
201
+ TupleType
202
+ type : array
203
+ items :
204
+ type : string
205
+ minItems : 1
206
+ maxItems : 2
207
+ ` ` `
208
+
209
+ Enabling ` --support-array-length` would change the typing like so:
210
+
211
+ ` ` ` diff
212
+ export interface components {
213
+ schemas: {
214
+ - TupleType: string[];
215
+ + TupleType: [string] | [string, string];
216
+ };
217
+ }
218
+ ` ` `
219
+
220
+ This results in more explicit typechecking of array lengths.
221
+
222
+ _Note : this has a reasonable limit, so for example `maxItems: 100` would simply flatten back down to `string[];`_
223
+
224
+ _Thanks, [@kgtkr](https://github.com/kgtkr)!_
160
225
161
226
# ## 🐢 Node
162
227
0 commit comments