Skip to content

Commit e103a0b

Browse files
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`.
1 parent f259093 commit e103a0b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

packages/openapi-typescript/README.md

+33
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)