forked from vercel/commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-customer.tsx
41 lines (33 loc) · 928 Bytes
/
use-customer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ConfigInterface } from 'swr'
import { HookFetcher } from '@lib/commerce/utils/types'
import useCommerceCustomer, { CustomerInput } from '@lib/commerce/use-customer'
import type { Customer, CustomerData } from './api/customers'
const defaultOpts = {
url: '/api/bigcommerce/customer',
method: 'GET',
}
export type { Customer }
export const fetcher: HookFetcher<CustomerData | null, CustomerInput> = (
options,
{ cartId },
fetch
) => {
return cartId ? fetch({ ...defaultOpts, ...options }) : null
}
export function extendHook(
customFetcher: typeof fetcher,
swrOptions?: ConfigInterface
) {
const useCustomer = () => {
const cart = useCommerceCustomer<CustomerData | null>(
defaultOpts,
[],
customFetcher,
{ revalidateOnFocus: false, ...swrOptions }
)
return cart
}
useCustomer.extend = extendHook
return useCustomer
}
export default extendHook(fetcher)