Skip to content

Commit

Permalink
feat: add atLeastOne to for methods
Browse files Browse the repository at this point in the history
  • Loading branch information
allohamora committed Apr 10, 2023
1 parent 51dbf8c commit 1b43a5f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/env/env.manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Manager } from 'src/utils/manager.utils.js';
import { BaseConfig, EnvPicker } from './env.picker.js';
import { BaseConfig, AtLeastOne, EnvPicker } from './env.picker.js';

interface Options<E, C> {
load?: () => C;
Expand Down Expand Up @@ -29,7 +29,7 @@ export class EnvManager<E extends string, C extends BaseConfig = BaseConfig> ext
return value;
}

private getKeyOrThrow<K extends keyof C>(envRecord: Partial<Record<E, K>>): K {
private getKeyOrThrow<K extends keyof C>(envRecord: AtLeastOne<Record<E, K>>): K {
const key = envRecord[this.nodeEnv as E] as string | undefined;

if (!key) {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class EnvManager<E extends string, C extends BaseConfig = BaseConfig> ext
return this.pick(key) as EnvPicker<E, C[K]>;
}

public pickFor<K extends keyof C>(envRecord: Partial<Record<E, K>>): undefined | EnvPicker<E, C[K] | undefined> {
public pickFor<K extends keyof C>(envRecord: AtLeastOne<Record<E, K>>): undefined | EnvPicker<E, C[K] | undefined> {
const key = envRecord[this.nodeEnv as E] as string | undefined;

if (!key) {
Expand All @@ -67,13 +67,13 @@ export class EnvManager<E extends string, C extends BaseConfig = BaseConfig> ext
return this.pick(key) as EnvPicker<E, C[K] | undefined>;
}

public pickForOrThrow<K extends keyof C>(envRecord: Partial<Record<E, K>>): EnvPicker<E, C[K]> {
public pickForOrThrow<K extends keyof C>(envRecord: AtLeastOne<Record<E, K>>): EnvPicker<E, C[K]> {
const key = this.getKeyOrThrow(envRecord);

return this.pickOrThrow(key as K);
}

public getFor<K extends keyof C>(envRecord: Partial<Record<E, K>>) {
public getFor<K extends keyof C>(envRecord: AtLeastOne<Record<E, K>>) {
const key = envRecord[this.nodeEnv as E] as string | undefined;

if (!key) {
Expand All @@ -83,7 +83,7 @@ export class EnvManager<E extends string, C extends BaseConfig = BaseConfig> ext
return this.get(key as K);
}

public getForOrThrow<K extends keyof C>(envRecord: Partial<Record<E, K>>) {
public getForOrThrow<K extends keyof C>(envRecord: AtLeastOne<Record<E, K>>) {
const key = this.getKeyOrThrow(envRecord);

return this.getOrThrow(key as K);
Expand Down
5 changes: 4 additions & 1 deletion src/env/env.picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type MoveOptional<T, R> = T extends null

export type BaseConfig = Record<string, string | undefined>;

// original https://stackoverflow.com/a/59987826/15681288
export type AtLeastOne<T> = { [K in keyof T]: Pick<T, K> }[keyof T];

export const wrapInEnvPickers = <E extends string, C extends BaseConfig>(config: C, nodeEnv: E) => {
return Object.keys(config).reduce((result, key) => {
return { ...result, [key]: new EnvPicker(config[key], nodeEnv) };
Expand All @@ -27,7 +30,7 @@ export class EnvPicker<E extends string, S> {
return this as unknown as EnvPicker<E, NS | NonNullable<S>>;
}

public defaultFor(envRecord: Partial<Record<E, S>>) {
public defaultFor(envRecord: AtLeastOne<Record<E, S>>) {
this.state ??= envRecord[this.nodeEnv] as S;

return this;
Expand Down

0 comments on commit 1b43a5f

Please sign in to comment.