Skip to content

Commit

Permalink
add typescript example (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeatty committed Jan 13, 2019
1 parent e1e7d57 commit c1645c9
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ errorReporter.client.report(new Error('faq example'))
`client` will not be configured correctly because it was constructed before `dotenv.config()` was executed. There are (at least) 3 ways to make this work.

1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
2. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call `dotenv.config()` and must pass options via the command line with this approach_)
2. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call `dotenv.config()` and must pass options via the command line or environment variables with this approach_)
3. Create a separate file that will execute `config` first as outlined in [this comment on #133](https://github.com/motdotla/dotenv/issues/133#issuecomment-255298822)

## Contributing Guide
Expand Down
3 changes: 3 additions & 0 deletions examples/typescript/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NODE_ENV=example
SAMPLE_KEY=defined
ERR_API_KEY=qwerty12345
1 change: 1 addition & 0 deletions examples/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
16 changes: 16 additions & 0 deletions examples/typescript/README.md
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
```
32 changes: 32 additions & 0 deletions examples/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/typescript/package.json
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"
}
}
8 changes: 8 additions & 0 deletions examples/typescript/src/index.ts
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"))
5 changes: 5 additions & 0 deletions examples/typescript/src/lib/env.ts
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") })
17 changes: 17 additions & 0 deletions examples/typescript/src/lib/errors.ts
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)
21 changes: 21 additions & 0 deletions examples/typescript/tsconfig.json
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/**/*"
]
}

0 comments on commit c1645c9

Please sign in to comment.