supaapps-config-fetcher 1.0.4 → 1.0.6

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.
@@ -0,0 +1,9 @@
1
+ export declare class ConfigManager<T> {
2
+ private static instances;
3
+ private config;
4
+ private appName;
5
+ private configHost;
6
+ private constructor();
7
+ static getInstance<T>(appName: string, configHost?: string): ConfigManager<T>;
8
+ loadConfig(): Promise<T>;
9
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ConfigManager = void 0;
13
+ const axios_1 = require("axios");
14
+ class ConfigManager {
15
+ constructor(appName, configHost = 'https://config.sacl.io') {
16
+ this.config = null;
17
+ this.appName = appName;
18
+ this.configHost = configHost;
19
+ }
20
+ static getInstance(appName, configHost) {
21
+ const key = `${appName}-${configHost || 'default'}`;
22
+ if (!ConfigManager.instances[key]) {
23
+ ConfigManager.instances[key] = new ConfigManager(appName, configHost);
24
+ }
25
+ return ConfigManager.instances[key];
26
+ }
27
+ loadConfig() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ if (this.config)
30
+ return this.config;
31
+ try {
32
+ const host = typeof window !== 'undefined' ? window.location.hostname : 'localhost';
33
+ const response = yield axios_1.default.get(`${this.configHost}/${this.appName}/${host}.json`);
34
+ this.config = response.data;
35
+ return this.config;
36
+ }
37
+ catch (error) {
38
+ console.error("Failed to load config:", error);
39
+ throw new Error('Received invalid config data');
40
+ }
41
+ });
42
+ }
43
+ }
44
+ exports.ConfigManager = ConfigManager;
45
+ ConfigManager.instances = {};
@@ -0,0 +1,2 @@
1
+ export { ConfigManager } from './ConfigManager';
2
+ export { useConfig } from './useConfig';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useConfig = exports.ConfigManager = void 0;
4
+ var ConfigManager_1 = require("./ConfigManager");
5
+ Object.defineProperty(exports, "ConfigManager", { enumerable: true, get: function () { return ConfigManager_1.ConfigManager; } });
6
+ var useConfig_1 = require("./useConfig");
7
+ Object.defineProperty(exports, "useConfig", { enumerable: true, get: function () { return useConfig_1.useConfig; } });
@@ -0,0 +1 @@
1
+ export declare function useConfig<T>(appName: string): [T | null, Error | null];
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.useConfig = void 0;
13
+ const react_1 = require("react");
14
+ const ConfigManager_1 = require("./ConfigManager");
15
+ function useConfig(appName) {
16
+ const [config, setConfig] = (0, react_1.useState)(null);
17
+ const [error, setError] = (0, react_1.useState)(null);
18
+ (0, react_1.useEffect)(() => {
19
+ const fetchConfig = () => __awaiter(this, void 0, void 0, function* () {
20
+ try {
21
+ const cfg = yield ConfigManager_1.ConfigManager.getInstance(appName).loadConfig();
22
+ setConfig(cfg);
23
+ }
24
+ catch (err) {
25
+ setError(err);
26
+ }
27
+ });
28
+ fetchConfig();
29
+ }, [appName]);
30
+ return [config, error];
31
+ }
32
+ exports.useConfig = useConfig;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "supaapps-config-fetcher",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
- "main": "index.js",
6
- "types": "index.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "test": "echo \"Error: no test specified\" && exit 1",
9
9
  "build": "tsc"
@@ -19,7 +19,7 @@ export class ConfigManager<T> {
19
19
  return ConfigManager.instances[key] as ConfigManager<T>;
20
20
  }
21
21
 
22
- public async loadConfig(): Promise<T | null> {
22
+ public async loadConfig(): Promise<T> {
23
23
  if (this.config) return this.config;
24
24
  try {
25
25
  const host = typeof window !== 'undefined' ? window.location.hostname : 'localhost';
@@ -28,11 +28,7 @@ export class ConfigManager<T> {
28
28
  return this.config;
29
29
  } catch (error) {
30
30
  console.error("Failed to load config:", error);
31
- return null;
31
+ throw new Error('Received invalid config data');
32
32
  }
33
33
  }
34
-
35
- public getConfig(): T | null {
36
- return this.config;
37
- }
38
34
  }