From f39eb6e981d279c24fa3bd831993c0e61500be1e Mon Sep 17 00:00:00 2001 From: Gypsophyllite <50662386+Gypsophyllite@users.noreply.github.com> Date: Wed, 17 May 2023 19:56:47 +0800 Subject: [PATCH] fix(nuxt): infer `useFetch` method when generic is passed (#20797) --- packages/nuxt/src/app/composables/fetch.ts | 8 ++++---- test/fixtures/basic/server/api/hey/index.post.ts | 3 +++ test/fixtures/basic/types.ts | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/basic/server/api/hey/index.post.ts diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index fe487dcecd..c06271cb43 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -31,7 +31,7 @@ export function useFetch< ResT = void, ErrorT = FetchError, ReqT extends NitroFetchRequest = NitroFetchRequest, - Method extends AvailableRouterMethod = 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod, + Method extends AvailableRouterMethod = ResT extends void ? 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod : AvailableRouterMethod, _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf @@ -43,7 +43,7 @@ export function useFetch< ResT = void, ErrorT = FetchError, ReqT extends NitroFetchRequest = NitroFetchRequest, - Method extends AvailableRouterMethod = 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod, + Method extends AvailableRouterMethod = ResT extends void ? 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod : AvailableRouterMethod, _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf @@ -124,7 +124,7 @@ export function useLazyFetch< ResT = void, ErrorT = FetchError, ReqT extends NitroFetchRequest = NitroFetchRequest, - Method extends AvailableRouterMethod = 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod, + Method extends AvailableRouterMethod = ResT extends void ? 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod : AvailableRouterMethod, _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf @@ -136,7 +136,7 @@ export function useLazyFetch< ResT = void, ErrorT = FetchError, ReqT extends NitroFetchRequest = NitroFetchRequest, - Method extends AvailableRouterMethod = 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod, + Method extends AvailableRouterMethod = ResT extends void ? 'get' extends AvailableRouterMethod ? 'get' : AvailableRouterMethod : AvailableRouterMethod, _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf diff --git a/test/fixtures/basic/server/api/hey/index.post.ts b/test/fixtures/basic/server/api/hey/index.post.ts new file mode 100644 index 0000000000..b09599aed3 --- /dev/null +++ b/test/fixtures/basic/server/api/hey/index.post.ts @@ -0,0 +1,3 @@ +export default defineEventHandler(() => ({ + method: 'post' as const +})) diff --git a/test/fixtures/basic/types.ts b/test/fixtures/basic/types.ts index 4caf8a256f..1b61186a60 100644 --- a/test/fixtures/basic/types.ts +++ b/test/fixtures/basic/types.ts @@ -17,6 +17,7 @@ describe('API routes', () => { expectTypeOf($fetch('/api/hello')).toEqualTypeOf>() expectTypeOf($fetch('/api/hey')).toEqualTypeOf>() expectTypeOf($fetch('/api/hey', { method: 'get' })).toEqualTypeOf>() + expectTypeOf($fetch('/api/hey', { method: 'post' })).toEqualTypeOf>() // @ts-expect-error not a valid method expectTypeOf($fetch('/api/hey', { method: 'patch ' })).toEqualTypeOf>() expectTypeOf($fetch('/api/union')).toEqualTypeOf>() @@ -51,8 +52,10 @@ describe('API routes', () => { it('works with useFetch', () => { expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf>() expectTypeOf(useFetch('/api/hey').data).toEqualTypeOf>() + // @ts-expect-error TODO: remove when fixed upstream: https://github.com/unjs/nitro/pull/1247 expectTypeOf(useFetch('/api/hey', { method: 'GET' }).data).toEqualTypeOf>() expectTypeOf(useFetch('/api/hey', { method: 'get' }).data).toEqualTypeOf>() + expectTypeOf(useFetch('/api/hey', { method: 'post' }).data).toEqualTypeOf>() // @ts-expect-error not a valid method useFetch('/api/hey', { method: 'PATCH' }) expectTypeOf(useFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf>() @@ -60,6 +63,7 @@ describe('API routes', () => { expectTypeOf(useFetch('/api/union', { pick: ['type'] }).data).toEqualTypeOf>() expectTypeOf(useFetch('/api/other').data).toEqualTypeOf>() expectTypeOf(useFetch('/test').data).toEqualTypeOf>() + expectTypeOf(useFetch('/test', { method: 'POST' }).data).toEqualTypeOf>() expectTypeOf(useFetch('/error').error).toEqualTypeOf>() expectTypeOf(useFetch('/error').error).toEqualTypeOf>()