mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
fix: asyncData default types
This commit is contained in:
parent
b74d41e150
commit
dfdc4af714
@ -90,28 +90,6 @@ const { data: modules } = await useAPI('/modules')
|
||||
</script>
|
||||
```
|
||||
|
||||
If you want to customize the type of any error returned, you can also do so:
|
||||
|
||||
```ts
|
||||
import type { FetchError } from 'ofetch'
|
||||
import type { UseFetchOptions } from 'nuxt/app'
|
||||
|
||||
interface CustomError {
|
||||
message: string
|
||||
statusCode: number
|
||||
}
|
||||
|
||||
export function useAPI<T>(
|
||||
url: string | (() => string),
|
||||
options?: UseFetchOptions<T>,
|
||||
) {
|
||||
return useFetch<T, FetchError<CustomError>>(url, {
|
||||
...options,
|
||||
$fetch: useNuxtApp().$api
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
::note
|
||||
This example demonstrates how to use a custom `useFetch`, but the same structure is identical for a custom `useAsyncData`.
|
||||
::
|
||||
|
@ -42,7 +42,7 @@ export interface AsyncDataOptions<
|
||||
ResT,
|
||||
DataT = ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> {
|
||||
/**
|
||||
* Whether to fetch on the server side.
|
||||
@ -131,7 +131,7 @@ export function useAsyncData<
|
||||
NuxtErrorDataT = unknown,
|
||||
DataT = ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
||||
@ -164,7 +164,7 @@ export function useAsyncData<
|
||||
NuxtErrorDataT = unknown,
|
||||
DataT = ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
key: string,
|
||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||
@ -193,7 +193,7 @@ export function useAsyncData<
|
||||
NuxtErrorDataT = unknown,
|
||||
DataT = ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (...args: any[]): AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | 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<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||
options?: Omit<AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, 'lazy'>
|
||||
@ -425,7 +425,7 @@ export function useLazyAsyncData<
|
||||
DataE = Error,
|
||||
DataT = ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
key: string,
|
||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||
@ -448,7 +448,7 @@ export function useLazyAsyncData<
|
||||
DataE = Error,
|
||||
DataT = ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (...args: any[]): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, DataE | undefined> {
|
||||
const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined
|
||||
if (typeof args[0] !== 'string') { args.unshift(autoKey) }
|
||||
|
@ -31,7 +31,7 @@ export interface UseFetchOptions<
|
||||
ResT,
|
||||
DataT = ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
R extends NitroFetchRequest = string & {},
|
||||
M extends AvailableRouterMethod<R> = AvailableRouterMethod<R>,
|
||||
> extends Omit<AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, 'watch'>, ComputedFetchOptions<R, M> {
|
||||
@ -55,7 +55,7 @@ export function useFetch<
|
||||
_ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT,
|
||||
DataT = _ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||
opts?: UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>
|
||||
@ -87,7 +87,7 @@ export function useFetch<
|
||||
_ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT,
|
||||
DataT = _ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||
arg1?: string | UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>,
|
||||
@ -193,7 +193,7 @@ export function useLazyFetch<
|
||||
_ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT,
|
||||
DataT = _ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||
opts?: Omit<UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>, 'lazy'>
|
||||
@ -219,7 +219,7 @@ export function useLazyFetch<
|
||||
_ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT,
|
||||
DataT = _ResT,
|
||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||
DefaultT = undefined,
|
||||
DefaultT = DataT | undefined,
|
||||
> (
|
||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||
arg1?: string | Omit<UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>, 'lazy'>,
|
||||
|
14
playground/composables/test.ts
Normal file
14
playground/composables/test.ts
Normal file
@ -0,0 +1,14 @@
|
||||
interface ResT {
|
||||
foo: string[]
|
||||
bar: string[]
|
||||
}
|
||||
|
||||
const { data } = await useFetchCustom<ResT>('/some/endpoint', {
|
||||
default: () => ({
|
||||
foo: [],
|
||||
bar: [],
|
||||
}),
|
||||
})
|
||||
if (data.value) {
|
||||
const a = data.value
|
||||
}
|
11
playground/composables/useFetchCustom.ts
Normal file
11
playground/composables/useFetchCustom.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import type { UseFetchOptions } from 'nuxt/app'
|
||||
|
||||
export function useFetchCustom<T> (
|
||||
url: string | (() => string),
|
||||
options?: UseFetchOptions<T>,
|
||||
) {
|
||||
return useFetch(url, {
|
||||
...options,
|
||||
$fetch: useNuxtApp().$customFetch as typeof $fetch,
|
||||
})
|
||||
}
|
11
playground/plugins/fetchCustom.ts
Normal file
11
playground/plugins/fetchCustom.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export default defineNuxtPlugin(() => {
|
||||
const fetchCustom = $fetch.create({
|
||||
baseURL: 'https://this-doesnt-matter.com',
|
||||
})
|
||||
|
||||
return {
|
||||
provide: {
|
||||
fetchCustom,
|
||||
},
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue
Block a user