@efebia/env-loader 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.d.ts CHANGED
@@ -1,14 +1,20 @@
1
1
  import { EnvSchema, EnvValues } from './utils';
2
2
  export type EnvironmentClientOptions = {
3
3
  storage: object;
4
- keyLoaders: (() => Record<string, unknown> | Promise<Record<string, unknown>>)[];
4
+ syncKeyLoaders?: (() => Record<string, unknown>)[];
5
+ asyncKeyLoaders?: (() => Promise<Record<string, unknown>>)[];
6
+ loadSyncKeysAtStartup?: boolean;
5
7
  };
6
8
  export default class EnvironmentClient {
7
9
  storage: EnvironmentClientOptions['storage'];
8
- keyLoaders: EnvironmentClientOptions['keyLoaders'];
10
+ syncKeyLoaders: EnvironmentClientOptions['syncKeyLoaders'];
11
+ asyncKeyLoaders: EnvironmentClientOptions['asyncKeyLoaders'];
9
12
  constructor(opts?: Partial<EnvironmentClientOptions>);
13
+ loadAsyncKeys(): Promise<void>;
14
+ loadSyncKeys(): void;
10
15
  loadKeys(): Promise<void>;
11
16
  setEnv(key: string, value: unknown): void;
12
17
  getEnv<Key extends keyof EnvSchema, Keys extends Key[] = Key[]>(...listKeys: Keys): EnvValues<Keys>;
18
+ getOptionalEnv<Key extends keyof EnvSchema, Keys extends Key[] = Key[]>(...listKeys: Keys): Partial<EnvValues<Keys>>;
13
19
  getKey<Key extends keyof EnvSchema>(key: Key): any;
14
20
  }
package/lib/client.js CHANGED
@@ -5,16 +5,35 @@ class EnvironmentClient {
5
5
  constructor(opts) {
6
6
  const options = Object.assign({
7
7
  storage: {},
8
- keyLoaders: []
8
+ syncKeyLoaders: [],
9
+ asyncKeyLoaders: [],
10
+ loadSyncKeysAtStartup: true
9
11
  }, opts);
10
12
  this.storage = options.storage;
11
- this.keyLoaders = options.keyLoaders;
13
+ this.syncKeyLoaders = options.syncKeyLoaders;
14
+ this.asyncKeyLoaders = options.asyncKeyLoaders;
15
+ if (options.loadSyncKeysAtStartup) {
16
+ this.loadSyncKeys();
17
+ }
12
18
  }
13
- async loadKeys() {
14
- const loadedKeys = await Promise.all(this.keyLoaders.map(keyLoader => keyLoader()));
19
+ async loadAsyncKeys() {
20
+ if (!this.asyncKeyLoaders)
21
+ return;
22
+ const loadedKeys = await Promise.all(this.asyncKeyLoaders.map(keyLoader => keyLoader()));
15
23
  const keysObject = loadedKeys.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {});
16
24
  Object.entries(keysObject).forEach(([key, value]) => this.setEnv(key, value));
17
25
  }
26
+ loadSyncKeys() {
27
+ if (!this.syncKeyLoaders)
28
+ return;
29
+ const loadedKeys = this.syncKeyLoaders.map(keyLoader => keyLoader());
30
+ const keysObject = loadedKeys.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {});
31
+ Object.entries(keysObject).forEach(([key, value]) => this.setEnv(key, value));
32
+ }
33
+ async loadKeys() {
34
+ await this.loadAsyncKeys();
35
+ this.loadSyncKeys();
36
+ }
18
37
  setEnv(key, value) {
19
38
  this.storage[key] = value;
20
39
  }
@@ -27,6 +46,18 @@ class EnvironmentClient {
27
46
  return values;
28
47
  }
29
48
  ;
49
+ getOptionalEnv(...listKeys) {
50
+ let values = {};
51
+ for (const key of listKeys) {
52
+ try {
53
+ const value = this.getKey(key);
54
+ values = Object.assign(Object.assign({}, values), { [key]: value, [(0, utils_1.convertToCamelCase)(key)]: value });
55
+ }
56
+ catch (_a) { }
57
+ }
58
+ return values;
59
+ }
60
+ ;
30
61
  getKey(key) {
31
62
  if (!Object.hasOwn(this.storage, key))
32
63
  throw Error(`@efebia/env-loader: Missing environment key ${key}`);
package/lib/utils.d.ts CHANGED
@@ -9,6 +9,8 @@ export type EnvValues<TKeys extends [...(keyof EnvSchema)[]]> = TKeys extends [
9
9
  [key in TRest[number] as key extends `${string}_${string}` ? ConvertToCamelCase<key> : never]: key extends keyof EnvSchema ? EnvSchema[key] : never;
10
10
  } & {
11
11
  [key in TFirst as key extends string ? ConvertToCamelCase<key> : never]: key extends keyof EnvSchema ? EnvSchema[key] : never;
12
+ } & {
13
+ [key in TFirst as key extends string ? key : never]: key extends keyof EnvSchema ? EnvSchema[key] : never;
12
14
  } : never;
13
15
  export interface EnvSchema {
14
16
  test_key_1: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@efebia/env-loader",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "license": "MIT",
5
5
  "devDependencies": {
6
6
  "@types/jest": "^29.2.5",