From 5c9e075d0773874819da752b78d3f6930273799f Mon Sep 17 00:00:00 2001 From: Joel Varty Date: Thu, 17 Jun 2021 15:33:02 -0400 Subject: [PATCH] added search --- pages/api/bigcommerce/search-products.ts | 41 ------------------ pages/api/search-products.ts | 55 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 41 deletions(-) delete mode 100644 pages/api/bigcommerce/search-products.ts create mode 100644 pages/api/search-products.ts diff --git a/pages/api/bigcommerce/search-products.ts b/pages/api/bigcommerce/search-products.ts deleted file mode 100644 index d5e738570..000000000 --- a/pages/api/bigcommerce/search-products.ts +++ /dev/null @@ -1,41 +0,0 @@ -export default {} - -// import { NextApiRequest, NextApiResponse } from "next" -// import getProducts from "../../shopify-api/get-products" - - - -// const searchProducts = async (req:NextApiRequest, res:NextApiResponse) => { - -// //cors stuff -// res.setHeader('Access-Control-Allow-Credentials', "true") -// res.setHeader('Access-Control-Allow-Origin', '*') -// res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT') -// res.setHeader( -// 'Access-Control-Allow-Headers', -// 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version' -// ) - -// if (req.method === 'OPTIONS') { -// res.status(200).end() -// return -// } - -// try { -// const filter = req.query.filter ?? "" - -// const products = await getProducts({filter}) - -// res.statusCode = 200 -// res.json(products) - -// } catch (e) { - -// res.statusCode = 500 -// res.json({ message: "An error occurred ", error: e }) - -// } -// } - - -// export default searchProducts \ No newline at end of file diff --git a/pages/api/search-products.ts b/pages/api/search-products.ts new file mode 100644 index 000000000..f0206b722 --- /dev/null +++ b/pages/api/search-products.ts @@ -0,0 +1,55 @@ + +import { NextApiRequest, NextApiResponse } from "next" + +import { getConfig } from '@framework/api' +import getSiteInfo from '@framework/api/operations/get-site-info' +import getAllProducts from '@framework/api/operations/get-all-products' + + +export default async (req: NextApiRequest, res: NextApiResponse) => { + + + //cors stuff + res.setHeader('Access-Control-Allow-Credentials', "true") + res.setHeader('Access-Control-Allow-Origin', '*') + res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT') + res.setHeader( + 'Access-Control-Allow-Headers', + 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version' + ) + + if (req.method === 'OPTIONS') { + res.status(200).end() + return + } + + try { + const filter = req.query.filter ?? "" + + // const products = await getProducts({filter}) + const locale = "en-US" + const preview = false + + + const config = getConfig({ locale }) + + + // Get Best Newest Products + const { products } = await getAllProducts({ + variables: { field: 'newestProducts', first: 12 }, + config, + preview, + }) + + res.statusCode = 200 + res.json(products) + + } catch (e) { + + res.statusCode = 500 + res.json({ message: "An error occurred ", error: e }) + + } +} + +