mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-04 19:37:18 +00:00
fix(nuxt): correct return type of useRequestFetch
(#30117)
This commit is contained in:
parent
37ce2b4f4b
commit
7b9f4b8dd9
@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
|
||||
import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders, getResponseHeader, removeResponseHeader, setResponseHeader } from 'h3'
|
||||
import { computed, getCurrentInstance, ref } from 'vue'
|
||||
import { useServerHead } from '@unhead/vue'
|
||||
import type { H3Event$Fetch } from 'nitro/types'
|
||||
|
||||
import type { NuxtApp } from '../nuxt'
|
||||
import { useNuxtApp } from '../nuxt'
|
||||
@ -39,11 +40,11 @@ export function useRequestHeader (header: string) {
|
||||
}
|
||||
|
||||
/** @since 3.2.0 */
|
||||
export function useRequestFetch (): typeof global.$fetch {
|
||||
export function useRequestFetch (): H3Event$Fetch | typeof global.$fetch {
|
||||
if (import.meta.client) {
|
||||
return globalThis.$fetch
|
||||
}
|
||||
return useRequestEvent()?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
|
||||
return useRequestEvent()?.$fetch || globalThis.$fetch
|
||||
}
|
||||
|
||||
/** @since 3.0.0 */
|
||||
|
17
test/fixtures/basic-types/types.ts
vendored
17
test/fixtures/basic-types/types.ts
vendored
@ -34,6 +34,23 @@ describe('API routes', () => {
|
||||
expectTypeOf($fetch<TestResponse>('/test')).toEqualTypeOf<Promise<TestResponse>>()
|
||||
})
|
||||
|
||||
it('works with useRequestFetch', () => {
|
||||
const $fetch = useRequestFetch()
|
||||
expectTypeOf($fetch('/api/hello')).toEqualTypeOf<Promise<string>>()
|
||||
// registered in extends
|
||||
expectTypeOf($fetch('/api/foo')).toEqualTypeOf<Promise<string>>()
|
||||
// registered in module
|
||||
expectTypeOf($fetch('/auto-registered-module')).toEqualTypeOf<Promise<string>>()
|
||||
expectTypeOf($fetch('/api/hey')).toEqualTypeOf<Promise<{ foo: string, baz: string }>>()
|
||||
expectTypeOf($fetch('/api/hey', { method: 'get' })).toEqualTypeOf<Promise<{ foo: string, baz: string }>>()
|
||||
expectTypeOf($fetch('/api/hey', { method: 'post' })).toEqualTypeOf<Promise<{ method: 'post' }>>()
|
||||
// @ts-expect-error not a valid method
|
||||
expectTypeOf($fetch('/api/hey', { method: 'patch ' })).toEqualTypeOf<Promise<{ foo: string, baz: string }>>()
|
||||
expectTypeOf($fetch('/api/union')).toEqualTypeOf<Promise<{ type: 'a', foo: string } | { type: 'b', baz: string }>>()
|
||||
expectTypeOf($fetch('/api/other')).toEqualTypeOf<Promise<unknown>>()
|
||||
expectTypeOf($fetch<TestResponse>('/test')).toEqualTypeOf<Promise<TestResponse>>()
|
||||
})
|
||||
|
||||
it('works with useAsyncData', () => {
|
||||
expectTypeOf(useAsyncData('api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf<Ref<string | DefaultAsyncDataValue>>()
|
||||
expectTypeOf(useAsyncData('api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf<Ref<{ foo: string, baz: string } | DefaultAsyncDataValue>>()
|
||||
|
Loading…
Reference in New Issue
Block a user