mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 11:11:24 +00:00
Added customer endpoint
This commit is contained in:
59
framework/bigcommerce/api/customer/get-logged-in-customer.ts
Normal file
59
framework/bigcommerce/api/customer/get-logged-in-customer.ts
Normal 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
|
10
framework/bigcommerce/api/customer/index.ts
Normal file
10
framework/bigcommerce/api/customer/index.ts
Normal 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 }
|
@@ -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>(
|
||||
|
5
framework/bigcommerce/types/customer.ts
Normal file
5
framework/bigcommerce/types/customer.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import * as Core from '@commerce/types/customer'
|
||||
|
||||
export * from '@commerce/types/customer'
|
||||
|
||||
export type CustomerSchema = Core.CustomerSchema
|
Reference in New Issue
Block a user