diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index 7c6730497..b19e609da 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -5,7 +5,6 @@ import type { HookFetcherFn } from '../utils/types' import useData from '../utils/use-data-2' import { Provider, useCommerce } from '..' -// Input expected by the `useCart` hook export type FetchCartInput = { cartId?: Cart['id'] } @@ -33,7 +32,7 @@ export const fetcher: HookFetcherFn = async ({ } export default function useCart

(...input: UseCartInput

) { - const { providerRef, cartCookie } = useCommerce

() + const { providerRef, fetcherRef, cartCookie } = useCommerce

() const provider = providerRef.current const opts = provider.cart?.useCart @@ -42,7 +41,11 @@ export default function useCart

(...input: UseCartInput

) { context.input.cartId = Cookies.get(cartCookie) 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( () => (opts?.onResponse ? opts.onResponse(response) : response), [response] diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index bef1f2d8b..5ff0360a1 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -2,6 +2,10 @@ import type { ResponseState, SwrOptions } from './use-data' export type Override = Omit & K +// Returns the properties in T with the properties in type K changed from optional to required +export type PickRequired = Omit & + Required> + // Core fetcher added by CommerceProvider export type Fetcher = (options: FetcherOptions) => T | Promise diff --git a/framework/commerce/utils/use-data-2.ts b/framework/commerce/utils/use-data-2.ts index 840251409..b8dc00b3d 100644 --- a/framework/commerce/utils/use-data-2.ts +++ b/framework/commerce/utils/use-data-2.ts @@ -3,7 +3,8 @@ import type { HookHandler, HookInput, HookFetcher, - HookFetcherFn, + PickRequired, + Fetcher, } from './types' import defineProperty from './define-property' import { CommerceError } from './errors' @@ -26,14 +27,15 @@ export type UseData = < Result = any, Body = any >( - options: HookHandler, + options: PickRequired< + HookHandler, + 'fetcher' + >, input: HookInput, - fetcherFn: HookFetcherFn, - swrOptions?: SwrOptions + fetcherFn: Fetcher ) => ResponseState -const useData: UseData = (options, input, fetcherFn, swrOptions) => { - const { fetcherRef } = useCommerce() +const useData: UseData = (options, input, fetcherFn) => { const fetcher = async ( url?: string, query?: string, @@ -41,14 +43,14 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => { ...args: any[] ) => { try { - return await fetcherFn({ + return await options.fetcher({ options: { url, query, method }, // Transform the input array into an object input: args.reduce((obj, val, i) => { obj[input[i][0]!] = val return obj }, {}), - fetch: fetcherRef.current, + fetch: fetcherFn, normalize: options.normalizer, }) } catch (error) { @@ -68,7 +70,7 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => { : null }, fetcher, - swrOptions + options.swrOptions ) if (!('isLoading' in response)) {