diff --git a/framework/bigcommerce/cart/use-add-item.tsx b/framework/bigcommerce/cart/use-add-item.tsx index 525fa98a3..3ce0ffddf 100644 --- a/framework/bigcommerce/cart/use-add-item.tsx +++ b/framework/bigcommerce/cart/use-add-item.tsx @@ -3,12 +3,11 @@ import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item' import { normalizeCart } from '../lib/normalize' -import type { BigcommerceCart, AddItemHook } from '../types/cart' +import type { BigcommerceCart, AddItemHook, CartTypes } from '../types/cart' import useCart from './use-cart' export default useAddItem as UseAddItem -// export const handler: MutationHook = { export const handler: MutationHook = { fetchOptions: { url: '/api/bigcommerce/cart', diff --git a/framework/bigcommerce/cart/use-remove-item.tsx b/framework/bigcommerce/cart/use-remove-item.tsx index 186780d6a..db6914b04 100644 --- a/framework/bigcommerce/cart/use-remove-item.tsx +++ b/framework/bigcommerce/cart/use-remove-item.tsx @@ -4,26 +4,23 @@ import type { HookFetcherContext, } from '@commerce/utils/types' import { ValidationError } from '@commerce/utils/errors' -import useRemoveItem, { - RemoveItemInput as RemoveItemInputBase, - UseRemoveItem, -} from '@commerce/cart/use-remove-item' +import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item' import { normalizeCart } from '../lib/normalize' import type { - RemoveCartItemBody, Cart, - BigcommerceCart, LineItem, -} from '../types' + RemoveItemHook, + BigcommerceCart, +} from '../types/cart' import useCart from './use-cart' export type RemoveItemFn = T extends LineItem - ? (input?: RemoveItemInput) => Promise - : (input: RemoveItemInput) => Promise + ? (input?: RemoveItemActionInput) => Promise + : (input: RemoveItemActionInput) => Promise -export type RemoveItemInput = T extends LineItem - ? Partial - : RemoveItemInputBase +export type RemoveItemActionInput = T extends LineItem + ? Partial + : RemoveItemHook['actionInput'] export default useRemoveItem as UseRemoveItem @@ -36,16 +33,14 @@ export const handler = { input: { itemId }, options, fetch, - }: HookFetcherContext) { + }: HookFetcherContext) { const data = await fetch({ ...options, body: { itemId }, }) return normalizeCart(data) }, - useHook: ({ - fetch, - }: MutationHookContext) => < + useHook: ({ fetch }: MutationHookContext) => < T extends LineItem | undefined = undefined >( ctx: { item?: T } = {} diff --git a/framework/bigcommerce/cart/use-update-item.tsx b/framework/bigcommerce/cart/use-update-item.tsx index f1840f806..55b99c5a8 100644 --- a/framework/bigcommerce/cart/use-update-item.tsx +++ b/framework/bigcommerce/cart/use-update-item.tsx @@ -5,23 +5,15 @@ import type { HookFetcherContext, } from '@commerce/utils/types' import { ValidationError } from '@commerce/utils/errors' -import useUpdateItem, { - UpdateItemInput as UpdateItemInputBase, - UseUpdateItem, -} from '@commerce/cart/use-update-item' +import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item' import { normalizeCart } from '../lib/normalize' -import type { - UpdateCartItemBody, - Cart, - BigcommerceCart, - LineItem, -} from '../types' +import type { Cart, BigcommerceCart, LineItem, UpdateItemHook } from '../types' import { handler as removeItemHandler } from './use-remove-item' import useCart from './use-cart' -export type UpdateItemInput = T extends LineItem - ? Partial> - : UpdateItemInputBase +export type UpdateItemActionInput = T extends LineItem + ? Partial + : UpdateItemHook['actionInput'] export default useUpdateItem as UseUpdateItem @@ -34,7 +26,7 @@ export const handler = { input: { itemId, item }, options, fetch, - }: HookFetcherContext) { + }: HookFetcherContext) { if (Number.isInteger(item.quantity)) { // Also allow the update hook to remove an item if the quantity is lower than 1 if (item.quantity! < 1) { @@ -50,16 +42,14 @@ export const handler = { }) } - const data = await fetch({ + const data = await fetch({ ...options, body: { itemId, item }, }) return normalizeCart(data) }, - useHook: ({ - fetch, - }: MutationHookContext) => < + useHook: ({ fetch }: MutationHookContext) => < T extends LineItem | undefined = undefined >( ctx: { @@ -71,7 +61,7 @@ export const handler = { const { mutate } = useCart() as any return useCallback( - debounce(async (input: UpdateItemInput) => { + debounce(async (input: UpdateItemActionInput) => { const itemId = input.id ?? item?.id const productId = input.productId ?? item?.productId const variantId = input.productId ?? item?.variantId diff --git a/framework/bigcommerce/types/cart.ts b/framework/bigcommerce/types/cart.ts index d0770056c..09b792dee 100644 --- a/framework/bigcommerce/types/cart.ts +++ b/framework/bigcommerce/types/cart.ts @@ -45,7 +45,8 @@ export type CartItemBody = Core.CartItemBody & { export type CartTypes = { cart: Cart - item: CartItemBody + item: Core.LineItem + itemBody: CartItemBody } export type CartHooks = Core.CartHooks diff --git a/framework/commerce/cart/use-add-item.tsx b/framework/commerce/cart/use-add-item.tsx index ab95979fc..a9d1db146 100644 --- a/framework/commerce/cart/use-add-item.tsx +++ b/framework/commerce/cart/use-add-item.tsx @@ -5,6 +5,10 @@ import type { AddItemHook } from '../types/cart' import type { Provider } from '..' export type UseAddItem< + H extends MutationHook> = MutationHook +> = ReturnType + +export type UseAddItem2< H extends MutationHook = MutationHook > = ReturnType diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index e86b1e28b..4950838c3 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 = SWRHook> = ReturnType< - H['useHook'] -> +export type UseCart< + H extends SWRHook> = SWRHook +> = ReturnType export const fetcher: HookFetcherFn = async ({ options, diff --git a/framework/commerce/cart/use-remove-item.tsx b/framework/commerce/cart/use-remove-item.tsx index a9d1b37d2..f2bb43ffb 100644 --- a/framework/commerce/cart/use-remove-item.tsx +++ b/framework/commerce/cart/use-remove-item.tsx @@ -1,29 +1,14 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, MutationHook } from '../utils/types' -import type { Cart, LineItem, RemoveCartItemBody } from '../types' +import type { RemoveItemHook } from '../types/cart' import type { Provider } from '..' -/** - * Input expected by the action returned by the `useRemoveItem` hook - */ -export type RemoveItemInput = { - id: string -} - export type UseRemoveItem< - H extends MutationHook = MutationHook< - Cart | null, - { item?: LineItem }, - RemoveItemInput, - RemoveCartItemBody - > + H extends MutationHook> = MutationHook > = ReturnType -export const fetcher: HookFetcherFn< - Cart | null, - RemoveCartItemBody -> = mutationFetcher +export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.cart?.useRemoveItem! diff --git a/framework/commerce/cart/use-update-item.tsx b/framework/commerce/cart/use-update-item.tsx index f8d0f1a40..2527732eb 100644 --- a/framework/commerce/cart/use-update-item.tsx +++ b/framework/commerce/cart/use-update-item.tsx @@ -1,32 +1,14 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, MutationHook } from '../utils/types' -import type { Cart, CartItemBody, LineItem, UpdateCartItemBody } from '../types' +import type { UpdateItemHook } from '../types/cart' import type { Provider } from '..' -/** - * Input expected by the action returned by the `useUpdateItem` hook - */ -export type UpdateItemInput = T & { - id: string -} - export type UseUpdateItem< - H extends MutationHook = MutationHook< - Cart | null, - { - item?: LineItem - wait?: number - }, - UpdateItemInput, - UpdateCartItemBody - > + H extends MutationHook> = MutationHook > = ReturnType -export const fetcher: HookFetcherFn< - Cart | null, - UpdateCartItemBody -> = mutationFetcher +export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.cart?.useUpdateItem! diff --git a/framework/commerce/types/cart.ts b/framework/commerce/types/cart.ts index 8a07ebdfb..7a366ab1b 100644 --- a/framework/commerce/types/cart.ts +++ b/framework/commerce/types/cart.ts @@ -87,7 +87,8 @@ export type CartItemBody = { export type CartTypes = { cart: Cart - item: CartItemBody + item: LineItem + itemBody: CartItemBody } export type CartHooks = { @@ -106,20 +107,26 @@ export type GetCartHook = { export type AddItemHook = { data: T['cart'] - body: { item: T['item'] } - input: T['item'] - fetchInput: T['item'] - actionInput: T['item'] + body: { item: T['itemBody'] } + input: T['itemBody'] + fetchInput: T['itemBody'] + actionInput: T['itemBody'] } export type UpdateItemHook = { - data: T['cart'] - body: { itemId: string; item: T['item'] } + data: T['cart'] | null + body: { itemId: string; item: T['itemBody'] } + input: { item?: T['item']; wait?: number } + fetchInput: { itemId: string; item: T['itemBody'] } + actionInput: T['itemBody'] & { id: string } } export type RemoveItemHook = { data: T['cart'] | null body: { itemId: string } + input: { item?: T['item'] } + fetchInput: { itemId: string } + actionInput: { id: string } } /** @@ -155,6 +162,7 @@ export type AddItemOperation< export type UpdateItemOperation< T extends CartTypes = CartTypes > = UpdateItemHook & { + data: T['cart'] body: { cartId: string } } diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index 9f9f8fd8a..6ef673d77 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -36,18 +36,14 @@ export type HookFetcher = ( fetch: (options: FetcherOptions) => Promise ) => Data | Promise -export type HookFetcherFn< - H extends HookSchemaBase, - Result = any, - Body = H['body'] -> = ( - context: HookFetcherContext +export type HookFetcherFn = ( + context: HookFetcherContext ) => H['data'] | Promise -export type HookFetcherContext = { +export type HookFetcherContext = { options: HookFetcherOptions - input: Input - fetch: (options: FetcherOptions) => Promise + input: H['fetchInput'] + fetch: (options: FetcherOptions) => Promise } export type HookFetcherOptions = { method?: string } & (