2021-10-20 09:43:08 +00:00
|
|
|
import type { FetchOptions, FetchRequest } from 'ohmyfetch'
|
2022-04-07 11:28:04 +00:00
|
|
|
import type { TypedInternalResponse } from 'nitropack'
|
2022-03-17 10:47:41 +00:00
|
|
|
import { hash } from 'ohash'
|
|
|
|
import { computed, isRef, Ref } from 'vue'
|
2021-10-11 22:36:50 +00:00
|
|
|
import type { AsyncDataOptions, _Transform, KeyOfRes } from './asyncData'
|
|
|
|
import { useAsyncData } from './asyncData'
|
|
|
|
|
2021-10-22 22:33:22 +00:00
|
|
|
export type FetchResult<ReqT extends FetchRequest> = TypedInternalResponse<ReqT, unknown>
|
2021-10-11 22:36:50 +00:00
|
|
|
|
2022-03-31 10:24:28 +00:00
|
|
|
export interface UseFetchOptions<
|
2021-10-11 22:36:50 +00:00
|
|
|
DataT,
|
|
|
|
Transform extends _Transform<DataT, any> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
2022-04-29 18:42:22 +00:00
|
|
|
> extends AsyncDataOptions<DataT, Transform, PickKeys>, FetchOptions {
|
2022-03-31 10:24:28 +00:00
|
|
|
key?: string
|
2022-04-29 18:42:22 +00:00
|
|
|
}
|
2021-10-11 22:36:50 +00:00
|
|
|
|
|
|
|
export function useFetch<
|
2022-02-16 20:50:19 +00:00
|
|
|
ResT = void,
|
2022-04-29 18:42:22 +00:00
|
|
|
ErrorT = Error,
|
2022-03-17 10:47:41 +00:00
|
|
|
ReqT extends FetchRequest = FetchRequest,
|
2022-02-16 20:50:19 +00:00
|
|
|
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
|
|
|
|
Transform extends (res: _ResT) => any = (res: _ResT) => _ResT,
|
2021-11-15 12:09:07 +00:00
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
|
|
|
> (
|
2022-03-17 10:47:41 +00:00
|
|
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
2022-02-16 20:50:19 +00:00
|
|
|
opts: UseFetchOptions<_ResT, Transform, PickKeys> = {}
|
2021-10-11 22:36:50 +00:00
|
|
|
) {
|
2022-04-27 16:19:10 +00:00
|
|
|
if (process.dev && opts.transform && !opts.key) {
|
|
|
|
console.warn('[nuxt] You should provide a key for `useFetch` when using a custom transform function.')
|
|
|
|
}
|
|
|
|
const key = '$f_' + (opts.key || hash([request, { ...opts, transform: null }]))
|
2022-03-17 10:47:41 +00:00
|
|
|
const _request = computed<FetchRequest>(() => {
|
|
|
|
let r = request
|
|
|
|
if (typeof r === 'function') {
|
|
|
|
r = r()
|
2021-10-11 22:36:50 +00:00
|
|
|
}
|
2022-03-17 10:47:41 +00:00
|
|
|
return isRef(r) ? r.value : r
|
|
|
|
})
|
2021-10-11 22:36:50 +00:00
|
|
|
|
2022-03-31 10:24:28 +00:00
|
|
|
const _fetchOptions = {
|
|
|
|
...opts,
|
|
|
|
cache: typeof opts.cache === 'boolean' ? undefined : opts.cache
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:56:41 +00:00
|
|
|
const _asyncDataOptions: AsyncDataOptions<_ResT, Transform, PickKeys> = {
|
2022-03-17 10:47:41 +00:00
|
|
|
...opts,
|
|
|
|
watch: [
|
|
|
|
_request,
|
|
|
|
...(opts.watch || [])
|
|
|
|
]
|
2022-03-31 10:24:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 18:42:22 +00:00
|
|
|
const asyncData = useAsyncData<_ResT, ErrorT, Transform, PickKeys>(key, () => {
|
|
|
|
return $fetch(_request.value, _fetchOptions)
|
2022-03-31 10:24:28 +00:00
|
|
|
}, _asyncDataOptions)
|
2022-03-17 10:47:41 +00:00
|
|
|
|
|
|
|
return asyncData
|
2021-10-11 22:36:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 12:09:07 +00:00
|
|
|
export function useLazyFetch<
|
2022-02-16 20:50:19 +00:00
|
|
|
ResT = void,
|
2022-04-29 18:42:22 +00:00
|
|
|
ErrorT = Error,
|
2021-11-15 12:09:07 +00:00
|
|
|
ReqT extends string = string,
|
2022-02-16 20:50:19 +00:00
|
|
|
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
|
|
|
|
Transform extends (res: _ResT) => any = (res: _ResT) => _ResT,
|
2021-11-15 12:09:07 +00:00
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
|
|
|
> (
|
2022-03-17 21:35:07 +00:00
|
|
|
request: Ref<ReqT> | ReqT | (() => ReqT),
|
2022-02-16 20:50:19 +00:00
|
|
|
opts: Omit<UseFetchOptions<_ResT, Transform, PickKeys>, 'lazy'> = {}
|
2021-11-15 12:09:07 +00:00
|
|
|
) {
|
2022-04-29 18:42:22 +00:00
|
|
|
return useFetch<ResT, ErrorT, ReqT, _ResT, Transform, PickKeys>(request, {
|
|
|
|
...opts,
|
|
|
|
lazy: true
|
|
|
|
})
|
2021-11-15 12:09:07 +00:00
|
|
|
}
|