-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdev-proxy.config.ts
More file actions
70 lines (61 loc) · 2.05 KB
/
dev-proxy.config.ts
File metadata and controls
70 lines (61 loc) · 2.05 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
65
66
67
68
69
70
import type { HttpProxy, ProxyOptions } from 'vite'
export const createProxyConfig = (
target: string,
rewritePath: string,
label = 'PROXY'
): ProxyOptions => {
return {
target,
changeOrigin: true,
rewrite: (path: string) => path.replace(rewritePath, ''),
configure: (proxy: HttpProxy.Server, options: ProxyOptions) => {
proxy.on('proxyReq', (proxyReq, req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, PATCH, POST, DELETE')
res.setHeader(
'Access-Control-Allow-Headers',
req.headers['access-control-request-headers'] || ''
)
// Remove referer and referrer headers to avoid being blocked
proxyReq.removeHeader('referer')
proxyReq.removeHeader('referrer')
if (req.method === 'OPTIONS') {
res.writeHead(200)
res.end()
return
}
// eslint-disable-next-line no-console
console.log(
`[${label}] - API CALL - [${req.method}] ${typeof options.target === 'string' ? options.target : JSON.stringify(options.target)}${req.url}`
)
proxyReq.setHeader('Authorization', req.headers.authorization || '')
})
proxy.on('error', (err, _req, res) => {
// eslint-disable-next-line no-console
console.error(`Proxy error: ${err.message}`)
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'Proxy error', details: err.message }))
})
}
}
}
export const qliApiProxy = createProxyConfig(
'https://api.qubic.li',
'/dev-proxy-qli-api',
'QLI-API-DEV-PROXY'
)
export const rpcApiProxy = createProxyConfig(
'https://rpc.qubic.org',
'/dev-proxy-rpc-api',
'RPC-API-DEV-PROXY'
)
export const staticApiProxy = createProxyConfig(
'https://static.qubic.org/dev',
'/dev-proxy-static-api',
'STATIC-API-DEV-PROXY'
)
export const eventsApiProxy = createProxyConfig(
'https://dev01.qubic.org',
'/dev-proxy-events-api',
'EVENTS-API-DEV-PROXY'
)