Skip to content

Commit

Permalink
feat(app): add return types and remove unnecessary as casting
Browse files Browse the repository at this point in the history
  • Loading branch information
allohamora committed Sep 6, 2023
1 parent c72fb59 commit 69243e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
38 changes: 21 additions & 17 deletions src/env/env.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Options<NodeEnv, Env> {
}

export class EnvManager<NodeEnv extends string, Config extends BaseConfig = BaseConfig> extends Manager<Config> {
private nodeEnv: string;
private nodeEnv: NodeEnv;

constructor({
getEnv = () => process.env as Config,
Expand All @@ -18,7 +18,7 @@ export class EnvManager<NodeEnv extends string, Config extends BaseConfig = Base
this.nodeEnv = getNodeEnv();
}

private getEnvValue<Key extends keyof Config>(key: Key) {
private getEnvValue<Key extends keyof Config>(key: Key): Config[Key] | undefined {
const value = this.source[key];

if (value === null || value === undefined || value.trim() === '') {
Expand All @@ -28,8 +28,8 @@ export class EnvManager<NodeEnv extends string, Config extends BaseConfig = Base
return value;
}

private getKeyOrThrow<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>) {
const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest;
private getKeyOrThrow<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>): Key {
const key = envRecord[this.nodeEnv] ?? envRecord.rest;

if (!key) {
throw new Error(`key is not found empty in nodeEnv: ${this.nodeEnv}`);
Expand All @@ -38,38 +38,42 @@ export class EnvManager<NodeEnv extends string, Config extends BaseConfig = Base
return key;
}

public pick<Key extends keyof Config>(key: Key) {
return new EnvPicker(this.getEnvValue(key), this.nodeEnv) as EnvPicker<NodeEnv, Config[Key] | undefined>;
public pick<Key extends keyof Config>(key: Key): EnvPicker<NodeEnv, Config[Key] | undefined> {
return new EnvPicker(this.getEnvValue(key), this.nodeEnv);
}

public pickOrThrow<Key extends keyof Config>(key: Key) {
public pickOrThrow<Key extends keyof Config>(key: Key): EnvPicker<NodeEnv, NonNullable<Config[Key]>> {
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<NodeEnv, NonNullable<Config[Key]>>;
return new EnvPicker(value, this.nodeEnv);
}

public pickFor<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>) {
const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest;
public pickFor<Key extends keyof Config>(
envRecord: EnvRecord<NodeEnv, Key>,
): EnvPicker<NodeEnv, Config[Key] | undefined> {
const key = envRecord[this.nodeEnv] ?? envRecord.rest;

if (!key) {
return new EnvPicker(undefined, this.nodeEnv) as EnvPicker<NodeEnv, undefined>;
return new EnvPicker(undefined, this.nodeEnv);
}

return this.pick(key) as EnvPicker<NodeEnv, Config[Key] | undefined>;
return this.pick(key);
}

public pickForOrThrow<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>) {
public pickForOrThrow<Key extends keyof Config>(
envRecord: EnvRecord<NodeEnv, Key>,
): EnvPicker<NodeEnv, NonNullable<Config[Key]>> {
const key = this.getKeyOrThrow(envRecord);

return this.pickOrThrow(key);
}

public getFor<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>) {
const key: Key | undefined = envRecord[this.nodeEnv as NodeEnv] ?? envRecord.rest;
public getFor<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>): Config[Key] | undefined {
const key = envRecord[this.nodeEnv] ?? envRecord.rest;

if (!key) {
return;
Expand All @@ -78,8 +82,8 @@ export class EnvManager<NodeEnv extends string, Config extends BaseConfig = Base
return this.get(key);
}

public getForOrThrow<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>) {
const key: Key | undefined = this.getKeyOrThrow(envRecord);
public getForOrThrow<Key extends keyof Config>(envRecord: EnvRecord<NodeEnv, Key>): Config[Key] {
const key = this.getKeyOrThrow(envRecord);

return this.getOrThrow(key);
}
Expand Down
16 changes: 10 additions & 6 deletions src/env/env.picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,39 @@ export const wrapInEnvPickers = <NodeEnv extends string, Config extends BaseConf
export class EnvPicker<NodeEnv extends string, State> {
constructor(private state: State, private nodeEnv: NodeEnv) {}

public default(newState: State) {
public default(newState: State): EnvPicker<NodeEnv, NonNullable<State>> {
this.state ??= newState;

return this as unknown as EnvPicker<NodeEnv, NonNullable<State>>;
return this as EnvPicker<NodeEnv, NonNullable<State>>;
}

public defaultFor<E extends EnvRecord<NodeEnv, State>>(envRecord: E) {
public defaultFor<E extends EnvRecord<NodeEnv, State>>(
envRecord: E,
): E extends { rest: State } ? EnvPicker<NodeEnv, NonNullable<State>> : EnvPicker<NodeEnv, State> {
this.state ??= (envRecord[this.nodeEnv] ?? envRecord.rest) as State;

return this as unknown as E extends { rest: State }
? EnvPicker<NodeEnv, NonNullable<State>>
: EnvPicker<NodeEnv, State>;
}

public map<Result>(mapper: (state: State) => Result) {
public map<Result>(mapper: (state: State) => Result): EnvPicker<NodeEnv, Result> {
this.state = mapper(this.state) as unknown as State;

return this as unknown as EnvPicker<NodeEnv, Result>;
}

public mapIfExists<Result>(mapper: (state: NonNullable<State>) => Result) {
public mapIfExists<Result>(
mapper: (state: NonNullable<State>) => Result,
): EnvPicker<NodeEnv, MoveOptional<State, Result>> {
if (this.state !== null && this.state !== undefined) {
this.state = mapper(this.state as NonNullable<State>) as unknown as State;
}

return this as unknown as EnvPicker<NodeEnv, MoveOptional<State, Result>>;
}

public value() {
public value(): State {
return this.state;
}
}
4 changes: 2 additions & 2 deletions src/utils/manager.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class Manager<Source extends Record<string, unknown>> {
this.source = getSource();
}

public get<Key extends keyof Source>(key: Key) {
public get<Key extends keyof Source>(key: Key): Source[Key] | undefined {
return this.source[key];
}

public getOrThrow<Key extends keyof Source>(key: Key) {
public getOrThrow<Key extends keyof Source>(key: Key): Source[Key] {
const value = this.source[key];

if (value === null || value === undefined) {
Expand Down

0 comments on commit 69243e6

Please sign in to comment.