40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
|
import { defineConfig } from 'vite';
|
||
|
import vue from '@vitejs/plugin-vue';
|
||
|
|
||
|
export default defineConfig({
|
||
|
plugins: [vue()],
|
||
|
server: {
|
||
|
proxy: {
|
||
|
'/api': {
|
||
|
target: 'http://localhost:7370',
|
||
|
changeOrigin: true,
|
||
|
configure: (proxy, options) => {
|
||
|
proxy.on('proxyReq', (proxyReq, req, res) => {
|
||
|
const apiUri = req.headers['x-api-uri'];
|
||
|
if (apiUri) {
|
||
|
const targetUrl = new URL(apiUri);
|
||
|
options.target = targetUrl.origin;
|
||
|
proxyReq.setHeader('host', targetUrl.host);
|
||
|
|
||
|
// Rewrite path to include the API URI path
|
||
|
const apiPath = targetUrl.pathname;
|
||
|
const requestPath = req.url.replace('/api', '');
|
||
|
proxyReq.path = `${apiPath}${requestPath}`;
|
||
|
|
||
|
console.log(`Proxying to: ${options.target}${proxyReq.path}`);
|
||
|
}
|
||
|
|
||
|
if (req.headers['x-api-key']) {
|
||
|
proxyReq.setHeader('X-Api-Key', req.headers['x-api-key']);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
proxy.on('error', (err, req, res) => {
|
||
|
console.error('Proxy error:', err);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|