This commit is contained in:
Luis Alvarez
2020-10-13 07:07:35 -05:00
parent e7e89e4c4c
commit ee6b65725f
9 changed files with 74 additions and 47 deletions

View File

@@ -1,3 +1,4 @@
import getAllProducts from '../../operations/get-all-products'
import type { ProductsHandlers } from '../products'
// Return current cart info
@@ -11,9 +12,18 @@ const getProducts: ProductsHandlers['getProducts'] = async ({
if (search) url.searchParams.set('keyword', search)
const { data } = await config.storeApiFetch(url.pathname + url.search)
// We only want the id of each product
url.searchParams.set('include_fields', 'id')
res.status(200).json({ data })
const { data } = await config.storeApiFetch<{ data: { id: number }[] }>(
url.pathname + url.search
)
const entityIds = data.map((p) => p.id)
const found = entityIds.length > 0
// We want the GraphQL version of each product
const { products } = await getAllProducts({ variables: { entityIds } })
res.status(200).json({ data: { products, found } })
}
export default getProducts

View File

@@ -1,4 +1,3 @@
import type { definitions } from '../definitions/catalog'
import isAllowedMethod from '../utils/is-allowed-method'
import createApiHandler, {
BigcommerceApiHandler,
@@ -6,22 +5,24 @@ import createApiHandler, {
} from '../utils/create-api-handler'
import { BigcommerceApiError } from '../utils/errors'
import getProducts from './handlers/get-products'
import { Products } from '../operations/get-all-products'
export type Product = definitions['product_Full']
export type SearchProductsData = {
products: Products
found: boolean
}
export type ProductsHandlers = {
getProducts: BigcommerceHandler<Product[], { search?: 'string' }>
getProducts: BigcommerceHandler<SearchProductsData, { search?: 'string' }>
}
const METHODS = ['GET']
// TODO: a complete implementation should have schema validation for `req.body`
const cartApi: BigcommerceApiHandler<Product[], ProductsHandlers> = async (
req,
res,
config,
handlers
) => {
const cartApi: BigcommerceApiHandler<
SearchProductsData,
ProductsHandlers
> = async (req, res, config, handlers) => {
if (!isAllowedMethod(req, res, METHODS)) return
try {