Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordercloud auth #936

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add use-customer orderload implementation
  • Loading branch information
Adam Clason committed Jan 22, 2023
commit dc3a981dc0b1ddc86444bcd83294555083e640f3
34 changes: 34 additions & 0 deletions packages/ordercloud/src/api/endpoints/customer/customer.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions packages/ordercloud/src/api/endpoints/customer/index.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions packages/ordercloud/src/api/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/ordercloud/src/auth/use-signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ 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) {
const data = await fetch({ input })
// await mutate()
return data
},
[fetch] //mutate]
[fetch, mutate]
)
},
}
31 changes: 22 additions & 9 deletions packages/ordercloud/src/customer/use-customer.tsx
Original file line number Diff line number Diff line change
@@ -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,
},
})
},
}