env2json-converter
v1.0.0
Published
A TypeScript library that converts .env files to JSON objects with type safety support
Maintainers
Readme
env2json-converter
A powerful TypeScript library that converts .env files to JSON objects with type safety support. Perfect for TypeScript projects that need type-safe environment variable handling.
🌟 Features
- ✨ Convert
.envfiles to JSON objects - 🔒 Type-safe environment variable handling
- ✅ Schema validation for required environment variables
- 🚨 Comprehensive error handling
- 📦 Zero runtime dependencies
- 🧪 Full test coverage
- 📝 Detailed TypeScript documentation
📦 Installation
npm install env2json-converter🚀 Usage
Basic Usage
import { envToJson } from "env2json-converter";
// Convert .env file to JSON object
const envObject = envToJson(".env");
// Example output:
// {
// DB_HOST: "localhost",
// DB_PORT: "5432",
// API_KEY: "secret123"
// }Type-Safe Usage
import { envToJsonTyped } from "env2json-converter";
// Define your environment variable types
interface DatabaseConfig {
DB_HOST: string;
DB_PORT: string;
API_KEY: string;
}
// Define schema for validation
const schema: Record<keyof DatabaseConfig, string> = {
DB_HOST: "string",
DB_PORT: "string",
API_KEY: "string",
};
// Convert with type safety
const config = envToJsonTyped<DatabaseConfig>(".env", schema);
// Now config is fully typed!
console.log(config.DB_HOST); // TypeScript knows this exists📚 API Documentation
envToJson(envPath: string): EnvConfig
Converts a .env file to a JSON object.
Parameters:
envPath: Path to the.envfile
Returns:
- An object containing all environment variables
Throws:
- Error if the
.envfile doesn't exist - Error if the
.envfile is invalid
envToJsonTyped<T extends EnvConfig>(envPath: string, schema?: Record<keyof T, string>): T
Converts a .env file to a typed JSON object with optional schema validation.
Parameters:
envPath: Path to the.envfileschema: Optional schema to validate environment variables
Returns:
- A typed object containing all environment variables
Throws:
- Error if the
.envfile doesn't exist - Error if the
.envfile is invalid - Error if required variables are missing (when schema is provided)
📝 Example .env File
DB_HOST=localhost
DB_PORT=5432
API_KEY=secret123🛠️ Development
# Install dependencies
npm install
# Run tests
npm test
# Build the project
npm run build
# Run linter
npm run lint📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
🔗 Links
👥 Author
- Nati Grossman - GitHub
🙏 Acknowledgments
- Thanks to all contributors who have helped shape this project
- Inspired by the need for type-safe environment variable handling in TypeScript projects
