mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 21:58:19 +00:00
feat(nuxt): support disabling watch
with useFetch
(#19823)
This commit is contained in:
parent
b1826ee9f4
commit
ee8d3f6ea6
@ -58,7 +58,7 @@ All fetch options can be given a `computed` or `ref` value. These will be watche
|
|||||||
* `server`: Whether to fetch the data on the server (defaults to `true`).
|
* `server`: Whether to fetch the data on the server (defaults to `true`).
|
||||||
* `default`: A factory function to set the default value of the data, before the async function resolves - particularly useful with the `lazy: true` option.
|
* `default`: A factory function to set the default value of the data, before the async function resolves - particularly useful with the `lazy: true` option.
|
||||||
* `pick`: Only pick specified keys in this array from the `handler` function result.
|
* `pick`: Only pick specified keys in this array from the `handler` function result.
|
||||||
* `watch`: watch reactive sources to auto-refresh.
|
* `watch`: Watch an array of reactive sources and auto-refresh the fetch result when they change. Fetch options and URL are watched by default. You can completely ignore reactive sources by using `watch: false`. Together with `immediate: false`, this allows for a fully-manual `useFetch`.
|
||||||
* `transform`: A function that can be used to alter `handler` function result after resolving.
|
* `transform`: A function that can be used to alter `handler` function result after resolving.
|
||||||
* `immediate`: When set to `false`, will prevent the request from firing immediately. (defaults to `true`)
|
* `immediate`: When set to `false`, will prevent the request from firing immediately. (defaults to `true`)
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ export type KeysOf<T> = Array<
|
|||||||
|
|
||||||
export type KeyOfRes<Transform extends _Transform> = KeysOf<ReturnType<Transform>>
|
export type KeyOfRes<Transform extends _Transform> = KeysOf<ReturnType<Transform>>
|
||||||
|
|
||||||
type MultiWatchSources = (WatchSource<unknown> | object)[]
|
export type MultiWatchSources = (WatchSource<unknown> | object)[]
|
||||||
|
|
||||||
export interface AsyncDataOptions<
|
export interface AsyncDataOptions<
|
||||||
ResT,
|
ResT,
|
||||||
|
@ -4,7 +4,7 @@ import type { Ref } from 'vue'
|
|||||||
import { computed, unref, reactive } from 'vue'
|
import { computed, unref, reactive } from 'vue'
|
||||||
import { hash } from 'ohash'
|
import { hash } from 'ohash'
|
||||||
import { useRequestFetch } from './ssr'
|
import { useRequestFetch } from './ssr'
|
||||||
import type { AsyncDataOptions, _Transform, KeysOf, AsyncData, PickFrom } from './asyncData'
|
import type { AsyncDataOptions, _Transform, KeysOf, AsyncData, PickFrom, MultiWatchSources } from './asyncData'
|
||||||
import { useAsyncData } from './asyncData'
|
import { useAsyncData } from './asyncData'
|
||||||
|
|
||||||
export type FetchResult<ReqT extends NitroFetchRequest, M extends AvailableRouterMethod<ReqT>> = TypedInternalResponse<ReqT, unknown, M>
|
export type FetchResult<ReqT extends NitroFetchRequest, M extends AvailableRouterMethod<ReqT>> = TypedInternalResponse<ReqT, unknown, M>
|
||||||
@ -21,9 +21,10 @@ export interface UseFetchOptions<
|
|||||||
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
|
||||||
R extends NitroFetchRequest = string & {},
|
R extends NitroFetchRequest = string & {},
|
||||||
M extends AvailableRouterMethod<R> = AvailableRouterMethod<R>
|
M extends AvailableRouterMethod<R> = AvailableRouterMethod<R>
|
||||||
> extends AsyncDataOptions<ResT, DataT, PickKeys>, ComputedFetchOptions<R, M> {
|
> extends Omit<AsyncDataOptions<ResT, DataT, PickKeys>, 'watch'>, ComputedFetchOptions<R, M> {
|
||||||
key?: string
|
key?: string
|
||||||
$fetch?: typeof globalThis.$fetch
|
$fetch?: typeof globalThis.$fetch
|
||||||
|
watch?: MultiWatchSources | false
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useFetch<
|
export function useFetch<
|
||||||
@ -92,11 +93,7 @@ export function useFetch<
|
|||||||
transform,
|
transform,
|
||||||
pick,
|
pick,
|
||||||
immediate,
|
immediate,
|
||||||
watch: [
|
watch: watch === false ? [] : [_fetchOptions, _request, ...(watch || [])]
|
||||||
_fetchOptions,
|
|
||||||
_request,
|
|
||||||
...(watch || [])
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let controller: AbortController
|
let controller: AbortController
|
||||||
|
Loading…
Reference in New Issue
Block a user