feat: zod as response transformer#3847
Conversation
|
|
|
|
@SukkaW is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
|
TL;DR — Extends the Key changes
Summary | 164 files | 5 commits | base:
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3847 +/- ##
==========================================
- Coverage 37.78% 37.76% -0.03%
==========================================
Files 580 581 +1
Lines 20777 20805 +28
Branches 6047 6058 +11
==========================================
+ Hits 7851 7856 +5
- Misses 10522 10543 +21
- Partials 2404 2406 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Important
Two design-level observations worth discussing before this merges. Neither is a correctness bug, but both affect maintainability.
Overall the approach is sound — the config normalization, plugin dispatch, and generated output all look correct. The snapshot coverage is thorough across all spec versions and compat modes. The boilerplate snapshot files (client scaffolding, utils, core modules) are standard generated code that doesn't change between the existing and new test scenarios.
@hey-api/codegen-core
@hey-api/json-schema-ref-parser
@hey-api/nuxt
@hey-api/openapi-ts
@hey-api/shared
@hey-api/spec-types
@hey-api/types
@hey-api/vite-plugin
commit: |
|
Acknowledged that the outer type guard in Task list (2/2 completed)
|
|
Acknowledged the author's reply on the Task list (3/3 completed)
|
|
@SukkaW don't mind me, I'm going to complete this pull request. I need to add support for Valibot and get rid of the "duplicate |
Feel free to do so. And feel free to change how plugins expose the transformer codegen API, and the options shape. |
|
@SukkaW I'm going to add one more optional method for returning response handlers together. This will be called only if the same plugin is configured for both. Then the plugin can decide whether to return only transformer or both. In this case, we want to return only transformer to avoid wasted cycles |

Fixes #3289
This PR extends the
@hey-api/sdkplugin'stransformeroption to accept'zod'. Whenzodis used as a transformer, itsparseAsyncreturn value is forwarded asresponseTransformeron the generated request options — enabling Zod's coercion and transformation rules (e.g.z.coerce.date()) to apply to response data.The PR changes the following things:
New
transformershapesEnabling
transformeroption as object form allows us to expand transformer abilities to request in the future.Zod plugin as transformer
@hey-api/transformerspath (existing): a named async transformer function is code-gen'd and passed as a reference toresponseTransformer.zodpath (new): the zod plugin now exposes acreateResponseTransformermethod. During code-gen we will check if the plugin exposes this method, and use that to generate an inline arrow function forresponseTransformer. In this case, we generate aparseAsynccall.Updated test snapshots
The test cases for this change uses the
type-format.yamlspec (which has anint64field) withtransformer: 'zod'andvalidator: true. The generated output lives in__snapshots__/3.1.x/{v3,v4,mini}/zod-transformer/.Two snapshot files are worth looking at:
zod.gen.ts— the generated Zod schema. Theint64field becomesz.coerce.bigint(), meaningparseAsyncwill coerce the raw wire value into a realBigInt:sdk.gen.ts— the generated SDK function. Three request options are present:The PR introduces no breaking changes:
transformer: falsebehaviour is unchanged.transformer: trueandtransformer: '@hey-api/transformers'continue to work exactly as before.validatorconfiguration is untouched.