mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 09:25:54 +00:00
test: add custom fetch test
This commit is contained in:
parent
2a239ef52d
commit
d405c9a190
25
test/fixtures/basic-types/composables/useFetchCustom.ts
vendored
Normal file
25
test/fixtures/basic-types/composables/useFetchCustom.ts
vendored
Normal file
@ -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<T, E = CustomError> (
|
||||
url: NitroFetchRequest,
|
||||
options?: UseFetchOptions<T>,
|
||||
) {
|
||||
return useFetch<T, E>(url, {
|
||||
...options,
|
||||
$fetch: useNuxtApp().$customFetch as typeof $fetch,
|
||||
})
|
||||
}
|
||||
|
||||
export function useAsyncFetchCustom<T, E = CustomError> (
|
||||
url: NitroFetchRequest,
|
||||
options?: AsyncDataOptions<T>,
|
||||
) {
|
||||
return useAsyncData<T, E>(() => $fetch(url as string), {
|
||||
...options,
|
||||
})
|
||||
}
|
11
test/fixtures/basic-types/plugins/fetchCustom.ts
vendored
Normal file
11
test/fixtures/basic-types/plugins/fetchCustom.ts
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
export default defineNuxtPlugin(() => {
|
||||
const fetchCustom = $fetch.create({
|
||||
baseURL: '',
|
||||
})
|
||||
|
||||
return {
|
||||
provide: {
|
||||
fetchCustom,
|
||||
},
|
||||
}
|
||||
})
|
32
test/fixtures/basic-types/types.ts
vendored
32
test/fixtures/basic-types/types.ts
vendored
@ -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<string, CustomError>('/test').error).toEqualTypeOf<Ref<CustomError | DefaultAsyncDataValue>>()
|
||||
expectTypeOf(useLazyFetch<string, CustomError>('/test').error).toEqualTypeOf<Ref<CustomError | DefaultAsyncDataValue>>()
|
||||
expectTypeOf(useAsyncData<string, CustomError>('custom-error-type', () => $fetch('/error')).error).toEqualTypeOf<Ref<CustomError | DefaultAsyncDataValue>>()
|
||||
expectTypeOf(useLazyAsyncData<string, CustomError>('custom-error-type', () => $fetch('/error')).error).toEqualTypeOf<Ref<CustomError | DefaultAsyncDataValue>>()
|
||||
expectTypeOf(useLazyAsyncData<string, OtherCustomError>('custom-error-type', () => $fetch('/error')).error).toEqualTypeOf<Ref<OtherCustomError | DefaultAsyncDataValue>>()
|
||||
|
||||
expectTypeOf(useFetchCustom<ResT>('/api/hey').data).toEqualTypeOf<Ref<ResT>>()
|
||||
expectTypeOf(useFetchCustom('/api/hey', { default: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf<Ref<ResT>>()
|
||||
expectTypeOf(useFetchCustom('/api/hey', { transform: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf<Ref<ResT>>()
|
||||
expectTypeOf(useFetchCustom('/api/hello').data).toEqualTypeOf<Ref<unknown>>()
|
||||
expectTypeOf(useFetchCustom('/api/hello', { default: () => 'default' }).data).toEqualTypeOf<Ref<string>>()
|
||||
expectTypeOf(useFetchCustom('/api/hello', { default: () => 'default', transform: () => 'transform' }).data).toEqualTypeOf<Ref<string>>()
|
||||
expectTypeOf(useFetchCustom<string>('/api/hello', { transform: () => 'transform' }).data).toEqualTypeOf<Ref<string>>()
|
||||
expectTypeOf(useFetchCustom('/api/hello').error).toEqualTypeOf<Ref<CustomError | DefaultAsyncDataValue>>()
|
||||
expectTypeOf(useFetchCustom<string, OtherCustomError>('/api/hello').error).toEqualTypeOf<Ref<OtherCustomError | DefaultAsyncDataValue>>()
|
||||
|
||||
expectTypeOf(useAsyncFetchCustom<ResT>('/api/hey').data).toEqualTypeOf<Ref<ResT>>()
|
||||
expectTypeOf(useAsyncFetchCustom('/api/hey', { default: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf<Ref<ResT>>()
|
||||
expectTypeOf(useAsyncFetchCustom('/api/hey', { transform: () => ({ foo: 'bar', baz: 'baz' }) }).data).toEqualTypeOf<Ref<ResT>>()
|
||||
expectTypeOf(useAsyncFetchCustom('/api/hello').data).toEqualTypeOf<Ref<unknown>>()
|
||||
expectTypeOf(useAsyncFetchCustom('/api/hello', { default: () => 'default' }).data).toEqualTypeOf<Ref<string>>()
|
||||
expectTypeOf(useAsyncFetchCustom('/api/hello', { default: () => 'default', transform: () => 'transform' }).data).toEqualTypeOf<Ref<string>>()
|
||||
expectTypeOf(useAsyncFetchCustom<string>('/api/hello', { transform: () => 'transform' }).data).toEqualTypeOf<Ref<string>>()
|
||||
expectTypeOf(useAsyncFetchCustom('/api/hello').error).toEqualTypeOf<Ref<CustomError | DefaultAsyncDataValue>>()
|
||||
expectTypeOf(useAsyncFetchCustom<string, OtherCustomError>('/api/hello').error).toEqualTypeOf<Ref<OtherCustomError | DefaultAsyncDataValue>>()
|
||||
})
|
||||
|
||||
it('supports asynchronous transform', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user