Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
coltenkrauter committed Oct 28, 2024
0 parents commit 8439769
Show file tree
Hide file tree
Showing 19 changed files with 8,081 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This auto assigns the following teams to pull requests
* @krauters/reviewers
18 changes: 18 additions & 0 deletions .github/workflows/node-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Node Publish
run-name: Node Publish [${{ github.ref_name }}] triggered by [${{ github.event_name }}/${{ github.actor }}]

on:
release:
types: published
push:
branches: '*'
workflow_dispatch:

jobs:
publish:
uses: krauters/shared-workflows/.github/workflows/node-publish.yaml@main
secrets:
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
with:
node_install: true
dry_run: ${{ github.event_name != 'release' }}
13 changes: 13 additions & 0 deletions .github/workflows/node-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Node Release
run-name: Node Release [${{ github.ref_name }}] triggered by [${{ github.event_name }}/${{ github.actor }}]

on:
push:
branches: main
workflow_dispatch:

jobs:
publish:
uses: krauters/shared-workflows/.github/workflows/node-release.yaml@main
secrets:
GH_TOKEN_RELEASES: ${{ secrets.GH_TOKEN_RELEASES }}
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Node.js dependencies
node_modules/

# Output directories
dist/

# IDE and editor files
.idea/
.vscode/

# System files
.DS_Store
Thumbs.db

# Coverage directories
coverage/

# Environment variable files
dev.env
.env
.env.local
.env.*.local
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

MAIN_DIR=./node_modules/@krauters/utils/scripts/pre-commit
. $MAIN_DIR/index.sh
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) 2024 Krauters

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<div align="center">

![Code Size](https://img.shields.io/github/languages/code-size/krauters/environment)
![Commits per Month](https://img.shields.io/github/commit-activity/m/krauters/environment)
![Contributors](https://img.shields.io/github/contributors/krauters/environment)
![Forks](https://img.shields.io/github/forks/krauters/environment)
![GitHub Stars](https://img.shields.io/github/stars/krauters/environment)
![Install Size](https://img.shields.io/npm/npm/dw/@krauters%2Futils)
![GitHub Issues](https://img.shields.io/github/issues/krauters/environment)
![Last Commit](https://img.shields.io/github/last-commit/krauters/environment)
![License](https://img.shields.io/github/license/krauters/environment)
<a href="https://www.linkedin.com/in/coltenkrauter" target="_blank"><img src="https://img.shields.io/badge/LinkedIn-%230077B5.svg?&style=flat-square&logo=linkedin&logoColor=white" alt="LinkedIn"></a>
[![npm version](https://img.shields.io/npm/v/@krauters%2Futils.svg?style=flat-square)](https://www.npmjs.org/package/@krauters/environment)
![Open PRs](https://img.shields.io/github/issues-pr/krauters/environment)
![Repo Size](https://img.shields.io/github/repo-size/krauters/environment)
![Version](https://img.shields.io/github/v/release/krauters/environment)
![visitors](https://visitor-badge.laobi.icu/badge?page_id=krauters/environment)

</div>

# @krauters/environment

Typesafe control over environment variables in TypeScript with powerful configurations for required and optional variables, custom transformations, and type safety. Inspired by [@hexlabs/env-vars-ts](https://github.com/hexlabsio/env-vars-ts).

[![Build](https://github.com/your-repo/actions/workflows/build.yml/badge.svg)](https://github.com/your-repo/actions/workflows/build.yml)
[![npm version](https://badge.fury.io/js/environment-builder-ts.svg)](https://badge.fury.io/js/environment-builder-ts)

## Get Started

### Installation

Install the package via npm:
```bash
npm install environment-builder-ts
```

### Defining Environment Variables

Define **required** and **optional** environment variables with ease, using strong TypeScript typing.

```typescript
import { EnvironmentBuilder } from 'environment-builder-ts'

// Define required variables
const builder = EnvironmentBuilder.create('API_URL', 'API_KEY')

// Define optional variables
const environment = builder.optionals('LOG_LEVEL', 'DEBUG_MODE').environment()
```

### Example Usage

Configure environment variables with custom defaults and transformations, then retrieve them from `process.env`.

```typescript
// Example setup with defaults and transformations
const config = EnvironmentBuilder
.create('API_URL', 'TIMEOUT') // Define required variables
.optionals('LOG_LEVEL') // Define optional variables
.defaults({ TIMEOUT: '3000' }) // Provide defaults
.transform(value => parseInt(value), 'TIMEOUT') // Custom transformation to number
.environment() // Retrieve environment variables

console.log(config.API_URL) // Outputs API_URL value
console.log(config.TIMEOUT) // Outputs timeout as a number, e.g., 3000
console.log(config.LOG_LEVEL) // Optional, may be undefined if not set
```

### Advanced Usage

Apply multiple custom transformations and defaults as needed.

```typescript
// Define a more complex environment with multiple types and transformations
const envConfig = EnvironmentBuilder
.create('ENABLED_FEATURES', 'MAX_CONNECTIONS')
.optionals('LOG_LEVEL', 'API_VERSION')
.transform(s => s.split(','), 'ENABLED_FEATURES') // Transform comma-separated string to array
.transform(s => parseInt(s), 'MAX_CONNECTIONS')
.defaults({ MAX_CONNECTIONS: 5 })
.environment()

// Typed values
console.log(envConfig.ENABLED_FEATURES) // ['feature1', 'feature2']
console.log(envConfig.MAX_CONNECTIONS) // 5
```

## Key Features

- **Type Safety**: Explicit typing for environment variables, catching issues at compile time.
- **Custom Transformations**: Define custom transformations (e.g., parse strings to numbers, arrays).
- **Built-In Validation**: Required variables are validated, and an error is thrown if any are missing.
- **Configurable**: Easily designate required/optional variables and specify defaults only where needed.

## Husky Git Hooks

Husky is integrated to streamline commit processes by automating checks before commits are finalized. This setup enforces code quality standards across your team and ensures that bundled assets remain consistent.

### Custom Pre-Commit Hook

This project uses a custom pre-commit hook that triggers `npm run bundle` before each commit. This ensures that any code related to bundling is always updated before it’s committed, especially useful for TypeScript applications in CI/CD. Husky automates this check to enforce consistency across commits.

## Acknowledgments

This project draws inspiration from `@hexlabs/env-vars-ts`, which laid the groundwork for a simple and effective approach to managing environment variables in TypeScript. Special thanks to the team for their work.

## Contributing

The goal of this project is to continually evolve and improve its core features, making it more efficient and easier to use. Development happens openly here on GitHub, and we’re thankful to the community for contributing bug fixes, enhancements, and fresh ideas. Whether you're fixing a small bug or suggesting a major improvement, your input is invaluable.

## License

This project is licensed under the ISC License. Please see the [LICENSE](./LICENSE) file for more details.

## 🥂 Thanks Contributors

Thanks for spending time on this project.

<a href="https://github.com/krauters/utils/graphs/contributors">
<img src="https://contrib.rocks/image?repo=krauters/utils" />
</a>

## 🔗 Other packages in the family
We’ve got more than just this one in our toolbox – check out the rest of our `@krauters` collection on [npm/@krauters](https://www.npmjs.com/search?q=%40krauters). It’s the whole kit and caboodle you didn’t know you needed.
4 changes: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable filenames/match-exported */
const eslintConfig = require('@krauters/eslint-config')

module.exports = eslintConfig
20 changes: 20 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable @typescript-eslint/naming-convention */

module.exports = {
collectCoverage: true,
coverageDirectory: 'coverage',
coverageThreshold: {
global: {
branches: 60,
functions: 60,
lines: 60,
statements: 60,
},
},
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/test/**/*.test.{ts,tsx}'],
transform: {
'^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tsconfig.jest.json' }],
},
}
Loading

0 comments on commit 8439769

Please sign in to comment.