This code violates both KISS and DRY principles by duplicating the configuration structure across different environment files. A better approach would be to use a base configuration and override only the differences:
const baseConfig = {
auth: {
tokenExpiration: '1h',
refreshTokenExpiration: '7d'
}
};
const envConfigs = {
development: {
apiUrl: 'http://localhost:3000',
database: {
host: 'localhost',
port: 5432,
name: 'myapp_dev',
user: 'dev_user',
password: 'dev_pass'
},
auth: {
jwtSecret: 'dev_secret'
},
logging: {
level: 'debug',
format: 'dev'
}
},
production: {
apiUrl: process.env.API_URL,
database: {
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
name: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
},
auth: {
jwtSecret: process.env.JWT_SECRET
},
logging: {
level: 'info',
format: 'combined'
}
}
};
module.exports = (env) => {
return {
...baseConfig,
...envConfigs[env],
auth: {
...baseConfig.auth,
...envConfigs[env].auth
}
};
};