Skip to content

Update README.md with transform example for Blob #1193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/nasty-windows-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"openapi-typescript": patch
"openapi-typescript-docs": patch
---

Add example for `Blob` type transforms
37 changes: 37 additions & 0 deletions docs/src/content/docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Use the `transform()` and `postTransform()` options to override the default Sche
- `transform()` runs **BEFORE** the conversion to TypeScript (you’re working with the original OpenAPI nodes)
- `postTransform()` runs **AFTER** the conversion to TypeScript (you’re working with TypeScript types)

#### Example: `Date` types

For example, say your schema has the following property:

```yaml
Expand Down Expand Up @@ -73,6 +75,41 @@ That would result in the following change:
+ updated_at?: Date;
```

#### Example: `Blob` types

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:

```yaml
Body_file_upload:
type: object;
properties:
file:
type: string;
format: binary;
}
}
}
```

Use the same pattern to transform the types:

```ts
const types = openapiTS(mySchema, {
transform(schemaObject, metadata): string {
if ("format" in schemaObject && schemaObject.format === "binary") {
return schemaObject.nullable ? "Blob | null" : "Blob";
}
},
});
```

Resultant diff with correctly-typed `file` property:

```diff
- file?: string;
+ file?: Blob;
```

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.

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).
33 changes: 33 additions & 0 deletions packages/openapi-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,39 @@ That would result in the following change:
+ updated_at?: Date;
```

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:

```yaml
Body_file_upload:
type: object;
properties:
file:
type: string;
format: binary;
}
}
}
```

Use the same pattern to transform the types:

```ts
const types = openapiTS(mySchema, {
transform(schemaObject, metadata): string {
if ("format" in schemaObject && schemaObject.format === "binary") {
return schemaObject.nullable ? "Blob | null" : "Blob";
}
},
});
```

Resultant diff with correctly-typed `file` property:

```diff
- file?: string;
+ file?: Blob;
```

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.

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).
Expand Down