diff --git a/src/env/env.manager.ts b/src/env/env.manager.ts index ecf632d..901b931 100644 --- a/src/env/env.manager.ts +++ b/src/env/env.manager.ts @@ -7,7 +7,7 @@ interface Options { } export class EnvManager extends Manager { - private nodeEnv: string; + private nodeEnv: NodeEnv; constructor({ getEnv = () => process.env as Config, @@ -18,7 +18,7 @@ export class EnvManager(key: Key) { + private getEnvValue(key: Key): Config[Key] | undefined { const value = this.source[key]; if (value === null || value === undefined || value.trim() === '') { @@ -28,8 +28,8 @@ export class EnvManager(envRecord: EnvRecord) { - const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest; + private getKeyOrThrow(envRecord: EnvRecord): Key { + const key = envRecord[this.nodeEnv] ?? envRecord.rest; if (!key) { throw new Error(`key is not found empty in nodeEnv: ${this.nodeEnv}`); @@ -38,38 +38,42 @@ export class EnvManager(key: Key) { - return new EnvPicker(this.getEnvValue(key), this.nodeEnv) as EnvPicker; + public pick(key: Key): EnvPicker { + return new EnvPicker(this.getEnvValue(key), this.nodeEnv); } - public pickOrThrow(key: Key) { + public pickOrThrow(key: Key): EnvPicker> { const value = this.source[key]; if (value === null || value === undefined || value.trim() === '') { throw new Error(`key: ${key.toString()} is empty`); } - return this.pick(key) as EnvPicker>; + return new EnvPicker(value, this.nodeEnv); } - public pickFor(envRecord: EnvRecord) { - const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest; + public pickFor( + envRecord: EnvRecord, + ): EnvPicker { + const key = envRecord[this.nodeEnv] ?? envRecord.rest; if (!key) { - return new EnvPicker(undefined, this.nodeEnv) as EnvPicker; + return new EnvPicker(undefined, this.nodeEnv); } - return this.pick(key) as EnvPicker; + return this.pick(key); } - public pickForOrThrow(envRecord: EnvRecord) { + public pickForOrThrow( + envRecord: EnvRecord, + ): EnvPicker> { const key = this.getKeyOrThrow(envRecord); return this.pickOrThrow(key); } - public getFor(envRecord: EnvRecord) { - const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest; + public getFor(envRecord: EnvRecord): Config[Key] | undefined { + const key = envRecord[this.nodeEnv] ?? envRecord.rest; if (!key) { return; @@ -78,8 +82,8 @@ export class EnvManager(envRecord: EnvRecord) { - const key: Key | undefined = this.getKeyOrThrow(envRecord); + public getForOrThrow(envRecord: EnvRecord): Config[Key] { + const key = this.getKeyOrThrow(envRecord); return this.getOrThrow(key); } diff --git a/src/env/env.picker.ts b/src/env/env.picker.ts index 6f9e011..809e00e 100644 --- a/src/env/env.picker.ts +++ b/src/env/env.picker.ts @@ -25,13 +25,15 @@ export const wrapInEnvPickers = { constructor(private state: State, private nodeEnv: NodeEnv) {} - public default(newState: State) { + public default(newState: State): EnvPicker> { this.state ??= newState; - return this as unknown as EnvPicker>; + return this as EnvPicker>; } - public defaultFor>(envRecord: E) { + public defaultFor>( + envRecord: E, + ): E extends { rest: State } ? EnvPicker> : EnvPicker { this.state ??= (envRecord[this.nodeEnv] ?? envRecord.rest) as State; return this as unknown as E extends { rest: State } @@ -39,13 +41,15 @@ export class EnvPicker { : EnvPicker; } - public map(mapper: (state: State) => Result) { + public map(mapper: (state: State) => Result): EnvPicker { this.state = mapper(this.state) as unknown as State; return this as unknown as EnvPicker; } - public mapIfExists(mapper: (state: NonNullable) => Result) { + public mapIfExists( + mapper: (state: NonNullable) => Result, + ): EnvPicker> { if (this.state !== null && this.state !== undefined) { this.state = mapper(this.state as NonNullable) as unknown as State; } @@ -53,7 +57,7 @@ export class EnvPicker { return this as unknown as EnvPicker>; } - public value() { + public value(): State { return this.state; } } diff --git a/src/utils/manager.utils.ts b/src/utils/manager.utils.ts index abb4550..da4e98e 100644 --- a/src/utils/manager.utils.ts +++ b/src/utils/manager.utils.ts @@ -9,11 +9,11 @@ export class Manager> { this.source = getSource(); } - public get(key: Key) { + public get(key: Key): Source[Key] | undefined { return this.source[key]; } - public getOrThrow(key: Key) { + public getOrThrow(key: Key): Source[Key] { const value = this.source[key]; if (value === null || value === undefined) {