From fe913805bf4278566061f9594378543b6c2aedea Mon Sep 17 00:00:00 2001 From: frank Date: Mon, 27 May 2024 17:39:54 -0700 Subject: [PATCH] Expose constants. Fix doc typos. --- Changelog.md | 4 ++++ README.md | 4 ++-- package-lock.json | 2 +- package.json | 2 +- src/eval-config.ts | 7 ++++--- src/index.ts | 4 ++++ src/process-initializers.ts | 7 ++++--- src/setup.ts | 6 +++--- 8 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Changelog.md b/Changelog.md index 12feebb..2d52167 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/README.md b/README.md index 9a51e2d..8aacd8d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/package-lock.json b/package-lock.json index 51c5115..a0b5e0e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dyflex-config", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 0e0a6de..47bc907 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/eval-config.ts b/src/eval-config.ts index d9c1e3a..54c02d5 100644 --- a/src/eval-config.ts +++ b/src/eval-config.ts @@ -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 @@ -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. @@ -54,9 +55,9 @@ export function evalConfig( 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]); diff --git a/src/index.ts b/src/index.ts index 53e7515..98c56d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}; diff --git a/src/process-initializers.ts b/src/process-initializers.ts index 97076e3..9b1586d 100644 --- a/src/process-initializers.ts +++ b/src/process-initializers.ts @@ -1,4 +1,5 @@ import {InitializerDesc, InitializerFn} from './initializers'; +import {ConfigMarkerPrefix, InitializeMarker} from './index'; /** * Contextual information describing an initializer @@ -17,7 +18,7 @@ export interface InitializerMeta { export type Initializers = Map[]>; /** - * 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. */ @@ -30,9 +31,9 @@ export function discoverInitializers( 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; if (typeof ro.fn === 'function' && (typeof ro.priority === 'number' || typeof ro.priority === 'undefined')) { diff --git a/src/setup.ts b/src/setup.ts index 727f116..cd816e0 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -12,10 +12,10 @@ export {keyValueToConfig, loadConfigFile, pkgToConfig, mergeConfig, evalConfig, */ export interface ConfigOpts { /** - * 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;