mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): use more specific FetchError
for useFetch
errors (#7435)
This commit is contained in:
parent
068e127551
commit
563cf7c1c9
@ -1,4 +1,4 @@
|
||||
import type { FetchOptions } from 'ohmyfetch'
|
||||
import type { FetchError, FetchOptions } from 'ohmyfetch'
|
||||
import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack'
|
||||
import { computed, isRef, Ref } from 'vue'
|
||||
import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData'
|
||||
@ -16,7 +16,7 @@ export interface UseFetchOptions<
|
||||
|
||||
export function useFetch<
|
||||
ResT = void,
|
||||
ErrorT = Error,
|
||||
ErrorT = FetchError,
|
||||
ReqT extends NitroFetchRequest = NitroFetchRequest,
|
||||
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
|
||||
Transform extends (res: _ResT) => any = (res: _ResT) => _ResT,
|
||||
@ -27,7 +27,7 @@ export function useFetch<
|
||||
): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, ErrorT | null | true>
|
||||
export function useFetch<
|
||||
ResT = void,
|
||||
ErrorT = Error,
|
||||
ErrorT = FetchError,
|
||||
ReqT extends NitroFetchRequest = NitroFetchRequest,
|
||||
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
|
||||
Transform extends (res: _ResT) => any = (res: _ResT) => _ResT,
|
||||
@ -93,7 +93,7 @@ export function useFetch<
|
||||
|
||||
export function useLazyFetch<
|
||||
ResT = void,
|
||||
ErrorT = Error,
|
||||
ErrorT = FetchError,
|
||||
ReqT extends NitroFetchRequest = NitroFetchRequest,
|
||||
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
|
||||
Transform extends (res: _ResT) => any = (res: _ResT) => _ResT,
|
||||
@ -104,7 +104,7 @@ export function useLazyFetch<
|
||||
): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, ErrorT | null | true>
|
||||
export function useLazyFetch<
|
||||
ResT = void,
|
||||
ErrorT = Error,
|
||||
ErrorT = FetchError,
|
||||
ReqT extends NitroFetchRequest = NitroFetchRequest,
|
||||
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
|
||||
Transform extends (res: _ResT) => any = (res: _ResT) => _ResT,
|
||||
|
5
test/fixtures/basic/types.ts
vendored
5
test/fixtures/basic/types.ts
vendored
@ -3,6 +3,7 @@ import { describe, it } from 'vitest'
|
||||
import type { Ref } from 'vue'
|
||||
import type { AppConfig } from '@nuxt/schema'
|
||||
|
||||
import type { FetchError } from 'ohmyfetch'
|
||||
import { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router'
|
||||
import { defineNuxtConfig } from '~~/../../../packages/nuxt/src'
|
||||
import type { NavigateToOptions } from '~~/../../../packages/nuxt/dist/app/composables/router'
|
||||
@ -47,7 +48,7 @@ describe('API routes', () => {
|
||||
expectTypeOf(useFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
||||
expectTypeOf(useFetch<TestResponse>('/test').data).toEqualTypeOf<Ref<TestResponse>>()
|
||||
|
||||
expectTypeOf(useFetch('/error').error).toEqualTypeOf<Ref<Error | null | true>>()
|
||||
expectTypeOf(useFetch('/error').error).toEqualTypeOf<Ref<FetchError | null | true>>()
|
||||
expectTypeOf(useFetch<any, string>('/error').error).toEqualTypeOf<Ref<string | null | true>>()
|
||||
|
||||
expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf<Ref<string>>()
|
||||
@ -57,7 +58,7 @@ describe('API routes', () => {
|
||||
expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
||||
expectTypeOf(useLazyFetch<TestResponse>('/test').data).toEqualTypeOf<Ref<TestResponse>>()
|
||||
|
||||
expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf<Ref<Error | null | true>>()
|
||||
expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf<Ref<FetchError | null | true>>()
|
||||
expectTypeOf(useLazyFetch<any, string>('/error').error).toEqualTypeOf<Ref<string | null | true>>()
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user