Skip to content

Commit

Permalink
feat: useRoadizFetch() overrides the Nuxt useFetch() composable
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelodelain committed Mar 25, 2024
1 parent d326f71 commit 409fac1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
31 changes: 26 additions & 5 deletions composables/use-roadiz-fetch.ts
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(),
})
}
6 changes: 5 additions & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<script setup lang="ts"></script>
<script setup lang="ts">
// const response = await useRoadizFetch('/common_content')
//
// console.log('response.data', response.data.value)
</script>

<template>
<div>
Expand Down

0 comments on commit 409fac1

Please # to comment.