mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
fix search api
This commit is contained in:
parent
649c78938f
commit
a9c929e17e
@ -1,23 +0,0 @@
|
||||
AGILITY_GUID=
|
||||
AGILITY_API_FETCH_KEY=
|
||||
AGILITY_API_PREVIEW_KEY=
|
||||
AGILITY_SECURITY_KEY=
|
||||
|
||||
# Available providers: bigcommerce, shopify, swell
|
||||
COMMERCE_PROVIDER=
|
||||
|
||||
BIGCOMMERCE_STOREFRONT_API_URL=
|
||||
BIGCOMMERCE_STOREFRONT_API_TOKEN=
|
||||
BIGCOMMERCE_STORE_API_URL=
|
||||
BIGCOMMERCE_STORE_API_TOKEN=
|
||||
BIGCOMMERCE_STORE_API_CLIENT_ID=
|
||||
BIGCOMMERCE_CHANNEL_ID=
|
||||
|
||||
NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN=
|
||||
NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN=
|
||||
|
||||
NEXT_PUBLIC_SWELL_STORE_ID=
|
||||
NEXT_PUBLIC_SWELL_PUBLIC_KEY=
|
||||
|
||||
NEXT_PUBLIC_SALEOR_API_URL=
|
||||
NEXT_PUBLIC_SALEOR_CHANNEL=
|
@ -1,75 +1,69 @@
|
||||
|
||||
import { NextApiRequest, NextApiResponse } from "next"
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
import commerce from '@lib/api/commerce'
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
//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'
|
||||
)
|
||||
try {
|
||||
const search = `${req.query.search}`.toLowerCase()
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.status(200).end()
|
||||
return
|
||||
}
|
||||
// const products = await getProducts({filter})
|
||||
|
||||
try {
|
||||
const search = `${req.query.search}`.toLowerCase()
|
||||
const locale = 'en-us'
|
||||
const preview = false
|
||||
|
||||
// const products = await getProducts({filter})
|
||||
const config = { locale, locales: [locale] }
|
||||
|
||||
const { products } = await commerce.getAllProducts({
|
||||
variables: { first: 50 },
|
||||
config,
|
||||
preview,
|
||||
})
|
||||
|
||||
const locale = "en-us"
|
||||
const preview = false
|
||||
const returnedProducts = products
|
||||
.filter((p: any) => {
|
||||
return (
|
||||
search === '' ||
|
||||
p.name.toLowerCase().indexOf(search) !== -1 ||
|
||||
(p.description && p.description.toLowerCase().indexOf(search) !== -1)
|
||||
)
|
||||
})
|
||||
.map((p: any) => {
|
||||
return {
|
||||
name: p.name,
|
||||
imageUrl: p.images[0].url,
|
||||
price: p.price,
|
||||
id: p.id,
|
||||
description: p.description,
|
||||
slug: p.path || p.slug,
|
||||
}
|
||||
})
|
||||
.sort((a: any, b: any) => {
|
||||
if (a.name > b.name) return 1
|
||||
return -1
|
||||
})
|
||||
|
||||
const config = { locale, locales: [locale] }
|
||||
|
||||
const { products } = await commerce.getAllProducts({
|
||||
variables: { first: 50 },
|
||||
config,
|
||||
preview,
|
||||
})
|
||||
|
||||
|
||||
const returnedProducts = products
|
||||
.filter(p => {
|
||||
return search === ""
|
||||
|| p.name.toLowerCase().indexOf(search) !== -1
|
||||
|| (p.description && p.description.toLowerCase().indexOf(search) !== -1)
|
||||
})
|
||||
.map(p => {
|
||||
|
||||
return {
|
||||
name: p.name,
|
||||
imageUrl: p.images[0].url,
|
||||
price: p.price,
|
||||
id: p.id,
|
||||
description: p.description,
|
||||
slug: p.path || p.slug
|
||||
}
|
||||
}).sort((a, b) => {
|
||||
if (a.name > b.name) return 1
|
||||
return -1
|
||||
})
|
||||
|
||||
res.setHeader("Content-Type", "application/json")
|
||||
res.statusCode = 200
|
||||
res.json(returnedProducts)
|
||||
|
||||
} catch (e) {
|
||||
|
||||
res.statusCode = 500
|
||||
res.json({ message: "An error occurred ", error: e })
|
||||
|
||||
}
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.statusCode = 200
|
||||
res.json(returnedProducts)
|
||||
} catch (e) {
|
||||
res.statusCode = 500
|
||||
res.json({ message: 'An error occurred ', error: e })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user