mirror of
https://github.com/vercel/commerce.git
synced 2025-06-08 01:06:59 +00:00
38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import { SWRHook } from '@vercel/commerce/utils/types'
|
|
import useCustomer, {
|
|
UseCustomer,
|
|
} from '@vercel/commerce/customer/use-customer'
|
|
import { getCustomerRoute } from '../utils/token/customer-route'
|
|
import { normalizeCustomer } from '../utils/normalize/normalize-customer'
|
|
import { getCustomerToken } from '../utils/token/customer-token'
|
|
import { CustomerHook } from '@vercel/commerce/types/customer'
|
|
|
|
export default useCustomer as UseCustomer<typeof handler>
|
|
|
|
export const handler: SWRHook<CustomerHook> = {
|
|
fetchOptions: {
|
|
url: `/api/v2/shop/customers/`,
|
|
method: 'GET',
|
|
},
|
|
fetcher: async ({ options, fetch }) => {
|
|
if (getCustomerToken()) {
|
|
const syliusCustomer = await fetch({
|
|
url: getCustomerRoute() ?? '',
|
|
method: options.method,
|
|
})
|
|
return normalizeCustomer(syliusCustomer)
|
|
}
|
|
return null
|
|
},
|
|
useHook:
|
|
({ useData }) =>
|
|
(input) => {
|
|
return useData({
|
|
swrOptions: {
|
|
revalidateOnFocus: false,
|
|
...input?.swrOptions,
|
|
},
|
|
})
|
|
},
|
|
}
|