Updated mutation types

This commit is contained in:
Luis Alvarez 2021-03-31 15:48:11 -06:00
parent 06dee5fcab
commit 543054225f
5 changed files with 18 additions and 13 deletions

View File

@ -3,17 +3,13 @@ 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 { normalizeCart } from '../lib/normalize'
import type { import type { BigcommerceCart, AddItemHook } from '../types/cart'
Cart,
BigcommerceCart,
CartItemBody,
AddCartItemBody,
} from '../types'
import useCart from './use-cart' import useCart from './use-cart'
export default useAddItem as UseAddItem<typeof handler> export default useAddItem as UseAddItem<typeof handler>
export const handler: MutationHook<Cart, {}, CartItemBody> = { // export const handler: MutationHook<Cart, {}, CartItemBody> = {
export const handler: MutationHook<AddItemHook> = {
fetchOptions: { fetchOptions: {
url: '/api/bigcommerce/cart', url: '/api/bigcommerce/cart',
method: 'POST', method: 'POST',
@ -28,7 +24,7 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
}) })
} }
const data = await fetch<BigcommerceCart, AddCartItemBody>({ const data = await fetch<BigcommerceCart>({
...options, ...options,
body: { item }, body: { item },
}) })

View File

@ -45,7 +45,13 @@ export type CartItemBody = Core.CartItemBody & {
export type CartHooks = Core.CartHooks & { export type CartHooks = Core.CartHooks & {
getCart: { data: Cart | null } getCart: { data: Cart | null }
addItem: { data: Cart; body: { item: CartItemBody } } addItem: {
data: Cart
body: { item: CartItemBody }
input: CartItemBody
fetchInput: CartItemBody
actionInput: CartItemBody
}
updateItem: { data: Cart; body: { item: CartItemBody } } updateItem: { data: Cart; body: { item: CartItemBody } }
removeItem: { data: Cart | null } removeItem: { data: Cart | null }
} }

View File

@ -5,7 +5,7 @@ import type { AddItemHook } from '../types/cart'
import type { Provider } from '..' import type { Provider } from '..'
export type UseAddItem< export type UseAddItem<
H extends MutationHook<AddItemHook> = MutationHook<AddItemHook> H extends MutationHook<any> = MutationHook<AddItemHook>
> = ReturnType<H['useHook']> > = ReturnType<H['useHook']>
export const fetcher: HookFetcherFn<AddItemHook> = mutationFetcher export const fetcher: HookFetcherFn<AddItemHook> = mutationFetcher

View File

@ -103,7 +103,8 @@ export type AddItemHook = {
data: Cart data: Cart
body: { item: CartItemBody } body: { item: CartItemBody }
input: CartItemBody input: CartItemBody
fetchInput: { item: CartItemBody } fetchInput: CartItemBody
actionInput: CartItemBody
} }
export type UpdateItemHook = { export type UpdateItemHook = {

View File

@ -39,7 +39,7 @@ export type HookFetcher<Data, Input = null, Result = any> = (
export type HookFetcherFn< export type HookFetcherFn<
H extends HookSchemaBase, H extends HookSchemaBase,
Result = any, Result = any,
Body = any Body = H['body']
> = ( > = (
context: HookFetcherContext<H['fetchInput'], Result, Body> context: HookFetcherContext<H['fetchInput'], Result, Body>
) => H['data'] | Promise<H['data']> ) => H['data'] | Promise<H['data']>
@ -77,6 +77,8 @@ export type HookSchemaBase = {
input: {} input: {}
// Input expected before doing a fetch operation // Input expected before doing a fetch operation
fetchInput?: {} fetchInput?: {}
// Data expected by the fetch operation
body?: {}
} }
export type SWRHookSchemaBase = HookSchemaBase & { export type SWRHookSchemaBase = HookSchemaBase & {
@ -123,7 +125,7 @@ export type MutationHook<H extends MutationSchemaBase> = {
HookFunction<H['actionInput'], H['data'] | Promise<H['data']>> HookFunction<H['actionInput'], H['data'] | Promise<H['data']>>
> >
fetchOptions: HookFetcherOptions fetchOptions: HookFetcherOptions
fetcher?: HookFetcherFn<H['data'], H['fetchInput']> fetcher?: HookFetcherFn<H>
} }
export type MutationHookContext<H extends MutationSchemaBase> = { export type MutationHookContext<H extends MutationSchemaBase> = {