57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import path from 'node:path';
|
|
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
import { defineConfig, loadEnv, type ProxyOptions } from 'vite';
|
|
|
|
export default defineConfig(({ command, mode }) => {
|
|
const env = loadEnv(mode, process.cwd(), 'DEV_CONSOLE_');
|
|
const target = env.DEV_CONSOLE_API_URL || 'http://localhost:8000';
|
|
const serviceToken = env.DEV_CONSOLE_SERVICE_TOKEN;
|
|
const testVKey = env.DEV_CONSOLE_TEST_VKEY;
|
|
const port = Number(env.DEV_CONSOLE_PORT) || 5173;
|
|
const previewPort = Number(env.DEV_CONSOLE_PREVIEW_PORT) || 4173;
|
|
|
|
const missingCredentials = command === 'serve' && (!serviceToken || !testVKey);
|
|
const proxy: Record<string, ProxyOptions> = {
|
|
'/dev-console-api': {
|
|
target,
|
|
changeOrigin: true,
|
|
rewrite: (requestPath) => requestPath.replace(/^\/dev-console-api/, ''),
|
|
configure: (proxyServer) => {
|
|
proxyServer.on('proxyReq', (proxyRequest, request) => {
|
|
if (missingCredentials || !serviceToken || !testVKey) {
|
|
proxyRequest.destroy(
|
|
new Error('Dev Console proxy credentials are not configured on the server.'),
|
|
);
|
|
return;
|
|
}
|
|
proxyRequest.setHeader('Authorization', `Bearer ${serviceToken}`);
|
|
const requestPath = request.url?.split('?', 1)[0] ?? '';
|
|
if (
|
|
request.method === 'POST' &&
|
|
/\/v1\/scoring-runs(?:\/|$)/.test(requestPath)
|
|
) {
|
|
proxyRequest.setHeader('X-Tenant-VKey', testVKey);
|
|
}
|
|
});
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
plugins: [svelte()],
|
|
resolve: {
|
|
alias: {
|
|
$lib: path.resolve(__dirname, './src/lib'),
|
|
},
|
|
},
|
|
server: {
|
|
port,
|
|
proxy,
|
|
},
|
|
preview: {
|
|
port: previewPort,
|
|
proxy,
|
|
},
|
|
};
|
|
});
|