Simplified cart types for the provider

This commit is contained in:
Luis Alvarez 2021-03-31 16:04:46 -06:00
parent 543054225f
commit 731486e07b
3 changed files with 52 additions and 54 deletions

View File

@ -43,36 +43,21 @@ export type CartItemBody = Core.CartItemBody & {
optionSelections?: OptionSelections optionSelections?: OptionSelections
} }
export type CartHooks = Core.CartHooks & { export type CartTypes = {
getCart: { data: Cart | null } cart: Cart
addItem: { item: CartItemBody
data: Cart
body: { item: CartItemBody }
input: CartItemBody
fetchInput: CartItemBody
actionInput: CartItemBody
}
updateItem: { data: Cart; body: { item: CartItemBody } }
removeItem: { data: Cart | null }
} }
export type CartHooks = Core.CartHooks<CartTypes>
export type GetCartHook = CartHooks['getCart'] export type GetCartHook = CartHooks['getCart']
export type AddItemHook = CartHooks['addItem'] export type AddItemHook = CartHooks['addItem']
export type UpdateItemHook = CartHooks['updateItem'] export type UpdateItemHook = CartHooks['updateItem']
export type RemoveItemHook = CartHooks['removeItem'] export type RemoveItemHook = CartHooks['removeItem']
export type CartSchema = Core.CartSchema & { export type CartSchema = Core.CartSchema<CartTypes>
endpoint: {
operations: CartOperations
}
}
export type CartOperations = { export type CartOperations = Core.CartOperations<CartTypes>
getCart: GetCartHook
addItem: AddItemHook
updateItem: UpdateItemHook
removeItem: RemoveItemHook
}
export type GetCartOperation = CartOperations['getCart'] export type GetCartOperation = CartOperations['getCart']
export type AddItemOperation = CartOperations['addItem'] export type AddItemOperation = CartOperations['addItem']

View File

@ -4,9 +4,9 @@ import type { SWRHook, HookFetcherFn } from '../utils/types'
import type { GetCartHook } from '../types/cart' import type { GetCartHook } from '../types/cart'
import { Provider, useCommerce } from '..' import { Provider, useCommerce } from '..'
export type UseCart< export type UseCart<H extends SWRHook<any> = SWRHook<GetCartHook>> = ReturnType<
H extends SWRHook<GetCartHook> = SWRHook<GetCartHook> H['useHook']
> = ReturnType<H['useHook']> >
export const fetcher: HookFetcherFn<GetCartHook> = async ({ export const fetcher: HookFetcherFn<GetCartHook> = async ({
options, options,

View File

@ -85,35 +85,40 @@ export type CartItemBody = {
* Hooks schema * Hooks schema
*/ */
export type CartHooks = { export type CartTypes = {
getCart: GetCartHook cart: Cart
addItem: AddItemHook item: CartItemBody
updateItem: UpdateItemHook
remoteItem: RemoveItemHook
} }
export type GetCartHook = { export type CartHooks<T extends CartTypes = CartTypes> = {
data: Cart | null getCart: GetCartHook<T>
addItem: AddItemHook<T>
updateItem: UpdateItemHook<T>
removeItem: RemoveItemHook<T>
}
export type GetCartHook<T extends CartTypes = CartTypes> = {
data: T['cart'] | null
input: {} input: {}
fetchInput: { cartId?: string } fetchInput: { cartId?: string }
swrState: { isEmpty: boolean } swrState: { isEmpty: boolean }
} }
export type AddItemHook = { export type AddItemHook<T extends CartTypes = CartTypes> = {
data: Cart data: T['cart']
body: { item: CartItemBody } body: { item: T['item'] }
input: CartItemBody input: T['item']
fetchInput: CartItemBody fetchInput: T['item']
actionInput: CartItemBody actionInput: T['item']
} }
export type UpdateItemHook = { export type UpdateItemHook<T extends CartTypes = CartTypes> = {
data: Cart data: T['cart']
body: { itemId: string; item: CartItemBody } body: { itemId: string; item: T['item'] }
} }
export type RemoveItemHook = { export type RemoveItemHook<T extends CartTypes = CartTypes> = {
data: Cart | null data: T['cart'] | null
body: { itemId: string } body: { itemId: string }
} }
@ -121,32 +126,40 @@ export type RemoveItemHook = {
* API Schema * API Schema
*/ */
export type CartSchema = { export type CartSchema<T extends CartTypes = CartTypes> = {
endpoint: { endpoint: {
options: {} options: {}
operations: CartOperations operations: CartOperations<T>
} }
} }
export type CartOperations = { export type CartOperations<T extends CartTypes = CartTypes> = {
getCart: GetCartOperation getCart: GetCartOperation<T>
addItem: AddItemOperation addItem: AddItemOperation<T>
updateItem: UpdateItemOperation updateItem: UpdateItemOperation<T>
removeItem: RemoveItemOperation removeItem: RemoveItemOperation<T>
} }
export type GetCartOperation = GetCartHook & { export type GetCartOperation<
T extends CartTypes = CartTypes
> = GetCartHook<T> & {
body: { cartId?: string } body: { cartId?: string }
} }
export type AddItemOperation = AddItemHook & { export type AddItemOperation<
T extends CartTypes = CartTypes
> = AddItemHook<T> & {
body: { cartId: string } body: { cartId: string }
} }
export type UpdateItemOperation = UpdateItemHook & { export type UpdateItemOperation<
T extends CartTypes = CartTypes
> = UpdateItemHook<T> & {
body: { cartId: string } body: { cartId: string }
} }
export type RemoveItemOperation = RemoveItemHook & { export type RemoveItemOperation<
T extends CartTypes = CartTypes
> = RemoveItemHook<T> & {
body: { cartId: string } body: { cartId: string }
} }