4
0
forked from crowetic/commerce

Added useSearch hook

This commit is contained in:
Luis Alvarez
2020-10-13 03:49:24 -05:00
parent 1629f718b0
commit beed2d23e0
13 changed files with 3195 additions and 31 deletions

View File

@@ -0,0 +1,19 @@
import type { ProductsHandlers } from '../products'
// Return current cart info
const getProducts: ProductsHandlers['getProducts'] = async ({
res,
body: { search },
config,
}) => {
// Use a dummy base as we only care about the relative path
const url = new URL('/v3/catalog/products', 'http://a')
if (search) url.searchParams.set('keyword', search)
const { data } = await config.storeApiFetch(url.pathname + url.search)
res.status(200).json({ data })
}
export default getProducts

View File

@@ -0,0 +1,47 @@
import type { definitions } from '../definitions/catalog'
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 Product = definitions['product_Full']
export type ProductsHandlers = {
getProducts: BigcommerceHandler<Product[], { 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
) => {
if (!isAllowedMethod(req, res, METHODS)) return
try {
// Return current cart info
if (req.method === 'GET') {
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(cartApi, handlers)

File diff suppressed because it is too large Load Diff