Added customer endpoint

This commit is contained in:
Luis Alvarez
2021-05-18 08:39:36 -05:00
parent 591cd6d4e6
commit eb2fd80ead
10 changed files with 151 additions and 9 deletions

View File

@@ -0,0 +1,59 @@
import type { GetLoggedInCustomerQuery } from '../../schema'
import type { CustomerEndpoint } from '.'
export const getLoggedInCustomerQuery = /* GraphQL */ `
query getLoggedInCustomer {
customer {
entityId
firstName
lastName
email
company
customerGroupId
notes
phone
addressCount
attributeCount
storeCredit {
value
currencyCode
}
}
}
`
export type Customer = NonNullable<GetLoggedInCustomerQuery['customer']>
const getLoggedInCustomer: CustomerEndpoint['operations']['getLoggedInCustomer'] = async ({
req,
res,
config,
}) => {
const token = req.cookies[config.customerCookie]
if (token) {
const { data } = await config.fetch<GetLoggedInCustomerQuery>(
getLoggedInCustomerQuery,
undefined,
{
headers: {
cookie: `${config.customerCookie}=${token}`,
},
}
)
const { customer } = data
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: null })
}
export default getLoggedInCustomer

View File

@@ -0,0 +1,10 @@
import type { GetAPISchema } from '@commerce/api'
import type { CustomerSchema } from '../../types/customer'
import type { BigcommerceAPI } from '..'
import getLoggedInCustomer from './get-logged-in-customer'
export type CustomerAPI = GetAPISchema<BigcommerceAPI, CustomerSchema>
export type CustomerEndpoint = CustomerAPI['endpoint']
export const operations = { getLoggedInCustomer }

View File

@@ -10,6 +10,7 @@ import fetchGraphqlApi from './utils/fetch-graphql-api'
import fetchStoreApi from './utils/fetch-store-api'
import type { CartAPI } from './cart'
import type { CustomerAPI } from './customer'
import login from './operations/login'
export interface BigcommerceConfig extends CommerceAPIConfig {
@@ -111,14 +112,14 @@ export const provider = {
export type Provider = typeof provider
export type APIs = CartAPI
export type APIs = CartAPI | CustomerAPI
export type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P>
export function getCommerceApi<P extends Provider>(
customProvider: P = provider as any
): BigcommerceAPI<P> {
const api = commerceApi(customProvider)
) {
const api: BigcommerceAPI<P> = commerceApi(customProvider)
return Object.assign(api, {
endpoint<E extends APIs>(

View File

@@ -0,0 +1,5 @@
import * as Core from '@commerce/types/customer'
export * from '@commerce/types/customer'
export type CustomerSchema = Core.CustomerSchema