diff --git a/framework/bigcommerce/provider.tsx b/framework/bigcommerce/provider.tsx index 653a28a87..6fd02e304 100644 --- a/framework/bigcommerce/provider.tsx +++ b/framework/bigcommerce/provider.tsx @@ -50,15 +50,16 @@ const useCart: HookHandler< Cart | null, {}, FetchCartInput, - any, - any, { isEmpty?: boolean } > = { fetchOptions: { url: '/api/bigcommerce/cart', method: 'GET', }, - normalizer: normalizeCart, + async fetcher({ input: { cartId }, options, fetch }) { + const data = cartId ? await fetch(options) : null + return data && normalizeCart(data) + }, useHook({ input, useData }) { const response = useData({ swrOptions: { revalidateOnFocus: false, ...input.swrOptions }, @@ -83,8 +84,6 @@ const useWishlist: HookHandler< Wishlist | null, { includeProducts?: boolean }, { customerId?: number; includeProducts: boolean }, - any, - any, { isEmpty?: boolean } > = { fetchOptions: { @@ -132,18 +131,15 @@ const useWishlist: HookHandler< }, } -const useCustomerHandler: HookHandler< - Customer | null, - {}, - {}, - CustomerData | null, - any -> = { +const useCustomerHandler: HookHandler = { fetchOptions: { url: '/api/bigcommerce/customers', method: 'GET', }, - normalizer: (data) => data.customer, + async fetcher({ options, fetch }) { + const data = await fetch(options) + return data?.customer ?? null + }, useHook({ input, useData }) { return useData({ swrOptions: { @@ -164,9 +160,7 @@ export type SearchProductsInput = { const useSearch: HookHandler< SearchProductsData, SearchProductsInput, - SearchProductsInput, - any, - any + SearchProductsInput > = { fetchOptions: { url: '/api/bigcommerce/catalog/products', diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index a1f1d0f84..f7b384047 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -34,10 +34,8 @@ export const fetcher: HookFetcherFn = async ({ options, input: { cartId }, fetch, - normalize, }) => { - const data = cartId ? await fetch({ ...options }) : null - return data && normalize ? normalize(data) : data + return cartId ? await fetch({ ...options }) : null } export default function useCart

( diff --git a/framework/commerce/utils/default-fetcher.ts b/framework/commerce/utils/default-fetcher.ts index 25211a689..8dc9def75 100644 --- a/framework/commerce/utils/default-fetcher.ts +++ b/framework/commerce/utils/default-fetcher.ts @@ -1,12 +1,6 @@ -import { HookFetcherFn } from './types' +import type { HookFetcherFn } from './types' -const defaultFetcher: HookFetcherFn = async ({ - options, - fetch, - normalize, -}) => { - const data = await fetch({ ...options }) - return data && normalize ? normalize(data) : data -} +const defaultFetcher: HookFetcherFn = ({ options, fetch }) => + fetch(options) export default defaultFetcher diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index 47da81a7f..98e4ebbae 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -40,7 +40,6 @@ export type HookFetcherFn< options: HookFetcherOptions input: Input fetch: (options: FetcherOptions) => Promise - normalize?(data: Result): Data }) => Data | Promise export type HookFetcherOptions = { method?: string } & ( @@ -63,23 +62,18 @@ export type HookHandler< Input extends { [k: string]: unknown } = {}, // Input expected before doing a fetch operation FetchInput extends HookFetchInput = {}, - // Data returned by the API after a fetch operation - Result = any, - // Body expected by the API endpoint - Body = any, // Custom state added to the response object of SWR State = {} > = { useHook?(context: { - input: Input & { swrOptions?: SwrOptions } + input: Input & { swrOptions?: SwrOptions } useData(context?: { input?: HookFetchInput | HookSwrInput - swrOptions?: SwrOptions + swrOptions?: SwrOptions }): ResponseState }): ResponseState & State fetchOptions: HookFetcherOptions - fetcher?: HookFetcherFn - normalizer?(data: NonNullable): Data + fetcher?: HookFetcherFn } export type SwrOptions = ConfigInterface< diff --git a/framework/commerce/utils/use-data.tsx b/framework/commerce/utils/use-data.tsx index cc4d2cc5b..94679a0c6 100644 --- a/framework/commerce/utils/use-data.tsx +++ b/framework/commerce/utils/use-data.tsx @@ -17,17 +17,12 @@ export type ResponseState = responseInterface & { export type UseData = < Data = any, Input extends { [k: string]: unknown } = {}, - FetchInput extends HookFetchInput = {}, - Result = any, - Body = any + FetchInput extends HookFetchInput = {} >( - options: PickRequired< - HookHandler, - 'fetcher' - >, + options: PickRequired, 'fetcher'>, input: HookFetchInput | HookSwrInput, fetcherFn: Fetcher, - swrOptions?: SwrOptions + swrOptions?: SwrOptions ) => ResponseState const useData: UseData = (options, input, fetcherFn, swrOptions) => { @@ -47,7 +42,6 @@ const useData: UseData = (options, input, fetcherFn, swrOptions) => { return obj }, {}), fetch: fetcherFn, - normalize: options.normalizer, }) } catch (error) { // SWR will not log errors, but any error that's not an instance