Remove normalizers from the hooks

This commit is contained in:
Luis Alvarez 2021-03-31 22:53:30 -06:00
parent 6d06d8b705
commit c9f13989ec
7 changed files with 13 additions and 32 deletions

View File

@ -2,8 +2,7 @@ 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/cart/use-add-item' import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
import { normalizeCart } from '../lib/normalize' import type { AddItemHook } from '../types/cart'
import type { BigcommerceCart, AddItemHook, CartTypes } from '../types/cart'
import useCart from './use-cart' import useCart from './use-cart'
export default useAddItem as UseAddItem<typeof handler> 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, ...options,
body: { item }, body: { item },
}) })
return normalizeCart(data) return data
}, },
useHook: ({ fetch }) => () => { useHook: ({ fetch }) => () => {
const { mutate } = useCart() const { mutate } = useCart()

View File

@ -1,7 +1,6 @@
import { useMemo } from 'react' import { useMemo } from 'react'
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useCart, { UseCart } from '@commerce/cart/use-cart' import useCart, { UseCart } from '@commerce/cart/use-cart'
import { normalizeCart } from '../lib/normalize'
import type { GetCartHook } from '../types/cart' import type { GetCartHook } from '../types/cart'
export default useCart as UseCart<typeof handler> export default useCart as UseCart<typeof handler>
@ -11,10 +10,6 @@ export const handler: SWRHook<GetCartHook> = {
url: '/api/bigcommerce/cart', url: '/api/bigcommerce/cart',
method: 'GET', method: 'GET',
}, },
async fetcher({ input: { cartId }, options, fetch }) {
const data = cartId ? await fetch(options) : null
return data && normalizeCart(data)
},
useHook: ({ useData }) => (input) => { useHook: ({ useData }) => (input) => {
const response = useData({ const response = useData({
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },

View File

@ -5,13 +5,7 @@ import type {
} from '@commerce/utils/types' } from '@commerce/utils/types'
import { ValidationError } from '@commerce/utils/errors' import { ValidationError } from '@commerce/utils/errors'
import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item' import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item'
import { normalizeCart } from '../lib/normalize' import type { Cart, LineItem, RemoveItemHook } from '../types/cart'
import type {
Cart,
LineItem,
RemoveItemHook,
BigcommerceCart,
} from '../types/cart'
import useCart from './use-cart' import useCart from './use-cart'
export type RemoveItemFn<T = any> = T extends LineItem export type RemoveItemFn<T = any> = T extends LineItem
@ -34,11 +28,7 @@ export const handler = {
options, options,
fetch, fetch,
}: HookFetcherContext<RemoveItemHook>) { }: HookFetcherContext<RemoveItemHook>) {
const data = await fetch<BigcommerceCart>({ return await fetch({ ...options, body: { itemId } })
...options,
body: { itemId },
})
return normalizeCart(data)
}, },
useHook: ({ fetch }: MutationHookContext<RemoveItemHook>) => < useHook: ({ fetch }: MutationHookContext<RemoveItemHook>) => <
T extends LineItem | undefined = undefined T extends LineItem | undefined = undefined

View File

@ -6,8 +6,7 @@ import type {
} from '@commerce/utils/types' } from '@commerce/utils/types'
import { ValidationError } from '@commerce/utils/errors' import { ValidationError } from '@commerce/utils/errors'
import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item' import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item'
import { normalizeCart } from '../lib/normalize' import type { LineItem, UpdateItemHook } from '../types'
import type { Cart, BigcommerceCart, LineItem, UpdateItemHook } from '../types'
import { handler as removeItemHandler } from './use-remove-item' import { handler as removeItemHandler } from './use-remove-item'
import useCart from './use-cart' import useCart from './use-cart'
@ -42,12 +41,10 @@ export const handler = {
}) })
} }
const data = await fetch<BigcommerceCart>({ return await fetch({
...options, ...options,
body: { itemId, item }, body: { itemId, item },
}) })
return normalizeCart(data)
}, },
useHook: ({ fetch }: MutationHookContext<UpdateItemHook>) => < useHook: ({ fetch }: MutationHookContext<UpdateItemHook>) => <
T extends LineItem | undefined = undefined T extends LineItem | undefined = undefined

View File

@ -13,7 +13,7 @@ export const fetcher: HookFetcherFn<GetCartHook> = async ({
input: { cartId }, input: { cartId },
fetch, fetch,
}) => { }) => {
return cartId ? await fetch({ ...options }) : null return cartId ? await fetch(options) : null
} }
const fn = (provider: Provider) => provider.cart?.useCart! const fn = (provider: Provider) => provider.cart?.useCart!

View File

@ -107,25 +107,25 @@ export type GetCartHook<T extends CartTypes = CartTypes> = {
export type AddItemHook<T extends CartTypes = CartTypes> = { export type AddItemHook<T extends CartTypes = CartTypes> = {
data: T['cart'] data: T['cart']
body: { item: T['itemBody'] }
input: T['itemBody'] input: T['itemBody']
fetchInput: T['itemBody'] fetchInput: T['itemBody']
body: { item: T['itemBody'] }
actionInput: T['itemBody'] actionInput: T['itemBody']
} }
export type UpdateItemHook<T extends CartTypes = CartTypes> = { export type UpdateItemHook<T extends CartTypes = CartTypes> = {
data: T['cart'] | null data: T['cart'] | null
body: { itemId: string; item: T['itemBody'] }
input: { item?: T['item']; wait?: number } input: { item?: T['item']; wait?: number }
fetchInput: { itemId: string; item: T['itemBody'] } fetchInput: { itemId: string; item: T['itemBody'] }
body: { itemId: string; item: T['itemBody'] }
actionInput: T['itemBody'] & { id: string } actionInput: T['itemBody'] & { id: string }
} }
export type RemoveItemHook<T extends CartTypes = CartTypes> = { export type RemoveItemHook<T extends CartTypes = CartTypes> = {
data: T['cart'] | null data: T['cart'] | null
body: { itemId: string }
input: { item?: T['item'] } input: { item?: T['item'] }
fetchInput: { itemId: string } fetchInput: { itemId: string }
body: { itemId: string }
actionInput: { id: string } actionInput: { id: string }
} }

View File

@ -1,9 +1,9 @@
import type { HookFetcherFn } from './types' import type { HookFetcherFn } from './types'
export const SWRFetcher: HookFetcherFn<any, any> = ({ options, fetch }) => export const SWRFetcher: HookFetcherFn<any> = ({ options, fetch }) =>
fetch(options) fetch(options)
export const mutationFetcher: HookFetcherFn<any, any> = ({ export const mutationFetcher: HookFetcherFn<any> = ({
input, input,
options, options,
fetch, fetch,