Skip to content

Commit

Permalink
refactor Vite configuration to conditionally set up proxy based on BE…
Browse files Browse the repository at this point in the history
…ARER_TOKEN presence; remove redundant error handling
  • Loading branch information
Precious-Macaulay committed Nov 13, 2024
1 parent 9f7b63f commit 97774e8
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ dotenv.config();

const BEARER_TOKEN = process.env.ASTRIA_API_KEY;

if (!BEARER_TOKEN) {
throw new Error('BEARER_TOKEN is not defined in the environment variables');
}

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
Expand All @@ -24,26 +20,28 @@ export default defineConfig({
},
server: {
port: 5173,
proxy: {
'/api': {
target: 'https://api.astria.ai',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
configure: (proxy) => {
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Authorization', `Bearer ${BEARER_TOKEN}`);
});
}
},
'/rails/active_storage/blobs': {
target: 'https://api.astria.ai',
changeOrigin: true,
configure: (proxy) => {
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Authorization', `Bearer ${BEARER_TOKEN}`);
});
...(BEARER_TOKEN && {
proxy: {
'/api': {
target: 'https://api.astria.ai',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
configure: (proxy) => {
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Authorization', `Bearer ${BEARER_TOKEN}`);
});
}
},
'/rails/active_storage/blobs': {
target: 'https://api.astria.ai',
changeOrigin: true,
configure: (proxy) => {
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Authorization', `Bearer ${BEARER_TOKEN}`);
});
}
}
},
}
}
})
}
})

0 comments on commit 97774e8

Please sign in to comment.