From a5f5506ef5531f68e4a9234144516be86c7bd2db Mon Sep 17 00:00:00 2001 From: Macaulay Precious Date: Sat, 9 Nov 2024 13:06:52 +0100 Subject: [PATCH] added vercel proxy --- api/proxy.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 api/proxy.js diff --git a/api/proxy.js b/api/proxy.js new file mode 100644 index 0000000..56d12fa --- /dev/null +++ b/api/proxy.js @@ -0,0 +1,24 @@ +import httpProxy from 'http-proxy'; +import dotenv from 'dotenv'; + +dotenv.config(); + +const proxy = httpProxy.createProxyServer({}); +const BEARER_TOKEN = process.env.ASTRIA_API_KEY; + +export default function handler(req, res) { + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, DELETE'); + res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type'); + + if (req.method === 'OPTIONS') { + return res.status(204).end(); + } + + req.headers['Authorization'] = `Bearer ${BEARER_TOKEN}`; + + proxy.web(req, res, { target: 'https://api.astria.ai', changeOrigin: true }, (error) => { + console.error('Proxy error:', error); + res.status(500).end('Proxy error occurred.'); + }); +}