diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index 6b1a3c789..a1f1d0f84 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -6,7 +6,7 @@ import type { UseHookInput, UseHookResponse, } from '../utils/types' -import useData from '../utils/use-data-2' +import useData from '../utils/use-data' import { Provider, useCommerce } from '..' export type FetchCartInput = { diff --git a/framework/commerce/customer/use-customer.tsx b/framework/commerce/customer/use-customer.tsx index 4fb9b430b..25112128e 100644 --- a/framework/commerce/customer/use-customer.tsx +++ b/framework/commerce/customer/use-customer.tsx @@ -6,7 +6,7 @@ import type { UseHookResponse, } from '../utils/types' import defaultFetcher from '../utils/default-fetcher' -import useData from '../utils/use-data-2' +import useData from '../utils/use-data' import { Provider, useCommerce } from '..' export type UseCustomerHandler

= Prop< diff --git a/framework/commerce/products/use-search.tsx b/framework/commerce/products/use-search.tsx index 9971c309d..1f887f5fe 100644 --- a/framework/commerce/products/use-search.tsx +++ b/framework/commerce/products/use-search.tsx @@ -6,7 +6,7 @@ import type { UseHookResponse, } from '../utils/types' import defaultFetcher from '../utils/default-fetcher' -import useData from '../utils/use-data-2' +import useData from '../utils/use-data' import { Provider, useCommerce } from '..' import { BigcommerceProvider } from '@framework' diff --git a/framework/commerce/utils/use-data-2.ts b/framework/commerce/utils/use-data-2.ts deleted file mode 100644 index cc4d2cc5b..000000000 --- a/framework/commerce/utils/use-data-2.ts +++ /dev/null @@ -1,84 +0,0 @@ -import useSWR, { responseInterface } from 'swr' -import type { - HookHandler, - HookSwrInput, - HookFetchInput, - PickRequired, - Fetcher, - SwrOptions, -} from './types' -import defineProperty from './define-property' -import { CommerceError } from './errors' - -export type ResponseState = responseInterface & { - isLoading: boolean -} - -export type UseData = < - Data = any, - Input extends { [k: string]: unknown } = {}, - FetchInput extends HookFetchInput = {}, - Result = any, - Body = any ->( - options: PickRequired< - HookHandler, - 'fetcher' - >, - input: HookFetchInput | HookSwrInput, - fetcherFn: Fetcher, - swrOptions?: SwrOptions -) => ResponseState - -const useData: UseData = (options, input, fetcherFn, swrOptions) => { - const hookInput = Array.isArray(input) ? input : Object.entries(input) - const fetcher = async ( - url: string, - query?: string, - method?: string, - ...args: any[] - ) => { - try { - return await options.fetcher({ - options: { url, query, method }, - // Transform the input array into an object - input: args.reduce((obj, val, i) => { - obj[hookInput[i][0]!] = val - return obj - }, {}), - fetch: fetcherFn, - normalize: options.normalizer, - }) - } catch (error) { - // SWR will not log errors, but any error that's not an instance - // of CommerceError is not welcomed by this hook - if (!(error instanceof CommerceError)) { - console.error(error) - } - throw error - } - } - const response = useSWR( - () => { - const opts = options.fetchOptions - return opts - ? [opts.url, opts.query, opts.method, ...hookInput.map((e) => e[1])] - : null - }, - fetcher, - swrOptions - ) - - if (!('isLoading' in response)) { - defineProperty(response, 'isLoading', { - get() { - return response.data === undefined - }, - enumerable: true, - }) - } - - return response -} - -export default useData diff --git a/framework/commerce/utils/use-data.tsx b/framework/commerce/utils/use-data.tsx index 58a1a0a47..cc4d2cc5b 100644 --- a/framework/commerce/utils/use-data.tsx +++ b/framework/commerce/utils/use-data.tsx @@ -1,44 +1,54 @@ -import useSWR, { ConfigInterface, responseInterface } from 'swr' -import type { HookSwrInput, HookFetcher, HookFetcherOptions } from './types' +import useSWR, { responseInterface } from 'swr' +import type { + HookHandler, + HookSwrInput, + HookFetchInput, + PickRequired, + Fetcher, + SwrOptions, +} from './types' import defineProperty from './define-property' import { CommerceError } from './errors' -import { useCommerce } from '..' - -export type SwrOptions = ConfigInterface< - Data, - CommerceError, - HookFetcher -> export type ResponseState = responseInterface & { isLoading: boolean } -export type UseData = ( - options: HookFetcherOptions | (() => HookFetcherOptions | null), - input: HookSwrInput, - fetcherFn: HookFetcher, - swrOptions?: SwrOptions +export type UseData = < + Data = any, + Input extends { [k: string]: unknown } = {}, + FetchInput extends HookFetchInput = {}, + Result = any, + Body = any +>( + options: PickRequired< + HookHandler, + 'fetcher' + >, + input: HookFetchInput | HookSwrInput, + fetcherFn: Fetcher, + swrOptions?: SwrOptions ) => ResponseState const useData: UseData = (options, input, fetcherFn, swrOptions) => { - const { fetcherRef } = useCommerce() + const hookInput = Array.isArray(input) ? input : Object.entries(input) const fetcher = async ( - url?: string, + url: string, query?: string, method?: string, ...args: any[] ) => { try { - return await fetcherFn( - { url, query, method }, + return await options.fetcher({ + options: { url, query, method }, // Transform the input array into an object - args.reduce((obj, val, i) => { - obj[input[i][0]!] = val + input: args.reduce((obj, val, i) => { + obj[hookInput[i][0]!] = val return obj }, {}), - fetcherRef.current - ) + fetch: fetcherFn, + normalize: options.normalizer, + }) } catch (error) { // SWR will not log errors, but any error that's not an instance // of CommerceError is not welcomed by this hook @@ -50,9 +60,9 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => { } const response = useSWR( () => { - const opts = typeof options === 'function' ? options() : options + const opts = options.fetchOptions return opts - ? [opts.url, opts.query, opts.method, ...input.map((e) => e[1])] + ? [opts.url, opts.query, opts.method, ...hookInput.map((e) => e[1])] : null }, fetcher, diff --git a/framework/commerce/wishlist/use-wishlist.tsx b/framework/commerce/wishlist/use-wishlist.tsx index 314f0a1c2..dc912bc98 100644 --- a/framework/commerce/wishlist/use-wishlist.tsx +++ b/framework/commerce/wishlist/use-wishlist.tsx @@ -6,7 +6,7 @@ import type { UseHookResponse, } from '../utils/types' import defaultFetcher from '../utils/default-fetcher' -import useData from '../utils/use-data-2' +import useData from '../utils/use-data' import { Provider, useCommerce } from '..' export type UseWishlistHandler

= Prop<