summaryrefslogtreecommitdiff
path: root/src/pages/api/shop/searchkey.js
blob: f5546a362e6bcf3614a8cdb59e4d58c68526fdf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import axios from 'axios';

export default async function handler(req, res) {
  const { url = '', page = 1, limit = 30, all } = req.query;

  let q = '*:*';

  // ✅ kalau BUKAN sitemap
  if (!all) {
    const cleanUrl = url.trim();
    if (!cleanUrl) {
      return res.status(400).json({ error: 'Missing url param' });
    }
    q = `keywords_s:"${cleanUrl}"`;
  }

  const offset = (page - 1) * limit;

  const params = [
    `q.op=AND`,
    `q=${q}`,
    `indent=true`,
    `rows=${limit}`,
    `start=${offset}`,
  ];

  try {
    const result = await axios.post(
      `${process.env.SOLR_HOST}/solr/searchkey/select`,
      params.join('&'),
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      }
    );

    res.status(200).json(result.data);
  } catch (error) {
    console.error(error?.response?.data || error);
    res.status(500).json({ error: 'Internal Server Error' });
  }
}