diff --git a/framework/bigcommerce/types/cart.ts b/framework/bigcommerce/types/cart.ts index d73c5b7e4..d0770056c 100644 --- a/framework/bigcommerce/types/cart.ts +++ b/framework/bigcommerce/types/cart.ts @@ -43,36 +43,21 @@ export type CartItemBody = Core.CartItemBody & { optionSelections?: OptionSelections } -export type CartHooks = Core.CartHooks & { - getCart: { data: Cart | null } - addItem: { - data: Cart - body: { item: CartItemBody } - input: CartItemBody - fetchInput: CartItemBody - actionInput: CartItemBody - } - updateItem: { data: Cart; body: { item: CartItemBody } } - removeItem: { data: Cart | null } +export type CartTypes = { + cart: Cart + item: CartItemBody } +export type CartHooks = Core.CartHooks + export type GetCartHook = CartHooks['getCart'] export type AddItemHook = CartHooks['addItem'] export type UpdateItemHook = CartHooks['updateItem'] export type RemoveItemHook = CartHooks['removeItem'] -export type CartSchema = Core.CartSchema & { - endpoint: { - operations: CartOperations - } -} +export type CartSchema = Core.CartSchema -export type CartOperations = { - getCart: GetCartHook - addItem: AddItemHook - updateItem: UpdateItemHook - removeItem: RemoveItemHook -} +export type CartOperations = Core.CartOperations export type GetCartOperation = CartOperations['getCart'] export type AddItemOperation = CartOperations['addItem'] diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index 57008a42d..e86b1e28b 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -4,9 +4,9 @@ import type { SWRHook, HookFetcherFn } from '../utils/types' import type { GetCartHook } from '../types/cart' import { Provider, useCommerce } from '..' -export type UseCart< - H extends SWRHook = SWRHook -> = ReturnType +export type UseCart = SWRHook> = ReturnType< + H['useHook'] +> export const fetcher: HookFetcherFn = async ({ options, diff --git a/framework/commerce/types/cart.ts b/framework/commerce/types/cart.ts index 24f649b1a..8a07ebdfb 100644 --- a/framework/commerce/types/cart.ts +++ b/framework/commerce/types/cart.ts @@ -85,35 +85,40 @@ export type CartItemBody = { * Hooks schema */ -export type CartHooks = { - getCart: GetCartHook - addItem: AddItemHook - updateItem: UpdateItemHook - remoteItem: RemoveItemHook +export type CartTypes = { + cart: Cart + item: CartItemBody } -export type GetCartHook = { - data: Cart | null +export type CartHooks = { + getCart: GetCartHook + addItem: AddItemHook + updateItem: UpdateItemHook + removeItem: RemoveItemHook +} + +export type GetCartHook = { + data: T['cart'] | null input: {} fetchInput: { cartId?: string } swrState: { isEmpty: boolean } } -export type AddItemHook = { - data: Cart - body: { item: CartItemBody } - input: CartItemBody - fetchInput: CartItemBody - actionInput: CartItemBody +export type AddItemHook = { + data: T['cart'] + body: { item: T['item'] } + input: T['item'] + fetchInput: T['item'] + actionInput: T['item'] } -export type UpdateItemHook = { - data: Cart - body: { itemId: string; item: CartItemBody } +export type UpdateItemHook = { + data: T['cart'] + body: { itemId: string; item: T['item'] } } -export type RemoveItemHook = { - data: Cart | null +export type RemoveItemHook = { + data: T['cart'] | null body: { itemId: string } } @@ -121,32 +126,40 @@ export type RemoveItemHook = { * API Schema */ -export type CartSchema = { +export type CartSchema = { endpoint: { options: {} - operations: CartOperations + operations: CartOperations } } -export type CartOperations = { - getCart: GetCartOperation - addItem: AddItemOperation - updateItem: UpdateItemOperation - removeItem: RemoveItemOperation +export type CartOperations = { + getCart: GetCartOperation + addItem: AddItemOperation + updateItem: UpdateItemOperation + removeItem: RemoveItemOperation } -export type GetCartOperation = GetCartHook & { +export type GetCartOperation< + T extends CartTypes = CartTypes +> = GetCartHook & { body: { cartId?: string } } -export type AddItemOperation = AddItemHook & { +export type AddItemOperation< + T extends CartTypes = CartTypes +> = AddItemHook & { body: { cartId: string } } -export type UpdateItemOperation = UpdateItemHook & { +export type UpdateItemOperation< + T extends CartTypes = CartTypes +> = UpdateItemHook & { body: { cartId: string } } -export type RemoveItemOperation = RemoveItemHook & { +export type RemoveItemOperation< + T extends CartTypes = CartTypes +> = RemoveItemHook & { body: { cartId: string } }