From 563cf7c1c96a8ade6cd09810f47ac39571296780 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 12 Sep 2022 10:23:19 +0100 Subject: [PATCH] fix(nuxt): use more specific `FetchError` for `useFetch` errors (#7435) --- packages/nuxt/src/app/composables/fetch.ts | 10 +++++----- test/fixtures/basic/types.ts | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index a1e8132cdc..f1edcdaeef 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -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 : ResT, Transform extends (res: _ResT) => any = (res: _ResT) => _ResT, @@ -27,7 +27,7 @@ export function useFetch< ): AsyncData, PickKeys>, ErrorT | null | true> export function useFetch< ResT = void, - ErrorT = Error, + ErrorT = FetchError, ReqT extends NitroFetchRequest = NitroFetchRequest, _ResT = ResT extends void ? FetchResult : 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 : ResT, Transform extends (res: _ResT) => any = (res: _ResT) => _ResT, @@ -104,7 +104,7 @@ export function useLazyFetch< ): AsyncData, PickKeys>, ErrorT | null | true> export function useLazyFetch< ResT = void, - ErrorT = Error, + ErrorT = FetchError, ReqT extends NitroFetchRequest = NitroFetchRequest, _ResT = ResT extends void ? FetchResult : ResT, Transform extends (res: _ResT) => any = (res: _ResT) => _ResT, diff --git a/test/fixtures/basic/types.ts b/test/fixtures/basic/types.ts index 5fc5894e9a..07dab0835e 100644 --- a/test/fixtures/basic/types.ts +++ b/test/fixtures/basic/types.ts @@ -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>() expectTypeOf(useFetch('/test').data).toEqualTypeOf>() - expectTypeOf(useFetch('/error').error).toEqualTypeOf>() + expectTypeOf(useFetch('/error').error).toEqualTypeOf>() expectTypeOf(useFetch('/error').error).toEqualTypeOf>() expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf>() @@ -57,7 +58,7 @@ describe('API routes', () => { expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf>() expectTypeOf(useLazyFetch('/test').data).toEqualTypeOf>() - expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf>() + expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf>() expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf>() }) })