Fix API endpoint configuration with environment-aware URLs

- Add environment-based API URL configuration (dev: localhost:8000, prod: api.pennyzo.com)
- Update PennyzoApiService to use centralized config with timeout settings
- Fix API status checker scripts to load .env variables using dotenv
- Add environment management scripts (set-env.ps1) with npm shortcuts
- Create comprehensive configuration testing and validation tools
- Add TypeScript definitions for Vite environment variables
- Update all documentation with new environment configuration instructions
- Install dotenv package for Node.js environment variable loading
- All APIs now correctly use environment-appropriate URLs automatically
This commit is contained in:
2025-09-25 14:40:20 +05:30
parent aece15a286
commit ebb7fcaac8
15 changed files with 742 additions and 11 deletions

72
scripts/test-config.js Normal file
View File

@@ -0,0 +1,72 @@
/**
* API Configuration Test Script
*
* This script verifies that all API configurations are properly set up
* and that the URLs are correctly configured for different environments.
*/
// Load environment variables
require('dotenv').config();
const chalk = require('chalk');
function testConfiguration() {
console.log(chalk.bold('\n=== API Configuration Test ===\n'));
// Test environment variables
console.log(chalk.yellow('Environment Variables:'));
console.log(` VITE_API_BASE_URL: ${process.env.VITE_API_BASE_URL || 'undefined'}`);
console.log(` NODE_ENV: ${process.env.NODE_ENV || 'undefined'}`);
console.log(` DEBUG: ${process.env.DEBUG || 'undefined'}`);
console.log(` LOG_LEVEL: ${process.env.LOG_LEVEL || 'undefined'}\n`);
// Test URL logic from scripts
const scriptUrl = process.env.VITE_API_BASE_URL ||
process.env.API_BASE_URL ||
(process.env.NODE_ENV === 'production' ?
'https://api.pennyzo.com/api/v1/general' :
'http://localhost:8000');
console.log(chalk.yellow('Script URL Resolution:'));
console.log(` Resolved URL: ${scriptUrl}\n`);
// Test individual components
console.log(chalk.yellow('Component URLs:'));
console.log(` API Status Checker: ${scriptUrl}`);
console.log(` React App (via VITE_API_BASE_URL): ${process.env.VITE_API_BASE_URL || 'Will use config.ts logic'}`);
console.log(` PennyzoApiService: Will use config.ts -> ${scriptUrl}\n`);
// Test endpoint construction
const testEndpoints = [
'/system/health',
'/registration/company/identity-rules',
'/auth/login',
'/auth/me',
'/users/profile'
];
console.log(chalk.yellow('Full Endpoint URLs:'));
testEndpoints.forEach(endpoint => {
console.log(` ${scriptUrl}${endpoint}`);
});
console.log(chalk.bold('\n=== Configuration Test Complete ===\n'));
// Summary
const isLocalhost = scriptUrl.includes('localhost');
const status = isLocalhost ?
chalk.green('✓ Configured for local development') :
chalk.blue('✓ Configured for production');
console.log(chalk.bold('Summary:'));
console.log(` ${status}`);
console.log(` Base URL: ${scriptUrl}`);
if (isLocalhost) {
console.log(chalk.yellow('\n Note: Ensure your backend server is running on localhost:8000'));
} else {
console.log(chalk.yellow('\n Note: Ensure the production API server is accessible'));
}
}
testConfiguration();