-
-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NODE_ENV=example | ||
SAMPLE_KEY=defined | ||
ERR_API_KEY=qwerty12345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# TypeScript Example | ||
|
||
> Use `dotenv` in a simple TypeScript project. | ||
`src/lib/errors.ts` requires an environment variable at time of export so `src/lib/env.ts` contains `dotenv` configuration and is imported first in `src/index.ts`. | ||
|
||
1. `npm install` | ||
2. `npm run build` | ||
3. `npm start` | ||
|
||
Expected output: | ||
|
||
``` | ||
Current NODE_ENV is example | ||
Sample key is defined | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"scripts": { | ||
"build": "tsc", | ||
"start": "node dist/index.js" | ||
}, | ||
"dependencies": { | ||
"dotenv": "latest" | ||
}, | ||
"devDependencies": { | ||
"@types/dotenv": "^6.1.0", | ||
"typescript": "^3.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import "./lib/env" | ||
import errorReporter from "./lib/errors" | ||
|
||
console.log(`Current NODE_ENV is ${process.env.NODE_ENV}`) | ||
|
||
console.log(`Sample key is ${process.env.SAMPLE_KEY}`) | ||
|
||
errorReporter.report(new Error("example")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { resolve } from "path" | ||
|
||
import { config } from "dotenv" | ||
|
||
config({ path: resolve(__dirname, "../../.env.example") }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class ErrorReporter { | ||
private apiKey: string | ||
|
||
constructor(apiKey: string) { | ||
if (apiKey === undefined || apiKey === "") { | ||
throw new Error("apiKey required") | ||
} | ||
|
||
this.apiKey = apiKey | ||
} | ||
|
||
report(err: Error) { | ||
// could use apiKey here to send error somewhere | ||
} | ||
} | ||
|
||
export default new ErrorReporter(process.env.ERR_API_KEY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"target": "es6", | ||
"noImplicitAny": true, | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"outDir": "dist", | ||
"baseUrl": ".", | ||
"paths": { | ||
"*": [ | ||
"node_modules/*", | ||
"src/types/*" | ||
] | ||
} | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |