You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function createUser({ name, password, type, address }: {
95
125
name:string,
@@ -108,10 +138,60 @@ createUser({
108
138
});
109
139
```
110
140
141
+
### Enums vs. Union Types `--useUnionTypes`
142
+
The OpenAPI spec allows you to define [enums](https://swagger.io/docs/specification/data-models/enums/) inside the
143
+
data model. By default, we convert these enums definitions to [TypeScript enums](https://www.typescriptlang.org/docs/handbook/enums.html).
144
+
However, these enums are merged inside the namespace of the model, this is unsupported by Babel, [see docs](https://babeljs.io/docs/en/babel-plugin-transform-typescript#impartial-namespace-support).
145
+
146
+
Because we also want to support projects that use Babel [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript), we offer the flag `--useOptions` to generate
147
+
[union types](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-types) instead of
148
+
the traditional enums. The difference can be seen below:
149
+
150
+
**Enums:**
151
+
```typescript
152
+
// Model
153
+
exportinterfaceOrder {
154
+
id?:number;
155
+
quantity?:number;
156
+
status?:Order.status;
157
+
}
158
+
159
+
exportnamespaceOrder {
160
+
exportenumstatus {
161
+
PLACED='placed',
162
+
APPROVED='approved',
163
+
DELIVERED='delivered',
164
+
}
165
+
}
166
+
167
+
// Usage
168
+
const order:Order= {
169
+
id: 1,
170
+
quantity: 40,
171
+
status: Order.status.PLACED
172
+
}
173
+
```
174
+
175
+
**Union Types:**
176
+
```typescript
177
+
// Model
178
+
exportinterfaceOrder {
179
+
id?:number;
180
+
quantity?:number;
181
+
status?:'placed'|'approved'|'delivered';
182
+
}
183
+
184
+
// Usage
185
+
const order:Order= {
186
+
id: 1,
187
+
quantity: 40,
188
+
status: 'placed'
189
+
}
190
+
```
111
191
112
-
### Runtime schemas
113
-
By default the OpenAPI generator only exports interfaces for your models. These interfaces will help you during
114
-
development, but will not be available in javascript during runtime. However, Swagger allows you to define properties
192
+
### Runtime schemas`--exportSchemas`
193
+
By default, the OpenAPI generator only exports interfaces for your models. These interfaces will help you during
194
+
development, but will not be available in JavaScript during runtime. However, Swagger allows you to define properties
115
195
that can be useful during runtime, for instance: `maxLength` of a string or a `pattern` to match, etc. Let's say
116
196
we have the following model:
117
197
@@ -192,7 +272,7 @@ export const $MyModel = {
192
272
```
193
273
194
274
These runtime object are prefixed with a `$` character and expose all the interesting attributes of a model
195
-
and it's properties. We can now use this object to generate the form:
275
+
and its properties. We can now use this object to generate the form:
196
276
197
277
```typescript jsx
198
278
import { $MyModel } from'./generated';
@@ -235,12 +315,12 @@ that can help developers use more meaningful enumerators.
235
315
],
236
316
"x-enum-varnames": [
237
317
"Success",
238
-
"Warning"
318
+
"Warning",
239
319
"Error"
240
320
],
241
321
"x-enum-descriptions": [
242
322
"Used when the status of something is successful",
243
-
"Used when the status of something has a warning"
323
+
"Used when the status of something has a warning",
244
324
"Used when the status of something has an error"
245
325
]
246
326
}
@@ -265,6 +345,7 @@ enum EnumWithStrings {
265
345
}
266
346
```
267
347
348
+
268
349
### Authorization
269
350
The OpenAPI generator supports Bearer Token authorization. In order to enable the sending
270
351
of tokens in each request you can set the token using the global OpenAPI configuration:
0 commit comments