@travetto/config 6.0.0-rc.1 → 6.0.0

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,18 +13,20 @@ 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/eemeli/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#L25) 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/eemeli/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#L13)-based classes, all the standard [@Schema](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/schema.ts#L13) and [@Field](https://github.com/travetto/travetto/tree/main/module/schema/src/decorator/field.ts#L25) functionality applies.
17
17
 
18
18
  ## Resolution
19
19
  The configuration information is comprised of:
20
20
  * configuration files - [YAML](https://en.wikipedia.org/wiki/YAML), [JSON](https://www.json.org), and basic properties file
21
21
  * configuration classes
22
+
22
23
  Config loading follows a defined resolution path, below is the order in increasing specificity (`ext` can be `yaml`, `yml`, `json`, `properties`):
23
24
  1. `resources/application.<ext>` - Priority `100` - Load the default `application.<ext>` if available.
24
25
  1. `resources/{env}.<ext>` - Priority `200` - Load environment specific profile configurations as defined by the values of `process.env.TRV_ENV`.
25
26
  1. `resources/*.<ext>` - Priority `300` - Load profile specific configurations as defined by the values in `process.env.TRV_PROFILES`
26
27
  1. [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) [ConfigSource](https://github.com/travetto/travetto/tree/main/module/config/src/source/types.ts#L11) - Priority `???` - These are custom config sources provided by the module, and are able to define their own priorities
27
- 1. [OverrideConfigSource](https://github.com/travetto/travetto/tree/main/module/config/src/source/override.ts#L11) - Priority `999` - This is for [EnvVar](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L34) overrides, and is at the top priority for all built-in config sources.
28
+ 1. [OverrideConfigSource](https://github.com/travetto/travetto/tree/main/module/config/src/source/override.ts#L20) - Priority `999` - This is for [EnvVar](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L34) overrides, and is at the top priority for all built-in config sources.
29
+
28
30
  By default all configuration data is inert, and will only be applied when constructing an instance of a configuration class.
29
31
 
30
32
  ### Mono Repo Support
@@ -101,8 +103,8 @@ The framework provides two simple base classes that assist with existing pattern
101
103
 
102
104
  **Code: Memory Provider**
103
105
  ```typescript
104
- import { ConfigData } from '../parser/types';
105
- import { ConfigSource, ConfigSpec } from './types';
106
+ import { ConfigData } from '../parser/types.ts';
107
+ import { ConfigSource, ConfigSpec } from './types.ts';
106
108
 
107
109
  /**
108
110
  * Meant to be instantiated and provided as a unique config source
@@ -122,7 +124,7 @@ export class MemoryConfigSource implements ConfigSource {
122
124
 
123
125
  **Code: Environment JSON Provider**
124
126
  ```typescript
125
- import { ConfigSource, ConfigSpec } from './types';
127
+ import { ConfigSource, ConfigSpec } from './types.ts';
126
128
 
127
129
  /**
128
130
  * Represents the environment mapped data as a JSON blob
@@ -148,29 +150,30 @@ export class EnvConfigSource implements ConfigSource {
148
150
  ```
149
151
 
150
152
  ### Custom Configuration Provider
151
- 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 [ConfigurationService](https://github.com/travetto/travetto/tree/main/module/config/src/service.ts#L20) service for input. This means any needed configuration will need to be accessed via specific patterns.
153
+ 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 [ConfigurationService](https://github.com/travetto/travetto/tree/main/module/config/src/service.ts#L24) service for input. This means any needed configuration will need to be accessed via specific patterns.
152
154
 
153
155
  **Code: Custom Configuration Source**
154
156
  ```typescript
155
- import { ConfigData, ConfigSource } from '@travetto/config';
157
+ import { ConfigSource, ConfigSpec } from '@travetto/config';
156
158
  import { Injectable } from '@travetto/di';
157
159
 
158
160
  @Injectable()
159
161
  export class CustomConfigSource implements ConfigSource {
160
- priority = 2000;
161
- source = 'custom://override';
162
-
163
- async getData(): Promise<ConfigData> {
164
- return { user: { name: 'bob' } };
162
+ async get(): Promise<ConfigSpec> {
163
+ return {
164
+ data: { user: { name: 'bob' } },
165
+ source: 'custom://override',
166
+ priority: 2000
167
+ };
165
168
  }
166
169
  }
167
170
  ```
168
171
 
169
172
  ## Startup
170
- At startup, the [ConfigurationService](https://github.com/travetto/travetto/tree/main/module/config/src/service.ts#L20) 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.
173
+ At startup, the [ConfigurationService](https://github.com/travetto/travetto/tree/main/module/config/src/service.ts#L24) 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.
171
174
 
172
175
  ## Consuming
173
- The [ConfigurationService](https://github.com/travetto/travetto/tree/main/module/config/src/service.ts#L20) 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.
176
+ The [ConfigurationService](https://github.com/travetto/travetto/tree/main/module/config/src/service.ts#L24) 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.
174
177
 
175
178
  ### Environment Variables
176
179
  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/__index__.ts CHANGED
@@ -1,12 +1,12 @@
1
- import type { } from './src/trv';
2
- export * from './src/decorator';
3
- export * from './src/service';
4
- export * from './src/source/env';
5
- export * from './src/source/file';
6
- export * from './src/parser/json';
7
- export * from './src/parser/types';
8
- export * from './src/parser/properties';
9
- export * from './src/source/override';
10
- export * from './src/source/memory';
11
- export * from './src/source/types';
12
- export * from './src/parser/yaml';
1
+ import type { } from './src/trv.d.ts';
2
+ export * from './src/decorator.ts';
3
+ export * from './src/service.ts';
4
+ export * from './src/source/env.ts';
5
+ export * from './src/source/file.ts';
6
+ export * from './src/parser/json.ts';
7
+ export * from './src/parser/types.ts';
8
+ export * from './src/parser/properties.ts';
9
+ export * from './src/source/override.ts';
10
+ export * from './src/source/memory.ts';
11
+ export * from './src/source/types.ts';
12
+ export * from './src/parser/yaml.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/config",
3
- "version": "6.0.0-rc.1",
3
+ "version": "6.0.0",
4
4
  "description": "Configuration support",
5
5
  "keywords": [
6
6
  "yaml",
@@ -26,9 +26,9 @@
26
26
  "directory": "module/config"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/di": "^6.0.0-rc.1",
30
- "@travetto/schema": "^6.0.0-rc.1",
31
- "yaml": "^2.7.0"
29
+ "@travetto/di": "^6.0.0",
30
+ "@travetto/schema": "^6.0.0",
31
+ "yaml": "^2.7.1"
32
32
  },
33
33
  "travetto": {
34
34
  "displayName": "Configuration"
package/src/decorator.ts CHANGED
@@ -2,8 +2,8 @@ import { Class, ClassInstance } from '@travetto/runtime';
2
2
  import { DependencyRegistry } from '@travetto/di';
3
3
  import { SchemaRegistry } from '@travetto/schema';
4
4
 
5
- import { ConfigurationService } from './service';
6
- import { ConfigTarget, ConfigOverrides, CONFIG_OVERRIDES } from './internal/types';
5
+ import { OverrideConfig, OverrideConfigSymbol } from './source/override.ts';
6
+ import { ConfigurationService, ConfigBaseType } from './service.ts';
7
7
 
8
8
  /**
9
9
  * Indicates that the given class should be populated with the configured fields, on instantiation
@@ -14,8 +14,8 @@ export function Config(ns: string) {
14
14
  return <T extends Class>(target: T): T => {
15
15
  const og: Function = target.prototype.postConstruct;
16
16
  // Declare as part of global config
17
- (DependencyRegistry.getOrCreatePending(target).interfaces ??= []).push(ConfigTarget);
18
- const env = SchemaRegistry.getOrCreatePendingMetadata<ConfigOverrides>(target, CONFIG_OVERRIDES, { ns, fields: {} });
17
+ (DependencyRegistry.getOrCreatePending(target).interfaces ??= []).push(ConfigBaseType);
18
+ const env = SchemaRegistry.getOrCreatePendingMetadata<OverrideConfig>(target, OverrideConfigSymbol, { ns, fields: {} });
19
19
  env.ns = ns;
20
20
 
21
21
  target.prototype.postConstruct = async function (): Promise<void> {
@@ -31,9 +31,10 @@ export function Config(ns: string) {
31
31
  /**
32
32
  * Allows for binding specific fields to environment variables as a top-level override
33
33
  */
34
- export function EnvVar(name: string) {
34
+ export function EnvVar(name: string, ...others: string[]) {
35
35
  return (inst: ClassInstance, prop: string): void => {
36
- const env = SchemaRegistry.getOrCreatePendingMetadata<ConfigOverrides>(inst.constructor, CONFIG_OVERRIDES, { ns: '', fields: {} });
37
- env.fields[prop] = (): string | undefined => process.env[name];
36
+ const env = SchemaRegistry.getOrCreatePendingMetadata<OverrideConfig>(inst.constructor, OverrideConfigSymbol, { ns: '', fields: {} });
37
+ env.fields[prop] = (): string | undefined =>
38
+ process.env[[name, ...others].find(x => !!process.env[x])!];
38
39
  };
39
40
  }
@@ -1,6 +1,6 @@
1
1
  import { Injectable } from '@travetto/di';
2
2
 
3
- import { ConfigParser } from './types';
3
+ import { ConfigParser } from './types.ts';
4
4
 
5
5
  @Injectable()
6
6
  export class JSONConfigParser implements ConfigParser {
@@ -2,10 +2,9 @@ import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
 
4
4
  import { DependencyRegistry, Injectable } from '@travetto/di';
5
- import { AppError } from '@travetto/runtime';
5
+ import { AppError, toConcrete } from '@travetto/runtime';
6
6
 
7
- import { ConfigParserTarget } from '../internal/types';
8
- import { ConfigData, ConfigParser } from './types';
7
+ import { ConfigData, ConfigParser } from './types.ts';
9
8
 
10
9
  @Injectable()
11
10
  export class ParserManager {
@@ -14,8 +13,7 @@ export class ParserManager {
14
13
  #parsers: Record<string, ConfigParser>;
15
14
 
16
15
  async postConstruct(): Promise<void> {
17
- const parserClasses = await DependencyRegistry.getCandidateTypes(ConfigParserTarget);
18
- const parsers = await Promise.all(parserClasses.map(x => DependencyRegistry.getInstance<ConfigParser>(x.class, x.qualifier)));
16
+ const parsers = await DependencyRegistry.getCandidateInstances(toConcrete<ConfigParser>());
19
17
 
20
18
  // Register parsers
21
19
  this.#parsers = Object.fromEntries(parsers.flatMap(p => p.ext.map(e => [e, p])));
@@ -1,6 +1,6 @@
1
1
  import { Injectable } from '@travetto/di';
2
2
 
3
- import { ConfigData, ConfigParser } from './types';
3
+ import { ConfigData, ConfigParser } from './types.ts';
4
4
 
5
5
  const BACKSLASH = '\\'.charCodeAt(0);
6
6
  const EQUALS = '='.charCodeAt(0);
@@ -1,7 +1,7 @@
1
1
  export type ConfigData = Record<string, unknown>;
2
2
 
3
3
  /**
4
- * @concrete ../internal/types#ConfigParserTarget
4
+ * @concrete
5
5
  */
6
6
  export interface ConfigParser {
7
7
  ext: string[];
@@ -1,6 +1,6 @@
1
1
  import { parse as parseYaml } from 'yaml';
2
2
  import { Injectable } from '@travetto/di';
3
- import { ConfigData, ConfigParser } from './types';
3
+ import { ConfigData, ConfigParser } from './types.ts';
4
4
 
5
5
  @Injectable()
6
6
  export class YAMLConfigParser implements ConfigParser {
package/src/service.ts CHANGED
@@ -1,18 +1,22 @@
1
1
  import util from 'node:util';
2
2
 
3
- import { AppError, castTo, Class, ClassInstance, Env, Runtime, RuntimeResources } from '@travetto/runtime';
3
+ import { AppError, toConcrete, castTo, Class, ClassInstance, Env, Runtime, RuntimeResources } from '@travetto/runtime';
4
4
  import { DependencyRegistry, Injectable } from '@travetto/di';
5
5
  import { BindUtil, DataUtil, SchemaRegistry, SchemaValidator, ValidationResultError } from '@travetto/schema';
6
6
 
7
- import { ConfigSourceTarget, ConfigTarget } from './internal/types';
8
- import { ParserManager } from './parser/parser';
9
- import { ConfigData } from './parser/types';
10
- import { ConfigSource, ConfigSpec } from './source/types';
11
- import { FileConfigSource } from './source/file';
12
- import { OverrideConfigSource } from './source/override';
7
+ import { ParserManager } from './parser/parser.ts';
8
+ import { ConfigData } from './parser/types.ts';
9
+ import { ConfigSource, ConfigSpec } from './source/types.ts';
10
+ import { FileConfigSource } from './source/file.ts';
11
+ import { OverrideConfigSource } from './source/override.ts';
13
12
 
14
13
  type ConfigSpecSimple = Omit<ConfigSpec, 'data'>;
15
14
 
15
+ /**
16
+ * Common Type for all configuration classes
17
+ */
18
+ export class ConfigBaseType { }
19
+
16
20
  /**
17
21
  * Manager for application configuration
18
22
  */
@@ -46,10 +50,10 @@ export class ConfigurationService {
46
50
  * - If of the same priority, then alpha sort on the source
47
51
  */
48
52
  async postConstruct(): Promise<void> {
49
- const providers = await DependencyRegistry.getCandidateTypes(ConfigSourceTarget);
53
+ const providers = await DependencyRegistry.getCandidateTypes(toConcrete<ConfigSource>());
50
54
 
51
55
  const configs = await Promise.all(
52
- providers.map(async (el) => await DependencyRegistry.getInstance<ConfigSource>(el.class, el.qualifier))
56
+ providers.map(async (el) => await DependencyRegistry.getInstance(el.class, el.qualifier))
53
57
  );
54
58
 
55
59
  const parser = await DependencyRegistry.getInstance(ParserManager);
@@ -63,13 +67,13 @@ export class ConfigurationService {
63
67
  const specs = possible
64
68
  .flat()
65
69
  .filter(x => !!x)
66
- .sort((a, b) => a.priority - b.priority);
70
+ .toSorted((a, b) => a.priority - b.priority);
67
71
 
68
72
  for (const spec of specs) {
69
73
  DataUtil.deepAssign(this.#storage, BindUtil.expandPaths(spec.data), 'coerce');
70
74
  }
71
75
 
72
- this.#specs = specs.map(({ data, ...v }) => v);
76
+ this.#specs = specs.map(({ data: _, ...v }) => v);
73
77
 
74
78
  // Initialize Secrets
75
79
  const userSpecified = (this.#get('config')?.secrets ?? []);
@@ -89,11 +93,11 @@ export class ConfigurationService {
89
93
  * - Will not show fields marked as secret
90
94
  */
91
95
  async exportActive(): Promise<{ sources: ConfigSpecSimple[], active: ConfigData }> {
92
- const configTargets = await DependencyRegistry.getCandidateTypes(ConfigTarget);
96
+ const configTargets = await DependencyRegistry.getCandidateTypes(ConfigBaseType);
93
97
  const configs = await Promise.all(
94
98
  configTargets
95
99
  .filter(el => el.qualifier === DependencyRegistry.get(el.class).qualifier) // Is primary?
96
- .sort((a, b) => a.class.name.localeCompare(b.class.name))
100
+ .toSorted((a, b) => a.class.name.localeCompare(b.class.name))
97
101
  .map(async el => {
98
102
  const inst = await DependencyRegistry.getInstance<ClassInstance>(el.class, el.qualifier);
99
103
  return [el, inst] as const;
package/src/source/env.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ConfigSource, ConfigSpec } from './types';
1
+ import { ConfigSource, ConfigSpec } from './types.ts';
2
2
 
3
3
  /**
4
4
  * Represents the environment mapped data as a JSON blob
@@ -3,8 +3,8 @@ import path from 'node:path';
3
3
 
4
4
  import { Env, Runtime, RuntimeResources } from '@travetto/runtime';
5
5
 
6
- import { ConfigSource, ConfigSpec } from './types';
7
- import { ParserManager } from '../parser/parser';
6
+ import { ConfigSource, ConfigSpec } from './types.ts';
7
+ import { ParserManager } from '../parser/parser.ts';
8
8
 
9
9
  type Profile = [string, number] | readonly [string, number];
10
10
 
@@ -19,7 +19,7 @@ export class FileConfigSource implements ConfigSource {
19
19
 
20
20
  constructor(parser: ParserManager) {
21
21
  this.#parser = parser;
22
- this.#searchPaths = RuntimeResources.searchPaths.slice().reverse();
22
+ this.#searchPaths = RuntimeResources.searchPaths.toReversed();
23
23
  this.#profiles = ([
24
24
  ['application', 100],
25
25
  [Runtime.env!, 200],
@@ -1,5 +1,5 @@
1
- import { ConfigData } from '../parser/types';
2
- import { ConfigSource, ConfigSpec } from './types';
1
+ import { ConfigData } from '../parser/types.ts';
2
+ import { ConfigSource, ConfigSpec } from './types.ts';
3
3
 
4
4
  /**
5
5
  * Meant to be instantiated and provided as a unique config source
@@ -1,8 +1,17 @@
1
1
  import { SchemaRegistry } from '@travetto/schema';
2
2
 
3
- import { ConfigOverrides, CONFIG_OVERRIDES } from '../internal/types';
4
- import { ConfigData } from '../parser/types';
5
- import { ConfigSource, ConfigSpec } from './types';
3
+ import { ConfigData } from '../parser/types.ts';
4
+ import { ConfigSource, ConfigSpec } from './types.ts';
5
+
6
+ export const OverrideConfigSymbol = Symbol.for('@travetto/config:overrides');
7
+
8
+ /**
9
+ * Configuration Override
10
+ */
11
+ export type OverrideConfig = {
12
+ ns: string;
13
+ fields: Record<string, () => (unknown | undefined)>;
14
+ };
6
15
 
7
16
  /**
8
17
  * Overridable config source, provides ability to override field level values, currently used by
@@ -12,7 +21,7 @@ export class OverrideConfigSource implements ConfigSource {
12
21
  #build(): ConfigData {
13
22
  const out: ConfigData = {};
14
23
  for (const cls of SchemaRegistry.getClasses()) {
15
- const { ns, fields = {} } = SchemaRegistry.getMetadata<ConfigOverrides>(cls, CONFIG_OVERRIDES) ?? {};
24
+ const { ns, fields = {} } = SchemaRegistry.getMetadata<OverrideConfig>(cls, OverrideConfigSymbol) ?? {};
16
25
  for (const [key, value] of Object.entries(fields)) {
17
26
  const val = value();
18
27
  if (val !== undefined && val !== '') {
@@ -1,4 +1,4 @@
1
- import { ConfigData } from '../parser/types';
1
+ import { ConfigData } from '../parser/types.ts';
2
2
 
3
3
  type OrProm<T> = T | Promise<T>;
4
4
  type OneOf<T> = T[] | T | undefined;
@@ -6,7 +6,7 @@ type OneOf<T> = T[] | T | undefined;
6
6
  export type ConfigSpec = { data: ConfigData, priority: number, source: string, detail?: string };
7
7
 
8
8
  /**
9
- * @concrete ../internal/types#ConfigSourceTarget
9
+ * @concrete
10
10
  */
11
11
  export interface ConfigSource {
12
12
  get(): OrProm<OneOf<ConfigSpec>>;
@@ -1,10 +0,0 @@
1
- export abstract class ConfigSourceTarget { }
2
- export abstract class ConfigTarget { }
3
- export abstract class ConfigParserTarget { }
4
-
5
- export const CONFIG_OVERRIDES = Symbol.for('@travetto/config:field-override');
6
-
7
- export type ConfigOverrides = {
8
- ns: string;
9
- fields: Record<string, () => (unknown | undefined)>;
10
- };