supaapps-config-fetcher 1.0.2

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,30 @@
1
+ name: Publish to NPM
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - name: Checkout code
13
+ uses: actions/checkout@v2
14
+
15
+ - name: Use Node.js
16
+ uses: actions/setup-node@v3
17
+ with:
18
+ node-version: lts/*
19
+ registry-url: 'https://registry.npmjs.org'
20
+
21
+ - name: Install dependencies
22
+ run: npm install
23
+
24
+ - name: Build
25
+ run: npm run build
26
+
27
+ - name: Publish to npm
28
+ run: npm publish
29
+ env:
30
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Notice: While this software is open-source under the MIT License, the "Supaapps" name, branding, and logo are proprietary and copyrighted by Supaapps GmbH. Any use, reproduction, or distribution of the "Supaapps" brand assets without explicit permission is strictly prohibited.
4
+
5
+ Copyright (c) 2023 Supaapps GmbH
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,72 @@
1
+
2
+ ![Supaapps Logo](https://supaapps.com/wp-content/uploads/logo.png)
3
+
4
+ ## Supaapps Config Fetcher
5
+
6
+
7
+ Managing configurations in applications can be cumbrersome, whether it's containerized deployments like Docker or traditional application distributions. Rebuilding an app or creating multiple versions for a single configuration change isn't efficient. `supaapps-config-fetcher` aims to simplify this.
8
+
9
+ `supaapps-config-fetcher` is an easy-to-use package developed by Supaapps GmbH to fetch configurations from a hosted JSON file based on the application's hostname. Although intended for internal use, we've made this package available under the MIT license. Feel free to integrate it into your projects.
10
+
11
+
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install supaapps-config-fetcher
17
+
18
+ ```
19
+ Or using Yarn:
20
+ ```bash
21
+ yarn add supaapps-config-fetcher
22
+
23
+ ```
24
+
25
+
26
+
27
+ ## Usage
28
+
29
+ #### Fetching Configurations: The primary purpose of this package is to fetch configurations<br from a hosted JSON file based on the application's hostname.
30
+ Below are an example config and how to use it with our package.
31
+ ```json
32
+ {
33
+ "apiUrl": "http://localhost:8080/api"
34
+ }
35
+ ```
36
+
37
+ #### TypeScript Support: Before using the package, define the expected return type for your<br configuration.
38
+
39
+ ```typescript
40
+ type MyAppConfig = {
41
+ apiUrl: string;
42
+ };
43
+ ```
44
+
45
+ ####
46
+ To initialize and fetch the configuration, use the code provided below:
47
+
48
+ ```typescript
49
+ import { ConfigManager } from 'supaapps-config-fetcher';
50
+
51
+ const configManager = ConfigManager.getInstance<MyAppConfig>('example-app@v1');
52
+ const config = await configManager.loadConfig();
53
+ console.log(config.apiUrl);
54
+ // Outputs: "http://localhost:8080/api"
55
+
56
+ ```
57
+
58
+ #
59
+
60
+ you should use custom host where your config.json files are hosted https://example.com and host your files with `example-app@v1` then hostnames example: `localhost.json`
61
+ ```typescript
62
+ const customHostManager = ConfigManager.getInstance<MyAppConfig>('example-app@v1', 'https://example.com');
63
+
64
+ ```
65
+
66
+ This will load the file from https://example.com/example-app@v1/localhost.json when the app is loaded from localhost
67
+
68
+ ## License
69
+
70
+ Notice: While this software is open-source under the MIT License, the "Supaapps" name, branding, and logo are proprietary and copyrighted by Supaapps GmbH. Any use, reproduction, or distribution of the "Supaapps" brand assets without explicit permission is strictly prohibited.
71
+
72
+ MIT License
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "supaapps-config-fetcher",
3
+ "version": "1.0.2",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "build": "tsc"
9
+ },
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "ISC",
13
+ "peerDependencies": {
14
+ "react": "^17.0.0 || ^18.0.0"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^20.8.8",
18
+ "@types/react": "^18.2.32",
19
+ "axios": "^1.5.1",
20
+ "typescript": "^5.2.2"
21
+ }
22
+ }
@@ -0,0 +1,38 @@
1
+ import axios from 'axios';
2
+
3
+ export class ConfigManager<T> {
4
+ private static instances: Record<string, ConfigManager<any>> = {};
5
+ private config: T | null = null;
6
+ private appName: string;
7
+ private configHost: string;
8
+
9
+ private constructor(appName: string, configHost: string = 'https://config.sacl.io') {
10
+ this.appName = appName;
11
+ this.configHost = configHost;
12
+ }
13
+
14
+ public static getInstance<T>(appName: string, configHost?: string): ConfigManager<T> {
15
+ const key = `${appName}-${configHost || 'default'}`;
16
+ if (!ConfigManager.instances[key]) {
17
+ ConfigManager.instances[key] = new ConfigManager<T>(appName, configHost);
18
+ }
19
+ return ConfigManager.instances[key] as ConfigManager<T>;
20
+ }
21
+
22
+ public async loadConfig(): Promise<T | null> {
23
+ if (this.config) return this.config;
24
+ try {
25
+ const host = typeof window !== 'undefined' ? window.location.hostname : 'localhost';
26
+ const response = await axios.get(`${this.configHost}/${this.appName}/${host}.json`);
27
+ this.config = response.data;
28
+ return this.config;
29
+ } catch (error) {
30
+ console.error("Failed to load config:", error);
31
+ return null;
32
+ }
33
+ }
34
+
35
+ public getConfig(): T | null {
36
+ return this.config;
37
+ }
38
+ }
@@ -0,0 +1,21 @@
1
+ import { useState, useEffect } from 'react';
2
+ import { ConfigManager } from './ConfigManager';
3
+
4
+ export function useConfig<T>(appName: string): [T | null, Error | null] {
5
+ const [config, setConfig] = useState<T | null>(null);
6
+ const [error, setError] = useState<Error | null>(null);
7
+
8
+ useEffect(() => {
9
+ const fetchConfig = async () => {
10
+ try {
11
+ const cfg = await ConfigManager.getInstance<T>(appName).loadConfig();
12
+ setConfig(cfg);
13
+ } catch (err) {
14
+ setError(err);
15
+ }
16
+ };
17
+ fetchConfig();
18
+ }, [appName]);
19
+
20
+ return [config, error];
21
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist",
4
+ "declaration": true,
5
+ "module": "commonjs",
6
+ "target": "ES6"
7
+ },
8
+ "include": ["src/**/*.ts"]
9
+ }