diff --git a/lib/bigcommerce/cart.tsx b/lib/bigcommerce/cart.tsx index f0bb46ac2..9e9df02c4 100644 --- a/lib/bigcommerce/cart.tsx +++ b/lib/bigcommerce/cart.tsx @@ -2,7 +2,8 @@ import { CartProvider as CommerceCartProvider, useCart as useCommerceCart, } from '../commerce/cart'; -import { Cart } from './index'; + +export type Cart = any; export function CartProvider({ children }) { return {children}; diff --git a/lib/bigcommerce/index.tsx b/lib/bigcommerce/index.tsx index 1f53b07d8..89798c71a 100644 --- a/lib/bigcommerce/index.tsx +++ b/lib/bigcommerce/index.tsx @@ -1,12 +1,9 @@ import { - CommerceProvider, + CommerceProvider as CoreCommerceProvider, Connector, - HookResolver, - useCommerce as useComm, + useCommerce as useCoreCommerce, } from '../commerce'; -export type Cart = any; - async function getText(res: Response) { try { return (await res.text()) || res.statusText; @@ -23,11 +20,7 @@ async function getError(res: Response) { return { message: await getText(res) }; } -async function fetcher( - url: string, - query: string, - resolver: HookResolver -) { +async function fetcher(url: string, query: string) { const res = await fetch(url); if (res.ok) { @@ -37,42 +30,18 @@ async function fetcher( throw await getError(res); } -export const bigcommerce: Connector = { - hooks: { - useCart: { - query: '', - resolver() { - return; - }, - }, - useAddItem: { - query: '', - resolver() { - return; - }, - }, - useUpdateItem: { - query: '', - resolver() { - return; - }, - }, - useRemoveItem: { - query: '', - resolver() { - return; - }, - }, - }, +export const bigcommerce: Connector = { locale: 'en-us', fetcher, }; // TODO: The connector should be extendable when a developer is using it -export function BigcommerceProvider({ children }) { +export function CommerceProvider({ children }) { return ( - {children} + + {children} + ); } -export const useCommerce = () => useComm(); +export const useCommerce = () => useCoreCommerce(); diff --git a/lib/commerce/cart.tsx b/lib/commerce/cart.tsx index 12f420186..509737fda 100644 --- a/lib/commerce/cart.tsx +++ b/lib/commerce/cart.tsx @@ -1,6 +1,8 @@ import { createContext, useContext } from 'react'; import useSWR, { responseInterface } from 'swr'; -import { Cart, useCommerce } from '.'; +import { useCommerce } from '.'; + +export type Cart = any; export type CartResponse = responseInterface & { isEmpty: boolean; diff --git a/lib/commerce/index.tsx b/lib/commerce/index.tsx index ac05c008e..267d7dfd8 100644 --- a/lib/commerce/index.tsx +++ b/lib/commerce/index.tsx @@ -1,44 +1,20 @@ import { createContext, ReactNode, useContext } from 'react'; -const Commerce = createContext>(null); +const Commerce = createContext(null); -export type Cart = any; - -export type CommerceProps = { +export type CommerceProps = { children?: ReactNode; - connector: Connector; + connector: Connector; }; -export type Connector = { - hooks: { - useCart: Hook; - useAddItem: Hook; - useUpdateItem: Hook; - useRemoveItem: Hook; - }; +export type Connector = { fetcher: Fetcher; locale: string; }; -export type Hook = { - query?: string; - url?: string; - resolver: HookResolver; -}; - -export type HookResolver = ( - fetcher: Fetcher, - context: ResolverContext -) => T | Promise; - export type Fetcher = (...args: any) => T | Promise; -export type ResolverContext = { - query?: string; - locale: string; -}; - -export function CommerceProvider({ children, connector }: CommerceProps) { +export function CommerceProvider({ children, connector }: CommerceProps) { if (!connector) { throw new Error( 'CommerceProvider requires a valid headless commerce connector' @@ -48,6 +24,6 @@ export function CommerceProvider({ children, connector }: CommerceProps) { return {children}; } -export function useCommerce() { - return useContext(Commerce) as Connector; +export function useCommerce() { + return useContext(Commerce) as T; }