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: docs/src/content/docs/node.md
+37
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).
0 commit comments