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
Copy file name to clipboardExpand all lines: packages/openapi-typescript/README.md
+33
Original file line number
Diff line number
Diff line change
@@ -255,6 +255,39 @@ That would result in the following change:
255
255
+ updated_at?: Date;
256
256
```
257
257
258
+
Another common transformation is for file uploads, where the `body` of a request is a `multipart/form-data` with some `Blob` fields. Here's an example schema:
259
+
260
+
```yaml
261
+
Body_file_upload:
262
+
type: object;
263
+
properties:
264
+
file:
265
+
type: string;
266
+
format: binary;
267
+
}
268
+
}
269
+
}
270
+
```
271
+
272
+
Use the same pattern to transform the types:
273
+
274
+
```ts
275
+
const types = openapiTS(mySchema, {
276
+
transform(schemaObject, metadata): string {
277
+
if ("format" in schemaObject && schemaObject.format === "binary") {
Resultant diff with correctly-typed `file` property:
285
+
286
+
```diff
287
+
- file?: string;
288
+
+ file?: Blob;
289
+
```
290
+
258
291
Any [Schema Object](https://spec.openapis.org/oas/latest.html#schema-object) present in your schema will be run through this formatter (even remote ones!). Also be sure to check the `metadata` parameter for additional context that may be helpful.
259
292
260
293
There are many other uses for this besides checking `format`. Because this must return a **string** you can produce any arbitrary TypeScript code you’d like (even your own custom types).
0 commit comments