Skip to content

Commit 64decb7

Browse files
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/`
1 parent 38ee8b4 commit 64decb7

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

.changeset/nasty-windows-chew.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"openapi-typescript": patch
3+
"openapi-typescript-docs": patch
4+
---
5+
6+
Add example for `Blob` type transforms

docs/src/content/docs/node.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Use the `transform()` and `postTransform()` options to override the default Sche
4545
- `transform()` runs **BEFORE** the conversion to TypeScript (you’re working with the original OpenAPI nodes)
4646
- `postTransform()` runs **AFTER** the conversion to TypeScript (you’re working with TypeScript types)
4747
48+
#### Example: `Date` types
49+
4850
For example, say your schema has the following property:
4951
5052
```yaml
@@ -73,6 +75,41 @@ That would result in the following change:
7375
+ updated_at?: Date;
7476
```
7577
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+
const types = openapiTS(mySchema, {
98+
transform(schemaObject, metadata): string {
99+
if ("format" in schemaObject && schemaObject.format === "binary") {
100+
return schemaObject.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+
76113
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.
77114
78115
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).

packages/openapi-typescript/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,39 @@ That would result in the following change:
255255
+ updated_at?: Date;
256256
```
257257

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") {
278+
return schemaObject.nullable ? "Blob | null" : "Blob";
279+
}
280+
},
281+
});
282+
```
283+
284+
Resultant diff with correctly-typed `file` property:
285+
286+
```diff
287+
- file?: string;
288+
+ file?: Blob;
289+
```
290+
258291
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.
259292

260293
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

Comments
 (0)