Skip to content

feat(core): add utility types for config inference#150

Merged
halvaradop merged 4 commits into
masterfrom
feat/add-infer-types
Apr 23, 2026
Merged

feat(core): add utility types for config inference#150
halvaradop merged 4 commits into
masterfrom
feat/add-infer-types

Conversation

@halvaradop
Copy link
Copy Markdown
Member

@halvaradop halvaradop commented Apr 22, 2026

Description

Introduces new identity inference types and renames existing ones to improve clarity, consistency, and intent when deriving User and Session types from either an auth instance or a Zod schema.


Changes

  • Add SessionFrom and InferSession types
  • Rename ShapeToObjectZodShapeToObject
  • Rename InferAuthIdentityInferUser
  • Rename InferShapeInferZodShape
  • Rename InferIdentityUserFrom
  • Remove UserIdentityType

Usage

import { z } from "zod"
import { createAuth } from "@aura-stack/auth"
import { UserIdentity, type InferUser, InferSession } from "@aura-stack/auth/identity"

const schema = UserIdentity.extend({
  role: z.enum(["admin", "user"]),
  permissions: z.array(z.string()),
})

export const auth = createAuth({
  oauth: [],
  identity: {
    schema,
  },
})

export type User = InferUser<typeof auth>
export type Session = InferSession<typeof auth>

Or, if you prefer to infer the types from the schema:

import { createAuth } from "@aura-stack/auth"
import type { UserIdentity, UserFrom, SessionFrom } from "@aura-stack/auth/identity"

const schema = UserIdentity.extend({
  role: z.enum(["admin", "user"]),
  permissions: z.array(z.string()),
})

type User = UserFrom<typeof schema>
type Session = SessionFrom<typeof schema>

export const auth = createAuth({
  oauth: [],
  identity: {
    schema,
  },
})

Summary by CodeRabbit

  • Refactor

    • Restructured type utilities for improved clarity: InferAuthIdentityInferUser, InferShapeInferZodShape, InferIdentityUserFrom, ShapeToObjectZodShapeToObject
    • Added new InferSession and SessionFrom type utilities for session type inference
    • Removed UserIdentityType; use User type instead
  • Documentation

    • Updated TypeScript configuration guide with new type inference patterns

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
auth Ready Ready Preview, Comment Apr 23, 2026 2:35am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Replaces 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

Cohort / File(s) Summary
Core type utilities & exports
packages/core/src/@types/utility.ts, packages/core/src/@types/index.ts, packages/core/src/shared/identity.ts, packages/core/CHANGELOG.md
Removed ShapeToObject/Infer* utilities; added ZodShapeToObject, Wrap<T>, InferUser, InferSession, InferZodShape, UserFrom, SessionFrom; removed UserIdentityType export and updated barrel exports and changelog.
Config / session / context typings
packages/core/src/@types/config.ts, packages/core/src/@types/session.ts, packages/core/src/session/strategy.ts, packages/core/src/router/context.ts
Switched generics and exported type annotations from ShapeToObject<Identity>ZodShapeToObject<Identity> (AuthConfig, CreateSession options, Router/JOSE context, session types).
createAuth / framework adapters
packages/core/src/createAuth.ts, packages/elysia/src/createAuth.ts, packages/express/src/createAuth.ts, packages/hono/src/createAuth.ts, packages/next/src/createAuth.ts, packages/react-router/src/createAuth.ts
Replaced uses/imports of ShapeToObject with ZodShapeToObject in compile-time generics for createAuth / withAuth / returned api. No runtime/signature changes.
Tests & documentation
packages/core/test/types.test-d.ts, packages/express/test/types.test-d.ts, docs/src/content/docs/configuration/typescript.mdx
Updated type-test assertions and docs snippets to reference ZodShapeToObject, new inference utilities (InferUser, InferSession, UserFrom, SessionFrom) and removed old InferAuthIdentity/InferShape examples.
Type-test surface adjustments
packages/core/src/@types/* (other small edits)
Minor import/type updates across internal type files to align exported names and Zod-based inference (imports of Infer/Session added/z usage adjusted).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰
I hop through types with eager paws,
ShapeToObject traded for Zod applause.
InferUser, Session — neat and spry,
A rabbit's cheer for cleaner types. 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: adding new utility types (InferSession, SessionFrom, etc.) and renaming existing types for config-driven inference of User and Session types.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/add-infer-types

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 | 🟠 Major

Fix optional property inference in ShapeToObject and dependent types.

The manual mapped type in ShapeToObject makes optional schema fields required as T | undefined instead of optional properties, and the new InferIdentity / InferUserFromSchema helpers 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) and InferUserFromSchema (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

📥 Commits

Reviewing files that changed from the base of the PR and between cd4790a and 17a62c4.

📒 Files selected for processing (3)
  • packages/core/src/@types/config.ts
  • packages/core/src/@types/utility.ts
  • packages/core/src/shared/identity.ts

Comment thread packages/core/src/@types/config.ts Outdated
Comment thread packages/core/src/@types/utility.ts
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

♻️ Duplicate comments (1)
packages/core/src/@types/utility.ts (1)

48-76: ⚠️ Potential issue | 🟡 Minor

Fix the identity examples to match AuthConfig.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.

EditableShape and ZodShapeToObject are type-only symbols and should be imported with the type keyword, 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

📥 Commits

Reviewing files that changed from the base of the PR and between 17a62c4 and 95802a5.

📒 Files selected for processing (17)
  • docs/src/content/docs/configuration/typescript.mdx
  • packages/core/CHANGELOG.md
  • packages/core/src/@types/config.ts
  • packages/core/src/@types/index.ts
  • packages/core/src/@types/session.ts
  • packages/core/src/@types/utility.ts
  • packages/core/src/createAuth.ts
  • packages/core/src/router/context.ts
  • packages/core/src/session/strategy.ts
  • packages/core/src/shared/identity.ts
  • packages/core/test/types.test-d.ts
  • packages/elysia/src/createAuth.ts
  • packages/express/src/createAuth.ts
  • packages/express/test/types.test-d.ts
  • packages/hono/src/createAuth.ts
  • packages/next/src/createAuth.ts
  • packages/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

Comment thread docs/src/content/docs/configuration/typescript.mdx Outdated
Comment thread packages/core/src/@types/utility.ts Outdated
@halvaradop halvaradop added enhancement New feature or request feature New functionality refactor Refactor without changing behavior labels Apr 23, 2026
@halvaradop halvaradop merged commit 37edb5b into master Apr 23, 2026
6 of 7 checks passed
@halvaradop halvaradop deleted the feat/add-infer-types branch April 23, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature New functionality refactor Refactor without changing behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant