From e103a0bb7754a219cc5adf9fad8555a3c72d5b0e Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Sat, 1 Jul 2023 16:50:12 +1000 Subject: [PATCH 1/2] 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`. --- packages/openapi-typescript/README.md | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/openapi-typescript/README.md b/packages/openapi-typescript/README.md index 4612b5e80..0671b073b 100644 --- a/packages/openapi-typescript/README.md +++ b/packages/openapi-typescript/README.md @@ -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). From 9126e8b39fd29b756cd4dd3b573eece1f7fe971f Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Sun, 2 Jul 2023 17:11:03 +1000 Subject: [PATCH 2/2] docs(openapi-typescript): add `Blob` transform example to `docs/` --- .changeset/nasty-windows-chew.md | 6 ++++++ docs/src/content/docs/node.md | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 .changeset/nasty-windows-chew.md diff --git a/.changeset/nasty-windows-chew.md b/.changeset/nasty-windows-chew.md new file mode 100644 index 000000000..2c30e8f8e --- /dev/null +++ b/.changeset/nasty-windows-chew.md @@ -0,0 +1,6 @@ +--- +"openapi-typescript": patch +"openapi-typescript-docs": patch +--- + +Add example for `Blob` type transforms diff --git a/docs/src/content/docs/node.md b/docs/src/content/docs/node.md index 8dc723bc7..ccae7f04a 100644 --- a/docs/src/content/docs/node.md +++ b/docs/src/content/docs/node.md @@ -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 @@ -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).