2022-03-09 21:41:08 +04:00

27 lines
712 B
TypeScript

import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import { Customer } from '@commerce/types'
import { SWRHook } from '@commerce/utils/types'
import { viewerQuery, normalizeCustomer } from '../utils'
export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<Customer | null> = {
fetchOptions: {
query: viewerQuery,
},
async fetcher({ options, fetch }) {
const data = await fetch<any | null>({
...options,
})
return normalizeCustomer(data.viewer) ?? null
},
useHook: ({ useData }) => (input) => {
return useData({
swrOptions: {
revalidateOnFocus: false,
...input?.swrOptions,
},
})
},
}