mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Updated useData and util hooks
This commit is contained in:
parent
e7980e17f5
commit
f8d69323bd
@ -2,10 +2,11 @@ import useSWR, { responseInterface } from 'swr'
|
||||
import type {
|
||||
HookSWRInput,
|
||||
HookFetchInput,
|
||||
Fetcher,
|
||||
SwrOptions,
|
||||
HookFetcherOptions,
|
||||
HookFetcherFn,
|
||||
Fetcher,
|
||||
SwrOptions,
|
||||
SWRHookSchemaBase,
|
||||
} from './types'
|
||||
import defineProperty from './define-property'
|
||||
import { CommerceError } from './errors'
|
||||
@ -14,15 +15,15 @@ export type ResponseState<Result> = responseInterface<Result, CommerceError> & {
|
||||
isLoading: boolean
|
||||
}
|
||||
|
||||
export type UseData = <Data = any, FetchInput extends HookFetchInput = {}>(
|
||||
export type UseData = <H extends SWRHookSchemaBase>(
|
||||
options: {
|
||||
fetchOptions: HookFetcherOptions
|
||||
fetcher: HookFetcherFn<Data, FetchInput>
|
||||
fetcher: HookFetcherFn<H>
|
||||
},
|
||||
input: HookFetchInput | HookSWRInput,
|
||||
fetcherFn: Fetcher,
|
||||
swrOptions?: SwrOptions<Data, FetchInput>
|
||||
) => ResponseState<Data>
|
||||
swrOptions?: SwrOptions<H['data'], H['fetchInput']>
|
||||
) => ResponseState<H['data']>
|
||||
|
||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
||||
const hookInput = Array.isArray(input) ? input : Object.entries(input)
|
||||
|
@ -10,14 +10,14 @@ export function useFetcher() {
|
||||
|
||||
export function useHook<
|
||||
P extends Provider,
|
||||
H extends MutationHook<any, any, any> | SWRHook<any, any, any>
|
||||
H extends MutationHook<any> | SWRHook<any>
|
||||
>(fn: (provider: P) => H) {
|
||||
const { providerRef } = useCommerce<P>()
|
||||
const provider = providerRef.current
|
||||
return fn(provider)
|
||||
}
|
||||
|
||||
export function useSWRHook<H extends SWRHook<any, any, any>>(
|
||||
export function useSWRHook<H extends SWRHook<any>>(
|
||||
hook: PickRequired<H, 'fetcher'>
|
||||
) {
|
||||
const fetcher = useFetcher()
|
||||
@ -30,7 +30,7 @@ export function useSWRHook<H extends SWRHook<any, any, any>>(
|
||||
})
|
||||
}
|
||||
|
||||
export function useMutationHook<H extends MutationHook<any, any, any>>(
|
||||
export function useMutationHook<H extends MutationHook<any>>(
|
||||
hook: PickRequired<H, 'fetcher'>
|
||||
) {
|
||||
const fetcher = useFetcher()
|
||||
|
Loading…
x
Reference in New Issue
Block a user