4
0
forked from crowetic/commerce

Updated types, hooks, useCustomer sample

This commit is contained in:
Luis Alvarez
2020-10-23 19:21:37 -05:00
parent 40049b27a1
commit 444456d376
9 changed files with 83 additions and 78 deletions

View File

@@ -10,14 +10,16 @@ const getCart: CartHandlers['getCart'] = async ({
}) => {
let result: { data?: Cart } = {}
try {
result = await config.storeApiFetch(`/v3/carts/${cartId}`)
} catch (error) {
if (error instanceof BigcommerceApiError && error.status === 404) {
// Remove the cookie if it exists but the cart wasn't found
res.setHeader('Set-Cookie', getCartCookie(config.cartCookie))
} else {
throw error
if (cartId) {
try {
result = await config.storeApiFetch(`/v3/carts/${cartId}`)
} catch (error) {
if (error instanceof BigcommerceApiError && error.status === 404) {
// Remove the cookie if it exists but the cart wasn't found
res.setHeader('Set-Cookie', getCartCookie(config.cartCookie))
} else {
throw error
}
}
}

View File

@@ -1,4 +1,4 @@
import { GetLoggedInCustomerQuery } from '@lib/bigcommerce/schema'
import type { GetLoggedInCustomerQuery } from '@lib/bigcommerce/schema'
import type { CustomersHandlers } from '..'
export const getLoggedInCustomerQuery = /* GraphQL */ `
@@ -22,23 +22,40 @@ export const getLoggedInCustomerQuery = /* GraphQL */ `
}
`
export type Customer = NonNullable<GetLoggedInCustomerQuery['customer']>
const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({
req,
res,
config,
}) => {
const { data } = await config.fetch<GetLoggedInCustomerQuery>(
getLoggedInCustomerQuery
)
const { customer } = data
const token = req.cookies[config.customerCookie]
if (!customer) {
return res.status(400).json({
data: null,
errors: [{ message: 'Customer not found', code: 'not_found' }],
})
if (token) {
const { data } = await config.fetch<GetLoggedInCustomerQuery>(
getLoggedInCustomerQuery,
undefined,
{
headers: {
cookie: `${config.customerCookie}=${token}`,
},
}
)
const { customer } = data
console.log('CUSTOMER', customer)
if (!customer) {
return res.status(400).json({
data: null,
errors: [{ message: 'Customer not found', code: 'not_found' }],
})
}
return res.status(200).json({ data: { customer } })
}
res.status(200).json({ data: { customer } })
res.status(200).json({ data: null })
}
export default getLoggedInCustomer

View File

@@ -4,16 +4,18 @@ import createApiHandler, {
} from '../utils/create-api-handler'
import isAllowedMethod from '../utils/is-allowed-method'
import { BigcommerceApiError } from '../utils/errors'
import getLoggedInCustomer from './handlers/get-logged-in-customer'
import getLoggedInCustomer, {
Customer,
} from './handlers/get-logged-in-customer'
export type Customer = any
export type { Customer }
export type CustomerData = {
customer: Customer
}
export type CustomersHandlers = {
getLoggedInCustomer: BigcommerceHandler<CustomerData, null>
getLoggedInCustomer: BigcommerceHandler<CustomerData>
}
const METHODS = ['GET']
@@ -25,10 +27,8 @@ const customersApi: BigcommerceApiHandler<
if (!isAllowedMethod(req, res, METHODS)) return
try {
if (req.method === 'GET') {
const body = null
return await handlers['getLoggedInCustomer']({ req, res, config, body })
}
const body = null
return await handlers['getLoggedInCustomer']({ req, res, config, body })
} catch (error) {
console.error(error)