@arc-js/config-manager 0.0.6 → 0.0.7
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/package.json +1 -1
- package/providers/ConfigProvider.jsx +11 -14
- package/providers/ConfigProvider.tsx +62 -64
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.7",
|
|
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": [],
|
|
@@ -3,14 +3,15 @@ import { createContext, useContext, useState, useEffect, useCallback } from 'rea
|
|
|
3
3
|
import { ConfigService } from '../core/ConfigService';
|
|
4
4
|
import { setConfigManagerConfig } from '../config';
|
|
5
5
|
const ConfigManagerContext = createContext(null);
|
|
6
|
-
const
|
|
6
|
+
export const ConfigManagerProvider = ({
|
|
7
|
+
configs,
|
|
8
|
+
children
|
|
9
|
+
}) => {
|
|
7
10
|
const [service] = useState(() => new ConfigService());
|
|
8
|
-
const [isLoading, setIsLoading] = useState(true)
|
|
9
|
-
|
|
11
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
10
12
|
useEffect(() => {
|
|
11
|
-
setConfigManagerConfig(
|
|
12
|
-
}, [
|
|
13
|
-
|
|
13
|
+
setConfigManagerConfig(configs);
|
|
14
|
+
}, [configs]);
|
|
14
15
|
useEffect(() => {
|
|
15
16
|
const initialize = async () => {
|
|
16
17
|
setIsLoading(true);
|
|
@@ -50,25 +51,21 @@ const useConfigManagerValue = configManagerConfig => {
|
|
|
50
51
|
const getConfig = useCallback(moduleName => {
|
|
51
52
|
return service.getConfig(moduleName);
|
|
52
53
|
}, [service]);
|
|
53
|
-
|
|
54
|
+
const value = {
|
|
54
55
|
get,
|
|
55
56
|
getConfig,
|
|
56
57
|
reloadConfig,
|
|
57
58
|
isLoading,
|
|
58
59
|
loadModuleConfig
|
|
59
60
|
};
|
|
60
|
-
};
|
|
61
|
-
export const ConfigManagerProvider = ({
|
|
62
|
-
configs,
|
|
63
|
-
children
|
|
64
|
-
}) => {
|
|
65
|
-
const value = useConfigManagerValue(configs);
|
|
66
61
|
return React.createElement(ConfigManagerContext.Provider, {
|
|
67
62
|
value: value
|
|
68
63
|
}, children);
|
|
69
64
|
};
|
|
70
65
|
export const useConfigManager = () => {
|
|
71
66
|
const context = useContext(ConfigManagerContext);
|
|
72
|
-
if (!context)
|
|
67
|
+
if (!context) {
|
|
68
|
+
throw new Error('useConfigManager must be used within a ConfigManagerProvider');
|
|
69
|
+
}
|
|
73
70
|
return context;
|
|
74
71
|
};
|
|
@@ -3,87 +3,85 @@ import { ConfigService } from '../core/ConfigService';
|
|
|
3
3
|
import { setConfigManagerConfig } from '../config';
|
|
4
4
|
import type { ConfigManagerConfig } from '../config';
|
|
5
5
|
|
|
6
|
-
const ConfigManagerContext = createContext<
|
|
6
|
+
const ConfigManagerContext = createContext<any>(null);
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export const ConfigManagerProvider: React.FC<{
|
|
9
|
+
children: React.ReactNode
|
|
10
|
+
configs: ConfigManagerConfig
|
|
11
|
+
}> = ({
|
|
12
|
+
configs,
|
|
13
|
+
children,
|
|
14
|
+
}) => {
|
|
15
|
+
const [service] = useState(() => new ConfigService());
|
|
16
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
setConfigManagerConfig(configs);
|
|
20
|
+
}, [configs]);
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const initialize = async () => {
|
|
24
|
+
setIsLoading(true);
|
|
25
|
+
try {
|
|
26
|
+
await service.initialize();
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error('Failed to initialize config manager:', error);
|
|
29
|
+
} finally {
|
|
30
|
+
setIsLoading(false);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
initialize();
|
|
34
|
+
}, [service]);
|
|
18
35
|
|
|
19
|
-
|
|
20
|
-
useEffect(() => {
|
|
21
|
-
const initialize = async () => {
|
|
36
|
+
const reloadConfig = useCallback(async () => {
|
|
22
37
|
setIsLoading(true);
|
|
23
38
|
try {
|
|
24
|
-
await service.
|
|
39
|
+
await service.reload();
|
|
25
40
|
} catch (error) {
|
|
26
|
-
console.error('Failed to
|
|
41
|
+
console.error('Failed to reload config:', error);
|
|
27
42
|
} finally {
|
|
28
43
|
setIsLoading(false);
|
|
29
44
|
}
|
|
30
|
-
};
|
|
31
|
-
initialize();
|
|
32
|
-
}, [service]);
|
|
45
|
+
}, [service]);
|
|
33
46
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
const loadModuleConfig = useCallback(async (moduleName: string) => {
|
|
48
|
+
setIsLoading(true);
|
|
49
|
+
try {
|
|
50
|
+
await service.loadModule(moduleName);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error(`Failed to load module config ${moduleName}:`, error);
|
|
53
|
+
} finally {
|
|
54
|
+
setIsLoading(false);
|
|
55
|
+
}
|
|
56
|
+
}, [service]);
|
|
44
57
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
await service.loadModule(moduleName);
|
|
49
|
-
} catch (error) {
|
|
50
|
-
console.error(`Failed to load module config ${moduleName}:`, error);
|
|
51
|
-
} finally {
|
|
52
|
-
setIsLoading(false);
|
|
53
|
-
}
|
|
54
|
-
}, [service]);
|
|
58
|
+
const get = useCallback((key: string, options?: any) => {
|
|
59
|
+
return service.get(key, options);
|
|
60
|
+
}, [service]);
|
|
55
61
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
const getConfig = useCallback((moduleName?: string) => {
|
|
63
|
+
return service.getConfig(moduleName);
|
|
64
|
+
}, [service]);
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
const value = {
|
|
67
|
+
get,
|
|
68
|
+
getConfig,
|
|
69
|
+
reloadConfig,
|
|
70
|
+
isLoading,
|
|
71
|
+
loadModuleConfig,
|
|
72
|
+
};
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
loadModuleConfig,
|
|
74
|
+
return (
|
|
75
|
+
<ConfigManagerContext.Provider value={value}>
|
|
76
|
+
{children}
|
|
77
|
+
</ConfigManagerContext.Provider>
|
|
78
|
+
);
|
|
70
79
|
};
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export const ConfigManagerProvider: React.FC<{
|
|
74
|
-
children: React.ReactNode
|
|
75
|
-
configs: ConfigManagerConfig
|
|
76
|
-
}> = ({
|
|
77
|
-
configs,
|
|
78
|
-
children,
|
|
79
|
-
}) => {
|
|
80
|
-
const value = useConfigManagerValue(configs);
|
|
81
|
-
|
|
82
|
-
return <ConfigManagerContext.Provider value={value}>{children}</ConfigManagerContext.Provider>;
|
|
83
|
-
};
|
|
84
80
|
|
|
85
81
|
export const useConfigManager = () => {
|
|
86
82
|
const context = useContext(ConfigManagerContext);
|
|
87
|
-
if (!context)
|
|
83
|
+
if (!context) {
|
|
84
|
+
throw new Error('useConfigManager must be used within a ConfigManagerProvider');
|
|
85
|
+
}
|
|
88
86
|
return context;
|
|
89
87
|
};
|