Updated types for useWishlist

This commit is contained in:
Luis Alvarez 2021-05-25 15:45:27 -05:00
parent 0e6372cf54
commit db8218d2c7
8 changed files with 51 additions and 55 deletions

View File

@ -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<typeof handler>
export const handler: MutationHook<any, {}, ItemBody, AddItemBody> = {
export const handler: MutationHook<AddItemHook> = {
fetchOptions: {
url: '/api/bigcommerce/wishlist',
url: '/api/wishlist',
method: 'POST',
},
useHook: ({ fetch }) => () => {

View File

@ -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<typeof handler>
export const handler: MutationHook<
Wishlist | null,
{ wishlist?: UseWishlistInput },
RemoveItemInput,
RemoveItemBody
> = {
export const handler: MutationHook<RemoveItemHook> = {
fetchOptions: {
url: '/api/bigcommerce/wishlist',
url: '/api/wishlist',
method: 'DELETE',
},
useHook: ({ fetch }) => ({ wishlist } = {}) => {

View File

@ -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<typeof handler>
export const handler: SWRHook<
Wishlist | null,
UseWishlistInput,
{ customerId?: number } & UseWishlistInput,
{ isEmpty?: boolean }
> = {
export const handler: SWRHook<GetWishlistHook> = {
fetchOptions: {
url: '/api/bigcommerce/wishlist',
method: 'GET',

View File

@ -5,7 +5,7 @@ import type { SearchProductsHook } from '../types/product'
import type { Provider } from '..'
export type UseSearch<
H extends SWRHook<any> = SWRHook<SearchProductsHook>
H extends SWRHook<SearchProductsHook<any>> = SWRHook<SearchProductsHook>
> = ReturnType<H['useHook']>
export const fetcher: HookFetcherFn<SearchProductsHook> = SWRFetcher

View File

@ -11,21 +11,42 @@ export type WishlistTypes = {
itemBody: WishlistItemBody
}
export type GetWishlistHook<T extends WishlistTypes = WishlistTypes> = {
data: T['wishlist'] | null
body: { includeProducts?: boolean }
input: { includeProducts?: boolean }
fetchInput: { customerId: string; includeProducts?: boolean }
swrState: { isEmpty: boolean }
}
export type AddItemHook<T extends WishlistTypes = WishlistTypes> = {
data: T['wishlist']
body: { item: T['itemBody'] }
fetchInput: { item: T['itemBody'] }
actionInput: T['itemBody']
}
export type RemoveItemHook<T extends WishlistTypes = WishlistTypes> = {
data: T['wishlist'] | null
body: { itemId: string }
fetchInput: { itemId: string }
actionInput: { id: string }
input: { wishlist?: { includeProducts?: boolean } }
}
export type WishlistSchema<T extends WishlistTypes = WishlistTypes> = {
endpoint: {
options: {}
handlers: {
getWishlist: {
getWishlist: GetWishlistHook<T> & {
data: T['wishlist'] | null
body: { customerToken?: string; includeProducts?: boolean }
body: { customerToken?: string }
}
addItem: {
data: T['wishlist']
body: { customerToken?: string; item: T['itemBody'] }
addItem: AddItemHook<T> & {
body: { customerToken?: string }
}
removeItem: {
data: T['wishlist'] | null
body: { customerToken?: string; itemId: string }
removeItem: RemoveItemHook<T> & {
body: { customerToken?: string }
}
}
}

View File

@ -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<any, any, any> = MutationHook<any, {}, {}>
H extends MutationHook<AddItemHook<any>> = MutationHook<AddItemHook>
> = ReturnType<H['useHook']>
export const fetcher = mutationFetcher

View File

@ -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<any, any, any> = MutationHook<
any | null,
{ wishlist?: any },
RemoveItemInput,
{}
>
H extends MutationHook<RemoveItemHook<any>> = MutationHook<RemoveItemHook>
> = ReturnType<H['useHook']>
export const fetcher: HookFetcherFn<any | null, {}> = mutationFetcher
export const fetcher: HookFetcherFn<RemoveItemHook> = 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

View File

@ -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<any, any, any> = SWRHook<
Wishlist | null,
{ includeProducts?: boolean },
{ customerId?: number; includeProducts: boolean },
{ isEmpty?: boolean }
>
H extends SWRHook<GetWishlistHook<any>> = SWRHook<GetWishlistHook>
> = ReturnType<H['useHook']>
export const fetcher: HookFetcherFn<Wishlist | null, any> = SWRFetcher
export const fetcher: HookFetcherFn<GetWishlistHook> = 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