mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-22 08:29:46 +00:00
Compare commits
10 Commits
b8a2d3485f
...
6db0e3cc69
Author | SHA1 | Date | |
---|---|---|---|
|
6db0e3cc69 | ||
|
160efe7a7f | ||
|
323745d0d4 | ||
|
d405c9a190 | ||
|
2a239ef52d | ||
|
d4cab63e04 | ||
|
b04bf764e3 | ||
|
39e36b2981 | ||
|
791f750974 | ||
|
dfdc4af714 |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -236,7 +236,7 @@ jobs:
|
|||||||
path: packages
|
path: packages
|
||||||
|
|
||||||
- name: Run benchmarks
|
- name: Run benchmarks
|
||||||
uses: CodSpeedHQ/action@da7c57859a7a565a3a92789ac64c41aca031ca1f # v3.3.0
|
uses: CodSpeedHQ/action@1015f4f828ff74b7a950909897fe581d6ba868cc # v3.3.1
|
||||||
with:
|
with:
|
||||||
run: pnpm vitest bench
|
run: pnpm vitest bench
|
||||||
token: ${{ secrets.CODSPEED_TOKEN }}
|
token: ${{ secrets.CODSPEED_TOKEN }}
|
||||||
|
@ -69,10 +69,11 @@ Wrapping with [`useAsyncData`](/docs/api/composables/use-async-data) **avoid dou
|
|||||||
Now that `$api` has the logic we want, let's create a `useAPI` composable to replace the usage of `useAsyncData` + `$api`:
|
Now that `$api` has the logic we want, let's create a `useAPI` composable to replace the usage of `useAsyncData` + `$api`:
|
||||||
|
|
||||||
```ts [composables/useAPI.ts]
|
```ts [composables/useAPI.ts]
|
||||||
|
import type { NitroFetchRequest } from 'nitropack'
|
||||||
import type { UseFetchOptions } from 'nuxt/app'
|
import type { UseFetchOptions } from 'nuxt/app'
|
||||||
|
|
||||||
export function useAPI<T>(
|
export function useAPI<T>(
|
||||||
url: string | (() => string),
|
url: NitroFetchRequest,
|
||||||
options?: UseFetchOptions<T>,
|
options?: UseFetchOptions<T>,
|
||||||
) {
|
) {
|
||||||
return useFetch(url, {
|
return useFetch(url, {
|
||||||
@ -92,8 +93,8 @@ const { data: modules } = await useAPI('/modules')
|
|||||||
|
|
||||||
If you want to customize the type of any error returned, you can also do so:
|
If you want to customize the type of any error returned, you can also do so:
|
||||||
|
|
||||||
```ts
|
```ts twoslash
|
||||||
import type { FetchError } from 'ofetch'
|
import type { NitroFetchRequest } from 'nitropack'
|
||||||
import type { UseFetchOptions } from 'nuxt/app'
|
import type { UseFetchOptions } from 'nuxt/app'
|
||||||
|
|
||||||
interface CustomError {
|
interface CustomError {
|
||||||
@ -102,10 +103,10 @@ interface CustomError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useAPI<T>(
|
export function useAPI<T>(
|
||||||
url: string | (() => string),
|
url: NitroFetchRequest,
|
||||||
options?: UseFetchOptions<T>,
|
options?: UseFetchOptions<T>,
|
||||||
) {
|
) {
|
||||||
return useFetch<T, FetchError<CustomError>>(url, {
|
return useFetch<T, CustomError>(url, {
|
||||||
...options,
|
...options,
|
||||||
$fetch: useNuxtApp().$api
|
$fetch: useNuxtApp().$api
|
||||||
})
|
})
|
||||||
|
36
package.json
36
package.json
@ -37,7 +37,7 @@
|
|||||||
"typecheck:docs": "DOCS_TYPECHECK=true pnpm nuxi prepare && nuxt-content-twoslash verify --content-dir docs --languages html"
|
"typecheck:docs": "DOCS_TYPECHECK=true pnpm nuxi prepare && nuxt-content-twoslash verify --content-dir docs --languages html"
|
||||||
},
|
},
|
||||||
"resolutions": {
|
"resolutions": {
|
||||||
"@babel/core": "7.26.8",
|
"@babel/core": "7.26.9",
|
||||||
"@babel/helper-plugin-utils": "7.26.5",
|
"@babel/helper-plugin-utils": "7.26.5",
|
||||||
"@nuxt/cli": "3.21.1",
|
"@nuxt/cli": "3.21.1",
|
||||||
"@nuxt/kit": "workspace:*",
|
"@nuxt/kit": "workspace:*",
|
||||||
@ -45,12 +45,12 @@
|
|||||||
"@nuxt/schema": "workspace:*",
|
"@nuxt/schema": "workspace:*",
|
||||||
"@nuxt/vite-builder": "workspace:*",
|
"@nuxt/vite-builder": "workspace:*",
|
||||||
"@nuxt/webpack-builder": "workspace:*",
|
"@nuxt/webpack-builder": "workspace:*",
|
||||||
"@types/node": "22.13.2",
|
"@types/node": "22.13.4",
|
||||||
"@unhead/dom": "1.11.18",
|
"@unhead/dom": "1.11.19",
|
||||||
"@unhead/schema": "1.11.18",
|
"@unhead/schema": "1.11.19",
|
||||||
"@unhead/shared": "1.11.18",
|
"@unhead/shared": "1.11.19",
|
||||||
"@unhead/ssr": "1.11.18",
|
"@unhead/ssr": "1.11.19",
|
||||||
"@unhead/vue": "1.11.18",
|
"@unhead/vue": "1.11.19",
|
||||||
"@vue/compiler-core": "3.5.13",
|
"@vue/compiler-core": "3.5.13",
|
||||||
"@vue/compiler-dom": "3.5.13",
|
"@vue/compiler-dom": "3.5.13",
|
||||||
"@vue/shared": "3.5.13",
|
"@vue/shared": "3.5.13",
|
||||||
@ -62,19 +62,19 @@
|
|||||||
"nuxt": "workspace:*",
|
"nuxt": "workspace:*",
|
||||||
"ohash": "1.1.4",
|
"ohash": "1.1.4",
|
||||||
"postcss": "8.5.2",
|
"postcss": "8.5.2",
|
||||||
"rollup": "4.34.6",
|
"rollup": "4.34.8",
|
||||||
"send": ">=1.1.0",
|
"send": ">=1.1.0",
|
||||||
"typescript": "5.7.3",
|
"typescript": "5.7.3",
|
||||||
"ufo": "1.5.4",
|
"ufo": "1.5.4",
|
||||||
"unbuild": "3.3.1",
|
"unbuild": "3.3.1",
|
||||||
"unhead": "1.11.18",
|
"unhead": "1.11.19",
|
||||||
"unimport": "4.1.1",
|
"unimport": "4.1.2",
|
||||||
"vite": "6.1.0",
|
"vite": "6.1.0",
|
||||||
"vue": "3.5.13"
|
"vue": "3.5.13"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@arethetypeswrong/cli": "0.17.3",
|
"@arethetypeswrong/cli": "0.17.3",
|
||||||
"@babel/core": "7.26.8",
|
"@babel/core": "7.26.9",
|
||||||
"@babel/helper-plugin-utils": "7.26.5",
|
"@babel/helper-plugin-utils": "7.26.5",
|
||||||
"@codspeed/vitest-plugin": "4.0.0",
|
"@codspeed/vitest-plugin": "4.0.0",
|
||||||
"@nuxt/cli": "3.21.1",
|
"@nuxt/cli": "3.21.1",
|
||||||
@ -86,10 +86,10 @@
|
|||||||
"@testing-library/vue": "8.1.0",
|
"@testing-library/vue": "8.1.0",
|
||||||
"@types/babel__core": "7.20.5",
|
"@types/babel__core": "7.20.5",
|
||||||
"@types/babel__helper-plugin-utils": "7.10.3",
|
"@types/babel__helper-plugin-utils": "7.10.3",
|
||||||
"@types/node": "22.13.2",
|
"@types/node": "22.13.4",
|
||||||
"@types/semver": "7.5.8",
|
"@types/semver": "7.5.8",
|
||||||
"@unhead/schema": "1.11.18",
|
"@unhead/schema": "1.11.19",
|
||||||
"@unhead/vue": "1.11.18",
|
"@unhead/vue": "1.11.19",
|
||||||
"@vitest/coverage-v8": "3.0.5",
|
"@vitest/coverage-v8": "3.0.5",
|
||||||
"@vue/test-utils": "2.4.6",
|
"@vue/test-utils": "2.4.6",
|
||||||
"acorn": "8.14.0",
|
"acorn": "8.14.0",
|
||||||
@ -120,12 +120,12 @@
|
|||||||
"pathe": "2.0.3",
|
"pathe": "2.0.3",
|
||||||
"pkg-pr-new": "0.0.39",
|
"pkg-pr-new": "0.0.39",
|
||||||
"playwright-core": "1.50.1",
|
"playwright-core": "1.50.1",
|
||||||
"rollup": "4.34.6",
|
"rollup": "4.34.8",
|
||||||
"semver": "7.7.1",
|
"semver": "7.7.1",
|
||||||
"sherif": "1.3.0",
|
"sherif": "1.3.0",
|
||||||
"std-env": "3.8.0",
|
"std-env": "3.8.0",
|
||||||
"tinyexec": "0.3.2",
|
"tinyexec": "0.3.2",
|
||||||
"tinyglobby": "0.2.10",
|
"tinyglobby": "0.2.11",
|
||||||
"ts-blank-space": "0.6.0",
|
"ts-blank-space": "0.6.0",
|
||||||
"typescript": "5.7.3",
|
"typescript": "5.7.3",
|
||||||
"ufo": "1.5.4",
|
"ufo": "1.5.4",
|
||||||
@ -133,9 +133,9 @@
|
|||||||
"vitest": "3.0.5",
|
"vitest": "3.0.5",
|
||||||
"vitest-environment-nuxt": "1.0.1",
|
"vitest-environment-nuxt": "1.0.1",
|
||||||
"vue": "3.5.13",
|
"vue": "3.5.13",
|
||||||
"vue-tsc": "2.2.0",
|
"vue-tsc": "2.2.2",
|
||||||
"webpack": "5.98.0"
|
"webpack": "5.98.0"
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@10.3.0",
|
"packageManager": "pnpm@10.4.1",
|
||||||
"version": ""
|
"version": ""
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@
|
|||||||
"std-env": "^3.8.0",
|
"std-env": "^3.8.0",
|
||||||
"ufo": "^1.5.4",
|
"ufo": "^1.5.4",
|
||||||
"unctx": "^2.4.1",
|
"unctx": "^2.4.1",
|
||||||
"unimport": "^4.1.1",
|
"unimport": "^4.1.2",
|
||||||
"untyped": "^1.5.2"
|
"untyped": "^1.5.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -66,15 +66,15 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/cli": "^3.21.1",
|
"@nuxt/cli": "^3.21.1",
|
||||||
"@nuxt/devalue": "^2.0.2",
|
"@nuxt/devalue": "^2.0.2",
|
||||||
"@nuxt/devtools": "^2.0.0",
|
"@nuxt/devtools": "^2.1.0",
|
||||||
"@nuxt/kit": "workspace:*",
|
"@nuxt/kit": "workspace:*",
|
||||||
"@nuxt/schema": "workspace:*",
|
"@nuxt/schema": "workspace:*",
|
||||||
"@nuxt/telemetry": "^2.6.5",
|
"@nuxt/telemetry": "^2.6.5",
|
||||||
"@nuxt/vite-builder": "workspace:*",
|
"@nuxt/vite-builder": "workspace:*",
|
||||||
"@unhead/dom": "^1.11.18",
|
"@unhead/dom": "^1.11.19",
|
||||||
"@unhead/shared": "^1.11.18",
|
"@unhead/shared": "^1.11.19",
|
||||||
"@unhead/ssr": "^1.11.18",
|
"@unhead/ssr": "^1.11.19",
|
||||||
"@unhead/vue": "^1.11.18",
|
"@unhead/vue": "^1.11.19",
|
||||||
"@vue/shared": "^3.5.13",
|
"@vue/shared": "^3.5.13",
|
||||||
"acorn": "8.14.0",
|
"acorn": "8.14.0",
|
||||||
"c12": "^2.0.2",
|
"c12": "^2.0.2",
|
||||||
@ -113,14 +113,14 @@
|
|||||||
"semver": "^7.7.1",
|
"semver": "^7.7.1",
|
||||||
"std-env": "^3.8.0",
|
"std-env": "^3.8.0",
|
||||||
"strip-literal": "^3.0.0",
|
"strip-literal": "^3.0.0",
|
||||||
"tinyglobby": "0.2.10",
|
"tinyglobby": "0.2.11",
|
||||||
"ufo": "^1.5.4",
|
"ufo": "^1.5.4",
|
||||||
"ultrahtml": "^1.5.3",
|
"ultrahtml": "^1.5.3",
|
||||||
"uncrypto": "^0.1.3",
|
"uncrypto": "^0.1.3",
|
||||||
"unctx": "^2.4.1",
|
"unctx": "^2.4.1",
|
||||||
"unenv": "^1.10.0",
|
"unenv": "^1.10.0",
|
||||||
"unhead": "^1.11.18",
|
"unhead": "^1.11.19",
|
||||||
"unimport": "^4.1.1",
|
"unimport": "^4.1.2",
|
||||||
"unplugin": "^2.2.0",
|
"unplugin": "^2.2.0",
|
||||||
"unplugin-vue-router": "^0.11.2",
|
"unplugin-vue-router": "^0.11.2",
|
||||||
"unstorage": "^1.14.4",
|
"unstorage": "^1.14.4",
|
||||||
@ -131,7 +131,7 @@
|
|||||||
"vue-router": "^4.5.0"
|
"vue-router": "^4.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxt/scripts": "0.10.1",
|
"@nuxt/scripts": "0.10.3",
|
||||||
"@parcel/watcher": "2.5.1",
|
"@parcel/watcher": "2.5.1",
|
||||||
"@types/estree": "1.0.6",
|
"@types/estree": "1.0.6",
|
||||||
"@vitejs/plugin-vue": "5.2.1",
|
"@vitejs/plugin-vue": "5.2.1",
|
||||||
|
@ -3,7 +3,6 @@ import type { MultiWatchSources, Ref } from 'vue'
|
|||||||
import type { NuxtApp } from '../nuxt'
|
import type { NuxtApp } from '../nuxt'
|
||||||
import { useNuxtApp } from '../nuxt'
|
import { useNuxtApp } from '../nuxt'
|
||||||
import { toArray } from '../utils'
|
import { toArray } from '../utils'
|
||||||
import type { NuxtError } from './error'
|
|
||||||
import { createError } from './error'
|
import { createError } from './error'
|
||||||
import { onNuxtReady } from './ready'
|
import { onNuxtReady } from './ready'
|
||||||
|
|
||||||
@ -42,7 +41,7 @@ export interface AsyncDataOptions<
|
|||||||
ResT,
|
ResT,
|
||||||
DataT = ResT,
|
DataT = ResT,
|
||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
DefaultT = undefined,
|
DefaultT = DataT,
|
||||||
> {
|
> {
|
||||||
/**
|
/**
|
||||||
* Whether to fetch on the server side.
|
* Whether to fetch on the server side.
|
||||||
@ -68,7 +67,7 @@ export interface AsyncDataOptions<
|
|||||||
* A function that can be used to alter handler function result after resolving.
|
* A function that can be used to alter handler function result after resolving.
|
||||||
* Do not use it along with the `pick` option.
|
* Do not use it along with the `pick` option.
|
||||||
*/
|
*/
|
||||||
transform?: _Transform<ResT, DataT>
|
transform?: _Transform<ResT extends void ? any : ResT, DataT>
|
||||||
/**
|
/**
|
||||||
* Only pick specified keys in this array from the handler function result.
|
* Only pick specified keys in this array from the handler function result.
|
||||||
* Do not use it along with the `transform` option.
|
* Do not use it along with the `transform` option.
|
||||||
@ -128,14 +127,14 @@ export type AsyncData<Data, Error> = _AsyncData<Data, Error> & Promise<_AsyncDat
|
|||||||
*/
|
*/
|
||||||
export function useAsyncData<
|
export function useAsyncData<
|
||||||
ResT,
|
ResT,
|
||||||
NuxtErrorDataT = unknown,
|
NuxtErrorDataT = Error,
|
||||||
DataT = ResT,
|
DataT = ResT,
|
||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
DefaultT = undefined,
|
DefaultT = undefined,
|
||||||
> (
|
> (
|
||||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||||
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, NuxtErrorDataT | undefined>
|
||||||
/**
|
/**
|
||||||
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
||||||
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
||||||
@ -144,14 +143,14 @@ export function useAsyncData<
|
|||||||
*/
|
*/
|
||||||
export function useAsyncData<
|
export function useAsyncData<
|
||||||
ResT,
|
ResT,
|
||||||
NuxtErrorDataT = unknown,
|
NuxtErrorDataT = Error,
|
||||||
DataT = ResT,
|
DataT = ResT,
|
||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
DefaultT = DataT,
|
DefaultT = DataT,
|
||||||
> (
|
> (
|
||||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||||
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, NuxtErrorDataT | undefined>
|
||||||
/**
|
/**
|
||||||
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
||||||
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
||||||
@ -161,7 +160,7 @@ export function useAsyncData<
|
|||||||
*/
|
*/
|
||||||
export function useAsyncData<
|
export function useAsyncData<
|
||||||
ResT,
|
ResT,
|
||||||
NuxtErrorDataT = unknown,
|
NuxtErrorDataT = Error,
|
||||||
DataT = ResT,
|
DataT = ResT,
|
||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
DefaultT = undefined,
|
DefaultT = undefined,
|
||||||
@ -169,7 +168,7 @@ export function useAsyncData<
|
|||||||
key: string,
|
key: string,
|
||||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||||
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, NuxtErrorDataT | undefined>
|
||||||
/**
|
/**
|
||||||
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
||||||
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
||||||
@ -179,7 +178,7 @@ export function useAsyncData<
|
|||||||
*/
|
*/
|
||||||
export function useAsyncData<
|
export function useAsyncData<
|
||||||
ResT,
|
ResT,
|
||||||
NuxtErrorDataT = unknown,
|
NuxtErrorDataT = Error,
|
||||||
DataT = ResT,
|
DataT = ResT,
|
||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
DefaultT = DataT,
|
DefaultT = DataT,
|
||||||
@ -187,14 +186,14 @@ export function useAsyncData<
|
|||||||
key: string,
|
key: string,
|
||||||
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
handler: (ctx?: NuxtApp) => Promise<ResT>,
|
||||||
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
options?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, NuxtErrorDataT | undefined>
|
||||||
export function useAsyncData<
|
export function useAsyncData<
|
||||||
ResT,
|
ResT,
|
||||||
NuxtErrorDataT = unknown,
|
NuxtErrorDataT = Error,
|
||||||
DataT = ResT,
|
DataT = ResT,
|
||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
DefaultT = undefined,
|
DefaultT = undefined,
|
||||||
> (...args: any[]): AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined> {
|
> (...args: any[]): AsyncData<PickFrom<DataT, PickKeys>, NuxtErrorDataT | undefined> {
|
||||||
const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined
|
const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined
|
||||||
if (typeof args[0] !== 'string') { args.unshift(autoKey) }
|
if (typeof args[0] !== 'string') { args.unshift(autoKey) }
|
||||||
|
|
||||||
@ -257,7 +256,7 @@ export function useAsyncData<
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Else, somehow check for conflicting keys with different defaults or fetcher
|
// TODO: Else, somehow check for conflicting keys with different defaults or fetcher
|
||||||
const asyncData = { ...nuxtApp._asyncData[key] } as { _default?: unknown } & AsyncData<DataT | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
const asyncData = { ...nuxtApp._asyncData[key] } as { _default?: unknown } & AsyncData<DataT | DefaultT, NuxtErrorDataT | undefined>
|
||||||
|
|
||||||
// Don't expose default function to end user
|
// Don't expose default function to end user
|
||||||
delete asyncData._default
|
delete asyncData._default
|
||||||
@ -294,7 +293,7 @@ export function useAsyncData<
|
|||||||
|
|
||||||
let result = _result as unknown as DataT
|
let result = _result as unknown as DataT
|
||||||
if (options.transform) {
|
if (options.transform) {
|
||||||
result = await options.transform(_result)
|
result = await options.transform(_result as any)
|
||||||
}
|
}
|
||||||
if (options.pick) {
|
if (options.pick) {
|
||||||
result = pick(result as any, options.pick) as DataT
|
result = pick(result as any, options.pick) as DataT
|
||||||
@ -315,7 +314,7 @@ export function useAsyncData<
|
|||||||
// If this request is cancelled, resolve to the latest request.
|
// If this request is cancelled, resolve to the latest request.
|
||||||
if ((promise as any).cancelled) { return nuxtApp._asyncDataPromises[key] }
|
if ((promise as any).cancelled) { return nuxtApp._asyncDataPromises[key] }
|
||||||
|
|
||||||
asyncData.error.value = createError<NuxtErrorDataT>(error) as (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)
|
asyncData.error.value = createError<NuxtErrorDataT>(error) as NuxtErrorDataT
|
||||||
asyncData.data.value = unref(options.default!())
|
asyncData.data.value = unref(options.default!())
|
||||||
asyncData.status.value = 'error'
|
asyncData.status.value = 'error'
|
||||||
})
|
})
|
||||||
@ -400,10 +399,10 @@ export function useAsyncData<
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Allow directly awaiting on asyncData
|
// Allow directly awaiting on asyncData
|
||||||
const asyncDataPromise = Promise.resolve(nuxtApp._asyncDataPromises[key]).then(() => asyncData) as AsyncData<ResT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
const asyncDataPromise = Promise.resolve(nuxtApp._asyncDataPromises[key]).then(() => asyncData) as AsyncData<ResT, NuxtErrorDataT | undefined>
|
||||||
Object.assign(asyncDataPromise, asyncData)
|
Object.assign(asyncDataPromise, asyncData)
|
||||||
|
|
||||||
return asyncDataPromise as AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
return asyncDataPromise as AsyncData<PickFrom<DataT, PickKeys>, NuxtErrorDataT | undefined>
|
||||||
}
|
}
|
||||||
/** @since 3.0.0 */
|
/** @since 3.0.0 */
|
||||||
export function useLazyAsyncData<
|
export function useLazyAsyncData<
|
||||||
|
@ -29,12 +29,13 @@ type ComputedFetchOptions<R extends NitroFetchRequest, M extends AvailableRouter
|
|||||||
|
|
||||||
export interface UseFetchOptions<
|
export interface UseFetchOptions<
|
||||||
ResT,
|
ResT,
|
||||||
DataT = ResT,
|
ReqT extends NitroFetchRequest = NitroFetchRequest,
|
||||||
|
Method extends AvailableRouterMethod<ReqT> = ResT extends void ? 'get' extends AvailableRouterMethod<ReqT> ? 'get' : AvailableRouterMethod<ReqT> : AvailableRouterMethod<ReqT>,
|
||||||
|
_ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT,
|
||||||
|
DataT = _ResT,
|
||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
DefaultT = undefined,
|
DefaultT = DataT,
|
||||||
R extends NitroFetchRequest = string & {},
|
> extends Omit<AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, 'watch'>, ComputedFetchOptions<ReqT, Method> {
|
||||||
M extends AvailableRouterMethod<R> = AvailableRouterMethod<R>,
|
|
||||||
> extends Omit<AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, 'watch'>, ComputedFetchOptions<R, M> {
|
|
||||||
key?: string
|
key?: string
|
||||||
$fetch?: typeof globalThis.$fetch
|
$fetch?: typeof globalThis.$fetch
|
||||||
watch?: MultiWatchSources | false
|
watch?: MultiWatchSources | false
|
||||||
@ -58,7 +59,7 @@ export function useFetch<
|
|||||||
DefaultT = undefined,
|
DefaultT = undefined,
|
||||||
> (
|
> (
|
||||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||||
opts?: UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>
|
opts?: UseFetchOptions<_ResT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
||||||
/**
|
/**
|
||||||
* Fetch data from an API endpoint with an SSR-friendly composable.
|
* Fetch data from an API endpoint with an SSR-friendly composable.
|
||||||
@ -77,7 +78,7 @@ export function useFetch<
|
|||||||
DefaultT = DataT,
|
DefaultT = DataT,
|
||||||
> (
|
> (
|
||||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||||
opts?: UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>
|
opts?: UseFetchOptions<_ResT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
||||||
export function useFetch<
|
export function useFetch<
|
||||||
ResT = void,
|
ResT = void,
|
||||||
@ -90,7 +91,7 @@ export function useFetch<
|
|||||||
DefaultT = undefined,
|
DefaultT = undefined,
|
||||||
> (
|
> (
|
||||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||||
arg1?: string | UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>,
|
arg1?: string | UseFetchOptions<_ResT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>,
|
||||||
arg2?: string,
|
arg2?: string,
|
||||||
) {
|
) {
|
||||||
const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2]
|
const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2]
|
||||||
@ -196,7 +197,7 @@ export function useLazyFetch<
|
|||||||
DefaultT = undefined,
|
DefaultT = undefined,
|
||||||
> (
|
> (
|
||||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||||
opts?: Omit<UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>, 'lazy'>
|
opts?: Omit<UseFetchOptions<_ResT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>, 'lazy'>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
||||||
export function useLazyFetch<
|
export function useLazyFetch<
|
||||||
ResT = void,
|
ResT = void,
|
||||||
@ -209,7 +210,7 @@ export function useLazyFetch<
|
|||||||
DefaultT = DataT,
|
DefaultT = DataT,
|
||||||
> (
|
> (
|
||||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||||
opts?: Omit<UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>, 'lazy'>
|
opts?: Omit<UseFetchOptions<_ResT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>, 'lazy'>
|
||||||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | undefined>
|
||||||
export function useLazyFetch<
|
export function useLazyFetch<
|
||||||
ResT = void,
|
ResT = void,
|
||||||
@ -222,7 +223,7 @@ export function useLazyFetch<
|
|||||||
DefaultT = undefined,
|
DefaultT = undefined,
|
||||||
> (
|
> (
|
||||||
request: Ref<ReqT> | ReqT | (() => ReqT),
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
||||||
arg1?: string | Omit<UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>, 'lazy'>,
|
arg1?: string | Omit<UseFetchOptions<_ResT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>, 'lazy'>,
|
||||||
arg2?: string,
|
arg2?: string,
|
||||||
) {
|
) {
|
||||||
const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2]
|
const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2]
|
||||||
@ -240,7 +241,7 @@ export function useLazyFetch<
|
|||||||
autoKey)
|
autoKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateOptionSegments<_ResT, DataT, DefaultT> (opts: UseFetchOptions<_ResT, DataT, any, DefaultT, any, any>) {
|
function generateOptionSegments<_ResT, DataT, DefaultT> (opts: UseFetchOptions<_ResT, any, any, _ResT, DataT, any, DefaultT>) {
|
||||||
const segments: Array<string | undefined | Record<string, string>> = [
|
const segments: Array<string | undefined | Record<string, string>> = [
|
||||||
toValue(opts.method as MaybeRef<string | undefined> | undefined)?.toUpperCase() || 'GET',
|
toValue(opts.method as MaybeRef<string | undefined> | undefined)?.toUpperCase() || 'GET',
|
||||||
toValue(opts.baseURL),
|
toValue(opts.baseURL),
|
||||||
|
@ -75,7 +75,7 @@
|
|||||||
"@types/pify": "5.0.4",
|
"@types/pify": "5.0.4",
|
||||||
"@types/webpack-bundle-analyzer": "4.7.0",
|
"@types/webpack-bundle-analyzer": "4.7.0",
|
||||||
"@types/webpack-hot-middleware": "2.25.9",
|
"@types/webpack-hot-middleware": "2.25.9",
|
||||||
"rollup": "4.34.6",
|
"rollup": "4.34.8",
|
||||||
"unbuild": "3.3.1",
|
"unbuild": "3.3.1",
|
||||||
"vue": "3.5.13"
|
"vue": "3.5.13"
|
||||||
},
|
},
|
||||||
|
@ -40,12 +40,12 @@
|
|||||||
"@types/rollup-plugin-visualizer": "4.2.4",
|
"@types/rollup-plugin-visualizer": "4.2.4",
|
||||||
"@types/webpack-bundle-analyzer": "4.7.0",
|
"@types/webpack-bundle-analyzer": "4.7.0",
|
||||||
"@types/webpack-hot-middleware": "2.25.9",
|
"@types/webpack-hot-middleware": "2.25.9",
|
||||||
"@unhead/schema": "1.11.18",
|
"@unhead/schema": "1.11.19",
|
||||||
"@vitejs/plugin-vue": "5.2.1",
|
"@vitejs/plugin-vue": "5.2.1",
|
||||||
"@vitejs/plugin-vue-jsx": "4.1.1",
|
"@vitejs/plugin-vue-jsx": "4.1.1",
|
||||||
"@vue/compiler-core": "3.5.13",
|
"@vue/compiler-core": "3.5.13",
|
||||||
"@vue/compiler-sfc": "3.5.13",
|
"@vue/compiler-sfc": "3.5.13",
|
||||||
"@vue/language-core": "2.2.0",
|
"@vue/language-core": "2.2.2",
|
||||||
"c12": "2.0.2",
|
"c12": "2.0.2",
|
||||||
"chokidar": "4.0.3",
|
"chokidar": "4.0.3",
|
||||||
"compatx": "0.1.8",
|
"compatx": "0.1.8",
|
||||||
@ -61,11 +61,11 @@
|
|||||||
"ofetch": "1.4.1",
|
"ofetch": "1.4.1",
|
||||||
"pkg-types": "1.3.1",
|
"pkg-types": "1.3.1",
|
||||||
"postcss": "8.5.2",
|
"postcss": "8.5.2",
|
||||||
"sass-loader": "16.0.4",
|
"sass-loader": "16.0.5",
|
||||||
"scule": "1.3.0",
|
"scule": "1.3.0",
|
||||||
"unbuild": "3.3.1",
|
"unbuild": "3.3.1",
|
||||||
"unctx": "2.4.1",
|
"unctx": "2.4.1",
|
||||||
"unimport": "4.1.1",
|
"unimport": "4.1.2",
|
||||||
"untyped": "1.5.2",
|
"untyped": "1.5.2",
|
||||||
"vite": "6.1.0",
|
"vite": "6.1.0",
|
||||||
"vue": "3.5.13",
|
"vue": "3.5.13",
|
||||||
|
@ -17,9 +17,9 @@
|
|||||||
"prerender": "pnpm build && jiti ./lib/prerender"
|
"prerender": "pnpm build && jiti ./lib/prerender"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@unocss/reset": "65.4.3",
|
"@unocss/reset": "66.0.0",
|
||||||
"beasties": "0.2.0",
|
"beasties": "0.2.0",
|
||||||
"html-validate": "9.2.2",
|
"html-validate": "9.3.0",
|
||||||
"htmlnano": "2.1.1",
|
"htmlnano": "2.1.1",
|
||||||
"jiti": "2.4.2",
|
"jiti": "2.4.2",
|
||||||
"knitwork": "1.2.0",
|
"knitwork": "1.2.0",
|
||||||
@ -28,8 +28,8 @@
|
|||||||
"scule": "1.3.0",
|
"scule": "1.3.0",
|
||||||
"svgo": "3.3.2",
|
"svgo": "3.3.2",
|
||||||
"tinyexec": "0.3.2",
|
"tinyexec": "0.3.2",
|
||||||
"tinyglobby": "0.2.10",
|
"tinyglobby": "0.2.11",
|
||||||
"unocss": "65.4.3",
|
"unocss": "66.0.0",
|
||||||
"vite": "6.1.0"
|
"vite": "6.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxt/schema": "workspace:*",
|
"@nuxt/schema": "workspace:*",
|
||||||
"rollup": "4.34.6",
|
"rollup": "4.34.8",
|
||||||
"unbuild": "3.3.1",
|
"unbuild": "3.3.1",
|
||||||
"vue": "3.5.13"
|
"vue": "3.5.13"
|
||||||
},
|
},
|
||||||
|
@ -77,7 +77,7 @@
|
|||||||
"@types/pify": "5.0.4",
|
"@types/pify": "5.0.4",
|
||||||
"@types/webpack-bundle-analyzer": "4.7.0",
|
"@types/webpack-bundle-analyzer": "4.7.0",
|
||||||
"@types/webpack-hot-middleware": "2.25.9",
|
"@types/webpack-hot-middleware": "2.25.9",
|
||||||
"rollup": "4.34.6",
|
"rollup": "4.34.8",
|
||||||
"unbuild": "3.3.1",
|
"unbuild": "3.3.1",
|
||||||
"vue": "3.5.13"
|
"vue": "3.5.13"
|
||||||
},
|
},
|
||||||
|
1447
pnpm-lock.yaml
1447
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
47
test/fixtures/basic-types/types.ts
vendored
47
test/fixtures/basic-types/types.ts
vendored
@ -9,6 +9,7 @@ import type { NitroRouteRules } from 'nitro/types'
|
|||||||
import type { AppConfig, RuntimeValue, UpperSnakeCase } from 'nuxt/schema'
|
import type { AppConfig, RuntimeValue, UpperSnakeCase } from 'nuxt/schema'
|
||||||
import { defineNuxtModule } from 'nuxt/kit'
|
import { defineNuxtModule } from 'nuxt/kit'
|
||||||
import { defineNuxtConfig } from 'nuxt/config'
|
import { defineNuxtConfig } from 'nuxt/config'
|
||||||
|
import { useAsyncFetchCustom, useFetchCustom } from './composables/useFetchCustom'
|
||||||
import { callWithNuxt, isVue3 } from '#app'
|
import { callWithNuxt, isVue3 } from '#app'
|
||||||
import type { NuxtError } from '#app'
|
import type { NuxtError } from '#app'
|
||||||
import type { NavigateToOptions } from '#app/composables/router'
|
import type { NavigateToOptions } from '#app/composables/router'
|
||||||
@ -63,8 +64,8 @@ describe('API routes', () => {
|
|||||||
expectTypeOf(useAsyncData('api-other', () => $fetch('/api/other')).data).toEqualTypeOf<Ref<unknown>>()
|
expectTypeOf(useAsyncData('api-other', () => $fetch('/api/other')).data).toEqualTypeOf<Ref<unknown>>()
|
||||||
expectTypeOf(useAsyncData<TestResponse>('api-generics', () => $fetch('/test')).data).toEqualTypeOf<Ref<TestResponse | DefaultAsyncDataValue>>()
|
expectTypeOf(useAsyncData<TestResponse>('api-generics', () => $fetch('/test')).data).toEqualTypeOf<Ref<TestResponse | DefaultAsyncDataValue>>()
|
||||||
|
|
||||||
expectTypeOf(useAsyncData('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<NuxtError<unknown> | DefaultAsyncDataErrorValue>>()
|
expectTypeOf(useAsyncData('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<Error | DefaultAsyncDataErrorValue>>()
|
||||||
expectTypeOf(useAsyncData<any, string>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<NuxtError<string> | DefaultAsyncDataErrorValue>>()
|
expectTypeOf(useAsyncData<any, string>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<string | DefaultAsyncDataErrorValue>>()
|
||||||
// backwards compatibility
|
// backwards compatibility
|
||||||
expectTypeOf(useAsyncData<any, Error>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<Error | DefaultAsyncDataErrorValue>>()
|
expectTypeOf(useAsyncData<any, Error>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<Error | DefaultAsyncDataErrorValue>>()
|
||||||
expectTypeOf(useAsyncData<any, NuxtError<string>>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<NuxtError<string> | DefaultAsyncDataErrorValue>>()
|
expectTypeOf(useAsyncData<any, NuxtError<string>>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<NuxtError<string> | DefaultAsyncDataErrorValue>>()
|
||||||
@ -502,6 +503,48 @@ describe('composables', () => {
|
|||||||
expectTypeOf(useLazyAsyncData<string>(() => $fetch('/test'), { default: () => 'test', transform: () => 'transformed' }).data).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useLazyAsyncData<string>(() => $fetch('/test'), { default: () => 'test', transform: () => 'transformed' }).data).toEqualTypeOf<Ref<string>>()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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', () => {
|
it('supports asynchronous transform', () => {
|
||||||
const { data } = useAsyncData('test', () => $fetch('/test') as Promise<{ foo: 'bar' }>, {
|
const { data } = useAsyncData('test', () => $fetch('/test') as Promise<{ foo: 'bar' }>, {
|
||||||
async transform (data) {
|
async transform (data) {
|
||||||
|
Loading…
Reference in New Issue
Block a user