Skip to content

Commit

Permalink
feat: simplify option type & support all async data options
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Feb 22, 2023
1 parent 8bf2cd2 commit 500d4a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 37 deletions.
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,17 @@ By default, Nuxt waits until a `refresh` is finished before it can be executed a

Responses **are cached** between function calls for the same path based on a calculated hash. You can disable this behavior by setting `cache` to `false`.

The composable supports every [`useAsyncData` option](https://nuxt.com/docs/api/composables/use-async-data/#params).

**Type Declarations**

```ts
function usePartyData<
T = any,
Transform extends (res: T) => any = (res: T) => T,
>(
function usePartyData<T = any>(
path: MaybeComputedRef<string>,
opts?: UseApiDataOptions<T, Transform>,
opts?: UseApiDataOptions<T>,
): AsyncData<T, FetchError>

type UseApiDataOptions<
T,
Transform extends (res: T) => any = (res: T) => T,
> = Pick<
AsyncDataOptions<T, Transform>,
| 'server'
| 'lazy'
| 'default'
| 'watch'
| 'immediate'
> & Pick<
type UseApiDataOptions<T> = AsyncDataOptions<T> & Pick<
ComputedOptions<NitroFetchOptions<string>>,
| 'onRequest'
| 'onRequestError'
Expand All @@ -434,8 +423,6 @@ type UseApiDataOptions<
}
```
The composable infers most of the [`useAsyncData` options](https://nuxt.com/docs/api/composables/use-async-data/#params).
**Basic example**
```vue
Expand Down
26 changes: 7 additions & 19 deletions src/runtime/composables/useApiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@ type ComputedOptions<T extends Record<string, any>> = {
: Ref<T[K]> | T[K];
}

export type UseApiDataOptions<
T,
Transform extends (res: T) => any = (res: T) => T,
> = Pick<
AsyncDataOptions<T, Transform>,
| 'server'
| 'lazy'
| 'default'
| 'transform'
| 'watch'
| 'immediate'
> & Pick<
export type UseApiDataOptions<T> = AsyncDataOptions<T> & Pick<
ComputedOptions<NitroFetchOptions<string>>,
| 'onRequest'
| 'onRequestError'
Expand Down Expand Up @@ -59,13 +48,10 @@ export type UseApiData = <T = any>(
opts?: UseApiDataOptions<T>,
) => AsyncData<T, FetchError>

export function _useApiData<
T = any,
Transform extends (res: T) => any = (res: T) => T,
>(
export function _useApiData<T = any>(
endpointId: string,
path: MaybeComputedRef<string>,
opts: UseApiDataOptions<T, Transform> = {},
opts: UseApiDataOptions<T> = {},
) {
const { apiParty } = useRuntimeConfig().public
const _path = computed(() => resolveUnref(path))
Expand All @@ -74,6 +60,7 @@ export function _useApiData<
lazy,
default: defaultFn,
transform,
pick,
watch,
immediate,
query,
Expand All @@ -100,11 +87,12 @@ export function _useApiData<
method,
})

const _asyncDataOptions: AsyncDataOptions<T, Transform> = {
const _asyncDataOptions: AsyncDataOptions<T> = {
server,
lazy,
default: defaultFn,
transform,
pick,
watch: [
_endpointFetchOptions,
...(watch || []),
Expand All @@ -130,7 +118,7 @@ export function _useApiData<
_$fetch = (event?.$fetch as typeof globalThis.$fetch) || globalThis.$fetch
}

return useAsyncData<T, FetchError, Transform>(
return useAsyncData<T, FetchError>(
key.value,
async (nuxt) => {
controller?.abort?.()
Expand Down

0 comments on commit 500d4a1

Please sign in to comment.