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

View File

@ -45,7 +45,13 @@ export type CartItemBody = Core.CartItemBody & {
export type CartHooks = Core.CartHooks & {
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 } }
removeItem: { data: Cart | null }
}

View File

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

View File

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

View File

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