Skip to content

Commit

Permalink
Expose constants.
Browse files Browse the repository at this point in the history
Fix doc typos.
  • Loading branch information
frank committed May 28, 2024
1 parent 1c3d5e2 commit fe91380
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
## 1.0.1 / 2024-05-27 (No functional changes)
* Expose some constants.
* Documentation typos.

## 1.0.0 / 2024-05-20
* Initial release
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ which describes the structure (e.g. `interface` type) of the configuration for a
This saves you from having to define a configuration `interface`, as it is derivable from the `const` declaration of the default base configuration
(e.g. `type AppConfigType = typeof DefaultAppConfig;`).

Your application can defines its own (global) default base configuration, by simply merging together
Your application can define its own (global) default base configuration, by simply merging together
default base configurations from various modules within your application.

### Loading
Expand Down Expand Up @@ -146,7 +146,7 @@ See `evalConfig` for more info.
The default base configuration (`const`) fragments, can contain **initializers** which are simply factory functions that
automatically process configuration data to construct (and typically register) a service with the Dependency Injection Container.
This means that adding services to your application is usually as simple as defining a default base configuration.
You will likely need to provide configuration override data, such as username and/or password (.env, json, etc), but that's it!
You will likely need to provide configuration override data, such as username and/or password (.env, json, etc.), but that's it!
See `discoverInitializers` and `invokeInitializers` for more info.

## Implementation Notes
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dyflex-config",
"version": "1.0.0",
"version": "1.0.1",
"description": "Simple, dynamic, flexible, configuration library.",
"author": "Frank Stock",
"license": "MIT",
Expand Down
7 changes: 4 additions & 3 deletions src/eval-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {template as lodashTemplate, TemplateExecutor, get as lodashGet, toPath as lodashToPath} from 'lodash';
import constants from 'node:constants';
import {ConfigMarkerPrefix, RegisterConfigMarker} from './index';

/**
* Helper to convert strings to false (pretty much any other string evaluates to truthy
Expand Down Expand Up @@ -33,7 +34,7 @@ export type RegistrarFn = (key: symbol, obj: object | undefined, path: string[]
* Walk the configuration looking for markers, as well as strings that should be interpolated.
*
* @param config The fully merged configuration.
* @param register User provided hook invoked for any object containing the '__reg_config' property.
* @param registrar User provided hook invoked for any object containing the @see RegisterConfigMarker property.
* Typically used to bind into a Dependency Injection Container.
* @see RegistrarFn
* @param evalExt Allows for caller defined interpolation functions.
Expand All @@ -54,9 +55,9 @@ export function evalConfig<T extends object>(
let propKey: PropertyKey;
for (propKey in obj) {
// Look for markers and process relevant ones.
if (typeof propKey === 'string' && propKey.startsWith('__conf_')) {
if (typeof propKey === 'string' && propKey.startsWith(ConfigMarkerPrefix)) {
switch (propKey) {
case '__conf_register':
case RegisterConfigMarker:
// This little snippet allows us to use our "default config as code" infrastructure to automatically bind sub-configurations.
if (typeof obj[propKey] === 'string')
obj[propKey] = Symbol.for(obj[propKey]);
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {cloneDeep, merge} from 'lodash';

export const ConfigMarkerPrefix = '__conf_';
export const RegisterConfigMarker = `${ConfigMarkerPrefix}register`;
export const InitializeMarker = `${ConfigMarkerPrefix}init`;

export * from './initializers';
export {merge as lodashMerge, cloneDeep as lodashCloneDeep};
7 changes: 4 additions & 3 deletions src/process-initializers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InitializerDesc, InitializerFn} from './initializers';
import {ConfigMarkerPrefix, InitializeMarker} from './index';

/**
* Contextual information describing an initializer
Expand All @@ -17,7 +18,7 @@ export interface InitializerMeta<T = any> {
export type Initializers<T = any> = Map<number, InitializerMeta<T>[]>;

/**
* Scan the configuration extracting the initialization markers (__conf_init).
* Scan the configuration extracting the initialization markers @see InitializeMarker.
* @param config A fully merged and evaluated configuration.
* @return A priority ordered Map of initializer functions discovered inside the configuration.
*/
Expand All @@ -30,9 +31,9 @@ export function discoverInitializers<T = any>(
let propKey: PropertyKey;
for (propKey in obj) {
// Scan for objects containing our magic properties
if (typeof propKey === 'string' && propKey.startsWith('__conf_')) {
if (typeof propKey === 'string' && propKey.startsWith(ConfigMarkerPrefix)) {
switch (propKey) {
case '__conf_init':
case InitializeMarker:
if (typeof obj[propKey] === 'object') {
const ro = obj[propKey] as InitializerDesc<T>;
if (typeof ro.fn === 'function' && (typeof ro.priority === 'number' || typeof ro.priority === 'undefined')) {
Expand Down
6 changes: 3 additions & 3 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export {keyValueToConfig, loadConfigFile, pkgToConfig, mergeConfig, evalConfig,
*/
export interface ConfigOpts<CTX = any> {
/**
* The callback will be invoked for each sub-object discovered in the configuration that has a '__conf_register' property.
* The callback will be invoked for each sub-object discovered in the configuration that has a @see RegisterConfigMarker property.
* This callback is typically used to bind the configuration into a dependency injection container.
* @param key The *value* of the __conf_register property (which must be a string) converted via Symbol.for().
* @param obj The object that contained the __conf_register property.
* @param key The *value* of the @see RegisterConfigMarker property (which must be a string) converted via Symbol.for().
* @param obj The object that contained the @see RegisterConfigMarker property.
* @param path The location of the 'obj' within the configuration.
*/
evalCb?: (key: symbol, obj: object, path: string[]) => void;
Expand Down

0 comments on commit fe91380

Please sign in to comment.