@morojs/moro 1.0.2 → 1.1.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.
@@ -26,6 +26,42 @@ export function getConfig(): AppConfig {
26
26
  return appConfig;
27
27
  }
28
28
 
29
+ /**
30
+ * Coerce environment variable string values to appropriate types
31
+ */
32
+ function coerceEnvironmentValue(value: string): any {
33
+ // Handle boolean values
34
+ if (value.toLowerCase() === 'true') return true;
35
+ if (value.toLowerCase() === 'false') return false;
36
+
37
+ // Handle numeric values
38
+ if (/^\d+$/.test(value)) {
39
+ const num = parseInt(value, 10);
40
+ return num;
41
+ }
42
+
43
+ if (/^\d+\.\d+$/.test(value)) {
44
+ const num = parseFloat(value);
45
+ return num;
46
+ }
47
+
48
+ // Handle JSON objects/arrays
49
+ if (
50
+ (value.startsWith('{') && value.endsWith('}')) ||
51
+ (value.startsWith('[') && value.endsWith(']'))
52
+ ) {
53
+ try {
54
+ return JSON.parse(value);
55
+ } catch {
56
+ // If JSON parsing fails, return as string
57
+ return value;
58
+ }
59
+ }
60
+
61
+ // Return as string for all other cases
62
+ return value;
63
+ }
64
+
29
65
  /**
30
66
  * Create module-specific configuration with environment override support
31
67
  */
@@ -34,9 +70,17 @@ export function createModuleConfig<T>(
34
70
  defaultConfig: Partial<T>,
35
71
  envPrefix?: string
36
72
  ): T {
37
- const globalConfig = getConfig();
73
+ // Try to get global config, but don't fail if not initialized
74
+ let globalConfig = {};
75
+ try {
76
+ const { getGlobalConfig } = require('./index');
77
+ globalConfig = getGlobalConfig();
78
+ } catch {
79
+ // Global config not initialized - use empty object (module config can still work independently)
80
+ globalConfig = {};
81
+ }
38
82
 
39
- // Build environment configuration object
83
+ // Build environment configuration object with type coercion
40
84
  const envConfig: Record<string, any> = {};
41
85
 
42
86
  if (envPrefix) {
@@ -48,14 +92,20 @@ export function createModuleConfig<T>(
48
92
  .toLowerCase()
49
93
  .replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
50
94
 
51
- envConfig[configKey] = process.env[key];
95
+ const envValue = process.env[key];
96
+ if (envValue !== undefined) {
97
+ // Attempt basic type coercion for common types
98
+ envConfig[configKey] = coerceEnvironmentValue(envValue);
99
+ }
52
100
  }
53
101
  });
54
102
  }
55
103
 
56
104
  // Merge default config, global defaults, and environment overrides
105
+ // Priority: environment variables > global config > default config
57
106
  const mergedConfig = {
58
107
  ...defaultConfig,
108
+ ...globalConfig, // Now actually using global config!
59
109
  ...envConfig,
60
110
  };
61
111
 
package/src/index.ts CHANGED
@@ -136,7 +136,10 @@ export {
136
136
  getConfigValue,
137
137
  } from './core/config/utils';
138
138
 
139
- export { initializeConfig, getGlobalConfig, isConfigInitialized } from './core/config';
139
+ export { initializeConfig, getGlobalConfig, isConfigInitialized, resetConfig } from './core/config';
140
+
141
+ // Export configuration types for TypeScript users
142
+ export type { AppConfig } from './core/config';
140
143
 
141
144
  // Middleware System
142
145
  export { MiddlewareManager } from './core/middleware';
package/tsconfig.json CHANGED
@@ -18,4 +18,4 @@
18
18
  },
19
19
  "include": ["src/**/*"],
20
20
  "exclude": ["node_modules", "dist"]
21
- }
21
+ }