mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
34 lines
889 B
TypeScript
34 lines
889 B
TypeScript
import { SWRHook } from '@commerce/utils/types'
|
|
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
|
|
import { gateway as MoltinGateway } from '@moltin/sdk'
|
|
import getCustomerCookie from '../utils/get-customer-creds'
|
|
|
|
const Moltin = MoltinGateway({
|
|
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
|
})
|
|
|
|
export default useCustomer as UseCustomer<typeof handler>
|
|
export const handler: SWRHook<any> = {
|
|
fetchOptions: {
|
|
query: '',
|
|
},
|
|
async fetcher() {
|
|
const creds = getCustomerCookie();
|
|
|
|
// if user is not logged-in return null
|
|
if(!creds) {
|
|
return null;
|
|
}
|
|
const data = await Moltin.Customers.Get(creds.customer_id, creds.token);
|
|
return data || null;
|
|
},
|
|
useHook: ({ useData }) => (input) => {
|
|
return useData({
|
|
swrOptions: {
|
|
revalidateOnFocus: false,
|
|
...input?.swrOptions,
|
|
},
|
|
})
|
|
},
|
|
}
|