mirror of
https://github.com/vercel/commerce.git
synced 2025-05-16 22:46:58 +00:00
Add use-customer orderload implementation
This commit is contained in:
parent
3605947564
commit
dc3a981dc0
34
packages/ordercloud/src/api/endpoints/customer/customer.ts
Normal file
34
packages/ordercloud/src/api/endpoints/customer/customer.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import type { CustomerEndpoint } from '.'
|
||||
import { CommerceAPIError } from '@vercel/commerce/api/utils/errors'
|
||||
|
||||
const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] =
|
||||
async ({ req, config: { restBuyerFetch, tokenCookie } }) => {
|
||||
const token = req.cookies.get(tokenCookie)?.value
|
||||
|
||||
if (token) {
|
||||
const customer = await restBuyerFetch('GET', '/me', undefined, {
|
||||
token,
|
||||
})
|
||||
|
||||
if (!customer) {
|
||||
throw new CommerceAPIError('Customer not found', {
|
||||
status: 404,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
data: {
|
||||
customer: {
|
||||
id: customer.ID,
|
||||
firstName: customer.FirstName,
|
||||
lastName: customer.LastName,
|
||||
email: customer.Email,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return { data: null }
|
||||
}
|
||||
|
||||
export default getLoggedInCustomer
|
18
packages/ordercloud/src/api/endpoints/customer/index.ts
Normal file
18
packages/ordercloud/src/api/endpoints/customer/index.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
|
||||
import customerEndpoint from '@vercel/commerce/api/endpoints/customer'
|
||||
import type { CustomerSchema } from '@vercel/commerce/types/customer'
|
||||
import type { OrdercloudAPI } from '../..'
|
||||
import getLoggedInCustomer from './customer'
|
||||
|
||||
export type CustomerAPI = GetAPISchema<OrdercloudAPI, CustomerSchema>
|
||||
|
||||
export type CustomerEndpoint = CustomerAPI['endpoint']
|
||||
|
||||
export const handlers: CustomerEndpoint['handlers'] = { getLoggedInCustomer }
|
||||
|
||||
const customerApi = createEndpoint<CustomerAPI>({
|
||||
handler: customerEndpoint,
|
||||
handlers,
|
||||
})
|
||||
|
||||
export default customerApi
|
@ -5,17 +5,19 @@ import createEndpoints from '@vercel/commerce/api/endpoints'
|
||||
import cart from './cart'
|
||||
import checkout from './checkout'
|
||||
import products from './catalog/products'
|
||||
import customer from './customer'
|
||||
import customerCard from './customer/card'
|
||||
import customerAddress from './customer/address'
|
||||
import signup from './signup';
|
||||
import signup from './signup'
|
||||
|
||||
const endpoints = {
|
||||
cart,
|
||||
checkout,
|
||||
customer: customer,
|
||||
'customer/card': customerCard,
|
||||
'customer/address': customerAddress,
|
||||
'catalog/products': products,
|
||||
'signup': signup
|
||||
signup: signup,
|
||||
}
|
||||
|
||||
export default function ordercloudAPI(commerce: OrdercloudAPI) {
|
||||
|
@ -32,7 +32,7 @@ export const handler: MutationHook<SignupHook> = {
|
||||
useHook:
|
||||
({ fetch }) =>
|
||||
() => {
|
||||
//const { mutate } = useCustomer() TODO add mutate back in once useCustomer is implemented
|
||||
const { mutate } = useCustomer()
|
||||
|
||||
return useCallback(
|
||||
async function signup(input) {
|
||||
@ -40,7 +40,7 @@ export const handler: MutationHook<SignupHook> = {
|
||||
// await mutate()
|
||||
return data
|
||||
},
|
||||
[fetch] //mutate]
|
||||
[fetch, mutate]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@ -1,15 +1,28 @@
|
||||
import { SWRHook } from '@vercel/commerce/utils/types'
|
||||
import useCustomer, { UseCustomer } from '@vercel/commerce/customer/use-customer'
|
||||
import type { SWRHook } from '@vercel/commerce/utils/types'
|
||||
import useCustomer, {
|
||||
type UseCustomer,
|
||||
} from '@vercel/commerce/customer/use-customer'
|
||||
import type { CustomerHook } from '@vercel/commerce/types/customer'
|
||||
|
||||
export default useCustomer as UseCustomer<typeof handler>
|
||||
export const handler: SWRHook<any> = {
|
||||
|
||||
export const handler: SWRHook<CustomerHook> = {
|
||||
fetchOptions: {
|
||||
query: '',
|
||||
url: '/api/commerce/customer',
|
||||
method: 'GET',
|
||||
},
|
||||
async fetcher({ input, options, fetch }) {},
|
||||
useHook: () => () => {
|
||||
return async function addItem() {
|
||||
return {}
|
||||
}
|
||||
async fetcher({ options, fetch }) {
|
||||
const data = await fetch(options)
|
||||
return data?.customer ?? null
|
||||
},
|
||||
useHook:
|
||||
({ useData }) =>
|
||||
(input) => {
|
||||
return useData({
|
||||
swrOptions: {
|
||||
revalidateOnFocus: false,
|
||||
...input?.swrOptions,
|
||||
},
|
||||
})
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user