feat(core): add utility types for config inference#150
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughReplaces legacy ShapeToObject-based identity/session type utilities with Zod-based equivalents (ZodShapeToObject, InferUser, InferSession, UserFrom, SessionFrom, etc.) and updates all related type imports, exports, tests, and docs. No runtime code paths were changed. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/core/src/@types/utility.ts (1)
28-35:⚠️ Potential issue | 🟠 MajorFix optional property inference in ShapeToObject and dependent types.
The manual mapped type in
ShapeToObjectmakes optional schema fields required asT | undefinedinstead of optional properties, and the newInferIdentity/InferUserFromSchemahelpers inherit this bug. Use Zod's native object inference to preserve optional property modifiers from.optional()calls.Suggested fix
export type ShapeToObject<S extends ZodRawShape = ZodRawShape> = Merge< - { - [K in keyof S]: z.infer<S[K]> - }, + z.infer<ZodObject<S>>, User >This affects
InferIdentity(line 86) andInferUserFromSchema(line 99), and propagates through consumer code that relies on these public types.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/`@types/utility.ts around lines 28 - 35, The current ShapeToObject mapped type forces optional schema fields to become required (T | undefined); replace the manual mapping with Zod's native object inference so optional modifiers are preserved (use z.infer<z.ZodObject<S, ...>> or directly infer the object type from S) and update InferIdentity and InferUserFromSchema to derive from that corrected ShapeToObject (or from z.infer<z.ZodObject<...>>) so optional properties remain optional across all dependent types.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/core/src/`@types/config.ts:
- Around line 5-11: Remove the unused runtime import ZodTypeAny from "zod/v3" in
packages/core/src/@types/config.ts and keep only the type-only imports from
"zod/v4"; specifically delete the import statement that brings in ZodTypeAny
(the v3 import) and, if a ZodTypeAny type is ever needed later, import it as a
type from "zod/v4" (e.g., import type { ZodTypeAny } from "zod/v4") so the file
consistently uses zod v4 types and has no unused runtime imports.
In `@packages/core/src/`@types/utility.ts:
- Around line 49-77: The examples in the JSDoc for InferUser and InferSession
pass a raw schema to createAuth.identity but AuthConfig.identity expects an
object with a schema property; update the examples to use createAuth({ ...,
identity: { schema: UserIdentity.extend({...}) } }) and adjust the InferSession
doc text to state it returns a Session type (not that it infers the user type).
Reference the types/functions: createAuth, AuthConfig.identity, UserIdentity,
InferUser, InferSession, AuthInstance, and Session so the examples and
description match the actual API shape.
---
Outside diff comments:
In `@packages/core/src/`@types/utility.ts:
- Around line 28-35: The current ShapeToObject mapped type forces optional
schema fields to become required (T | undefined); replace the manual mapping
with Zod's native object inference so optional modifiers are preserved (use
z.infer<z.ZodObject<S, ...>> or directly infer the object type from S) and
update InferIdentity and InferUserFromSchema to derive from that corrected
ShapeToObject (or from z.infer<z.ZodObject<...>>) so optional properties remain
optional across all dependent types.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 80c00939-2476-4428-a0e9-6e9ce8e71b2b
📒 Files selected for processing (3)
packages/core/src/@types/config.tspackages/core/src/@types/utility.tspackages/core/src/shared/identity.ts
There was a problem hiding this comment.
Actionable comments posted: 2
♻️ Duplicate comments (1)
packages/core/src/@types/utility.ts (1)
48-76:⚠️ Potential issue | 🟡 MinorFix the
identityexamples to matchAuthConfig.identity.The examples still pass the schema directly, but the config shape expects
identity: { schema: ... }.Docs fix
- * identity: UserIdentity.extend({ - * role: z.string().nullable().optional(), - * username: z.string().optional(), - * }) + * identity: { + * schema: UserIdentity.extend({ + * role: z.string().nullable().optional(), + * username: z.string().optional(), + * }), + * } @@ - * identity: UserIdentity.extend({ - * role: z.string().nullable().optional(), - * username: z.string().optional(), - * }) + * identity: { + * schema: UserIdentity.extend({ + * role: z.string().nullable().optional(), + * username: z.string().optional(), + * }), + * }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/`@types/utility.ts around lines 48 - 76, Update the example configs for InferUser/InferSession so the identity is passed as AuthConfig.identity's shape (i.e., an object with a schema property) rather than the raw Zod schema; in the examples that createAuth refer to identity: { schema: UserIdentity.extend({ ... }) } (and any similar occurrences) so the examples match the AuthInstance/AuthConfig.identity contract used by InferUser, InferSession, createAuth and UserIdentity.
🧹 Nitpick comments (1)
packages/core/src/session/strategy.ts (1)
4-4: Use type-only imports for consistency.
EditableShapeandZodShapeToObjectare type-only symbols and should be imported with thetypekeyword, matching the pattern on line 3 of this file.-import { EditableShape, ZodShapeToObject } from "@/@types/utility.ts" +import type { EditableShape, ZodShapeToObject } from "@/@types/utility.ts"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/core/src/session/strategy.ts` at line 4, The import line brings in type-only symbols EditableShape and ZodShapeToObject as runtime imports; change that line to use type-only imports so they are imported with the type keyword (e.g., import type { EditableShape, ZodShapeToObject } from "...") to match the pattern used elsewhere in this module and avoid emitting runtime references for these types.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@docs/src/content/docs/configuration/typescript.mdx`:
- Around line 67-97: The TypeScript examples mix type-only imports and runtime
usage and miss the z import; change the first snippet to import InferSession as
type-only (e.g., import type { InferUser, InferSession } ...) since InferSession
is only used as a type for Session, and in the second snippet import
UserIdentity as a runtime value (remove type-only) so .extend() can be called
and add an import for z (e.g., import { z } from "zod"); ensure the examples
consistently reference the same schema variable (schema) and type helpers
(UserFrom, SessionFrom) when deriving User and Session types from the schema.
In `@packages/core/src/`@types/utility.ts:
- Around line 27-34: ZodShapeToObject currently maps each field with {[K in
keyof S]: Infer<S[K]>} which loses schema-level optionality; replace the mapped
inference with an inference of the whole object schema (e.g. use
Infer<ZodObject<S>> or Merge<Infer<ZodObject<S>>, User>) so keys defined with
.optional() become optional (preserve key?:) instead of required with unioned
undefined; update the ZodShapeToObject type to infer from ZodObject<S> and keep
the Merge with User to retain existing merged properties.
---
Duplicate comments:
In `@packages/core/src/`@types/utility.ts:
- Around line 48-76: Update the example configs for InferUser/InferSession so
the identity is passed as AuthConfig.identity's shape (i.e., an object with a
schema property) rather than the raw Zod schema; in the examples that createAuth
refer to identity: { schema: UserIdentity.extend({ ... }) } (and any similar
occurrences) so the examples match the AuthInstance/AuthConfig.identity contract
used by InferUser, InferSession, createAuth and UserIdentity.
---
Nitpick comments:
In `@packages/core/src/session/strategy.ts`:
- Line 4: The import line brings in type-only symbols EditableShape and
ZodShapeToObject as runtime imports; change that line to use type-only imports
so they are imported with the type keyword (e.g., import type { EditableShape,
ZodShapeToObject } from "...") to match the pattern used elsewhere in this
module and avoid emitting runtime references for these types.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 54e66c01-c4b9-42ed-9a1f-0a84e2ae0125
📒 Files selected for processing (17)
docs/src/content/docs/configuration/typescript.mdxpackages/core/CHANGELOG.mdpackages/core/src/@types/config.tspackages/core/src/@types/index.tspackages/core/src/@types/session.tspackages/core/src/@types/utility.tspackages/core/src/createAuth.tspackages/core/src/router/context.tspackages/core/src/session/strategy.tspackages/core/src/shared/identity.tspackages/core/test/types.test-d.tspackages/elysia/src/createAuth.tspackages/express/src/createAuth.tspackages/express/test/types.test-d.tspackages/hono/src/createAuth.tspackages/next/src/createAuth.tspackages/react-router/src/createAuth.ts
✅ Files skipped from review due to trivial changes (1)
- packages/core/CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/core/src/@types/config.ts
Description
Introduces new identity inference types and renames existing ones to improve clarity, consistency, and intent when deriving
UserandSessiontypes from either an auth instance or a Zod schema.Changes
SessionFromandInferSessiontypesShapeToObject→ZodShapeToObjectInferAuthIdentity→InferUserInferShape→InferZodShapeInferIdentity→UserFromUserIdentityTypeUsage
Or, if you prefer to infer the types from the schema:
Summary by CodeRabbit
Refactor
InferAuthIdentity→InferUser,InferShape→InferZodShape,InferIdentity→UserFrom,ShapeToObject→ZodShapeToObjectInferSessionandSessionFromtype utilities for session type inferenceUserIdentityType; useUsertype insteadDocumentation