@webitel/ui-sdk 26.6.18 → 26.6.19
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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webitel/ui-sdk",
|
|
3
|
-
"version": "26.6.
|
|
3
|
+
"version": "26.6.19",
|
|
4
4
|
"private": false,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "npm run docs:dev",
|
|
@@ -91,6 +91,7 @@
|
|
|
91
91
|
"lodash-es": "^4.17.21",
|
|
92
92
|
"mitt": "^3.0.1",
|
|
93
93
|
"path-browserify": "^1.0.1",
|
|
94
|
+
"tiny-jsonc": "^1.0.2",
|
|
94
95
|
"vidstack": "^1.12.13",
|
|
95
96
|
"vue-i18n": "^11.1.2",
|
|
96
97
|
"vue-router": "^4.5.0",
|
|
@@ -320,6 +321,10 @@
|
|
|
320
321
|
"types": "./types/modules*",
|
|
321
322
|
"import": "./src/modules*"
|
|
322
323
|
},
|
|
324
|
+
"./modules/AppConfig": {
|
|
325
|
+
"types": "./types/modules/AppConfig/index.d.ts",
|
|
326
|
+
"import": "./src/modules/AppConfig/index.ts"
|
|
327
|
+
},
|
|
323
328
|
"./modules/Flow": {
|
|
324
329
|
"types": "./types/modules/Flow/index.d.ts",
|
|
325
330
|
"import": "./src/modules/Flow/index.ts"
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import deepmerge from 'deepmerge';
|
|
2
|
+
import type { PartialDeep } from 'type-fest';
|
|
3
|
+
|
|
4
|
+
import { fetchConfig } from './scripts/fetchConfig';
|
|
5
|
+
|
|
6
|
+
// Config values overwrite, they don't accumulate — let the source array win.
|
|
7
|
+
const overwriteArrays = (_destination: unknown[], source: unknown[]) => source;
|
|
8
|
+
|
|
9
|
+
export interface AppConfigModule<T> {
|
|
10
|
+
/**
|
|
11
|
+
* Returns the resolved config, initializing it on first access.
|
|
12
|
+
*/
|
|
13
|
+
getConfig: () => Promise<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Fetches and resolves the config, replacing any previously cached value.
|
|
16
|
+
*/
|
|
17
|
+
initializeConfig: () => Promise<T>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates a reusable app config module.
|
|
22
|
+
*
|
|
23
|
+
* Each application provides its own config type `T` and a `defaultConfig` used
|
|
24
|
+
* as the base. The fetched runtime config (see {@link fetchConfig}) is deep-merged
|
|
25
|
+
* over the defaults.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { createAppConfig } from '@webitel/ui-sdk/modules/AppConfig';
|
|
30
|
+
* import { defaultConfig } from './defaults/defaultConfig';
|
|
31
|
+
* import type { AppConfig } from './types/AppConfig';
|
|
32
|
+
*
|
|
33
|
+
* export const { getConfig, initializeConfig } = createAppConfig<AppConfig>(defaultConfig);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function createAppConfig<T>(
|
|
37
|
+
defaultConfig: PartialDeep<T>,
|
|
38
|
+
): AppConfigModule<T> {
|
|
39
|
+
let config: T;
|
|
40
|
+
|
|
41
|
+
async function initializeConfig(): Promise<T> {
|
|
42
|
+
const fetchedConfig = await fetchConfig<T>();
|
|
43
|
+
config = deepmerge<Partial<T>>(defaultConfig as Partial<T>, fetchedConfig, {
|
|
44
|
+
arrayMerge: overwriteArrays,
|
|
45
|
+
}) as T;
|
|
46
|
+
return config;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function getConfig(): Promise<T> {
|
|
50
|
+
if (!config) {
|
|
51
|
+
config = await initializeConfig();
|
|
52
|
+
}
|
|
53
|
+
return config;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
getConfig,
|
|
58
|
+
initializeConfig,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { fetchConfig };
|
|
63
|
+
export type { PartialDeep };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import deepmerge from 'deepmerge';
|
|
2
|
+
import jsoncParser from 'tiny-jsonc';
|
|
3
|
+
|
|
4
|
+
// Config values overwrite, they don't accumulate — let the source array win.
|
|
5
|
+
const overwriteArrays = (_destination: unknown[], source: unknown[]) => source;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Fetches the application runtime config from static files served next to the app.
|
|
9
|
+
*
|
|
10
|
+
* Looks up `config.{json,jsonc}` and an optional `config.local.{json,jsonc}` overlay,
|
|
11
|
+
* with the local file taking priority. The shape of the returned object is
|
|
12
|
+
* application-specific, so the concrete type is provided by the caller.
|
|
13
|
+
*/
|
|
14
|
+
export async function fetchConfig<T>(): Promise<Partial<T>> {
|
|
15
|
+
return await fetchWithLocalConfigPriority<T>();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function fetchWithLocalConfigPriority<T>(): Promise<Partial<T>> {
|
|
19
|
+
const localConfig = await fetchSupportedExtConfig('config.local');
|
|
20
|
+
const config = await fetchSupportedExtConfig('config');
|
|
21
|
+
|
|
22
|
+
return deepmerge(config ?? {}, localConfig ?? {}, {
|
|
23
|
+
arrayMerge: overwriteArrays,
|
|
24
|
+
}) as Partial<T>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function fetchSupportedExtConfig(fileName = 'config') {
|
|
28
|
+
const supportedExtensions = [
|
|
29
|
+
'.json',
|
|
30
|
+
'.jsonc',
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
for (const extension of supportedExtensions) {
|
|
34
|
+
try {
|
|
35
|
+
const response = await fetch(`./${fileName}${extension}`);
|
|
36
|
+
if (response.ok) {
|
|
37
|
+
const configText = await response.text();
|
|
38
|
+
return jsoncParser.parse(configText);
|
|
39
|
+
}
|
|
40
|
+
} catch {}
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { PartialDeep } from 'type-fest';
|
|
2
|
+
import { fetchConfig } from './scripts/fetchConfig';
|
|
3
|
+
export interface AppConfigModule<T> {
|
|
4
|
+
/**
|
|
5
|
+
* Returns the resolved config, initializing it on first access.
|
|
6
|
+
*/
|
|
7
|
+
getConfig: () => Promise<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Fetches and resolves the config, replacing any previously cached value.
|
|
10
|
+
*/
|
|
11
|
+
initializeConfig: () => Promise<T>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Creates a reusable app config module.
|
|
15
|
+
*
|
|
16
|
+
* Each application provides its own config type `T` and a `defaultConfig` used
|
|
17
|
+
* as the base. The fetched runtime config (see {@link fetchConfig}) is deep-merged
|
|
18
|
+
* over the defaults.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { createAppConfig } from '@webitel/ui-sdk/modules/AppConfig';
|
|
23
|
+
* import { defaultConfig } from './defaults/defaultConfig';
|
|
24
|
+
* import type { AppConfig } from './types/AppConfig';
|
|
25
|
+
*
|
|
26
|
+
* export const { getConfig, initializeConfig } = createAppConfig<AppConfig>(defaultConfig);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function createAppConfig<T>(defaultConfig: PartialDeep<T>): AppConfigModule<T>;
|
|
30
|
+
export { fetchConfig };
|
|
31
|
+
export type { PartialDeep };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetches the application runtime config from static files served next to the app.
|
|
3
|
+
*
|
|
4
|
+
* Looks up `config.{json,jsonc}` and an optional `config.local.{json,jsonc}` overlay,
|
|
5
|
+
* with the local file taking priority. The shape of the returned object is
|
|
6
|
+
* application-specific, so the concrete type is provided by the caller.
|
|
7
|
+
*/
|
|
8
|
+
export declare function fetchConfig<T>(): Promise<Partial<T>>;
|