From def7154dad588b12c7c44f35e2c823e87ed36211 Mon Sep 17 00:00:00 2001 From: allohamora Date: Wed, 6 Sep 2023 19:00:29 +0300 Subject: [PATCH 1/5] feat: remove env.manager cache --- src/env/env.manager.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/env/env.manager.ts b/src/env/env.manager.ts index b9a9e99..ecf632d 100644 --- a/src/env/env.manager.ts +++ b/src/env/env.manager.ts @@ -8,7 +8,6 @@ interface Options { export class EnvManager extends Manager { private nodeEnv: string; - private cache = new Map>(); constructor({ getEnv = () => process.env as Config, @@ -40,14 +39,7 @@ export class EnvManager(key: Key) { - if (!this.cache.has(key)) { - this.cache.set( - key, - new EnvPicker(this.getEnvValue(key), this.nodeEnv) as EnvPicker, - ); - } - - return this.cache.get(key) as EnvPicker; + return new EnvPicker(this.getEnvValue(key), this.nodeEnv) as EnvPicker; } public pickOrThrow(key: Key) { From 642ccde036efe3cdad46edf114588c8c2f323378 Mon Sep 17 00:00:00 2001 From: allohamora Date: Wed, 6 Sep 2023 19:15:24 +0300 Subject: [PATCH 2/5] feat: add return types and remove unnecessary as casting --- src/env/env.manager.ts | 38 +++++++++++++++++++++----------------- src/env/env.picker.ts | 16 ++++++++++------ src/utils/manager.utils.ts | 4 ++-- 3 files changed, 33 insertions(+), 25 deletions(-) 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) { From 4893b925313bbe50a6b4d7bdc4c616fbe5dd4e16 Mon Sep 17 00:00:00 2001 From: allohamora Date: Wed, 6 Sep 2023 19:41:06 +0300 Subject: [PATCH 3/5] feat: improve EnvPicker types --- src/env/env.picker.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/env/env.picker.ts b/src/env/env.picker.ts index 809e00e..586f540 100644 --- a/src/env/env.picker.ts +++ b/src/env/env.picker.ts @@ -25,36 +25,36 @@ export const wrapInEnvPickers = { constructor(private state: State, private nodeEnv: NodeEnv) {} - public default(newState: State): EnvPicker> { + public default(newState: NewState): EnvPicker> { this.state ??= newState; - return this as EnvPicker>; + return this as EnvPicker>; } - public defaultFor>( + public defaultFor>( envRecord: E, - ): E extends { rest: State } ? EnvPicker> : EnvPicker { + ): E extends { rest: NewState } ? EnvPicker : EnvPicker { this.state ??= (envRecord[this.nodeEnv] ?? envRecord.rest) as State; - return this as unknown as E extends { rest: State } - ? EnvPicker> - : EnvPicker; + return this as unknown as E extends { rest: NewState } + ? EnvPicker + : EnvPicker; } - public map(mapper: (state: State) => Result): EnvPicker { + public map(mapper: (state: State) => NewState): EnvPicker { this.state = mapper(this.state) as unknown as State; - return this as unknown as EnvPicker; + return this as unknown as EnvPicker; } - public mapIfExists( - mapper: (state: NonNullable) => Result, - ): EnvPicker> { + public mapIfExists( + mapper: (state: NonNullable) => NewState, + ): EnvPicker> { if (this.state !== null && this.state !== undefined) { this.state = mapper(this.state as NonNullable) as unknown as State; } - return this as unknown as EnvPicker>; + return this as unknown as EnvPicker>; } public value(): State { From 7726b0f2bf3bebd616042cbd557a32da1f88a135 Mon Sep 17 00:00:00 2001 From: allohamora Date: Wed, 6 Sep 2023 21:33:03 +0300 Subject: [PATCH 4/5] feat: improve defaultFor response types --- src/env/env.picker.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/env/env.picker.ts b/src/env/env.picker.ts index 586f540..5e92063 100644 --- a/src/env/env.picker.ts +++ b/src/env/env.picker.ts @@ -13,6 +13,12 @@ type MoveOptional = Type extends null export type BaseConfig = Record; export type EnvRecord = Partial>; +export type DefaultForResponse = EnvOptions extends { + rest: NewState; +} + ? EnvPicker | EnvOptions[keyof EnvOptions] | EnvOptions['rest']> + : EnvPicker; + export const wrapInEnvPickers = ( config: Config, nodeEnv: NodeEnv, @@ -31,14 +37,12 @@ export class EnvPicker { return this as EnvPicker>; } - public defaultFor>( - envRecord: E, - ): E extends { rest: NewState } ? EnvPicker : EnvPicker { + public defaultFor>( + envRecord: EnvOptions, + ): DefaultForResponse { this.state ??= (envRecord[this.nodeEnv] ?? envRecord.rest) as State; - return this as unknown as E extends { rest: NewState } - ? EnvPicker - : EnvPicker; + return this as unknown as DefaultForResponse; } public map(mapper: (state: State) => NewState): EnvPicker { From c337949ad62391932daf88f71de155867dd6b3ff Mon Sep 17 00:00:00 2001 From: allohamora Date: Wed, 6 Sep 2023 21:41:12 +0300 Subject: [PATCH 5/5] chore(release): 0.8.0 --- CHANGELOG.md | 12 ++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b7a3d3..6b52bca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [0.8.0](https://github.com/allohamora/config-manager/compare/0.6.0...0.8.0) (2023-09-06) + +### Features + +- add rest to envRecord ([2d57b5d](https://github.com/allohamora/config-manager/commit/2d57b5d4e9ce2555cafd5b302ce0b4195c1b4a83)) +- add return types and remove unnecessary as casting ([642ccde](https://github.com/allohamora/config-manager/commit/642ccde036efe3cdad46edf114588c8c2f323378)) +- improve defaultFor response types ([7726b0f](https://github.com/allohamora/config-manager/commit/7726b0f2bf3bebd616042cbd557a32da1f88a135)) +- improve defaultFor with rest return value ([304530d](https://github.com/allohamora/config-manager/commit/304530d2106ac0575fe06cc8a532be48e441d924)) +- improve EnvPicker types ([4893b92](https://github.com/allohamora/config-manager/commit/4893b925313bbe50a6b4d7bdc4c616fbe5dd4e16)) +- improve orThrow methods typing ([d7f7590](https://github.com/allohamora/config-manager/commit/d7f7590e3ee04eef31edeb028752105056982346)) +- remove env.manager cache ([def7154](https://github.com/allohamora/config-manager/commit/def7154dad588b12c7c44f35e2c823e87ed36211)) + ## [0.7.0](https://github.com/allohamora/config-manager/compare/0.6.0...0.7.0) (2023-07-27) ### Features diff --git a/package-lock.json b/package-lock.json index 1219211..2432a13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@allohamora/config-manager", - "version": "0.7.0", + "version": "0.8.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@allohamora/config-manager", - "version": "0.7.0", + "version": "0.8.0", "license": "MIT", "devDependencies": { "@commitlint/cli": "^17.5.1", diff --git a/package.json b/package.json index a34e0d2..7a37b34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@allohamora/config-manager", - "version": "0.7.0", + "version": "0.8.0", "description": "config manager", "main": "./dist/index.cjs", "exports": {