mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
Added customer endpoint
This commit is contained in:
parent
591cd6d4e6
commit
eb2fd80ead
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 fetchStoreApi from './utils/fetch-store-api'
|
||||||
|
|
||||||
import type { CartAPI } from './cart'
|
import type { CartAPI } from './cart'
|
||||||
|
import type { CustomerAPI } from './customer'
|
||||||
import login from './operations/login'
|
import login from './operations/login'
|
||||||
|
|
||||||
export interface BigcommerceConfig extends CommerceAPIConfig {
|
export interface BigcommerceConfig extends CommerceAPIConfig {
|
||||||
@ -111,14 +112,14 @@ export const provider = {
|
|||||||
|
|
||||||
export type Provider = typeof 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 type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P>
|
||||||
|
|
||||||
export function getCommerceApi<P extends Provider>(
|
export function getCommerceApi<P extends Provider>(
|
||||||
customProvider: P = provider as any
|
customProvider: P = provider as any
|
||||||
): BigcommerceAPI<P> {
|
) {
|
||||||
const api = commerceApi(customProvider)
|
const api: BigcommerceAPI<P> = commerceApi(customProvider)
|
||||||
|
|
||||||
return Object.assign(api, {
|
return Object.assign(api, {
|
||||||
endpoint<E extends APIs>(
|
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
|
@ -3,9 +3,10 @@ import { CommerceAPIError } from '../utils/errors'
|
|||||||
import isAllowedOperation from '../utils/is-allowed-operation'
|
import isAllowedOperation from '../utils/is-allowed-operation'
|
||||||
import type { GetAPISchema } from '..'
|
import type { GetAPISchema } from '..'
|
||||||
|
|
||||||
const cartApi: GetAPISchema<any, CartSchema>['endpoint']['handler'] = async (
|
const cartEndpoint: GetAPISchema<
|
||||||
ctx
|
any,
|
||||||
) => {
|
CartSchema
|
||||||
|
>['endpoint']['handler'] = async (ctx) => {
|
||||||
const { req, res, operations, config } = ctx
|
const { req, res, operations, config } = ctx
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -19,7 +20,6 @@ const cartApi: GetAPISchema<any, CartSchema>['endpoint']['handler'] = async (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const body2 = req.body
|
|
||||||
const { cookies } = req
|
const { cookies } = req
|
||||||
const cartId = cookies[config.cartCookie]
|
const cartId = cookies[config.cartCookie]
|
||||||
|
|
||||||
@ -59,4 +59,4 @@ const cartApi: GetAPISchema<any, CartSchema>['endpoint']['handler'] = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default cartApi
|
export default cartEndpoint
|
||||||
|
35
framework/commerce/api/endpoints/customer.ts
Normal file
35
framework/commerce/api/endpoints/customer.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import type { CustomerSchema } from '../../types/customer'
|
||||||
|
import { CommerceAPIError } from '../utils/errors'
|
||||||
|
import isAllowedOperation from '../utils/is-allowed-operation'
|
||||||
|
import type { GetAPISchema } from '..'
|
||||||
|
|
||||||
|
const customerEndpoint: GetAPISchema<
|
||||||
|
any,
|
||||||
|
CustomerSchema
|
||||||
|
>['endpoint']['handler'] = async (ctx) => {
|
||||||
|
const { req, res, operations } = ctx
|
||||||
|
|
||||||
|
if (
|
||||||
|
!isAllowedOperation(req, res, {
|
||||||
|
GET: operations['getLoggedInCustomer'],
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const body = null
|
||||||
|
return await operations['getLoggedInCustomer']({ ...ctx, body })
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
|
||||||
|
const message =
|
||||||
|
error instanceof CommerceAPIError
|
||||||
|
? 'An unexpected error ocurred with the Commerce API'
|
||||||
|
: 'An unexpected error ocurred'
|
||||||
|
|
||||||
|
res.status(500).json({ data: null, errors: [{ message }] })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default customerEndpoint
|
@ -2,6 +2,7 @@ import type { NextApiHandler } from 'next'
|
|||||||
import type { RequestInit, Response } from '@vercel/fetch'
|
import type { RequestInit, Response } from '@vercel/fetch'
|
||||||
import type { APIEndpoint, APIHandler } from './utils/types'
|
import type { APIEndpoint, APIHandler } from './utils/types'
|
||||||
import type { CartSchema } from '../types/cart'
|
import type { CartSchema } from '../types/cart'
|
||||||
|
import type { CustomerSchema } from '../types/customer'
|
||||||
import {
|
import {
|
||||||
defaultOperations,
|
defaultOperations,
|
||||||
OPERATIONS,
|
OPERATIONS,
|
||||||
@ -9,7 +10,7 @@ import {
|
|||||||
APIOperations,
|
APIOperations,
|
||||||
} from './operations'
|
} from './operations'
|
||||||
|
|
||||||
export type APISchemas = CartSchema
|
export type APISchemas = CartSchema | CustomerSchema
|
||||||
|
|
||||||
export type GetAPISchema<
|
export type GetAPISchema<
|
||||||
C extends CommerceAPI<any>,
|
C extends CommerceAPI<any>,
|
||||||
|
23
framework/commerce/types/customer.ts
Normal file
23
framework/commerce/types/customer.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// TODO: define this type
|
||||||
|
export type Customer = any
|
||||||
|
|
||||||
|
export type CustomerTypes = {
|
||||||
|
customer: Customer
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CustomerSchema<T extends CustomerTypes = CustomerTypes> = {
|
||||||
|
endpoint: {
|
||||||
|
options: {}
|
||||||
|
operations: {
|
||||||
|
getLoggedInCustomer: {
|
||||||
|
data: { customer: T['customer'] } | null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// export type CustomerOperations<T extends CustomerTypes = CustomerTypes> = {
|
||||||
|
// getLoggedInCustomer: GetCartOperation<T>
|
||||||
|
// }
|
||||||
|
|
||||||
|
// export type GetLoggedInCustomerOperation = {}
|
8
pages/api/customer.ts
Normal file
8
pages/api/customer.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import customer from '@commerce/api/endpoints/customer'
|
||||||
|
import { CustomerAPI, operations } from '@framework/api/customer'
|
||||||
|
import commerce from '@lib/api/commerce'
|
||||||
|
|
||||||
|
export default commerce.endpoint({
|
||||||
|
handler: customer as CustomerAPI['endpoint']['handler'],
|
||||||
|
operations,
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user