Removed duplicated fetcher

This commit is contained in:
Luis Alvarez 2021-02-08 19:50:45 -05:00
parent 7f3174bcd0
commit 2a5cbadd3a
2 changed files with 4 additions and 17 deletions

View File

@ -90,21 +90,6 @@ export type BigcommerceProvider = typeof bigcommerceProvider
export const bigcommerceConfig: CommerceConfig = {
locale: 'en-us',
cartCookie: 'bc_cartId',
async fetcher({ url, method = 'GET', variables, body: bodyObj }) {
const hasBody = Boolean(variables || bodyObj)
const body = hasBody
? JSON.stringify(variables ? { variables } : bodyObj)
: undefined
const headers = hasBody ? { 'Content-Type': 'application/json' } : undefined
const res = await fetch(url!, { method, body, headers })
if (res.ok) {
const { data } = await res.json()
return data
}
throw await getError(res)
},
}
export type BigcommerceConfig = Partial<CommerceConfig>

View File

@ -14,6 +14,7 @@ import type { FetchCartInput } from './cart/use-cart'
const Commerce = createContext<CommerceContextValue<any> | {}>({})
export type Provider = CommerceConfig & {
fetcher: Fetcher
cart?: {
useCart?: HookHandler<Cart | null, [...any], FetchCartInput>
}
@ -25,7 +26,7 @@ export type CommerceProps<P extends Provider> = {
config: CommerceConfig
}
export type CommerceConfig = { fetcher: Fetcher } & Omit<
export type CommerceConfig = Omit<
CommerceContextValue<any>,
'providerRef' | 'fetcherRef'
>
@ -47,7 +48,8 @@ export function CommerceProvider<P extends Provider>({
}
const providerRef = useRef(provider)
const fetcherRef = useRef(config.fetcher)
// TODO: Remove the fetcherRef
const fetcherRef = useRef(provider.fetcher)
// Because the config is an object, if the parent re-renders this provider
// will re-render every consumer unless we memoize the config
const cfg = useMemo(