Skip to content

Commit c9d963d

Browse files
authored
fix(types): preserve define server auth literals (#135)
1 parent d8497d7 commit c9d963d

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

src/module/type-templates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ declare module '@onmax/nuxt-better-auth/config' {
8686
type ServerAuthConfig = Omit<BetterAuthOptions, 'secret' | 'baseURL'> & {
8787
plugins?: readonly BetterAuthPlugin[]
8888
}
89-
export function defineServerAuth<const R extends ServerAuthConfig>(config: R): (ctx: _AugmentedServerAuthContext) => R
90-
export function defineServerAuth<const R extends ServerAuthConfig>(config: (ctx: _AugmentedServerAuthContext) => R): (ctx: _AugmentedServerAuthContext) => R
89+
export function defineServerAuth<const R>(config: (ctx: _AugmentedServerAuthContext) => R & ServerAuthConfig): (ctx: _AugmentedServerAuthContext) => R
90+
export function defineServerAuth<const R>(config: R & ServerAuthConfig): (ctx: _AugmentedServerAuthContext) => R
9191
}
9292
`,
9393
}, { nuxt: true, nitro: true, node: true })

src/runtime/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export interface AuthPrivateRuntimeConfig {
7070
secondaryStorage: boolean
7171
}
7272

73-
export function defineServerAuth<const R extends ServerAuthConfig>(config: R): (ctx: ServerAuthContext) => R
74-
export function defineServerAuth<const R extends ServerAuthConfig>(config: (ctx: ServerAuthContext) => R): (ctx: ServerAuthContext) => R
75-
export function defineServerAuth<T extends ServerAuthConfig>(config: T | ((ctx: ServerAuthContext) => T)): (ctx: ServerAuthContext) => T {
73+
export function defineServerAuth<const R>(config: (ctx: ServerAuthContext) => R & ServerAuthConfig): (ctx: ServerAuthContext) => R
74+
export function defineServerAuth<const R>(config: R & ServerAuthConfig): (ctx: ServerAuthContext) => R
75+
export function defineServerAuth(config: ServerAuthConfig | ((ctx: ServerAuthContext) => ServerAuthConfig)): (ctx: ServerAuthContext) => ServerAuthConfig {
7676
return typeof config === 'function' ? config : () => config
7777
}
7878

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"lib": [
5+
"ESNext",
6+
"DOM"
7+
],
8+
"module": "preserve",
9+
"moduleResolution": "bundler",
10+
"types": [],
11+
"strict": true,
12+
"noEmit": true,
13+
"skipLibCheck": true
14+
},
15+
"files": [
16+
"./typecheck-target.ts"
17+
]
18+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { defineServerAuth } from '../../../../src/runtime/config'
2+
3+
const objectConfig = defineServerAuth({
4+
advanced: { database: { generateId: 'uuid' } },
5+
user: {
6+
additionalFields: {
7+
address: { type: 'string', required: false, fieldName: 'address' },
8+
},
9+
},
10+
})
11+
12+
const callbackConfig = defineServerAuth(() => ({
13+
advanced: { database: { generateId: 'uuid' } },
14+
user: {
15+
additionalFields: {
16+
city: { type: 'string', required: false },
17+
},
18+
},
19+
}))
20+
21+
void objectConfig
22+
void callbackConfig
23+
24+
// @ts-expect-error invalid generateId literal
25+
defineServerAuth({
26+
advanced: { database: { generateId: 'invalid-id' } },
27+
})
28+
29+
// @ts-expect-error invalid additionalFields type literal
30+
defineServerAuth({
31+
user: {
32+
additionalFields: {
33+
badField: { type: 'invalid-type' },
34+
},
35+
},
36+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { spawnSync } from 'node:child_process'
2+
import { fileURLToPath } from 'node:url'
3+
import { describe, expect, it } from 'vitest'
4+
5+
const fixtureDir = fileURLToPath(new URL('./cases/define-server-auth-literal-inference', import.meta.url))
6+
7+
describe('defineServerAuth literal inference regression #134', () => {
8+
it('typechecks nested literals without as const and rejects invalid literals', () => {
9+
const typecheck = spawnSync('pnpm', ['exec', 'tsc', '--noEmit', '--pretty', 'false', '-p', 'tsconfig.json'], {
10+
cwd: fixtureDir,
11+
encoding: 'utf8',
12+
})
13+
14+
expect(typecheck.status, `tsc failed:\n${typecheck.stdout}\n${typecheck.stderr}`).toBe(0)
15+
})
16+
})

0 commit comments

Comments
 (0)