diff --git a/framework/bigcommerce/cart/use-remove-item.tsx b/framework/bigcommerce/cart/use-remove-item.tsx index f279b2dc6..186780d6a 100644 --- a/framework/bigcommerce/cart/use-remove-item.tsx +++ b/framework/bigcommerce/cart/use-remove-item.tsx @@ -1,5 +1,8 @@ import { useCallback } from 'react' -import { HookContext, HookFetcherContext } from '@commerce/utils/types' +import type { + MutationHookContext, + HookFetcherContext, +} from '@commerce/utils/types' import { ValidationError } from '@commerce/utils/errors' import useRemoveItem, { RemoveItemInput as RemoveItemInputBase, @@ -40,7 +43,9 @@ export const handler = { }) return normalizeCart(data) }, - useHook: ({ fetch }: HookContext) => < + useHook: ({ + fetch, + }: MutationHookContext) => < T extends LineItem | undefined = undefined >( ctx: { item?: T } = {} diff --git a/framework/bigcommerce/cart/use-update-item.tsx b/framework/bigcommerce/cart/use-update-item.tsx index cfd6007d1..f1840f806 100644 --- a/framework/bigcommerce/cart/use-update-item.tsx +++ b/framework/bigcommerce/cart/use-update-item.tsx @@ -1,6 +1,9 @@ import { useCallback } from 'react' import debounce from 'lodash.debounce' -import type { HookContext, HookFetcherContext } from '@commerce/utils/types' +import type { + MutationHookContext, + HookFetcherContext, +} from '@commerce/utils/types' import { ValidationError } from '@commerce/utils/errors' import useUpdateItem, { UpdateItemInput as UpdateItemInputBase, @@ -54,7 +57,9 @@ export const handler = { return normalizeCart(data) }, - useHook: ({ fetch }: HookContext) => < + useHook: ({ + fetch, + }: MutationHookContext) => < T extends LineItem | undefined = undefined >( ctx: { diff --git a/framework/commerce/index.tsx b/framework/commerce/index.tsx index baaf65406..22b8de8ed 100644 --- a/framework/commerce/index.tsx +++ b/framework/commerce/index.tsx @@ -6,7 +6,7 @@ import { useMemo, useRef, } from 'react' -import { Fetcher, HookHandler, SWRHook, MutationHook } from './utils/types' +import { Fetcher, SWRHook, MutationHook } from './utils/types' import type { FetchCartInput } from './cart/use-cart' import type { Cart, Wishlist, Customer, SearchProductsData } from './types' diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts index 01f1c2eb3..2bab3f800 100644 --- a/framework/commerce/utils/types.ts +++ b/framework/commerce/utils/types.ts @@ -1,8 +1,10 @@ -import { LineItem } from '@framework/types' import type { ConfigInterface } from 'swr' import type { CommerceError } from './errors' import type { ResponseState } from './use-data' +/** + * Returns the properties in T with the properties in type K, overriding properties defined in T + */ export type Override = Omit & K /** @@ -51,31 +53,10 @@ export type HookFetcherOptions = { method?: string } & ( export type HookInputValue = string | number | boolean | undefined -export type HookSwrInput = [string, HookInputValue][] +export type HookSWRInput = [string, HookInputValue][] export type HookFetchInput = { [k: string]: HookInputValue } -export type HookHandler< - // Data obj returned by the hook and fetch operation - Data, - // Input expected by the hook - Input extends { [k: string]: unknown } = {}, - // Input expected before doing a fetch operation - FetchInput extends HookFetchInput = {}, - // Custom state added to the response object of SWR - State = {} -> = { - useHook?(context: { - input: Input & { swrOptions?: SwrOptions } - useData(context?: { - input?: HookFetchInput | HookSwrInput - swrOptions?: SwrOptions - }): ResponseState - }): ResponseState & State - fetchOptions: HookFetcherOptions - fetcher?: HookFetcherFn -} - export type HookFunction< Input extends { [k: string]: unknown } | {}, T @@ -110,7 +91,7 @@ export type SWRHookContext< FetchInput extends { [k: string]: unknown } = {} > = { useData(context?: { - input?: HookFetchInput | HookSwrInput + input?: HookFetchInput | HookSWRInput swrOptions?: SwrOptions }): ResponseState } @@ -126,13 +107,13 @@ export type MutationHook< FetchInput extends { [k: string]: unknown } = ActionInput > = { useHook( - context: HookContext + context: MutationHookContext ): HookFunction>> fetchOptions: HookFetcherOptions fetcher?: HookFetcherFn } -export type HookContext< +export type MutationHookContext< Data, FetchInput extends { [k: string]: unknown } = {} > = { @@ -144,22 +125,3 @@ export type SwrOptions = ConfigInterface< CommerceError, HookFetcher > - -/** - * Returns the property K from type T excluding nullables - */ -export type Prop = NonNullable - -export type HookHandlerType = HookHandler - -export type UseHookParameters = Parameters< - Prop -> - -export type UseHookResponse = ReturnType< - Prop -> - -export type UseHookInput< - H extends HookHandlerType -> = UseHookParameters[0]['input'] diff --git a/framework/commerce/utils/use-data.tsx b/framework/commerce/utils/use-data.tsx index fe21bd3d3..43db9b534 100644 --- a/framework/commerce/utils/use-data.tsx +++ b/framework/commerce/utils/use-data.tsx @@ -1,6 +1,6 @@ import useSWR, { responseInterface } from 'swr' import type { - HookSwrInput, + HookSWRInput, HookFetchInput, Fetcher, SwrOptions, @@ -23,7 +23,7 @@ export type UseData = < fetchOptions: HookFetcherOptions fetcher: HookFetcherFn }, - input: HookFetchInput | HookSwrInput, + input: HookFetchInput | HookSWRInput, fetcherFn: Fetcher, swrOptions?: SwrOptions ) => ResponseState