forked from crowetic/commerce
Simplified useData types
This commit is contained in:
parent
74c55cc50e
commit
8bf42c3b50
@ -5,7 +5,6 @@ import type { HookFetcherFn } from '../utils/types'
|
|||||||
import useData from '../utils/use-data-2'
|
import useData from '../utils/use-data-2'
|
||||||
import { Provider, useCommerce } from '..'
|
import { Provider, useCommerce } from '..'
|
||||||
|
|
||||||
// Input expected by the `useCart` hook
|
|
||||||
export type FetchCartInput = {
|
export type FetchCartInput = {
|
||||||
cartId?: Cart['id']
|
cartId?: Cart['id']
|
||||||
}
|
}
|
||||||
@ -33,7 +32,7 @@ export const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function useCart<P extends Provider>(...input: UseCartInput<P>) {
|
export default function useCart<P extends Provider>(...input: UseCartInput<P>) {
|
||||||
const { providerRef, cartCookie } = useCommerce<P>()
|
const { providerRef, fetcherRef, cartCookie } = useCommerce<P>()
|
||||||
|
|
||||||
const provider = providerRef.current
|
const provider = providerRef.current
|
||||||
const opts = provider.cart?.useCart
|
const opts = provider.cart?.useCart
|
||||||
@ -42,7 +41,11 @@ export default function useCart<P extends Provider>(...input: UseCartInput<P>) {
|
|||||||
context.input.cartId = Cookies.get(cartCookie)
|
context.input.cartId = Cookies.get(cartCookie)
|
||||||
return fetcherFn(context)
|
return fetcherFn(context)
|
||||||
}
|
}
|
||||||
const response = useData(opts!, input, wrapper, opts?.swrOptions)
|
const response = useData(
|
||||||
|
{ ...opts, fetcher: wrapper },
|
||||||
|
input,
|
||||||
|
provider.fetcher ?? fetcherRef.current
|
||||||
|
)
|
||||||
const memoizedResponse = useMemo(
|
const memoizedResponse = useMemo(
|
||||||
() => (opts?.onResponse ? opts.onResponse(response) : response),
|
() => (opts?.onResponse ? opts.onResponse(response) : response),
|
||||||
[response]
|
[response]
|
||||||
|
@ -2,6 +2,10 @@ import type { ResponseState, SwrOptions } from './use-data'
|
|||||||
|
|
||||||
export type Override<T, K> = Omit<T, keyof K> & K
|
export type Override<T, K> = Omit<T, keyof K> & K
|
||||||
|
|
||||||
|
// Returns the properties in T with the properties in type K changed from optional to required
|
||||||
|
export type PickRequired<T, K extends keyof T> = Omit<T, K> &
|
||||||
|
Required<Pick<T, K>>
|
||||||
|
|
||||||
// Core fetcher added by CommerceProvider
|
// Core fetcher added by CommerceProvider
|
||||||
export type Fetcher<T> = (options: FetcherOptions) => T | Promise<T>
|
export type Fetcher<T> = (options: FetcherOptions) => T | Promise<T>
|
||||||
|
|
||||||
|
@ -3,7 +3,8 @@ import type {
|
|||||||
HookHandler,
|
HookHandler,
|
||||||
HookInput,
|
HookInput,
|
||||||
HookFetcher,
|
HookFetcher,
|
||||||
HookFetcherFn,
|
PickRequired,
|
||||||
|
Fetcher,
|
||||||
} from './types'
|
} from './types'
|
||||||
import defineProperty from './define-property'
|
import defineProperty from './define-property'
|
||||||
import { CommerceError } from './errors'
|
import { CommerceError } from './errors'
|
||||||
@ -26,14 +27,15 @@ export type UseData = <
|
|||||||
Result = any,
|
Result = any,
|
||||||
Body = any
|
Body = any
|
||||||
>(
|
>(
|
||||||
options: HookHandler<Data, Input, FetchInput, Result, Body>,
|
options: PickRequired<
|
||||||
|
HookHandler<Data, Input, FetchInput, Result, Body>,
|
||||||
|
'fetcher'
|
||||||
|
>,
|
||||||
input: HookInput,
|
input: HookInput,
|
||||||
fetcherFn: HookFetcherFn<Data, FetchInput, Result, Body>,
|
fetcherFn: Fetcher<any>
|
||||||
swrOptions?: SwrOptions<Data, FetchInput, Result>
|
|
||||||
) => ResponseState<Data>
|
) => ResponseState<Data>
|
||||||
|
|
||||||
const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
const useData: UseData = (options, input, fetcherFn) => {
|
||||||
const { fetcherRef } = useCommerce()
|
|
||||||
const fetcher = async (
|
const fetcher = async (
|
||||||
url?: string,
|
url?: string,
|
||||||
query?: string,
|
query?: string,
|
||||||
@ -41,14 +43,14 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
|||||||
...args: any[]
|
...args: any[]
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
return await fetcherFn({
|
return await options.fetcher({
|
||||||
options: { url, query, method },
|
options: { url, query, method },
|
||||||
// Transform the input array into an object
|
// Transform the input array into an object
|
||||||
input: args.reduce((obj, val, i) => {
|
input: args.reduce((obj, val, i) => {
|
||||||
obj[input[i][0]!] = val
|
obj[input[i][0]!] = val
|
||||||
return obj
|
return obj
|
||||||
}, {}),
|
}, {}),
|
||||||
fetch: fetcherRef.current,
|
fetch: fetcherFn,
|
||||||
normalize: options.normalizer,
|
normalize: options.normalizer,
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -68,7 +70,7 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => {
|
|||||||
: null
|
: null
|
||||||
},
|
},
|
||||||
fetcher,
|
fetcher,
|
||||||
swrOptions
|
options.swrOptions
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!('isLoading' in response)) {
|
if (!('isLoading' in response)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user