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
Update README.md with transform example for Blob (#1193)
* Update README.md with `transform` example for `Blob`
Add example `transform` callback for file uploads, where the `multipart/form-data` has a field that should be typed as `Blob`.
* docs(openapi-typescript): add `Blob` transform example to `docs/`
Copy file name to clipboardExpand all lines: docs/src/content/docs/node.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,8 @@ Use the `transform()` and `postTransform()` options to override the default Sche
45
45
- `transform()` runs **BEFORE** the conversion to TypeScript (you’re working with the original OpenAPI nodes)
46
46
- `postTransform()` runs **AFTER** the conversion to TypeScript (you’re working with TypeScript types)
47
47
48
+
#### Example: `Date` types
49
+
48
50
For example, say your schema has the following property:
49
51
50
52
```yaml
@@ -73,6 +75,41 @@ That would result in the following change:
73
75
+ updated_at?:Date;
74
76
```
75
77
78
+
#### Example: `Blob` types
79
+
80
+
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:
81
+
82
+
```yaml
83
+
Body_file_upload:
84
+
type: object;
85
+
properties:
86
+
file:
87
+
type: string;
88
+
format: binary;
89
+
}
90
+
}
91
+
}
92
+
```
93
+
94
+
Use the same pattern to transform the types:
95
+
96
+
```ts
97
+
consttypes=openapiTS(mySchema, {
98
+
transform(schemaObject, metadata): string {
99
+
if ("format"in schemaObject &&schemaObject.format==="binary") {
100
+
returnschemaObject.nullable?"Blob | null":"Blob";
101
+
}
102
+
},
103
+
});
104
+
```
105
+
106
+
Resultant diff with correctly-typed `file` property:
107
+
108
+
```diff
109
+
- file?: string;
110
+
+ file?:Blob;
111
+
```
112
+
76
113
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.
77
114
78
115
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).
Copy file name to clipboardExpand all lines: packages/openapi-typescript/README.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
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