mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Remove normalizers from the hooks
This commit is contained in:
parent
6d06d8b705
commit
c9f13989ec
@ -2,8 +2,7 @@ import { useCallback } from 'react'
|
||||
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, CartTypes } from '../types/cart'
|
||||
import type { AddItemHook } from '../types/cart'
|
||||
import useCart from './use-cart'
|
||||
|
||||
export default useAddItem as UseAddItem<typeof handler>
|
||||
@ -23,12 +22,12 @@ export const handler: MutationHook<AddItemHook> = {
|
||||
})
|
||||
}
|
||||
|
||||
const data = await fetch<BigcommerceCart>({
|
||||
const data = await fetch({
|
||||
...options,
|
||||
body: { item },
|
||||
})
|
||||
|
||||
return normalizeCart(data)
|
||||
return data
|
||||
},
|
||||
useHook: ({ fetch }) => () => {
|
||||
const { mutate } = useCart()
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { useMemo } from 'react'
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type { GetCartHook } from '../types/cart'
|
||||
|
||||
export default useCart as UseCart<typeof handler>
|
||||
@ -11,10 +10,6 @@ export const handler: SWRHook<GetCartHook> = {
|
||||
url: '/api/bigcommerce/cart',
|
||||
method: 'GET',
|
||||
},
|
||||
async fetcher({ input: { cartId }, options, fetch }) {
|
||||
const data = cartId ? await fetch(options) : null
|
||||
return data && normalizeCart(data)
|
||||
},
|
||||
useHook: ({ useData }) => (input) => {
|
||||
const response = useData({
|
||||
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
|
||||
|
@ -5,13 +5,7 @@ import type {
|
||||
} from '@commerce/utils/types'
|
||||
import { ValidationError } from '@commerce/utils/errors'
|
||||
import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type {
|
||||
Cart,
|
||||
LineItem,
|
||||
RemoveItemHook,
|
||||
BigcommerceCart,
|
||||
} from '../types/cart'
|
||||
import type { Cart, LineItem, RemoveItemHook } from '../types/cart'
|
||||
import useCart from './use-cart'
|
||||
|
||||
export type RemoveItemFn<T = any> = T extends LineItem
|
||||
@ -34,11 +28,7 @@ export const handler = {
|
||||
options,
|
||||
fetch,
|
||||
}: HookFetcherContext<RemoveItemHook>) {
|
||||
const data = await fetch<BigcommerceCart>({
|
||||
...options,
|
||||
body: { itemId },
|
||||
})
|
||||
return normalizeCart(data)
|
||||
return await fetch({ ...options, body: { itemId } })
|
||||
},
|
||||
useHook: ({ fetch }: MutationHookContext<RemoveItemHook>) => <
|
||||
T extends LineItem | undefined = undefined
|
||||
|
@ -6,8 +6,7 @@ import type {
|
||||
} from '@commerce/utils/types'
|
||||
import { ValidationError } from '@commerce/utils/errors'
|
||||
import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type { Cart, BigcommerceCart, LineItem, UpdateItemHook } from '../types'
|
||||
import type { LineItem, UpdateItemHook } from '../types'
|
||||
import { handler as removeItemHandler } from './use-remove-item'
|
||||
import useCart from './use-cart'
|
||||
|
||||
@ -42,12 +41,10 @@ export const handler = {
|
||||
})
|
||||
}
|
||||
|
||||
const data = await fetch<BigcommerceCart>({
|
||||
return await fetch({
|
||||
...options,
|
||||
body: { itemId, item },
|
||||
})
|
||||
|
||||
return normalizeCart(data)
|
||||
},
|
||||
useHook: ({ fetch }: MutationHookContext<UpdateItemHook>) => <
|
||||
T extends LineItem | undefined = undefined
|
||||
|
@ -13,7 +13,7 @@ export const fetcher: HookFetcherFn<GetCartHook> = async ({
|
||||
input: { cartId },
|
||||
fetch,
|
||||
}) => {
|
||||
return cartId ? await fetch({ ...options }) : null
|
||||
return cartId ? await fetch(options) : null
|
||||
}
|
||||
|
||||
const fn = (provider: Provider) => provider.cart?.useCart!
|
||||
|
@ -107,25 +107,25 @@ export type GetCartHook<T extends CartTypes = CartTypes> = {
|
||||
|
||||
export type AddItemHook<T extends CartTypes = CartTypes> = {
|
||||
data: T['cart']
|
||||
body: { item: T['itemBody'] }
|
||||
input: T['itemBody']
|
||||
fetchInput: T['itemBody']
|
||||
body: { item: T['itemBody'] }
|
||||
actionInput: T['itemBody']
|
||||
}
|
||||
|
||||
export type UpdateItemHook<T extends CartTypes = CartTypes> = {
|
||||
data: T['cart'] | null
|
||||
body: { itemId: string; item: T['itemBody'] }
|
||||
input: { item?: T['item']; wait?: number }
|
||||
fetchInput: { itemId: string; item: T['itemBody'] }
|
||||
body: { itemId: string; item: T['itemBody'] }
|
||||
actionInput: T['itemBody'] & { id: string }
|
||||
}
|
||||
|
||||
export type RemoveItemHook<T extends CartTypes = CartTypes> = {
|
||||
data: T['cart'] | null
|
||||
body: { itemId: string }
|
||||
input: { item?: T['item'] }
|
||||
fetchInput: { itemId: string }
|
||||
body: { itemId: string }
|
||||
actionInput: { id: string }
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
import type { HookFetcherFn } from './types'
|
||||
|
||||
export const SWRFetcher: HookFetcherFn<any, any> = ({ options, fetch }) =>
|
||||
export const SWRFetcher: HookFetcherFn<any> = ({ options, fetch }) =>
|
||||
fetch(options)
|
||||
|
||||
export const mutationFetcher: HookFetcherFn<any, any> = ({
|
||||
export const mutationFetcher: HookFetcherFn<any> = ({
|
||||
input,
|
||||
options,
|
||||
fetch,
|
||||
|
Loading…
x
Reference in New Issue
Block a user