mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 22:42:33 +00:00
Improved HookHandler type
This commit is contained in:
parent
25b14007da
commit
17b336017c
@ -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>
|
||||
|
@ -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',
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user