Skip to content

Commit 9126e8b

Browse files
docs(openapi-typescript): add Blob transform example to docs/
1 parent e103a0b commit 9126e8b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

.changeset/nasty-windows-chew.md

+6
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

+37
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).

0 commit comments

Comments
 (0)