Skip to content

Commit d4fe786

Browse files
committed
Initial Commit
0 parents  commit d4fe786

35 files changed

+13148
-0
lines changed

.editorconfig

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.php]
12+
indent_style = tab
13+
indent_size = 4
14+
15+
[*.py]
16+
charset = utf-8
17+
indent_style = space
18+
indent_size = 4
19+
end_of_line = lf
20+
insert_final_newline = true
21+
trim_trailing_whitespace = true
22+
23+
[Makefile]
24+
indent_style = tab
25+
indent_size = 4
26+
27+
[*.sln]
28+
indent_style = tab
29+
30+
[*.{md,mdx}]
31+
trim_trailing_whitespace = false

.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.config.js
2+
*.config.ts
3+
config/*.js
4+
dist

.eslintrc.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
extends: ['plugin:@typescript-eslint/recommended', 'prettier'],
4+
parserOptions: {
5+
sourceType: 'module',
6+
},
7+
rules: {
8+
'prettier/prettier': 'error',
9+
},
10+
plugins: ['@typescript-eslint', 'prettier'],
11+
}

.github/workflows/publish.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Release & Publish
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
- name: Create Release
15+
id: create_release
16+
uses: actions/create-release@v1
17+
env:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
with:
20+
tag_name: ${{ github.ref }}
21+
release_name: ${{ github.ref }}
22+
draft: false
23+
prerelease: false
24+
25+
publish:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v2
29+
- name: Setup .npmrc file to publish to npm
30+
uses: actions/setup-node@v1
31+
with:
32+
node-version: '14.x'
33+
registry-url: 'https://registry.npmjs.org'
34+
- name: Install modules
35+
run: npm install
36+
- name: Build
37+
run: npm run build
38+
- name: Publish to npm
39+
run: npm publish --access public
40+
env:
41+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
42+
- name: Setup .npmrc file to publish to GitHub Packages
43+
uses: actions/setup-node@v1
44+
with:
45+
node-version: '14.x'
46+
registry-url: 'https://npm.pkg.github.com'
47+
scope: '@amccarthy1'
48+
- run: npm run addscope
49+
- name: Publish to GitHub Packages
50+
run: npm publish
51+
env:
52+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- 'docs/**'
7+
- 'README.md'
8+
- 'LICENSE'
9+
- '.editorconfig'
10+
branches:
11+
- main
12+
- master
13+
pull_request:
14+
paths-ignore:
15+
- 'docs/**'
16+
- 'README.md'
17+
- 'LICENSE'
18+
- '.editorconfig'
19+
branches:
20+
- main
21+
- master
22+
23+
jobs:
24+
test:
25+
runs-on: ${{ matrix.os }}
26+
strategy:
27+
matrix:
28+
os:
29+
- ubuntu-latest
30+
node: [12.x, 14.x, 16.x]
31+
32+
steps:
33+
- uses: actions/checkout@v2
34+
- name: Set up Node.js
35+
uses: actions/setup-node@v1
36+
with:
37+
node-version: ${{ matrix.node }}
38+
- name: Install modules
39+
run: npm i
40+
- name: Lint
41+
run: npm run lint
42+
- name: Run tests
43+
run: npm test

.gitignore

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2+
# Logs
3+
logs
4+
*.log
5+
npm-debug.log*
6+
yarn-debug.log*
7+
yarn-error.log*
8+
lerna-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Microbundle cache
58+
.rpt2_cache/
59+
.rts2_cache_cjs/
60+
.rts2_cache_es/
61+
.rts2_cache_umd/
62+
63+
# Optional REPL history
64+
.node_repl_history
65+
66+
# Output of 'npm pack'
67+
*.tgz
68+
69+
# Yarn Integrity file
70+
.yarn-integrity
71+
72+
# dotenv environment variables file
73+
.env
74+
.env.test
75+
76+
# parcel-bundler cache (https://parceljs.org/)
77+
.cache
78+
.parcel-cache
79+
80+
# Next.js build output
81+
.next
82+
out
83+
84+
# Nuxt.js build / generate output
85+
.nuxt
86+
dist
87+
88+
# Gatsby files
89+
.cache/
90+
# Comment in the public line in if your project uses Gatsby and not Next.js
91+
# https://nextjs.org/blog/next-9-1#public-directory-support
92+
# public
93+
94+
# vuepress build output
95+
.vuepress/dist
96+
97+
# Serverless directories
98+
.serverless/
99+
100+
# FuseBox cache
101+
.fusebox/
102+
103+
# DynamoDB Local files
104+
.dynamodb/
105+
106+
# TernJS port file
107+
.tern-port
108+
109+
# Stores VSCode versions used for testing VSCode extensions
110+
.vscode-test
111+
112+
# yarn v2
113+
.yarn/cache
114+
.yarn/unplugged
115+
.yarn/build-state.yml
116+
.yarn/install-state.gz
117+
.pnp.*

.husky/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/common.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
command_exists () {
2+
command -v "$1" >/dev/null 2>&1
3+
}
4+
5+
# Windows 10, Git Bash and Yarn workaround
6+
if command_exists winpty && test -t 1; then
7+
exec < /dev/tty
8+
fi

.husky/pre-commit

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
. "$(dirname "$0")/common.sh"
4+
5+
npm run test

.prettierrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
semi: false,
3+
singleQuote: true,
4+
trailingComma: 'es5',
5+
arrowParens: 'always',
6+
printWidth: 80,
7+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Adam McCarthy ([email protected])
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Typed Env
2+
3+
A strongly-typed, 0-dependency environment variable parser for Typescript!
4+
5+
## Why is this useful
6+
7+
Many services use environment variables for runtime configuration, anything from the current
8+
environment or logging verbosity, to things like API keys and secrets. But many times, these
9+
environment variables are unvalidated and naively parsed from strings when needed.
10+
11+
This library aims to allow you to define environment variables in a more type-safe way. Take this
12+
example:
13+
14+
```ts
15+
const environment = process.env.ENVIRONMENT;
16+
17+
const makePayment = (amount: bigint) => {
18+
if (environment === 'prod') {
19+
makeRealPayment(amount);
20+
} else {
21+
makeMockPayment(amount);
22+
}
23+
};
24+
```
25+
26+
What if you configured your server with `ENVIRONMENT=production` instead of `ENVIRONMENT=prod`?
27+
Suddenly, all your users are getting free products because you're making mock payments instead of
28+
real ones!
29+
30+
If you'd used TypedEnv instead, you'd get this
31+
32+
```ts
33+
const env = TypedEnv({
34+
ENVIRONMENT: EnumVar({options: ['dev', 'staging', 'production']}),
35+
});
36+
37+
const makePayment = (amount: bigint) => {
38+
if (env.ENVIRONMENT === 'prod') {
39+
// TypeError!
40+
// `This condition will always return 'false' since
41+
// the types '"dev" | "staging" | "production"' and
42+
// '"prod"' have no overlap.`
43+
return makeRealPayment(amount);
44+
} else {
45+
return makeFakePayment(amount);
46+
}
47+
};
48+
```
49+
50+
## What types are supported?
51+
52+
Currently, strings, integers, and enums are supported, although you can define your own custom type
53+
using the `Declaration<T>` type
54+
55+
```ts
56+
export type Declaration<T> = {
57+
variable?: string; // The name of the environment variable; defaults to match the key if not specified
58+
parser: Parser<T>; // A function (value: string) => T
59+
};
60+
```

commitlint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {extends: ['@commitlint/config-conventional']}

config/fileTransformer.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// source: https://jestjs.io/docs/code-transformation#examples
2+
3+
const path = require('path')
4+
5+
module.exports = {
6+
process(src, filename, config, options) {
7+
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';'
8+
},
9+
}

config/tsconfig.cjs.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig",
3+
"compilerOptions": {
4+
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
5+
"outDir": "../dist/cjs" /* Redirect output structure to the directory. */
6+
}
7+
}

0 commit comments

Comments
 (0)