mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 22:42:33 +00:00
Allow custom swr
This commit is contained in:
parent
d67c728e80
commit
939bc388bb
@ -23,7 +23,8 @@ export function extendHook(customFetcher: typeof fetcher) {
|
||||
const useCart = () => {
|
||||
const cart = useCommerceCart<Cart | null>(
|
||||
[defaultOpts.url, undefined],
|
||||
customFetcher
|
||||
customFetcher,
|
||||
{ revalidateOnFocus: false }
|
||||
)
|
||||
|
||||
// Uses a getter to only calculate the prop when required
|
||||
|
@ -1,4 +1,4 @@
|
||||
import useSWR, { responseInterface } from 'swr'
|
||||
import useSWR, { responseInterface, ConfigInterface } from 'swr'
|
||||
import Cookies from 'js-cookie'
|
||||
import { HookDeps, HookFetcher } from '../utils/types'
|
||||
import { useCommerce } from '..'
|
||||
@ -9,16 +9,15 @@ export type CartResponse<C> = responseInterface<C, Error> & {
|
||||
|
||||
export function useCart<T>(
|
||||
deps: [string | undefined, string | undefined, ...HookDeps[]],
|
||||
fetcherFn: HookFetcher<T, HookDeps[]>
|
||||
fetcherFn: HookFetcher<T, HookDeps[]>,
|
||||
swrOptions?: ConfigInterface<T | null>
|
||||
) {
|
||||
const { fetcherRef, cartCookie } = useCommerce()
|
||||
const fetcher = (url?: string, query?: string, ...args: HookDeps[]) =>
|
||||
Cookies.get(cartCookie)
|
||||
? fetcherFn({ url, query }, args, fetcherRef.current)
|
||||
: null
|
||||
const response = useSWR(deps, fetcher, {
|
||||
revalidateOnFocus: false,
|
||||
})
|
||||
const response = useSWR(deps, fetcher, swrOptions)
|
||||
|
||||
return Object.assign(response, { isEmpty: true }) as CartResponse<T>
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user