-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: useRoadizFetch() overrides the Nuxt useFetch() composable
- Loading branch information
1 parent
d326f71
commit 409fac1
Showing
2 changed files
with
31 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
import type { AsyncDataOptions } from '#app' | ||
import type { AvailableRouterMethod, NitroFetchRequest } from 'nitropack' | ||
import { FetchError } from 'ofetch' | ||
import type { Ref } from 'vue' | ||
import type { KeysOf, PickFrom } from 'nuxt/dist/app/composables/asyncData' | ||
import type { AsyncData, FetchResult, UseFetchOptions } from 'nuxt/app' | ||
|
||
export function useRoadizFetch<T>(url?: string | AsyncDataOptions<T>, options?: AsyncDataOptions<T>) { | ||
const fetch = useRoadizFetchFactory() | ||
|
||
return useAsyncData<T>(() => fetch(url), options) | ||
export function useRoadizFetch< | ||
ResT = void, | ||
ErrorT = FetchError, | ||
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>, | ||
DefaultT = DataT, | ||
>( | ||
request: Ref<ReqT> | ReqT | (() => ReqT), | ||
opts?: UseFetchOptions<_ResT, DataT, PickKeys, DefaultT, ReqT, Method>, | ||
): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, ErrorT | null> { | ||
return useFetch<ResT, ErrorT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>(request, { | ||
...opts, | ||
$fetch: opts?.$fetch || useRoadizFetchFactory(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters