-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
64 lines (57 loc) · 2.14 KB
/
Copy pathvite.config.js
File metadata and controls
64 lines (57 loc) · 2.14 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
plugins: [
vue(),
// Proxy middleware: browser → /api/openrouter → openrouter.ai
// Runs in Node.js — no CORS, key never exposed to browser.
{
name: 'openrouter-proxy',
configureServer(server) {
server.middlewares.use('/api/openrouter', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ error: 'Method not allowed' }))
return
}
const apiKey = env.OPENROUTER_API_KEY
if (!apiKey) {
res.statusCode = 401
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ error: 'OPENROUTER_API_KEY is not set in .env' }))
return
}
const chunks = []
req.on('data', (c) => chunks.push(c))
req.on('end', async () => {
try {
const body = Buffer.concat(chunks).toString()
const upstream = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
'HTTP-Referer': 'http://localhost:5173',
'X-Title': 'AI Study Coach',
},
body,
})
const data = await upstream.json()
res.statusCode = upstream.status
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
} catch (err) {
res.statusCode = 500
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ error: err.message }))
}
})
})
},
},
],
}
})