From 791f7509742584adee30c6794b69ae429b479393 Mon Sep 17 00:00:00 2001 From: xjccc <546534045@qq.com> Date: Wed, 27 Nov 2024 18:09:58 +0800 Subject: [PATCH] feat: options add type DataT --- packages/nuxt/src/app/composables/asyncData.ts | 12 ++++++------ packages/nuxt/src/app/composables/fetch.ts | 8 ++++---- playground/composables/test.ts | 14 -------------- playground/composables/useFetchCustom.ts | 11 ----------- playground/plugins/fetchCustom.ts | 11 ----------- 5 files changed, 10 insertions(+), 46 deletions(-) delete mode 100644 playground/composables/test.ts delete mode 100644 playground/composables/useFetchCustom.ts delete mode 100644 playground/plugins/fetchCustom.ts diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 27399fc96b..8ad7f0905f 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -131,7 +131,7 @@ export function useAsyncData< NuxtErrorDataT = unknown, DataT = ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( handler: (ctx?: NuxtApp) => Promise, options?: AsyncDataOptions @@ -164,7 +164,7 @@ export function useAsyncData< NuxtErrorDataT = unknown, DataT = ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( key: string, handler: (ctx?: NuxtApp) => Promise, @@ -193,7 +193,7 @@ export function useAsyncData< NuxtErrorDataT = unknown, DataT = ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > (...args: any[]): AsyncData, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | undefined> { const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined if (typeof args[0] !== 'string') { args.unshift(autoKey) } @@ -405,7 +405,7 @@ export function useLazyAsyncData< DataE = Error, DataT = ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( handler: (ctx?: NuxtApp) => Promise, options?: Omit, 'lazy'> @@ -425,7 +425,7 @@ export function useLazyAsyncData< DataE = Error, DataT = ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( key: string, handler: (ctx?: NuxtApp) => Promise, @@ -448,7 +448,7 @@ export function useLazyAsyncData< DataE = Error, DataT = ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > (...args: any[]): AsyncData | DefaultT, DataE | undefined> { const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined if (typeof args[0] !== 'string') { args.unshift(autoKey) } diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 3ffa4709b2..c5b9628ad7 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -55,7 +55,7 @@ export function useFetch< _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( request: Ref | ReqT | (() => ReqT), opts?: UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method> @@ -87,7 +87,7 @@ export function useFetch< _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( request: Ref | ReqT | (() => ReqT), arg1?: string | UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>, @@ -193,7 +193,7 @@ export function useLazyFetch< _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( request: Ref | ReqT | (() => ReqT), opts?: Omit, 'lazy'> @@ -219,7 +219,7 @@ export function useLazyFetch< _ResT = ResT extends void ? FetchResult : ResT, DataT = _ResT, PickKeys extends KeysOf = KeysOf, - DefaultT = DataT | undefined, + DefaultT = undefined, > ( request: Ref | ReqT | (() => ReqT), arg1?: string | Omit, 'lazy'>, diff --git a/playground/composables/test.ts b/playground/composables/test.ts deleted file mode 100644 index cf920f9f3c..0000000000 --- a/playground/composables/test.ts +++ /dev/null @@ -1,14 +0,0 @@ -interface ResT { - foo: string[] - bar: string[] -} - -const { data } = await useFetchCustom('/some/endpoint', { - default: () => ({ - foo: [], - bar: [], - }), -}) -if (data.value) { - const a = data.value -} diff --git a/playground/composables/useFetchCustom.ts b/playground/composables/useFetchCustom.ts deleted file mode 100644 index 7add2deca0..0000000000 --- a/playground/composables/useFetchCustom.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { UseFetchOptions } from 'nuxt/app' - -export function useFetchCustom ( - url: string | (() => string), - options?: UseFetchOptions, -) { - return useFetch(url, { - ...options, - $fetch: useNuxtApp().$customFetch as typeof $fetch, - }) -} diff --git a/playground/plugins/fetchCustom.ts b/playground/plugins/fetchCustom.ts deleted file mode 100644 index 2e208c1a62..0000000000 --- a/playground/plugins/fetchCustom.ts +++ /dev/null @@ -1,11 +0,0 @@ -export default defineNuxtPlugin(() => { - const fetchCustom = $fetch.create({ - baseURL: 'https://this-doesnt-matter.com', - }) - - return { - provide: { - fetchCustom, - }, - } -})