mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
- Changed the loging fetch - Store the token and user id in cookie - Ue the cookie to make user calls like get user
42 lines
964 B
TypeScript
42 lines
964 B
TypeScript
import { SWRHook } from '@commerce/utils/types'
|
|
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
|
|
import getCustomerCookie from '../utils/get-customer-creds'
|
|
|
|
const creds = getCustomerCookie();
|
|
export default useCustomer as UseCustomer<typeof handler>
|
|
export const handler: SWRHook<any> = {
|
|
fetchOptions: {
|
|
url: 'Customers',
|
|
method: 'Get',
|
|
},
|
|
async fetcher({fetch, options, input}) {
|
|
const creds = getCustomerCookie();
|
|
if(!creds.id || !creds.token) {
|
|
return null;
|
|
}
|
|
|
|
const {data} = await fetch({
|
|
...options,
|
|
variables:{
|
|
params: [creds.id, creds.token]
|
|
}
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
return {
|
|
...data,
|
|
firstName: data.name.split(" ")[0],
|
|
lastName: data.name.split(" ")[1]
|
|
}
|
|
},
|
|
useHook: ({ useData }) => (input) => {
|
|
return useData({
|
|
swrOptions: {
|
|
revalidateOnFocus: false,
|
|
...input?.swrOptions,
|
|
},
|
|
})
|
|
},
|
|
}
|