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 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>(

View File

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

View File

@ -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

View 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

View File

@ -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>,

View 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
View 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,
})