@arc-js/config-manager 0.0.92 → 0.0.94
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/core/ConfigService.d.ts +1 -2
- package/core/ConfigService.js +40 -47
- package/core/ConfigService.min.js +1 -1
- package/hooks/useConfig.tsx +1 -0
- package/package.json +1 -1
- package/providers/ConfigProvider.jsx +17 -10
- package/providers/ConfigProvider.tsx +65 -58
- package/index.d.ts +0 -3
- package/index.js +0 -3
- package/index.min.js +0 -2
package/core/ConfigService.d.ts
CHANGED
|
@@ -7,8 +7,7 @@ interface ConfigOptions {
|
|
|
7
7
|
declare class ConfigService {
|
|
8
8
|
private resources;
|
|
9
9
|
private loadedModules;
|
|
10
|
-
|
|
11
|
-
constructor(configs?: any);
|
|
10
|
+
constructor();
|
|
12
11
|
initialize(): Promise<void>;
|
|
13
12
|
loadAllConfigs(): Promise<void>;
|
|
14
13
|
loadModule(moduleName: string): Promise<void>;
|
package/core/ConfigService.js
CHANGED
|
@@ -36,6 +36,37 @@ const mergeDeep = (target, source) => {
|
|
|
36
36
|
|
|
37
37
|
let configManagerConfig = null;
|
|
38
38
|
|
|
39
|
+
const loadBaseConfig = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
+
try {
|
|
41
|
+
if (!(configManagerConfig === null || configManagerConfig === void 0 ? void 0 : configManagerConfig.base)) {
|
|
42
|
+
console.warn('No base config found');
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
const baseConfigs = yield Promise.all(Object.entries(configManagerConfig.base).map((_a) => __awaiter(void 0, [_a], void 0, function* ([key, loader]) {
|
|
46
|
+
try {
|
|
47
|
+
const config = yield loader();
|
|
48
|
+
return { key, config };
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error(`Failed to load base config ${key}:`, error);
|
|
52
|
+
return { key, config: {} };
|
|
53
|
+
}
|
|
54
|
+
})));
|
|
55
|
+
return baseConfigs.reduce((acc, { key, config }) => {
|
|
56
|
+
return Object.assign(Object.assign({}, acc), config);
|
|
57
|
+
}, {});
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
console.error('Failed to load base config:', error);
|
|
61
|
+
return {};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const loadModulesConfigs = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
65
|
+
const results = [];
|
|
66
|
+
{
|
|
67
|
+
return results;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
39
70
|
const loadModuleConfig = (moduleName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
71
|
var _a;
|
|
41
72
|
if (!((_a = void 0 ) === null || _a === void 0 ? void 0 : _a[moduleName])) {
|
|
@@ -56,7 +87,7 @@ const loadModuleConfig = (moduleName) => __awaiter(void 0, void 0, void 0, funct
|
|
|
56
87
|
});
|
|
57
88
|
|
|
58
89
|
class ConfigService {
|
|
59
|
-
constructor(
|
|
90
|
+
constructor() {
|
|
60
91
|
Object.defineProperty(this, "resources", {
|
|
61
92
|
enumerable: true,
|
|
62
93
|
configurable: true,
|
|
@@ -69,13 +100,6 @@ class ConfigService {
|
|
|
69
100
|
writable: true,
|
|
70
101
|
value: new Set()
|
|
71
102
|
});
|
|
72
|
-
Object.defineProperty(this, "configs", {
|
|
73
|
-
enumerable: true,
|
|
74
|
-
configurable: true,
|
|
75
|
-
writable: true,
|
|
76
|
-
value: null
|
|
77
|
-
});
|
|
78
|
-
this.configs = configs;
|
|
79
103
|
}
|
|
80
104
|
initialize() {
|
|
81
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -84,45 +108,14 @@ class ConfigService {
|
|
|
84
108
|
}
|
|
85
109
|
loadAllConfigs() {
|
|
86
110
|
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
catch (error) {
|
|
96
|
-
console.error(`Failed to load base config ${key}:`, error);
|
|
97
|
-
return { key, config: {} };
|
|
98
|
-
}
|
|
99
|
-
})));
|
|
100
|
-
const base = baseConfigs.reduce((acc, { key, config }) => {
|
|
101
|
-
return Object.assign(Object.assign({}, acc), config);
|
|
102
|
-
}, {});
|
|
103
|
-
console.log('Base config loaded:', base);
|
|
104
|
-
this.resources.base = base;
|
|
105
|
-
}
|
|
106
|
-
if ((_b = this.configs) === null || _b === void 0 ? void 0 : _b.modules) {
|
|
107
|
-
const modules = [];
|
|
108
|
-
for (const [moduleName, loader] of Object.entries(this.configs.modules)) {
|
|
109
|
-
try {
|
|
110
|
-
const config = yield loader();
|
|
111
|
-
modules.push({
|
|
112
|
-
moduleName,
|
|
113
|
-
config
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
console.error(`Failed to load config for module "${moduleName}"`, error);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
console.log('Modules configs loaded:', modules);
|
|
121
|
-
modules.forEach(({ moduleName, config }) => {
|
|
122
|
-
this.resources[moduleName] = mergeDeep(this.resources[moduleName] || {}, config);
|
|
123
|
-
this.loadedModules.add(moduleName);
|
|
124
|
-
});
|
|
125
|
-
}
|
|
111
|
+
const base = yield loadBaseConfig();
|
|
112
|
+
const modules = yield loadModulesConfigs();
|
|
113
|
+
this.resources = modules.reduce((acc, { moduleName, config }) => {
|
|
114
|
+
return mergeDeep(acc, { [moduleName]: config });
|
|
115
|
+
}, { base });
|
|
116
|
+
modules.forEach(({ moduleName }) => {
|
|
117
|
+
this.loadedModules.add(moduleName);
|
|
118
|
+
});
|
|
126
119
|
});
|
|
127
120
|
}
|
|
128
121
|
loadModule(moduleName) {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function __awaiter(e,
|
|
1
|
+
function __awaiter(e,a,d,l){return new(d=d||Promise)(function(i,o){function n(e){try{t(l.next(e))}catch(e){o(e)}}function r(e){try{t(l.throw(e))}catch(e){o(e)}}function t(e){var o;e.done?i(e.value):((o=e.value)instanceof d?o:new d(function(e){e(o)})).then(n,r)}t((l=l.apply(e,a||[])).next())})}let mergeDeep=(e,o)=>{if("object"!=typeof e||"object"!=typeof o)return o;var i,n=Object.assign({},e);for(i in o)o.hasOwnProperty(i)&&(e.hasOwnProperty(i)?n[i]=mergeDeep(e[i],o[i]):n[i]=o[i]);return n},configManagerConfig=null,loadBaseConfig=()=>__awaiter(void 0,void 0,void 0,function*(){try{return null!=configManagerConfig&&configManagerConfig.base?(yield Promise.all(Object.entries(configManagerConfig.base).map(e=>__awaiter(void 0,[e],void 0,function*([o,e]){try{return{key:o,config:yield e()}}catch(e){return{key:o,config:{}}}})))).reduce((e,{config:o})=>Object.assign(Object.assign({},e),o),{}):{}}catch(e){return{}}}),loadModulesConfigs=()=>__awaiter(void 0,void 0,void 0,function*(){return[]}),loadModuleConfig=e=>__awaiter(void 0,void 0,void 0,function*(){});class ConfigService{constructor(){Object.defineProperty(this,"resources",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"loadedModules",{enumerable:!0,configurable:!0,writable:!0,value:new Set})}initialize(){return __awaiter(this,void 0,void 0,function*(){yield this.loadAllConfigs()})}loadAllConfigs(){return __awaiter(this,void 0,void 0,function*(){var e=yield loadBaseConfig(),o=yield loadModulesConfigs();this.resources=o.reduce((e,{moduleName:o,config:i})=>mergeDeep(e,{[o]:i}),{base:e}),o.forEach(({moduleName:e})=>{this.loadedModules.add(e)})})}loadModule(o){return __awaiter(this,void 0,void 0,function*(){var e;this.loadedModules.has(o)||null!=(e=yield loadModuleConfig(o))&&e.config&&(this.resources[o]=mergeDeep(this.resources[o]||{},e.config),this.loadedModules.add(o))})}get(e,o={}){var{moduleName:o="base",defaultValue:i=void 0,pathSeparator:n="."}=o,o=this.resources[o];return o&&void 0!==(e=e.split(n).reduce((e,o)=>e&&void 0!==e[o]?e[o]:void 0,o))?e:i}getConfig(e="base"){return this.resources[e]||{}}reload(){return this.loadAllConfigs()}}export{ConfigService};
|
|
2
2
|
//# sourceMappingURL=ConfigService.min.js.map
|
package/hooks/useConfig.tsx
CHANGED
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.94",
|
|
7
7
|
"description": "CONFIG-MANAGER est un système de gestion de configuration modulaire pour les applications React avec TypeScript/JavaScript. Il fournit une gestion centralisée des configurations, un chargement dynamique des modules, et une intégration transparente avec les variables d'environnement.",
|
|
8
8
|
"main": "index.js",
|
|
9
9
|
"keywords": [],
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
|
3
3
|
import { ConfigService } from '../core/ConfigService';
|
|
4
|
+
import { setConfigManagerConfig } from '../config';
|
|
4
5
|
const ConfigManagerContext = createContext(null);
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
const useConfigManagerValue = configManagerConfig => {
|
|
7
|
+
const [service] = useState(() => new ConfigService());
|
|
8
|
+
const [isLoading, setIsLoading] = useState(true)
|
|
9
|
+
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
setConfigManagerConfig(configManagerConfig);
|
|
12
|
+
}, [configManagerConfig])
|
|
13
|
+
|
|
11
14
|
useEffect(() => {
|
|
12
15
|
const initialize = async () => {
|
|
13
16
|
setIsLoading(true);
|
|
@@ -47,21 +50,25 @@ export const ConfigManagerProvider = ({
|
|
|
47
50
|
const getConfig = useCallback(moduleName => {
|
|
48
51
|
return service.getConfig(moduleName);
|
|
49
52
|
}, [service]);
|
|
50
|
-
|
|
53
|
+
return {
|
|
51
54
|
get,
|
|
52
55
|
getConfig,
|
|
53
56
|
reloadConfig,
|
|
54
57
|
isLoading,
|
|
55
58
|
loadModuleConfig
|
|
56
59
|
};
|
|
60
|
+
};
|
|
61
|
+
export const ConfigManagerProvider = ({
|
|
62
|
+
configs,
|
|
63
|
+
children
|
|
64
|
+
}) => {
|
|
65
|
+
const value = useConfigManagerValue(configs);
|
|
57
66
|
return React.createElement(ConfigManagerContext.Provider, {
|
|
58
67
|
value: value
|
|
59
68
|
}, children);
|
|
60
69
|
};
|
|
61
70
|
export const useConfigManager = () => {
|
|
62
71
|
const context = useContext(ConfigManagerContext);
|
|
63
|
-
if (!context)
|
|
64
|
-
throw new Error('useConfigManager must be used within a ConfigManagerProvider');
|
|
65
|
-
}
|
|
72
|
+
if (!context) throw new Error('useConfigManager must be used within a ConfigManagerProvider');
|
|
66
73
|
return context;
|
|
67
74
|
};
|
|
@@ -1,83 +1,90 @@
|
|
|
1
|
+
// @arc-js/config-manager/providers/ConfigProvider.tsx
|
|
1
2
|
import { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
|
2
3
|
import { ConfigService } from '../core/ConfigService';
|
|
3
4
|
import { setConfigManagerConfig } from '../config';
|
|
4
5
|
import type { ConfigManagerConfig } from '../config';
|
|
5
6
|
|
|
6
|
-
const ConfigManagerContext = createContext<
|
|
7
|
+
const ConfigManagerContext = createContext<ReturnType<typeof useConfigManagerValue> | null>(null);
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
children,
|
|
14
|
-
}) => {
|
|
15
|
-
const [service] = useState(() => new ConfigService(configs));
|
|
16
|
-
const [isLoading, setIsLoading] = useState(true);
|
|
9
|
+
const useConfigManagerValue = (
|
|
10
|
+
configManagerConfig: ConfigManagerConfig
|
|
11
|
+
) => {
|
|
12
|
+
const [service] = useState(() => new ConfigService());
|
|
13
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
await service.initialize();
|
|
23
|
-
} catch (error) {
|
|
24
|
-
console.error('Failed to initialize config manager:', error);
|
|
25
|
-
} finally {
|
|
26
|
-
setIsLoading(false);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
initialize();
|
|
30
|
-
}, [service]);
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
setConfigManagerConfig(configManagerConfig);
|
|
18
|
+
}, [configManagerConfig]);
|
|
31
19
|
|
|
32
|
-
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const initialize = async () => {
|
|
33
23
|
setIsLoading(true);
|
|
34
24
|
try {
|
|
35
|
-
await service.
|
|
25
|
+
await service.initialize();
|
|
36
26
|
} catch (error) {
|
|
37
|
-
console.error('Failed to
|
|
27
|
+
console.error('Failed to initialize config manager:', error);
|
|
38
28
|
} finally {
|
|
39
29
|
setIsLoading(false);
|
|
40
30
|
}
|
|
41
|
-
}
|
|
31
|
+
};
|
|
32
|
+
initialize();
|
|
33
|
+
}, [service]);
|
|
42
34
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
35
|
+
const reloadConfig = useCallback(async () => {
|
|
36
|
+
setIsLoading(true);
|
|
37
|
+
try {
|
|
38
|
+
await service.reload();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error('Failed to reload config:', error);
|
|
41
|
+
} finally {
|
|
42
|
+
setIsLoading(false);
|
|
43
|
+
}
|
|
44
|
+
}, [service]);
|
|
53
45
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
const loadModuleConfig = useCallback(async (moduleName: string) => {
|
|
47
|
+
setIsLoading(true);
|
|
48
|
+
try {
|
|
49
|
+
await service.loadModule(moduleName);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(`Failed to load module config ${moduleName}:`, error);
|
|
52
|
+
} finally {
|
|
53
|
+
setIsLoading(false);
|
|
54
|
+
}
|
|
55
|
+
}, [service]);
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
const get = useCallback((key: string, options?: any) => {
|
|
58
|
+
return service.get(key, options);
|
|
59
|
+
}, [service]);
|
|
61
60
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
reloadConfig,
|
|
66
|
-
isLoading,
|
|
67
|
-
loadModuleConfig,
|
|
68
|
-
};
|
|
61
|
+
const getConfig = useCallback((moduleName?: string) => {
|
|
62
|
+
return service.getConfig(moduleName);
|
|
63
|
+
}, [service]);
|
|
69
64
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
65
|
+
return {
|
|
66
|
+
get,
|
|
67
|
+
getConfig,
|
|
68
|
+
reloadConfig,
|
|
69
|
+
isLoading,
|
|
70
|
+
loadModuleConfig,
|
|
75
71
|
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const ConfigManagerProvider: React.FC<{
|
|
75
|
+
children: React.ReactNode
|
|
76
|
+
configs: ConfigManagerConfig
|
|
77
|
+
}> = ({
|
|
78
|
+
configs,
|
|
79
|
+
children,
|
|
80
|
+
}) => {
|
|
81
|
+
const value = useConfigManagerValue(configs);
|
|
82
|
+
|
|
83
|
+
return <ConfigManagerContext.Provider value={value}>{children}</ConfigManagerContext.Provider>;
|
|
84
|
+
};
|
|
76
85
|
|
|
77
86
|
export const useConfigManager = () => {
|
|
78
87
|
const context = useContext(ConfigManagerContext);
|
|
79
|
-
if (!context)
|
|
80
|
-
throw new Error('useConfigManager must be used within a ConfigManagerProvider');
|
|
81
|
-
}
|
|
88
|
+
if (!context) throw new Error('useConfigManager must be used within a ConfigManagerProvider');
|
|
82
89
|
return context;
|
|
83
90
|
};
|
package/index.d.ts
DELETED
package/index.js
DELETED
package/index.min.js
DELETED