From d405c9a19098e008554ada32bd6945f43a53e0f1 Mon Sep 17 00:00:00 2001 From: xjccc <546534045@qq.com> Date: Mon, 2 Dec 2024 11:35:33 +0800 Subject: [PATCH] test: add custom fetch test --- .../basic-types/composables/useFetchCustom.ts | 25 +++++++++++++++ .../basic-types/plugins/fetchCustom.ts | 11 +++++++ test/fixtures/basic-types/types.ts | 32 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 test/fixtures/basic-types/composables/useFetchCustom.ts create mode 100644 test/fixtures/basic-types/plugins/fetchCustom.ts diff --git a/test/fixtures/basic-types/composables/useFetchCustom.ts b/test/fixtures/basic-types/composables/useFetchCustom.ts new file mode 100644 index 0000000000..b02961a58e --- /dev/null +++ b/test/fixtures/basic-types/composables/useFetchCustom.ts @@ -0,0 +1,25 @@ +import type { AsyncDataOptions, UseFetchOptions } from 'nuxt/app' +import type { NitroFetchRequest } from 'nitro/types' + +interface CustomError { + code: string + message: string +} +export function useFetchCustom ( + url: NitroFetchRequest, + options?: UseFetchOptions, +) { + return useFetch(url, { + ...options, + $fetch: useNuxtApp().$customFetch as typeof $fetch, + }) +} + +export function useAsyncFetchCustom ( + url: NitroFetchRequest, + options?: AsyncDataOptions, +) { + return useAsyncData(() => $fetch(url as string), { + ...options, + }) +} diff --git a/test/fixtures/basic-types/plugins/fetchCustom.ts b/test/fixtures/basic-types/plugins/fetchCustom.ts new file mode 100644 index 0000000000..340e0d37f4 --- /dev/null +++ b/test/fixtures/basic-types/plugins/fetchCustom.ts @@ -0,0 +1,11 @@ +export default defineNuxtPlugin(() => { + const fetchCustom = $fetch.create({ + baseURL: '', + }) + + return { + provide: { + fetchCustom, + }, + } +}) diff --git a/test/fixtures/basic-types/types.ts b/test/fixtures/basic-types/types.ts index 3887cf5b45..2141352c66 100644 --- a/test/fixtures/basic-types/types.ts +++ b/test/fixtures/basic-types/types.ts @@ -6,6 +6,7 @@ import type { NavigationFailure, RouteLocationNormalized, RouteLocationRaw, Rout import type { AppConfig, RuntimeValue, UpperSnakeCase } from 'nuxt/schema' import { defineNuxtModule } from 'nuxt/kit' import { defineNuxtConfig } from 'nuxt/config' +import { useAsyncFetchCustom, useFetchCustom } from './composables/useFetchCustom' import { callWithNuxt, isVue3 } from '#app' import type { NuxtError } from '#app' import type { NavigateToOptions } from '#app/composables/router' @@ -470,14 +471,45 @@ describe('composables', () => { }) it('correct types when using custom error type', () => { + interface ResT { + foo: string + baz: string + } interface CustomError { + message: string + code: string + } + + interface OtherCustomError { message: string code: number } + expectTypeOf(useFetch('/test').error).toEqualTypeOf>() expectTypeOf(useLazyFetch('/test').error).toEqualTypeOf>() expectTypeOf(useAsyncData('custom-error-type', () => $fetch('/error')).error).toEqualTypeOf>() expectTypeOf(useLazyAsyncData('custom-error-type', () => $fetch('/error')).error).toEqualTypeOf>() + expectTypeOf(useLazyAsyncData('custom-error-type', () => $fetch('/error')).error).toEqualTypeOf>() + + expectTypeOf(useFetchCustom('/api/hey').data).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hey', { default: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hey', { transform: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hello').data).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hello', { default: () => 'default' }).data).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hello', { default: () => 'default', transform: () => 'transform' }).data).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hello', { transform: () => 'transform' }).data).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hello').error).toEqualTypeOf>() + expectTypeOf(useFetchCustom('/api/hello').error).toEqualTypeOf>() + + expectTypeOf(useAsyncFetchCustom('/api/hey').data).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hey', { default: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hey', { transform: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hello').data).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hello', { default: () => 'default' }).data).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hello', { default: () => 'default', transform: () => 'transform' }).data).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hello', { transform: () => 'transform' }).data).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hello').error).toEqualTypeOf>() + expectTypeOf(useAsyncFetchCustom('/api/hello').error).toEqualTypeOf>() }) it('supports asynchronous transform', () => {