forked from crowetic/commerce
Fixed commerce config types
This commit is contained in:
parent
eb3f29fa95
commit
b5ee7feb46
@ -6,7 +6,7 @@ import type { ItemBody, UpdateItemBody } from '../api/cart'
|
|||||||
import { fetcher as removeFetcher } from './use-remove-item'
|
import { fetcher as removeFetcher } from './use-remove-item'
|
||||||
import { Cart, useCart } from '.'
|
import { Cart, useCart } from '.'
|
||||||
|
|
||||||
const defualtOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/cart',
|
url: '/api/bigcommerce/cart',
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
}
|
}
|
||||||
@ -28,8 +28,8 @@ export const fetcher: HookFetcher<Cart | null, UpdateItemBody> = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
return fetch({
|
return fetch({
|
||||||
url: options?.url ?? defualtOpts.url,
|
url: options?.url ?? defaultOpts.url,
|
||||||
method: options?.method ?? defualtOpts.method,
|
method: options?.method ?? defaultOpts.method,
|
||||||
body: { itemId, item },
|
body: { itemId, item },
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ function extend(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
|||||||
const useUpdateItem = (item?: any) => {
|
const useUpdateItem = (item?: any) => {
|
||||||
const { mutate } = useCart()
|
const { mutate } = useCart()
|
||||||
const fn = useCartUpdateItem<Cart | null, UpdateItemBody>(
|
const fn = useCartUpdateItem<Cart | null, UpdateItemBody>(
|
||||||
defualtOpts,
|
defaultOpts,
|
||||||
customFetcher
|
customFetcher
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -8,14 +8,19 @@ import {
|
|||||||
} from 'react'
|
} from 'react'
|
||||||
import { Fetcher } from './utils/types'
|
import { Fetcher } from './utils/types'
|
||||||
|
|
||||||
const Commerce = createContext<CommerceConfig | null>(null)
|
const Commerce = createContext<CommerceContextValue | null>(null)
|
||||||
|
|
||||||
export type CommerceProps = {
|
export type CommerceProps = {
|
||||||
children?: ReactNode
|
children?: ReactNode
|
||||||
config: { fetcher: Fetcher<any> } & CommerceConfig
|
config: CommerceConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CommerceConfig = {
|
export type CommerceConfig = { fetcher: Fetcher<any> } & Omit<
|
||||||
|
CommerceContextValue,
|
||||||
|
'fetcherRef'
|
||||||
|
>
|
||||||
|
|
||||||
|
export type CommerceContextValue = {
|
||||||
fetcherRef: MutableRefObject<any>
|
fetcherRef: MutableRefObject<any>
|
||||||
locale: string
|
locale: string
|
||||||
cartCookie: string
|
cartCookie: string
|
||||||
@ -41,6 +46,6 @@ export function CommerceProvider({ children, config }: CommerceProps) {
|
|||||||
return <Commerce.Provider value={cfg}>{children}</Commerce.Provider>
|
return <Commerce.Provider value={cfg}>{children}</Commerce.Provider>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useCommerce<T extends CommerceConfig>() {
|
export function useCommerce<T extends CommerceContextValue>() {
|
||||||
return useContext(Commerce) as T
|
return useContext(Commerce) as T
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,6 @@ export type HookFetcher<T, Input> = (
|
|||||||
fetch: Fetcher<T>
|
fetch: Fetcher<T>
|
||||||
) => T | Promise<T>
|
) => T | Promise<T>
|
||||||
|
|
||||||
// export type HookFetcher<T> = (
|
|
||||||
// options: FetcherOptions,
|
|
||||||
// fetch: Fetcher<T>
|
|
||||||
// ) => T | Promise<T>
|
|
||||||
|
|
||||||
export type HookFetcherOptions = {
|
export type HookFetcherOptions = {
|
||||||
query?: string
|
query?: string
|
||||||
url?: string
|
url?: string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user