Skip to content

Commit 4268dda

Browse files
committed
Add Environment bootstrap system
1 parent 3815ada commit 4268dda

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strontium",
3-
"version": "2.7.1",
3+
"version": "2.7.2",
44
"description": "Strontium is a TypeScript toolkit for High Performance API servers built for Production not Projects.",
55
"main": "lib/src/index.js",
66
"types": "lib/src/index.d.ts",

src/bootstrap/drivers/Environment.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Container } from "inversify"
2+
import { Process } from "../../runtime"
3+
import { ObjectValidator, ValidatedObject, isObject } from "../../validation"
4+
5+
export class Environment<O extends ObjectValidator> implements Process {
6+
private validatedEnvironment?: ValidatedObject<O>
7+
8+
constructor(private validator: O) {}
9+
10+
public getKey<K extends keyof ValidatedObject<O>>(key: K): O[K] {
11+
if (this.validatedEnvironment !== undefined) {
12+
return this.validatedEnvironment[key]
13+
} else {
14+
throw new Error(
15+
"An environment value was accessed before the Environment container completed initialization."
16+
)
17+
}
18+
}
19+
20+
public isHealthy(): boolean {
21+
return this.validatedEnvironment !== undefined
22+
}
23+
24+
public async startup(container: Container): Promise<void> {
25+
container.bind(Environment).toConstantValue(this)
26+
27+
this.validatedEnvironment = await isObject(this.validator)(process.env)
28+
}
29+
30+
public async shutdown(container: Container): Promise<void> {
31+
container.unbind(Environment)
32+
33+
this.validatedEnvironment = undefined
34+
}
35+
}

src/bootstrap/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./drivers/Environment"

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./bootstrap"
12
export * from "./cryptography"
23
export * from "./datastore"
34
export * from "./errors"

0 commit comments

Comments
 (0)