@travetto/config 3.1.0-rc.0 → 3.1.0-rc.2

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/README.md CHANGED
@@ -13,7 +13,7 @@ npm install @travetto/config
13
13
  yarn add @travetto/config
14
14
  ```
15
15
 
16
- The config module provides support for loading application config on startup. Configuration values support the common [YAML](https://en.wikipedia.org/wiki/YAML) constructs as defined in [YAML](https://github.com/travetto/travetto/tree/main/module/yaml#readme "Simple YAML support, provides only clean subset of yaml"). Additionally, the configuration is built upon the [Schema](https://github.com/travetto/travetto/tree/main/module/schema#readme "Data type registry for runtime validation, reflection and binding.") module, to enforce type correctness, and allow for validation of configuration as an entrypoint into the application. Given that all [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L13) classes are [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L14)-based classes, all the standard [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L14) and [@Field](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L38) functionality applies.
16
+ The config module provides support for loading application config on startup. Configuration values support the common [YAML](https://en.wikipedia.org/wiki/YAML) constructs as defined in [YAML](https://github.com/travetto/travetto/tree/main/module/yaml#readme "Simple YAML support, provides only clean subset of yaml"). Additionally, the configuration is built upon the [Schema](https://github.com/travetto/travetto/tree/main/module/schema#readme "Data type registry for runtime validation, reflection and binding.") module, to enforce type correctness, and allow for validation of configuration as an entrypoint into the application. Given that all [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L13) classes are [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L14)-based classes, all the standard [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L14) and [@Field](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L31) functionality applies.
17
17
 
18
18
  ## Resolution
19
19
  The configuration information is comprised of:
@@ -76,8 +76,72 @@ Config {
76
76
  }
77
77
  ```
78
78
 
79
+ ### Standard Configuration Extension
80
+ The framework provides two simple base classes that assist with existing patterns of usage to make adding in new configuration sources as easy as possible. The goal here is for the developer to either instantiate or extend these classes and produce a configuration source unique to their needs:
81
+
82
+ **Code: Memory Provider**
83
+ ```typescript
84
+ import { ConfigData } from '../parser/types';
85
+ import { ConfigSource, ConfigValue } from './types';
86
+
87
+ /**
88
+ * Meant to be instantiated and provided as a unique config source
89
+ */
90
+ export class MemoryConfigSource implements ConfigSource {
91
+ priority = 1;
92
+ data: Record<string, ConfigData>;
93
+ name = 'memory';
94
+
95
+ constructor(data: Record<string, ConfigData>, priority: number = 1) {
96
+ this.data = data;
97
+ this.priority = priority;
98
+ }
99
+
100
+ getValues(profiles: string[]): ConfigValue[] {
101
+ const out: ConfigValue[] = [];
102
+ for (const profile of profiles) {
103
+ if (this.data[profile]) {
104
+ out.push({ profile, config: this.data[profile], source: `${this.name}://${profile}`, priority: this.priority });
105
+ }
106
+ }
107
+ return out;
108
+ }
109
+ }
110
+ ```
111
+
112
+ **Code: Environment JSON Provider**
113
+ ```typescript
114
+ import { Env, GlobalEnv } from '@travetto/base';
115
+
116
+ import { ConfigSource, ConfigValue } from './types';
117
+
118
+ /**
119
+ * Represents the environment mapped data as a JSON blob
120
+ */
121
+ export class EnvConfigSource implements ConfigSource {
122
+ priority: number;
123
+ name = 'env';
124
+ #envKey: string;
125
+
126
+ constructor(key: string, priority: number) {
127
+ this.#envKey = key;
128
+ this.priority = priority;
129
+ }
130
+
131
+ getValues(profiles: string[]): ConfigValue[] {
132
+ try {
133
+ const data = JSON.parse(Env.get(this.#envKey, '{}'));
134
+ return [{ profile: GlobalEnv.envName, config: data, source: `${this.name}://${this.#envKey}`, priority: this.priority }];
135
+ } catch (e) {
136
+ console.error(`env.${this.#envKey} is an invalid format`, { text: Env.get(this.#envKey) });
137
+ return [];
138
+ }
139
+ }
140
+ }
141
+ ```
142
+
79
143
  ### Custom Configuration Provider
80
- In addition to files and environment variables, configuration sources can also be provided via the class itself. This is useful for reading remote configurations, or dealing with complex configuration normalization. The only caveat to this pattern, is that the these configuration sources cannot rely on the [Configuration](https://github.com/travetto/travetto/tree/main/module/config/src/configuration.ts#L14) service for input. This means any needed configuration will need to be accessed via specific patterns.
144
+ In addition to files and environment variables, configuration sources can also be provided via the class itself. This is useful for reading remote configurations, or dealing with complex configuration normalization. The only caveat to this pattern, is that the these configuration sources cannot rely on the [Configuration](https://github.com/travetto/travetto/tree/main/module/config/src/configuration.ts#L16) service for input. This means any needed configuration will need to be accessed via specific patterns.
81
145
 
82
146
  **Code: Custom Configuration Source**
83
147
  ```typescript
@@ -103,10 +167,10 @@ export class CustomConfigSource implements ConfigSource {
103
167
  ```
104
168
 
105
169
  ## Startup
106
- At startup, the [Configuration](https://github.com/travetto/travetto/tree/main/module/config/src/configuration.ts#L14) service will log out all the registered configuration objects. The configuration state output is useful to determine if everything is configured properly when diagnosing runtime errors. This service will find all configurations, and output a redacted version with all secrets removed. The default pattern for secrets is `/password|private|secret/i`. More values can be added in your configuration under the path `config.secrets`. These values can either be simple strings (for exact match), or `/pattern/` to create a regular expression.
170
+ At startup, the [Configuration](https://github.com/travetto/travetto/tree/main/module/config/src/configuration.ts#L16) service will log out all the registered configuration objects. The configuration state output is useful to determine if everything is configured properly when diagnosing runtime errors. This service will find all configurations, and output a redacted version with all secrets removed. The default pattern for secrets is `/password|private|secret/i`. More values can be added in your configuration under the path `config.secrets`. These values can either be simple strings (for exact match), or `/pattern/` to create a regular expression.
107
171
 
108
172
  ## Consuming
109
- The [Configuration](https://github.com/travetto/travetto/tree/main/module/config/src/configuration.ts#L14) service provides injectable access to all of the loaded configuration. For simplicity, a decorator, [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L13) allows for classes to automatically be bound with config information on post construction via the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") module. The decorator will install a `postConstruct` method if not already defined, that performs the binding of configuration. This is due to the fact that we cannot rewrite the constructor, and order of operation matters.
173
+ The [Configuration](https://github.com/travetto/travetto/tree/main/module/config/src/configuration.ts#L16) service provides injectable access to all of the loaded configuration. For simplicity, a decorator, [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L13) allows for classes to automatically be bound with config information on post construction via the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") module. The decorator will install a `postConstruct` method if not already defined, that performs the binding of configuration. This is due to the fact that we cannot rewrite the constructor, and order of operation matters.
110
174
 
111
175
  ### Environment Variables
112
176
  Additionally there are times in which you may want to also support configuration via environment variables. [EnvVar](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L34) supports override configuration values when environment variables are present.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/config",
3
- "version": "3.1.0-rc.0",
3
+ "version": "3.1.0-rc.2",
4
4
  "description": "Configuration support",
5
5
  "keywords": [
6
6
  "yaml",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@travetto/di": "^3.1.0-rc.0",
30
- "@travetto/schema": "^3.1.0-rc.0",
30
+ "@travetto/schema": "^3.1.0-rc.2",
31
31
  "@travetto/yaml": "^3.1.0-rc.0"
32
32
  },
33
33
  "travetto": {
@@ -1,3 +1,5 @@
1
+ import util from 'util';
2
+
1
3
  import { AppError, Class, ClassInstance, GlobalEnv, DataUtil } from '@travetto/base';
2
4
  import { DependencyRegistry, Injectable } from '@travetto/di';
3
5
  import { RootIndex } from '@travetto/manifest';
@@ -129,4 +131,18 @@ export class Configuration {
129
131
  }
130
132
  return out;
131
133
  }
134
+
135
+ /**
136
+ * Log current configuration state
137
+ */
138
+ async initBanner(): Promise<void> {
139
+ const og = util.inspect.defaultOptions.depth;
140
+ util.inspect.defaultOptions.depth = 100;
141
+ console.log('Initialized', {
142
+ manifest: RootIndex.mainDigest(),
143
+ env: GlobalEnv.toJSON(),
144
+ config: await this.exportActive()
145
+ });
146
+ util.inspect.defaultOptions.depth = og;
147
+ }
132
148
  }
@@ -0,0 +1,27 @@
1
+ import { Env, GlobalEnv } from '@travetto/base';
2
+
3
+ import { ConfigSource, ConfigValue } from './types';
4
+
5
+ /**
6
+ * Represents the environment mapped data as a JSON blob
7
+ */
8
+ export class EnvConfigSource implements ConfigSource {
9
+ priority: number;
10
+ name = 'env';
11
+ #envKey: string;
12
+
13
+ constructor(key: string, priority: number) {
14
+ this.#envKey = key;
15
+ this.priority = priority;
16
+ }
17
+
18
+ getValues(profiles: string[]): ConfigValue[] {
19
+ try {
20
+ const data = JSON.parse(Env.get(this.#envKey, '{}'));
21
+ return [{ profile: GlobalEnv.envName, config: data, source: `${this.name}://${this.#envKey}`, priority: this.priority }];
22
+ } catch (e) {
23
+ console.error(`env.${this.#envKey} is an invalid format`, { text: Env.get(this.#envKey) });
24
+ return [];
25
+ }
26
+ }
27
+ }