mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 15:06:59 +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 cart from './cart'
|
||||||
import checkout from './checkout'
|
import checkout from './checkout'
|
||||||
import products from './catalog/products'
|
import products from './catalog/products'
|
||||||
|
import customer from './customer'
|
||||||
import customerCard from './customer/card'
|
import customerCard from './customer/card'
|
||||||
import customerAddress from './customer/address'
|
import customerAddress from './customer/address'
|
||||||
import signup from './signup';
|
import signup from './signup'
|
||||||
|
|
||||||
const endpoints = {
|
const endpoints = {
|
||||||
cart,
|
cart,
|
||||||
checkout,
|
checkout,
|
||||||
|
customer: customer,
|
||||||
'customer/card': customerCard,
|
'customer/card': customerCard,
|
||||||
'customer/address': customerAddress,
|
'customer/address': customerAddress,
|
||||||
'catalog/products': products,
|
'catalog/products': products,
|
||||||
'signup': signup
|
signup: signup,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ordercloudAPI(commerce: OrdercloudAPI) {
|
export default function ordercloudAPI(commerce: OrdercloudAPI) {
|
||||||
|
@ -32,7 +32,7 @@ export const handler: MutationHook<SignupHook> = {
|
|||||||
useHook:
|
useHook:
|
||||||
({ fetch }) =>
|
({ fetch }) =>
|
||||||
() => {
|
() => {
|
||||||
//const { mutate } = useCustomer() TODO add mutate back in once useCustomer is implemented
|
const { mutate } = useCustomer()
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function signup(input) {
|
async function signup(input) {
|
||||||
@ -40,7 +40,7 @@ export const handler: MutationHook<SignupHook> = {
|
|||||||
// await mutate()
|
// await mutate()
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fetch] //mutate]
|
[fetch, mutate]
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,28 @@
|
|||||||
import { SWRHook } from '@vercel/commerce/utils/types'
|
import type { SWRHook } from '@vercel/commerce/utils/types'
|
||||||
import useCustomer, { UseCustomer } from '@vercel/commerce/customer/use-customer'
|
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 default useCustomer as UseCustomer<typeof handler>
|
||||||
export const handler: SWRHook<any> = {
|
|
||||||
|
export const handler: SWRHook<CustomerHook> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: '',
|
url: '/api/commerce/customer',
|
||||||
|
method: 'GET',
|
||||||
},
|
},
|
||||||
async fetcher({ input, options, fetch }) {},
|
async fetcher({ options, fetch }) {
|
||||||
useHook: () => () => {
|
const data = await fetch(options)
|
||||||
return async function addItem() {
|
return data?.customer ?? null
|
||||||
return {}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
useHook:
|
||||||
|
({ useData }) =>
|
||||||
|
(input) => {
|
||||||
|
return useData({
|
||||||
|
swrOptions: {
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
...input?.swrOptions,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user