From 7edd5aa1825320acc0c0ba2a0f0b2b3df9bf3509 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Mon, 25 Jan 2021 16:58:19 -0500 Subject: [PATCH] Moved normalizer to happen after fetch --- framework/bigcommerce/cart/use-cart.tsx | 10 ++++++---- framework/commerce/cart/use-cart.tsx | 10 +++++----- framework/commerce/utils/types.ts | 4 ++-- framework/commerce/utils/use-data.tsx | 14 +++++++------- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/framework/bigcommerce/cart/use-cart.tsx b/framework/bigcommerce/cart/use-cart.tsx index b3ea897c9..9cdd8f56c 100644 --- a/framework/bigcommerce/cart/use-cart.tsx +++ b/framework/bigcommerce/cart/use-cart.tsx @@ -10,17 +10,20 @@ const defaultOpts = { method: 'GET', } -export const fetcher: HookFetcher = ( +export const fetcher: HookFetcher = async ( options, { cartId }, fetch ) => { - return cartId ? fetch({ ...defaultOpts, ...options }) : null + const data = cartId + ? await fetch({ ...defaultOpts, ...options }) + : null + return data && normalizeCart(data) } export function extendHook( customFetcher: typeof fetcher, - swrOptions?: SwrOptions + swrOptions?: SwrOptions ) { const useCart = () => { const response = useCommerceCart(defaultOpts, [], customFetcher, { @@ -28,7 +31,6 @@ export function extendHook( ...swrOptions, }) const res = useResponse(response, { - normalizer: normalizeCart, descriptors: { isEmpty: { get() { diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index c0fd10a5e..223767b6f 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -3,18 +3,18 @@ import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types' import useData, { ResponseState, SwrOptions } from '../utils/use-data' import { useCommerce } from '..' -export type CartResponse = ResponseState & { isEmpty?: boolean } +export type CartResponse = ResponseState & { isEmpty?: boolean } export type CartInput = { cartId: Cart['id'] } -export default function useCart( +export default function useCart( options: HookFetcherOptions, input: HookInput, - fetcherFn: HookFetcher, - swrOptions?: SwrOptions -): CartResponse { + fetcherFn: HookFetcher, + swrOptions?: SwrOptions +): CartResponse { const { cartCookie } = useCommerce() const fetcher: typeof fetcherFn = (options, input, fetch) => { input.cartId = Cookies.get(cartCookie) diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index 2cd838964..483b0c73c 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -9,11 +9,11 @@ export type FetcherOptions = { body?: any } -export type HookFetcher = ( +export type HookFetcher = ( options: HookFetcherOptions | null, input: Input, fetch: (options: FetcherOptions) => Promise -) => Result | Promise +) => Data | Promise export type HookFetcherOptions = { query?: string diff --git a/framework/commerce/utils/use-data.tsx b/framework/commerce/utils/use-data.tsx index a921eb529..38af46a44 100644 --- a/framework/commerce/utils/use-data.tsx +++ b/framework/commerce/utils/use-data.tsx @@ -4,22 +4,22 @@ import defineProperty from './define-property' import { CommerceError } from './errors' import { useCommerce } from '..' -export type SwrOptions = ConfigInterface< - Result, +export type SwrOptions = ConfigInterface< + Data, CommerceError, - HookFetcher + HookFetcher > export type ResponseState = responseInterface & { isLoading: boolean } -export type UseData = ( +export type UseData = ( options: HookFetcherOptions | (() => HookFetcherOptions | null), input: HookInput, - fetcherFn: HookFetcher, - swrOptions?: SwrOptions -) => ResponseState + fetcherFn: HookFetcher, + swrOptions?: SwrOptions +) => ResponseState const useData: UseData = (options, input, fetcherFn, swrOptions) => { const { fetcherRef } = useCommerce()