4
0
forked from crowetic/commerce

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 useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
import { BigcommerceProvider } from '..' import type { BigcommerceProvider } from '..'
export default useCommerceCart as UseCart<BigcommerceProvider> export default useCommerceCart as UseCart<BigcommerceProvider>

View File

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

View File

@ -16,29 +16,19 @@ const Commerce = createContext<CommerceContextValue<any> | {}>({})
export type Provider = CommerceConfig & { export type Provider = CommerceConfig & {
cart?: { cart?: {
useCart?: HookHandler<Cart, CartInput> useCart?: HookHandler<Cart | null, CartInput>
} }
cartNormalizer(data: any): Cart cartNormalizer(data: any): Cart
} }
export type HookHandler<Data, Input, Result = any, Body = any, State = {}> = { export type HookHandler<Data, Input, Result = any, Body = any, State = {}> = {
swrOptions?: SwrOptions<Data | null, Input, Result> swrOptions?: SwrOptions<Data, Input, Result>
onResponse?( onResponse?(response: ResponseState<Data>): ResponseState<Data> & State
response: ResponseState<Data | null>
): ResponseState<Data | null> & State
onMutation?: any onMutation?: any
fetchOptions?: HookFetcherOptions fetchOptions?: HookFetcherOptions
} & ( fetcher?: HookFetcherFn<Data, Input, Result, Body>
| // 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 normalizer?(data: Result): Data
} }
| {
fetcher?: never
normalizer(data: Result): Data
}
)
export type CommerceProps<P extends Provider> = { export type CommerceProps<P extends Provider> = {
children?: ReactNode children?: ReactNode