forked from crowetic/commerce
Added useSearch hook
This commit is contained in:
19
lib/bigcommerce/api/catalog/handlers/get-products.ts
Normal file
19
lib/bigcommerce/api/catalog/handlers/get-products.ts
Normal 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
|
47
lib/bigcommerce/api/catalog/products.ts
Normal file
47
lib/bigcommerce/api/catalog/products.ts
Normal 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)
|
3001
lib/bigcommerce/api/definitions/catalog.ts
Normal file
3001
lib/bigcommerce/api/definitions/catalog.ts
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user