Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/5.api/1.composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const { data: customerById } = await useLazyFetch('/api/auth/customer/123/state'
customerById.value?.customerId.toUpperCase()
```

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

```ts [pages/app.vue]
const requestFetch = useAuthRequestFetch()
Expand Down
3 changes: 0 additions & 3 deletions src/module/type-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ declare module 'nitropack' {
interface NitroRouteConfig {
auth?: import('${runtimeTypesPath}').AuthMeta
}
interface InternalApi extends _GeneratedAuthInternalApi {}
}
declare module 'nitropack/types' {
interface NitroRouteRules {
Expand All @@ -329,7 +328,6 @@ declare module 'nitropack/types' {
interface NitroRouteConfig {
auth?: import('${runtimeTypesPath}').AuthMeta
}
interface InternalApi extends _GeneratedAuthInternalApi {}
}
declare module 'nitro/types' {
interface NitroRouteRules {
Expand All @@ -338,7 +336,6 @@ declare module 'nitro/types' {
interface NitroRouteConfig {
auth?: import('${runtimeTypesPath}').AuthMeta
}
interface InternalApi extends _GeneratedAuthInternalApi {}
}
export {}
`,
Expand Down
30 changes: 4 additions & 26 deletions test/cases/nitro-endpoints-type-inference/typecheck-target.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
import type { Base$Fetch } from 'nitropack/types'
import type { AuthApiEndpointPath, AuthApiEndpointResponse } from '#nuxt-better-auth'
import type { AuthApiEndpointPath } from '#nuxt-better-auth'

declare const requestFetch: Base$Fetch

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

async function assertEndpointInference() {
async function assertNoGlobalEndpointInference() {
const customerState = await requestFetch('/api/auth/customer/state')
// @ts-expect-error use useAuthRequestFetch for typed auth endpoint payloads
customerState.activeSubscriptions[0]?.toUpperCase()
customerState.hasBillingIssue.valueOf()
// @ts-expect-error no unknown key
void customerState.missingField

const customerViaHelper: AuthApiEndpointResponse<'/api/auth/customer/state'> = customerState
void customerViaHelper

const customerSessionGet = await requestFetch('/api/auth/customer/session')
const customerSessionPost = await requestFetch('/api/auth/customer/session', { method: 'POST' })
customerSessionGet.ok.valueOf()
customerSessionPost.ok.valueOf()

const session = await requestFetch('/api/auth/get-session')
if (session) {
void session.user.id
void session.session.expiresAt
}
// @ts-expect-error get-session can be null
const shouldFailNullability: { user: { id: string } } = session
void shouldFailNullability

const sessionViaHelper: AuthApiEndpointResponse<'/api/auth/get-session', 'get'> = session
void sessionViaHelper
}

void customerStatePath
void assertEndpointInference
void assertNoGlobalEndpointInference
4 changes: 2 additions & 2 deletions test/infer-nitro-endpoints-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const env = {
BETTER_AUTH_SECRET: 'test-secret-for-testing-only-32chars',
}

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