From 17461fb03bf7c0c165c7c3f5940eaa8730769d40 Mon Sep 17 00:00:00 2001 From: anhao <117057608+ah-dc@users.noreply.github.com> Date: Tue, 1 Aug 2023 06:56:37 +0800 Subject: [PATCH] fix(nuxt): support uppercase route methods in `useFetch` (#22418) --- packages/nuxt/src/app/composables/fetch.ts | 15 +++++++++++---- test/fixtures/basic-types/types.ts | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/composables/fetch.ts b/packages/nuxt/src/app/composables/fetch.ts index 3ca36e005a..a2c84fbc5e 100644 --- a/packages/nuxt/src/app/composables/fetch.ts +++ b/packages/nuxt/src/app/composables/fetch.ts @@ -1,18 +1,25 @@ -import type { FetchError } from 'ofetch' -import type { AvailableRouterMethod, NitroFetchOptions, NitroFetchRequest, TypedInternalResponse } from 'nitropack' +import type { FetchError, FetchOptions } from 'ofetch' +import type { NitroFetchRequest, TypedInternalResponse, AvailableRouterMethod as _AvailableRouterMethod } from 'nitropack' import type { Ref } from 'vue' import { computed, reactive, unref } from 'vue' import { hash } from 'ohash' import { useRequestFetch } from './ssr' -import type { AsyncData, AsyncDataOptions, KeysOf, MultiWatchSources, PickFrom, _Transform } from './asyncData' +import type { AsyncData, AsyncDataOptions, KeysOf, MultiWatchSources, PickFrom } from './asyncData' import { useAsyncData } from './asyncData' -export type FetchResult> = TypedInternalResponse +// support uppercase methods, detail: https://github.com/nuxt/nuxt/issues/22313 +type AvailableRouterMethod = _AvailableRouterMethod | Uppercase<_AvailableRouterMethod> + +export type FetchResult> = TypedInternalResponse> type ComputedOptions> = { [K in keyof T]: T[K] extends Function ? T[K] : T[K] extends Record ? ComputedOptions | Ref | T[K] : Ref | T[K] } +interface NitroFetchOptions = AvailableRouterMethod> extends FetchOptions { + method?: M; +} + type ComputedFetchOptions> = ComputedOptions> export interface UseFetchOptions< diff --git a/test/fixtures/basic-types/types.ts b/test/fixtures/basic-types/types.ts index c0b3f28d55..d0a46e66fa 100644 --- a/test/fixtures/basic-types/types.ts +++ b/test/fixtures/basic-types/types.ts @@ -56,9 +56,9 @@ describe('API routes', () => { it('works with useFetch', () => { expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf>() expectTypeOf(useFetch('/api/hey').data).toEqualTypeOf>() - // @ts-expect-error TODO: remove when fixed upstream: https://github.com/unjs/nitro/pull/1247 expectTypeOf(useFetch('/api/hey', { method: 'GET' }).data).toEqualTypeOf>() expectTypeOf(useFetch('/api/hey', { method: 'get' }).data).toEqualTypeOf>() + expectTypeOf(useFetch('/api/hey', { method: 'POST' }).data).toEqualTypeOf>() expectTypeOf(useFetch('/api/hey', { method: 'post' }).data).toEqualTypeOf>() // @ts-expect-error not a valid method useFetch('/api/hey', { method: 'PATCH' })