Improved HookHandler type

This commit is contained in:
Luis Alvarez 2021-02-08 11:16:14 -05:00
parent 25b14007da
commit 17b336017c
3 changed files with 14 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
import { BigcommerceProvider } from '..'
import type { BigcommerceProvider } from '..'
export default useCommerceCart as UseCart<BigcommerceProvider>

View File

@ -49,7 +49,13 @@ const fetcher: Fetcher<any> = async ({
throw await getError(res)
}
const useCart: HookHandler<Cart, CartInput, any, any, { isEmpty?: boolean }> = {
const useCart: HookHandler<
Cart | null,
CartInput,
any,
any,
{ isEmpty?: boolean }
> = {
fetchOptions: {
url: '/api/bigcommerce/cart',
method: 'GET',

View File

@ -16,29 +16,19 @@ const Commerce = createContext<CommerceContextValue<any> | {}>({})
export type Provider = CommerceConfig & {
cart?: {
useCart?: HookHandler<Cart, CartInput>
useCart?: HookHandler<Cart | null, CartInput>
}
cartNormalizer(data: any): Cart
}
export type HookHandler<Data, Input, Result = any, Body = any, State = {}> = {
swrOptions?: SwrOptions<Data | null, Input, Result>
onResponse?(
response: ResponseState<Data | null>
): ResponseState<Data | null> & State
swrOptions?: SwrOptions<Data, Input, Result>
onResponse?(response: ResponseState<Data>): ResponseState<Data> & State
onMutation?: any
fetchOptions?: HookFetcherOptions
} & (
| // TODO: Maybe the normalizer is not required if it's used by the API route directly?
{
fetcher: HookFetcherFn<Data | null, Input, Result, Body>
normalizer?(data: Result): Data
}
| {
fetcher?: never
normalizer(data: Result): Data
}
)
fetcher?: HookFetcherFn<Data, Input, Result, Body>
normalizer?(data: Result): Data
}
export type CommerceProps<P extends Provider> = {
children?: ReactNode