moved products catalog endpoint

This commit is contained in:
Luis Alvarez
2021-05-21 19:49:04 -05:00
parent d2abdabf8c
commit 564e9cfa8d
17 changed files with 144 additions and 145 deletions

View File

@@ -1,48 +0,0 @@
import type { Product } from '@commerce/types'
import isAllowedMethod from '../utils/is-allowed-method'
import createApiHandler, {
BigcommerceApiHandler,
BigcommerceHandler,
} from '../utils/create-api-handler'
import { BigcommerceApiError } from '../utils/errors'
import getProducts from './handlers/get-products'
export type SearchProductsData = {
products: Product[]
found: boolean
}
export type ProductsHandlers = {
getProducts: BigcommerceHandler<
SearchProductsData,
{ search?: string; category?: string; brand?: string; sort?: string }
>
}
const METHODS = ['GET']
// TODO(lf): a complete implementation should have schema validation for `req.body`
const productsApi: BigcommerceApiHandler<
SearchProductsData,
ProductsHandlers
> = async (req, res, config, handlers) => {
if (!isAllowedMethod(req, res, METHODS)) return
try {
const body = req.query
return await handlers['getProducts']({ req, res, config, body })
} catch (error) {
console.error(error)
const message =
error instanceof BigcommerceApiError
? 'An unexpected error ocurred with the Bigcommerce API'
: 'An unexpected error ocurred'
res.status(500).json({ data: null, errors: [{ message }] })
}
}
export const handlers = { getProducts }
export default createApiHandler(productsApi, handlers, {})

View File

@@ -1,6 +1,6 @@
import { Product } from '@commerce/types'
import getAllProducts, { ProductEdge } from '../../../product/get-all-products'
import type { ProductsHandlers } from '../products'
import { Product } from '@commerce/types/product'
import { ProductsEndpoint } from '.'
import getAllProducts from '../../../../product/get-all-products'
const SORT: { [key: string]: string | undefined } = {
latest: 'id',
@@ -11,7 +11,7 @@ const SORT: { [key: string]: string | undefined } = {
const LIMIT = 12
// Return current cart info
const getProducts: ProductsHandlers['getProducts'] = async ({
const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
res,
body: { search, category, brand, sort },
config,

View File

@@ -0,0 +1,10 @@
import type { GetAPISchema } from '@commerce/api'
import type { ProductsSchema } from '../../../../types/product'
import type { BigcommerceAPI } from '../../..'
import getProducts from './get-products'
export type ProductsAPI = GetAPISchema<BigcommerceAPI, ProductsSchema>
export type ProductsEndpoint = ProductsAPI['endpoint']
export const handlers = { getProducts }

View File

@@ -14,6 +14,7 @@ import type { CustomerAPI } from './endpoints/customer'
import type { LoginAPI } from './endpoints/login'
import type { LogoutAPI } from './endpoints/logout'
import type { SignupAPI } from './endpoints/signup'
import type { ProductsAPI } from './endpoints/catalog/products'
import login from './operations/login'
import getAllPages from './operations/get-all-pages'
@@ -119,7 +120,13 @@ export const provider = {
export type Provider = typeof provider
export type APIs = CartAPI | CustomerAPI | LoginAPI | LogoutAPI | SignupAPI
export type APIs =
| CartAPI
| CustomerAPI
| LoginAPI
| LogoutAPI
| SignupAPI
| ProductsAPI
export type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P>

View File

@@ -0,0 +1 @@
export * from '@commerce/types/product'