Skip to content

Commit 7fb3e3b

Browse files
authored
fix(types): avoid global auth endpoint inference (#374)
1 parent 549a4d2 commit 7fb3e3b

4 files changed

Lines changed: 7 additions & 32 deletions

File tree

docs/content/5.api/1.composables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ const { data: customerById } = await useLazyFetch('/api/auth/customer/123/state'
228228
customerById.value?.customerId.toUpperCase()
229229
```
230230

231-
Global `$fetch` keeps Nitro `InternalApi` response inference, but path autocomplete is best on `useFetch`/`useLazyFetch` and `useAuthRequestFetch`.
231+
Global `$fetch` is not augmented by this module; use `useAuthRequestFetch()` for typed auth endpoint payloads with request-scoped cookies.
232232

233233
```ts [pages/app.vue]
234234
const requestFetch = useAuthRequestFetch()

src/module/type-templates.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ declare module 'nitropack' {
320320
interface NitroRouteConfig {
321321
auth?: import('${runtimeTypesPath}').AuthMeta
322322
}
323-
interface InternalApi extends _GeneratedAuthInternalApi {}
324323
}
325324
declare module 'nitropack/types' {
326325
interface NitroRouteRules {
@@ -329,7 +328,6 @@ declare module 'nitropack/types' {
329328
interface NitroRouteConfig {
330329
auth?: import('${runtimeTypesPath}').AuthMeta
331330
}
332-
interface InternalApi extends _GeneratedAuthInternalApi {}
333331
}
334332
declare module 'nitro/types' {
335333
interface NitroRouteRules {
@@ -338,7 +336,6 @@ declare module 'nitro/types' {
338336
interface NitroRouteConfig {
339337
auth?: import('${runtimeTypesPath}').AuthMeta
340338
}
341-
interface InternalApi extends _GeneratedAuthInternalApi {}
342339
}
343340
export {}
344341
`,
Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,16 @@
11
import type { Base$Fetch } from 'nitropack/types'
2-
import type { AuthApiEndpointPath, AuthApiEndpointResponse } from '#nuxt-better-auth'
2+
import type { AuthApiEndpointPath } from '#nuxt-better-auth'
33

44
declare const requestFetch: Base$Fetch
55

66
type CustomerStatePath = Extract<AuthApiEndpointPath, '/api/auth/customer/state'>
77
const customerStatePath: CustomerStatePath = '/api/auth/customer/state'
88

9-
async function assertEndpointInference() {
9+
async function assertNoGlobalEndpointInference() {
1010
const customerState = await requestFetch('/api/auth/customer/state')
11+
// @ts-expect-error use useAuthRequestFetch for typed auth endpoint payloads
1112
customerState.activeSubscriptions[0]?.toUpperCase()
12-
customerState.hasBillingIssue.valueOf()
13-
// @ts-expect-error no unknown key
14-
void customerState.missingField
15-
16-
const customerViaHelper: AuthApiEndpointResponse<'/api/auth/customer/state'> = customerState
17-
void customerViaHelper
18-
19-
const customerSessionGet = await requestFetch('/api/auth/customer/session')
20-
const customerSessionPost = await requestFetch('/api/auth/customer/session', { method: 'POST' })
21-
customerSessionGet.ok.valueOf()
22-
customerSessionPost.ok.valueOf()
23-
24-
const session = await requestFetch('/api/auth/get-session')
25-
if (session) {
26-
void session.user.id
27-
void session.session.expiresAt
28-
}
29-
// @ts-expect-error get-session can be null
30-
const shouldFailNullability: { user: { id: string } } = session
31-
void shouldFailNullability
32-
33-
const sessionViaHelper: AuthApiEndpointResponse<'/api/auth/get-session', 'get'> = session
34-
void sessionViaHelper
3513
}
3614

3715
void customerStatePath
38-
void assertEndpointInference
16+
void assertNoGlobalEndpointInference

test/infer-nitro-endpoints-types.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const env = {
88
BETTER_AUTH_SECRET: 'test-secret-for-testing-only-32chars',
99
}
1010

11-
describe('typed nitro route inference #194', () => {
12-
it('typechecks generated InternalApi endpoint inference for /api/auth routes', () => {
11+
describe('nitro route inference', () => {
12+
it('does not augment global InternalApi with /api/auth routes', () => {
1313
const prepare = spawnSync('npx', ['nuxi', 'prepare'], {
1414
cwd: fixtureDir,
1515
env,

0 commit comments

Comments
 (0)