diff --git a/framework/bigcommerce/wishlist/use-add-item.tsx b/framework/bigcommerce/wishlist/use-add-item.tsx index 402e7da8b..1bf086731 100644 --- a/framework/bigcommerce/wishlist/use-add-item.tsx +++ b/framework/bigcommerce/wishlist/use-add-item.tsx @@ -2,15 +2,15 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item' -import type { ItemBody, AddItemBody } from '../api/wishlist' +import type { AddItemHook } from '../types/wishlist' import useCustomer from '../customer/use-customer' import useWishlist from './use-wishlist' export default useAddItem as UseAddItem -export const handler: MutationHook = { +export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/wishlist', + url: '/api/wishlist', method: 'POST', }, useHook: ({ fetch }) => () => { diff --git a/framework/bigcommerce/wishlist/use-remove-item.tsx b/framework/bigcommerce/wishlist/use-remove-item.tsx index 622f321db..9d25c1439 100644 --- a/framework/bigcommerce/wishlist/use-remove-item.tsx +++ b/framework/bigcommerce/wishlist/use-remove-item.tsx @@ -2,23 +2,17 @@ import { useCallback } from 'react' import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useRemoveItem, { - RemoveItemInput, UseRemoveItem, } from '@commerce/wishlist/use-remove-item' -import type { RemoveItemBody, Wishlist } from '../api/wishlist' +import type { RemoveItemHook } from '../types/wishlist' import useCustomer from '../customer/use-customer' -import useWishlist, { UseWishlistInput } from './use-wishlist' +import useWishlist from './use-wishlist' export default useRemoveItem as UseRemoveItem -export const handler: MutationHook< - Wishlist | null, - { wishlist?: UseWishlistInput }, - RemoveItemInput, - RemoveItemBody -> = { +export const handler: MutationHook = { fetchOptions: { - url: '/api/bigcommerce/wishlist', + url: '/api/wishlist', method: 'DELETE', }, useHook: ({ fetch }) => ({ wishlist } = {}) => { diff --git a/framework/bigcommerce/wishlist/use-wishlist.tsx b/framework/bigcommerce/wishlist/use-wishlist.tsx index 4850d1cd9..134e9cc24 100644 --- a/framework/bigcommerce/wishlist/use-wishlist.tsx +++ b/framework/bigcommerce/wishlist/use-wishlist.tsx @@ -1,19 +1,12 @@ import { useMemo } from 'react' import { SWRHook } from '@commerce/utils/types' import useWishlist, { UseWishlist } from '@commerce/wishlist/use-wishlist' -import type { Wishlist } from '../api/wishlist' +import type { GetWishlistHook } from '../types/wishlist' import useCustomer from '../customer/use-customer' -export type UseWishlistInput = { includeProducts?: boolean } - export default useWishlist as UseWishlist -export const handler: SWRHook< - Wishlist | null, - UseWishlistInput, - { customerId?: number } & UseWishlistInput, - { isEmpty?: boolean } -> = { +export const handler: SWRHook = { fetchOptions: { url: '/api/bigcommerce/wishlist', method: 'GET', diff --git a/framework/commerce/product/use-search.tsx b/framework/commerce/product/use-search.tsx index a80d598ce..342b49e6e 100644 --- a/framework/commerce/product/use-search.tsx +++ b/framework/commerce/product/use-search.tsx @@ -5,7 +5,7 @@ import type { SearchProductsHook } from '../types/product' import type { Provider } from '..' export type UseSearch< - H extends SWRHook = SWRHook + H extends SWRHook> = SWRHook > = ReturnType export const fetcher: HookFetcherFn = SWRFetcher diff --git a/framework/commerce/types/wishlist.ts b/framework/commerce/types/wishlist.ts index 24c0a7c28..7a547e9fc 100644 --- a/framework/commerce/types/wishlist.ts +++ b/framework/commerce/types/wishlist.ts @@ -11,21 +11,42 @@ export type WishlistTypes = { itemBody: WishlistItemBody } +export type GetWishlistHook = { + data: T['wishlist'] | null + body: { includeProducts?: boolean } + input: { includeProducts?: boolean } + fetchInput: { customerId: string; includeProducts?: boolean } + swrState: { isEmpty: boolean } +} + +export type AddItemHook = { + data: T['wishlist'] + body: { item: T['itemBody'] } + fetchInput: { item: T['itemBody'] } + actionInput: T['itemBody'] +} + +export type RemoveItemHook = { + data: T['wishlist'] | null + body: { itemId: string } + fetchInput: { itemId: string } + actionInput: { id: string } + input: { wishlist?: { includeProducts?: boolean } } +} + export type WishlistSchema = { endpoint: { options: {} handlers: { - getWishlist: { + getWishlist: GetWishlistHook & { data: T['wishlist'] | null - body: { customerToken?: string; includeProducts?: boolean } + body: { customerToken?: string } } - addItem: { - data: T['wishlist'] - body: { customerToken?: string; item: T['itemBody'] } + addItem: AddItemHook & { + body: { customerToken?: string } } - removeItem: { - data: T['wishlist'] | null - body: { customerToken?: string; itemId: string } + removeItem: RemoveItemHook & { + body: { customerToken?: string } } } } diff --git a/framework/commerce/wishlist/use-add-item.tsx b/framework/commerce/wishlist/use-add-item.tsx index 11c8cc241..f464be1ca 100644 --- a/framework/commerce/wishlist/use-add-item.tsx +++ b/framework/commerce/wishlist/use-add-item.tsx @@ -1,10 +1,11 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { MutationHook } from '../utils/types' +import type { AddItemHook } from '../types/wishlist' import type { Provider } from '..' export type UseAddItem< - H extends MutationHook = MutationHook + H extends MutationHook> = MutationHook > = ReturnType export const fetcher = mutationFetcher diff --git a/framework/commerce/wishlist/use-remove-item.tsx b/framework/commerce/wishlist/use-remove-item.tsx index c8c34a5af..4419c17af 100644 --- a/framework/commerce/wishlist/use-remove-item.tsx +++ b/framework/commerce/wishlist/use-remove-item.tsx @@ -1,28 +1,20 @@ import { useHook, useMutationHook } from '../utils/use-hook' import { mutationFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, MutationHook } from '../utils/types' +import type { RemoveItemHook } from '../types/wishlist' import type { Provider } from '..' -export type RemoveItemInput = { - id: string | number -} - export type UseRemoveItem< - H extends MutationHook = MutationHook< - any | null, - { wishlist?: any }, - RemoveItemInput, - {} - > + H extends MutationHook> = MutationHook > = ReturnType -export const fetcher: HookFetcherFn = mutationFetcher +export const fetcher: HookFetcherFn = mutationFetcher const fn = (provider: Provider) => provider.wishlist?.useRemoveItem! -const useRemoveItem: UseRemoveItem = (input) => { +const useRemoveItem: UseRemoveItem = (...args) => { const hook = useHook(fn) - return useMutationHook({ fetcher, ...hook })(input) + return useMutationHook({ fetcher, ...hook })(...args) } export default useRemoveItem diff --git a/framework/commerce/wishlist/use-wishlist.tsx b/framework/commerce/wishlist/use-wishlist.tsx index 7a93b20b1..672203f79 100644 --- a/framework/commerce/wishlist/use-wishlist.tsx +++ b/framework/commerce/wishlist/use-wishlist.tsx @@ -1,25 +1,20 @@ import { useHook, useSWRHook } from '../utils/use-hook' import { SWRFetcher } from '../utils/default-fetcher' import type { HookFetcherFn, SWRHook } from '../utils/types' -import type { Wishlist } from '../types' +import type { GetWishlistHook } from '../types/wishlist' import type { Provider } from '..' export type UseWishlist< - H extends SWRHook = SWRHook< - Wishlist | null, - { includeProducts?: boolean }, - { customerId?: number; includeProducts: boolean }, - { isEmpty?: boolean } - > + H extends SWRHook> = SWRHook > = ReturnType -export const fetcher: HookFetcherFn = SWRFetcher +export const fetcher: HookFetcherFn = SWRFetcher const fn = (provider: Provider) => provider.wishlist?.useWishlist! -const useWishlist: UseWishlist = (input) => { +const useWishlist: UseWishlist = (...args) => { const hook = useHook(fn) - return useSWRHook({ fetcher, ...hook })(input) + return useSWRHook({ fetcher, ...hook })(...args) } export default useWishlist