Skip to content

Commit

Permalink
Add ability to set up a proxy to http (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jan 13, 2025
1 parent b2b70d8 commit 2dce0a9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 33 deletions.
48 changes: 46 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import { defineConfig, HttpProxy, loadEnv, type ProxyOptions, type UserConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import { BootstrapVueNextResolver } from 'bootstrap-vue-next';
import postcssNesting from 'postcss-nesting';

// https://vitejs.dev/config/
export default defineConfig({
const baseConfig = {
server: {
port: 8080,
cors: false,
Expand Down Expand Up @@ -61,4 +61,48 @@ export default defineConfig({
reporters: ['verbose', 'vitest-sonar-reporter'],
outputFile: 'sonar-report.xml',
},
} as UserConfig;

function defineProxyConfig(url: string) {
return {
'/resc': {
target: url,
changeOrigin: true,
secure: true,
ws: true,
configure: (proxy: HttpProxy.Server, _options: ProxyOptions) => {
proxy.on('error', (err, _req, _res) => {
console.log('proxy error', err);
});
proxy.on('proxyReq', (proxyReq, req, _res) => {
console.log(req.method?.padEnd(4), '=> ', proxyReq.host + req.url);
});
proxy.on('proxyRes', (proxyRes, req, _res) => {
console.log(proxyRes.statusCode?.toString().padEnd(4), '<=', req.url);
});
},
},
};
}

/** @type {import('vite').UserConfig} */
export default defineConfig(({ command, mode }) => {
const config = baseConfig;
const env = loadEnv(mode, process.cwd(), '');

// We define it but otherwise TS is complaining.
if (config.server === undefined) {
throw new Error('server config is missing');
}

if (command === 'serve') {
console.log('LOCAL VITE MODE detected');
if (env.VITE_HTTP_PROXY_TARGET !== undefined) {
console.log('api calls will be forwarded to:');
console.log(env.VITE_HTTP_PROXY_TARGET);
config.server.proxy = defineProxyConfig(env.VITE_HTTP_PROXY_TARGET);
}
}

return config;
});
64 changes: 33 additions & 31 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@ import { fileURLToPath } from 'node:url';
import { mergeConfig, defineConfig, configDefaults } from 'vitest/config';
import viteConfig from './vite.config';

export default mergeConfig(
viteConfig,
defineConfig({
test: {
environment: 'jsdom',
exclude: [...configDefaults.exclude, 'e2e/*', 'resc-repository-scanner/components/*'],
root: fileURLToPath(new URL('./', import.meta.url)),
globals: true,
coverage: {
include: ['src/**/*'],
exclude: ['**/main.ts', '**/axios-config.ts'],
enabled: true,
provider: 'istanbul',
all: true,
reporter: ['lcov', 'text', 'cobertura', 'html'],
reportsDirectory: 'tests/reports/coverage',
// Next version of Vite
// thresholds: {
// branches: 95,
// functions: 95,
// lines: 95,
// statements: 95,
// 'src/**.ts': {
// branches: 60,
// functions: 60,
// lines: 80,
// statements: 80,
// },
// },
export default defineConfig((configEnv) =>
mergeConfig(
viteConfig(configEnv),
defineConfig({
test: {
environment: 'jsdom',
exclude: [...configDefaults.exclude, 'e2e/*', 'resc-repository-scanner/components/*'],
root: fileURLToPath(new URL('./', import.meta.url)),
globals: true,
coverage: {
include: ['src/**/*'],
exclude: ['**/main.ts', '**/axios-config.ts'],
enabled: true,
provider: 'istanbul',
all: true,
reporter: ['lcov', 'text', 'cobertura', 'html'],
reportsDirectory: 'tests/reports/coverage',
// Next version of Vite
// thresholds: {
// branches: 95,
// functions: 95,
// lines: 95,
// statements: 95,
// 'src/**.ts': {
// branches: 60,
// functions: 60,
// lines: 80,
// statements: 80,
// },
// },
},
},
},
}),
}),
),
);

0 comments on commit 2dce0a9

Please sign in to comment.