File size: 1,622 Bytes
f6522be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
/**
 * Vercel Serverless Function: /api/generate
 * Proxies POST quote generation requests to Hugging Face backend
 */

export default async function handler(req, res) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');

  if (req.method === 'OPTIONS') {
    return res.status(204).end();
  }

  if (req.method === 'POST') {
    try {
      const body = typeof req.body === 'string' ? req.body : JSON.stringify(req.body);
      const hfRes = await fetch('https://ilhamdev-quote-api.hf.space/', {
        method: 'POST',
        headers: {
          'Content-Type': req.headers['content-type'] || 'application/json',
        },
        body: body,
      });

      const contentType = hfRes.headers.get('content-type') || 'application/json';
      res.setHeader('Content-Type', contentType);

      if (contentType.includes('image')) {
        const arrayBuf = await hfRes.arrayBuffer();
        return res.status(hfRes.status).send(Buffer.from(arrayBuf));
      }

      const text = await hfRes.text();
      return res.status(hfRes.status).send(text);
    } catch (err) {
      return res.status(502).json({ status: false, message: `Proxy error: ${err.message}` });
    }
  }

  if (req.method === 'GET') {
    return res.status(200).json({
      status: true,
      message: 'Quote API Subdomain Proxy active. Send POST requests to generate quotes.',
    });
  }

  return res.status(405).json({ status: false, message: 'Method Not Allowed' });
}