mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
Updated types for useWishlist
This commit is contained in:
parent
0e6372cf54
commit
db8218d2c7
@ -2,15 +2,15 @@ import { useCallback } from 'react'
|
|||||||
import type { MutationHook } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item'
|
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 useCustomer from '../customer/use-customer'
|
||||||
import useWishlist from './use-wishlist'
|
import useWishlist from './use-wishlist'
|
||||||
|
|
||||||
export default useAddItem as UseAddItem<typeof handler>
|
export default useAddItem as UseAddItem<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<any, {}, ItemBody, AddItemBody> = {
|
export const handler: MutationHook<AddItemHook> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: '/api/bigcommerce/wishlist',
|
url: '/api/wishlist',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }) => () => {
|
useHook: ({ fetch }) => () => {
|
||||||
|
@ -2,23 +2,17 @@ import { useCallback } from 'react'
|
|||||||
import type { MutationHook } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useRemoveItem, {
|
import useRemoveItem, {
|
||||||
RemoveItemInput,
|
|
||||||
UseRemoveItem,
|
UseRemoveItem,
|
||||||
} from '@commerce/wishlist/use-remove-item'
|
} 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 useCustomer from '../customer/use-customer'
|
||||||
import useWishlist, { UseWishlistInput } from './use-wishlist'
|
import useWishlist from './use-wishlist'
|
||||||
|
|
||||||
export default useRemoveItem as UseRemoveItem<typeof handler>
|
export default useRemoveItem as UseRemoveItem<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<
|
export const handler: MutationHook<RemoveItemHook> = {
|
||||||
Wishlist | null,
|
|
||||||
{ wishlist?: UseWishlistInput },
|
|
||||||
RemoveItemInput,
|
|
||||||
RemoveItemBody
|
|
||||||
> = {
|
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: '/api/bigcommerce/wishlist',
|
url: '/api/wishlist',
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }) => ({ wishlist } = {}) => {
|
useHook: ({ fetch }) => ({ wishlist } = {}) => {
|
||||||
|
@ -1,19 +1,12 @@
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import { SWRHook } from '@commerce/utils/types'
|
import { SWRHook } from '@commerce/utils/types'
|
||||||
import useWishlist, { UseWishlist } from '@commerce/wishlist/use-wishlist'
|
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'
|
import useCustomer from '../customer/use-customer'
|
||||||
|
|
||||||
export type UseWishlistInput = { includeProducts?: boolean }
|
|
||||||
|
|
||||||
export default useWishlist as UseWishlist<typeof handler>
|
export default useWishlist as UseWishlist<typeof handler>
|
||||||
|
|
||||||
export const handler: SWRHook<
|
export const handler: SWRHook<GetWishlistHook> = {
|
||||||
Wishlist | null,
|
|
||||||
UseWishlistInput,
|
|
||||||
{ customerId?: number } & UseWishlistInput,
|
|
||||||
{ isEmpty?: boolean }
|
|
||||||
> = {
|
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: '/api/bigcommerce/wishlist',
|
url: '/api/bigcommerce/wishlist',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
@ -5,7 +5,7 @@ import type { SearchProductsHook } from '../types/product'
|
|||||||
import type { Provider } from '..'
|
import type { Provider } from '..'
|
||||||
|
|
||||||
export type UseSearch<
|
export type UseSearch<
|
||||||
H extends SWRHook<any> = SWRHook<SearchProductsHook>
|
H extends SWRHook<SearchProductsHook<any>> = SWRHook<SearchProductsHook>
|
||||||
> = ReturnType<H['useHook']>
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
export const fetcher: HookFetcherFn<SearchProductsHook> = SWRFetcher
|
export const fetcher: HookFetcherFn<SearchProductsHook> = SWRFetcher
|
||||||
|
@ -11,21 +11,42 @@ export type WishlistTypes = {
|
|||||||
itemBody: WishlistItemBody
|
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> = {
|
export type WishlistSchema<T extends WishlistTypes = WishlistTypes> = {
|
||||||
endpoint: {
|
endpoint: {
|
||||||
options: {}
|
options: {}
|
||||||
handlers: {
|
handlers: {
|
||||||
getWishlist: {
|
getWishlist: GetWishlistHook<T> & {
|
||||||
data: T['wishlist'] | null
|
data: T['wishlist'] | null
|
||||||
body: { customerToken?: string; includeProducts?: boolean }
|
body: { customerToken?: string }
|
||||||
}
|
}
|
||||||
addItem: {
|
addItem: AddItemHook<T> & {
|
||||||
data: T['wishlist']
|
body: { customerToken?: string }
|
||||||
body: { customerToken?: string; item: T['itemBody'] }
|
|
||||||
}
|
}
|
||||||
removeItem: {
|
removeItem: RemoveItemHook<T> & {
|
||||||
data: T['wishlist'] | null
|
body: { customerToken?: string }
|
||||||
body: { customerToken?: string; itemId: string }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
import { useHook, useMutationHook } from '../utils/use-hook'
|
import { useHook, useMutationHook } from '../utils/use-hook'
|
||||||
import { mutationFetcher } from '../utils/default-fetcher'
|
import { mutationFetcher } from '../utils/default-fetcher'
|
||||||
import type { MutationHook } from '../utils/types'
|
import type { MutationHook } from '../utils/types'
|
||||||
|
import type { AddItemHook } from '../types/wishlist'
|
||||||
import type { Provider } from '..'
|
import type { Provider } from '..'
|
||||||
|
|
||||||
export type UseAddItem<
|
export type UseAddItem<
|
||||||
H extends MutationHook<any, any, any> = MutationHook<any, {}, {}>
|
H extends MutationHook<AddItemHook<any>> = MutationHook<AddItemHook>
|
||||||
> = ReturnType<H['useHook']>
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
export const fetcher = mutationFetcher
|
export const fetcher = mutationFetcher
|
||||||
|
@ -1,28 +1,20 @@
|
|||||||
import { useHook, useMutationHook } from '../utils/use-hook'
|
import { useHook, useMutationHook } from '../utils/use-hook'
|
||||||
import { mutationFetcher } from '../utils/default-fetcher'
|
import { mutationFetcher } from '../utils/default-fetcher'
|
||||||
import type { HookFetcherFn, MutationHook } from '../utils/types'
|
import type { HookFetcherFn, MutationHook } from '../utils/types'
|
||||||
|
import type { RemoveItemHook } from '../types/wishlist'
|
||||||
import type { Provider } from '..'
|
import type { Provider } from '..'
|
||||||
|
|
||||||
export type RemoveItemInput = {
|
|
||||||
id: string | number
|
|
||||||
}
|
|
||||||
|
|
||||||
export type UseRemoveItem<
|
export type UseRemoveItem<
|
||||||
H extends MutationHook<any, any, any> = MutationHook<
|
H extends MutationHook<RemoveItemHook<any>> = MutationHook<RemoveItemHook>
|
||||||
any | null,
|
|
||||||
{ wishlist?: any },
|
|
||||||
RemoveItemInput,
|
|
||||||
{}
|
|
||||||
>
|
|
||||||
> = ReturnType<H['useHook']>
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
export const fetcher: HookFetcherFn<any | null, {}> = mutationFetcher
|
export const fetcher: HookFetcherFn<RemoveItemHook> = mutationFetcher
|
||||||
|
|
||||||
const fn = (provider: Provider) => provider.wishlist?.useRemoveItem!
|
const fn = (provider: Provider) => provider.wishlist?.useRemoveItem!
|
||||||
|
|
||||||
const useRemoveItem: UseRemoveItem = (input) => {
|
const useRemoveItem: UseRemoveItem = (...args) => {
|
||||||
const hook = useHook(fn)
|
const hook = useHook(fn)
|
||||||
return useMutationHook({ fetcher, ...hook })(input)
|
return useMutationHook({ fetcher, ...hook })(...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useRemoveItem
|
export default useRemoveItem
|
||||||
|
@ -1,25 +1,20 @@
|
|||||||
import { useHook, useSWRHook } from '../utils/use-hook'
|
import { useHook, useSWRHook } from '../utils/use-hook'
|
||||||
import { SWRFetcher } from '../utils/default-fetcher'
|
import { SWRFetcher } from '../utils/default-fetcher'
|
||||||
import type { HookFetcherFn, SWRHook } from '../utils/types'
|
import type { HookFetcherFn, SWRHook } from '../utils/types'
|
||||||
import type { Wishlist } from '../types'
|
import type { GetWishlistHook } from '../types/wishlist'
|
||||||
import type { Provider } from '..'
|
import type { Provider } from '..'
|
||||||
|
|
||||||
export type UseWishlist<
|
export type UseWishlist<
|
||||||
H extends SWRHook<any, any, any> = SWRHook<
|
H extends SWRHook<GetWishlistHook<any>> = SWRHook<GetWishlistHook>
|
||||||
Wishlist | null,
|
|
||||||
{ includeProducts?: boolean },
|
|
||||||
{ customerId?: number; includeProducts: boolean },
|
|
||||||
{ isEmpty?: boolean }
|
|
||||||
>
|
|
||||||
> = ReturnType<H['useHook']>
|
> = 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 fn = (provider: Provider) => provider.wishlist?.useWishlist!
|
||||||
|
|
||||||
const useWishlist: UseWishlist = (input) => {
|
const useWishlist: UseWishlist = (...args) => {
|
||||||
const hook = useHook(fn)
|
const hook = useHook(fn)
|
||||||
return useSWRHook({ fetcher, ...hook })(input)
|
return useSWRHook({ fetcher, ...hook })(...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useWishlist
|
export default useWishlist
|
||||||
|
Loading…
x
Reference in New Issue
Block a user