Skip to content

Commit 8c8a610

Browse files
committed
types: replace any with unknown, re-export $Infer
1 parent 28464c9 commit 8c8a610

6 files changed

Lines changed: 25 additions & 12 deletions

File tree

src/module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ declare module '#auth/secondary-storage' {
100100
getContents: () => `
101101
// Type augmentation support
102102
export * from '${resolver.resolve('./runtime/types/augment')}'
103-
export type { AuthMeta, AuthMode, AuthRouteRules, UserMatch, RequireSessionOptions } from '${resolver.resolve('./runtime/types')}'
103+
export type { AuthMeta, AuthMode, AuthRouteRules, UserMatch, RequireSessionOptions, Auth, InferUser, InferSession } from '${resolver.resolve('./runtime/types')}'
104104
`,
105105
})
106106

@@ -110,13 +110,18 @@ export type { AuthMeta, AuthMode, AuthRouteRules, UserMatch, RequireSessionOptio
110110
getContents: () => `
111111
// Auto-generated types from auth.config.ts
112112
import type { InferUser, InferSession } from 'better-auth'
113+
import type { RuntimeConfig } from 'nuxt/schema'
113114
import type configFn from '${serverConfigPath}'
114115
115116
type _Config = ReturnType<typeof configFn>
116117
117118
declare module '#nuxt-better-auth' {
118119
interface AuthUser extends InferUser<_Config> {}
119120
interface AuthSession { session: InferSession<_Config>['session'], user: InferUser<_Config> }
121+
interface ServerAuthContext {
122+
runtimeConfig: RuntimeConfig
123+
${hasDb ? `db: typeof import('hub:db')['db']` : ''}
124+
}
120125
}
121126
`,
122127
})
@@ -270,4 +275,4 @@ async function setupBetterAuthSchema(nuxt: any, serverConfigPath: string) {
270275

271276
// Re-export config helpers
272277
export { defineClientAuth, defineServerAuth } from './runtime/config'
273-
export type { AuthMeta, AuthMode, AuthRouteRules, AuthSession, AuthUser, RequireSessionOptions, UserMatch } from './runtime/types'
278+
export type { Auth, AuthMeta, AuthMode, AuthRouteRules, AuthSession, AuthUser, InferSession, InferUser, RequireSessionOptions, ServerAuthContext, UserMatch } from './runtime/types'

src/runtime/app/composables/useUserSession.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ export function useUserSession() {
8585
type SignIn = NonNullable<AppAuthClient>['signIn']
8686
type SignUp = NonNullable<AppAuthClient>['signUp']
8787

88-
function wrapAuthMethod<T extends (...args: any[]) => Promise<any>>(method: T): T {
89-
return (async (...args: any[]) => {
90-
const [data, options] = args
88+
function wrapAuthMethod<T extends (...args: unknown[]) => Promise<unknown>>(method: T): T {
89+
return (async (...args: unknown[]) => {
90+
const [data, options] = args as [unknown, { onSuccess?: (ctx: unknown) => void } | undefined]
9191
const originalOnSuccess = options?.onSuccess
9292

9393
// If no onSuccess callback, pass options through unchanged
@@ -97,7 +97,7 @@ export function useUserSession() {
9797
// Wrap onSuccess to wait for session sync before calling
9898
const wrappedOptions = {
9999
...options,
100-
onSuccess: async (ctx: any) => {
100+
onSuccess: async (ctx: unknown) => {
101101
await waitForSession()
102102
await originalOnSuccess(ctx)
103103
},

src/runtime/config.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { BetterAuthOptions } from 'better-auth'
22
import type { ClientOptions } from 'better-auth/client'
3+
import type { ServerAuthContext } from './types/augment'
34

4-
export interface ServerAuthContext {
5-
runtimeConfig: any
6-
db?: any // optional in database-less mode
7-
}
5+
// Re-export for declaration merging with generated types
6+
export type { ServerAuthContext }
87

98
export interface ClientAuthContext {
109
siteUrl: string

src/runtime/server/utils/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function serverAuth(): Promise<AuthInstance> {
3535

3636
// Dynamic import hub:db only when using database
3737
let database: ReturnType<typeof drizzleAdapter> | undefined
38-
let db: any
38+
let db: unknown
3939
if (useDatabase) {
4040
const hubDb = await import('hub:db')
4141
db = hubDb.db

src/runtime/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type { NitroRouteRules } from 'nitropack/types'
22
import type { AuthSession, AuthUser } from './types/augment'
33

44
// Re-export augmentable types
5-
export type { AuthSession, AuthUser, UserSessionComposable } from './types/augment'
5+
export type { AuthSession, AuthUser, ServerAuthContext, UserSessionComposable } from './types/augment'
6+
7+
// Re-export better-auth types for $Infer access
8+
export type { Auth, InferSession, InferUser } from 'better-auth'
69

710
export type AuthMode = 'guest' | 'user'
811

src/runtime/types/augment.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ export interface AuthSession {
2626
userAgent?: string | null
2727
}
2828

29+
// Server auth context - extended by generated types with hub:db types
30+
export interface ServerAuthContext {
31+
runtimeConfig: Record<string, unknown>
32+
db?: unknown
33+
}
34+
2935
// Composable return type
3036
export interface UserSessionComposable {
3137
user: Ref<AuthUser | null>

0 commit comments

Comments
 (0)