shared-lib-web 0.0.2 → 0.0.3
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/README.md +381 -127
- package/dist/components/app-brand.d.ts.map +1 -1
- package/dist/components/app-brand.js +9 -5
- package/dist/components/app-brand.js.map +1 -1
- package/dist/components/coming-soon.d.ts.map +1 -1
- package/dist/components/coming-soon.js +3 -2
- package/dist/components/coming-soon.js.map +1 -1
- package/dist/components/language-switcher.d.ts +5 -1
- package/dist/components/language-switcher.d.ts.map +1 -1
- package/dist/components/language-switcher.js +20 -7
- package/dist/components/language-switcher.js.map +1 -1
- package/dist/components/settings-menu.d.ts +4 -0
- package/dist/components/settings-menu.d.ts.map +1 -1
- package/dist/components/settings-menu.js +15 -2
- package/dist/components/settings-menu.js.map +1 -1
- package/dist/context/use-ui.d.ts.map +1 -1
- package/dist/context/use-ui.js +4 -3
- package/dist/context/use-ui.js.map +1 -1
- package/dist/i18n/browser-language.d.ts +9 -0
- package/dist/i18n/browser-language.d.ts.map +1 -0
- package/dist/i18n/browser-language.js +89 -0
- package/dist/i18n/browser-language.js.map +1 -0
- package/dist/i18n/default-messages.d.ts.map +1 -1
- package/dist/i18n/default-messages.js +3 -0
- package/dist/i18n/default-messages.js.map +1 -1
- package/dist/i18n/locale-groups.d.ts +18 -0
- package/dist/i18n/locale-groups.d.ts.map +1 -0
- package/dist/i18n/locale-groups.js +74 -0
- package/dist/i18n/locale-groups.js.map +1 -0
- package/dist/i18n/locale-service.d.ts +22 -4
- package/dist/i18n/locale-service.d.ts.map +1 -1
- package/dist/i18n/locale-service.js +95 -27
- package/dist/i18n/locale-service.js.map +1 -1
- package/dist/i18n/locale-signal.d.ts +18 -0
- package/dist/i18n/locale-signal.d.ts.map +1 -0
- package/dist/i18n/locale-signal.js +142 -0
- package/dist/i18n/locale-signal.js.map +1 -0
- package/dist/i18n/translator.d.ts +8 -1
- package/dist/i18n/translator.d.ts.map +1 -1
- package/dist/i18n/translator.js +36 -12
- package/dist/i18n/translator.js.map +1 -1
- package/dist/index.d.ts +21 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -8
- package/dist/index.js.map +1 -1
- package/dist/interfaces/app-types.d.ts +6 -0
- package/dist/interfaces/app-types.d.ts.map +1 -1
- package/dist/interfaces/ui-config.d.ts +162 -15
- package/dist/interfaces/ui-config.d.ts.map +1 -1
- package/dist/interfaces/ui-context.d.ts +1 -1
- package/dist/interfaces/ui-context.d.ts.map +1 -1
- package/dist/layouts/auth-layout.js +1 -1
- package/dist/layouts/auth-layout.js.map +1 -1
- package/dist/providers/provider-effects.d.ts +2 -2
- package/dist/providers/provider-effects.d.ts.map +1 -1
- package/dist/providers/provider-effects.js +4 -1
- package/dist/providers/provider-effects.js.map +1 -1
- package/dist/providers/register-config.d.ts.map +1 -1
- package/dist/providers/register-config.js +2 -0
- package/dist/providers/register-config.js.map +1 -1
- package/dist/providers/shared-web-ui-provider.d.ts +7 -19
- package/dist/providers/shared-web-ui-provider.d.ts.map +1 -1
- package/dist/providers/shared-web-ui-provider.js +21 -22
- package/dist/providers/shared-web-ui-provider.js.map +1 -1
- package/dist/query/query-param.d.ts +4 -1
- package/dist/query/query-param.d.ts.map +1 -1
- package/dist/query/query-param.js +6 -3
- package/dist/query/query-param.js.map +1 -1
- package/dist/state/api-loader.d.ts +19 -0
- package/dist/state/api-loader.d.ts.map +1 -0
- package/dist/state/api-loader.js +187 -0
- package/dist/state/api-loader.js.map +1 -0
- package/dist/state/auth-service.d.ts +4 -0
- package/dist/state/auth-service.d.ts.map +1 -1
- package/dist/state/auth-service.js +47 -3
- package/dist/state/auth-service.js.map +1 -1
- package/dist/state/branch-service.d.ts +1 -1
- package/dist/state/branch-service.js +3 -3
- package/dist/state/branch-service.js.map +1 -1
- package/dist/state/company-service.d.ts +16 -0
- package/dist/state/company-service.d.ts.map +1 -0
- package/dist/state/company-service.js +97 -0
- package/dist/state/company-service.js.map +1 -0
- package/dist/state/language-service.d.ts +5 -2
- package/dist/state/language-service.d.ts.map +1 -1
- package/dist/state/language-service.js +12 -7
- package/dist/state/language-service.js.map +1 -1
- package/dist/state/layout-choice-service.d.ts +10 -0
- package/dist/state/layout-choice-service.d.ts.map +1 -0
- package/dist/state/layout-choice-service.js +34 -0
- package/dist/state/layout-choice-service.js.map +1 -0
- package/dist/state/menu-service.d.ts +1 -1
- package/dist/state/menu-service.js +2 -2
- package/dist/state/menu-service.js.map +1 -1
- package/dist/state/preference-sync.d.ts +1 -1
- package/dist/state/preference-sync.d.ts.map +1 -1
- package/dist/state/preference-sync.js +11 -3
- package/dist/state/preference-sync.js.map +1 -1
- package/dist/state/signals.d.ts +9 -1
- package/dist/state/signals.d.ts.map +1 -1
- package/dist/state/signals.js +28 -4
- package/dist/state/signals.js.map +1 -1
- package/dist/theme/custom-themes.d.ts +28 -0
- package/dist/theme/custom-themes.d.ts.map +1 -0
- package/dist/theme/custom-themes.js +218 -0
- package/dist/theme/custom-themes.js.map +1 -0
- package/dist/theme/dark-themes.d.ts +9 -1
- package/dist/theme/dark-themes.d.ts.map +1 -1
- package/dist/theme/dark-themes.js +74 -2
- package/dist/theme/dark-themes.js.map +1 -1
- package/dist/theme/logo.d.ts +6 -3
- package/dist/theme/logo.d.ts.map +1 -1
- package/dist/theme/logo.js +17 -3
- package/dist/theme/logo.js.map +1 -1
- package/dist/theme/theme-service.d.ts +14 -3
- package/dist/theme/theme-service.d.ts.map +1 -1
- package/dist/theme/theme-service.js +64 -10
- package/dist/theme/theme-service.js.map +1 -1
- package/dist/utils/app-http.d.ts +29 -1
- package/dist/utils/app-http.d.ts.map +1 -1
- package/dist/utils/app-http.js +113 -11
- package/dist/utils/app-http.js.map +1 -1
- package/dist/utils/content-util.d.ts +6 -0
- package/dist/utils/content-util.d.ts.map +1 -0
- package/dist/utils/content-util.js +45 -0
- package/dist/utils/content-util.js.map +1 -0
- package/dist/utils/error-util.d.ts +3 -0
- package/dist/utils/error-util.d.ts.map +1 -0
- package/dist/utils/error-util.js +58 -0
- package/dist/utils/error-util.js.map +1 -0
- package/dist/utils/language-util.d.ts +3 -3
- package/dist/utils/language-util.d.ts.map +1 -1
- package/dist/utils/language-util.js +7 -7
- package/dist/utils/language-util.js.map +1 -1
- package/dist/utils/menu-util.js +1 -1
- package/dist/utils/menu-util.js.map +1 -1
- package/dist/utils/signal-source.d.ts +4 -0
- package/dist/utils/signal-source.d.ts.map +1 -0
- package/dist/utils/signal-source.js +13 -0
- package/dist/utils/signal-source.js.map +1 -0
- package/dist/utils/storage-keys.d.ts +5 -0
- package/dist/utils/storage-keys.d.ts.map +1 -1
- package/dist/utils/storage-keys.js +5 -0
- package/dist/utils/storage-keys.js.map +1 -1
- package/package.json +3 -3
- package/src/components/app-brand.tsx +9 -5
- package/src/components/coming-soon.tsx +3 -2
- package/src/components/language-switcher.tsx +26 -7
- package/src/components/settings-menu.tsx +28 -2
- package/src/context/use-ui.ts +4 -3
- package/src/i18n/browser-language.ts +98 -0
- package/src/i18n/default-messages.ts +3 -0
- package/src/i18n/locale-groups.ts +87 -0
- package/src/i18n/locale-service.ts +99 -27
- package/src/i18n/locale-signal.ts +173 -0
- package/src/i18n/translator.ts +38 -12
- package/src/index.ts +71 -3
- package/src/interfaces/app-types.ts +6 -0
- package/src/interfaces/ui-config.ts +168 -15
- package/src/interfaces/ui-context.ts +1 -1
- package/src/layouts/auth-layout.tsx +1 -1
- package/src/providers/provider-effects.ts +5 -2
- package/src/providers/register-config.ts +2 -0
- package/src/providers/shared-web-ui-provider.tsx +26 -43
- package/src/query/query-param.ts +6 -2
- package/src/state/api-loader.ts +205 -0
- package/src/state/auth-service.ts +58 -3
- package/src/state/branch-service.ts +3 -3
- package/src/state/company-service.ts +107 -0
- package/src/state/language-service.ts +12 -7
- package/src/state/layout-choice-service.ts +40 -0
- package/src/state/menu-service.ts +2 -2
- package/src/state/preference-sync.ts +11 -3
- package/src/state/signals.ts +36 -4
- package/src/theme/custom-themes.ts +236 -0
- package/src/theme/dark-themes.ts +80 -3
- package/src/theme/logo.ts +19 -4
- package/src/theme/org-themes.json +70 -0
- package/src/theme/theme-service.ts +70 -10
- package/src/utils/app-http.ts +125 -11
- package/src/utils/content-util.ts +49 -0
- package/src/utils/error-util.ts +64 -0
- package/src/utils/language-util.ts +7 -7
- package/src/utils/menu-util.ts +1 -1
- package/src/utils/signal-source.ts +15 -0
- package/src/utils/storage-keys.ts +6 -0
|
@@ -3,30 +3,89 @@
|
|
|
3
3
|
//
|
|
4
4
|
// changeTheme("dark"); -> <html data-theme="dark">, saved to localStorage
|
|
5
5
|
//
|
|
6
|
+
// First visit (nothing saved): config.defaultTheme, or without it the browser preference
|
|
7
|
+
// (prefers-color-scheme: dark -> "dark", otherwise "light").
|
|
8
|
+
//
|
|
6
9
|
// The <html> attribute is written by state/document-sync.ts.
|
|
7
10
|
|
|
8
11
|
import type { Theme, UiConfig } from "../interfaces/ui-config.ts";
|
|
9
|
-
import { themeSignal, uiConfigSignal } from "../state/signals.ts";
|
|
12
|
+
import { customThemesSignal, themeSignal, uiConfigSignal } from "../state/signals.ts";
|
|
10
13
|
import { AppStorage } from "../utils/app-storage.ts";
|
|
11
14
|
import { THEME } from "../utils/storage-keys.ts";
|
|
12
|
-
import {
|
|
15
|
+
import { buildDarkThemeNames, buildThemeNames } from "./dark-themes.ts";
|
|
16
|
+
|
|
17
|
+
/** The theme names of the switcher: the custom themes first, then config.themes (default: all DaisyUI themes). */
|
|
18
|
+
export function readThemeNames(config: UiConfig): Theme[] {
|
|
19
|
+
const customThemes = customThemesSignal.value;
|
|
20
|
+
return buildThemeNames(config.themes, customThemes);
|
|
21
|
+
}
|
|
13
22
|
|
|
14
|
-
/** true when the theme is
|
|
23
|
+
/** true when the theme is offered (config.themes or a custom theme). */
|
|
15
24
|
export function isKnownTheme(config: UiConfig, theme: string): boolean {
|
|
16
|
-
const
|
|
17
|
-
return
|
|
25
|
+
const themeNames = readThemeNames(config);
|
|
26
|
+
return themeNames.includes(theme);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const DARK_SCHEME_QUERY = "(prefers-color-scheme: dark)";
|
|
30
|
+
|
|
31
|
+
/** true when the browser / OS asks for a dark colour scheme. */
|
|
32
|
+
function isBrowserDarkPreferred(): boolean {
|
|
33
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
const darkQuery = window.matchMedia(DARK_SCHEME_QUERY);
|
|
37
|
+
return darkQuery.matches;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The first-visit theme: config.defaultTheme, else "dark" or "light" from the browser preference. */
|
|
41
|
+
export function readDefaultTheme(config: UiConfig): Theme {
|
|
42
|
+
if (config.defaultTheme) {
|
|
43
|
+
return config.defaultTheme;
|
|
44
|
+
}
|
|
45
|
+
const isDarkPreferred = isBrowserDarkPreferred();
|
|
46
|
+
return isDarkPreferred ? "dark" : "light";
|
|
18
47
|
}
|
|
19
48
|
|
|
20
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Picks the theme to start with: the saved one if it is in the config, else the default.
|
|
51
|
+
* With config.loadThemes the saved theme is kept while the custom themes load (no flash of the default theme).
|
|
52
|
+
*/
|
|
21
53
|
export function readInitialTheme(config: UiConfig): Theme {
|
|
22
54
|
const storedTheme = AppStorage.getText(THEME);
|
|
23
55
|
if (storedTheme) {
|
|
24
56
|
const isStoredThemeKnown = isKnownTheme(config, storedTheme);
|
|
25
|
-
|
|
57
|
+
const isThemeLoading = Boolean(config.loadThemes);
|
|
58
|
+
if (isStoredThemeKnown || isThemeLoading) {
|
|
26
59
|
return storedTheme;
|
|
27
60
|
}
|
|
28
61
|
}
|
|
29
|
-
return config
|
|
62
|
+
return readDefaultTheme(config);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** After config.loadThemes: a theme that is still unknown (not in the loaded list) falls back to the default. */
|
|
66
|
+
export function leaveUnknownTheme(): void {
|
|
67
|
+
const config = uiConfigSignal.peek();
|
|
68
|
+
if (!config) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const currentTheme = themeSignal.peek();
|
|
72
|
+
const isCurrentThemeKnown = isKnownTheme(config, currentTheme);
|
|
73
|
+
if (!isCurrentThemeKnown) {
|
|
74
|
+
themeSignal.value = readDefaultTheme(config);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Custom themes can arrive after the start (fetched JSON): switch to the saved theme once it is known. */
|
|
79
|
+
export function applySavedThemeWhenKnown(): void {
|
|
80
|
+
const config = uiConfigSignal.peek();
|
|
81
|
+
const storedTheme = AppStorage.getText(THEME);
|
|
82
|
+
if (!config || !storedTheme || storedTheme === themeSignal.peek()) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const isStoredThemeKnown = isKnownTheme(config, storedTheme);
|
|
86
|
+
if (isStoredThemeKnown) {
|
|
87
|
+
themeSignal.value = storedTheme;
|
|
88
|
+
}
|
|
30
89
|
}
|
|
31
90
|
|
|
32
91
|
/** Switches the theme. Themes not listed in the config are ignored. */
|
|
@@ -48,10 +107,11 @@ export interface ThemeGroups {
|
|
|
48
107
|
darkThemes: Theme[];
|
|
49
108
|
}
|
|
50
109
|
|
|
51
|
-
/** The dark theme names: config.darkThemes, else the DaisyUI dark themes. */
|
|
110
|
+
/** The dark theme names: the dark custom themes + (config.darkThemes, else the DaisyUI dark themes). */
|
|
52
111
|
export function readDarkThemeNames(): Theme[] {
|
|
53
112
|
const config = uiConfigSignal.value;
|
|
54
|
-
|
|
113
|
+
const customThemes = customThemesSignal.value;
|
|
114
|
+
return buildDarkThemeNames(config?.darkThemes, customThemes);
|
|
55
115
|
}
|
|
56
116
|
|
|
57
117
|
/** true when the theme has a dark background. */
|
package/src/utils/app-http.ts
CHANGED
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
//
|
|
14
14
|
// Service URLs (MsUrl) are NOT part of AppHttp: each project keeps them in its own config.ts.
|
|
15
15
|
//
|
|
16
|
+
// Every method takes a full URL or a path: a URL that starts with http:// or https:// is used as it is,
|
|
17
|
+
// anything else is appended to the base URL ("/profile/search" -> "<baseUrl>/profile/search").
|
|
18
|
+
//
|
|
16
19
|
// // hooks/service-http.ts
|
|
17
20
|
// const listUrl = `${MsUrl.main}/profile/search`;
|
|
18
21
|
// const response = await AppHttp.Get(listUrl, params);
|
|
@@ -61,6 +64,50 @@ function isProgressEnabled(): boolean {
|
|
|
61
64
|
}
|
|
62
65
|
|
|
63
66
|
/** JWT from AppStorage (config.http.tokenKey, default TOKEN, sessionStorage). "" when missing. */
|
|
67
|
+
/**
|
|
68
|
+
* Requests shared by every copy of the library on the page (a shell and its microfrontends each bundle their own
|
|
69
|
+
* copy): the key is global (Symbol.for), so the cache lives on window once for all copies.
|
|
70
|
+
*/
|
|
71
|
+
const SHARED_GET_KEY = Symbol.for("shared-lib-web:shared-get");
|
|
72
|
+
|
|
73
|
+
type SharedGetCache = Map<string, Promise<unknown>>;
|
|
74
|
+
|
|
75
|
+
/** The page-wide cache of GetShared (created by the first copy that needs it). */
|
|
76
|
+
function readSharedGetCache(): SharedGetCache {
|
|
77
|
+
const holder = globalThis as unknown as Record<symbol, SharedGetCache | undefined>;
|
|
78
|
+
let cache = holder[SHARED_GET_KEY];
|
|
79
|
+
if (!cache) {
|
|
80
|
+
cache = new Map();
|
|
81
|
+
holder[SHARED_GET_KEY] = cache;
|
|
82
|
+
}
|
|
83
|
+
return cache;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Cache key of a request: the full URL + the params sorted by name. */
|
|
87
|
+
export function buildSharedGetKey(fullUrl: string, params: HttpParams): string {
|
|
88
|
+
const names = Object.keys(params);
|
|
89
|
+
names.sort();
|
|
90
|
+
const parts: string[] = [];
|
|
91
|
+
for (const name of names) {
|
|
92
|
+
const value = params[name];
|
|
93
|
+
parts.push(`${name}=${String(value)}`);
|
|
94
|
+
}
|
|
95
|
+
const paramText = parts.join("&");
|
|
96
|
+
return `${fullUrl}?${paramText}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A failed shared request is removed, so the next call tries again. */
|
|
100
|
+
function forgetSharedGet(key: string): void {
|
|
101
|
+
const cache = readSharedGetCache();
|
|
102
|
+
cache.delete(key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Empties the shared request cache (for example after the texts or themes changed on the server, or in tests). */
|
|
106
|
+
export function clearSharedGetCache(): void {
|
|
107
|
+
const cache = readSharedGetCache();
|
|
108
|
+
cache.clear();
|
|
109
|
+
}
|
|
110
|
+
|
|
64
111
|
export function readJwtToken(): string {
|
|
65
112
|
const config = readAppConfig();
|
|
66
113
|
const tokenKey = config?.http?.tokenKey ?? TOKEN;
|
|
@@ -79,6 +126,18 @@ export function toAbsoluteUrl(url: string): string {
|
|
|
79
126
|
return fullUrl.replace(/\/+$/, "");
|
|
80
127
|
}
|
|
81
128
|
|
|
129
|
+
/** true for "http://..." and "https://..." (any case). */
|
|
130
|
+
export function isHttpUrl(url: string): boolean {
|
|
131
|
+
return /^https?:\/\//i.test(url);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Joins a base URL and a path with exactly one "/" between them. */
|
|
135
|
+
export function joinUrl(baseUrl: string, path: string): string {
|
|
136
|
+
const trimmedBase = baseUrl.replace(/\/+$/, "");
|
|
137
|
+
const trimmedPath = path.replace(/^\/+/, "");
|
|
138
|
+
return trimmedPath.length > 0 ? `${trimmedBase}/${trimmedPath}` : trimmedBase;
|
|
139
|
+
}
|
|
140
|
+
|
|
82
141
|
/** Turns a Response body into JSON or text. */
|
|
83
142
|
async function readResponseBody(response: Response): Promise<unknown> {
|
|
84
143
|
const contentType = response.headers.get("content-type") ?? "";
|
|
@@ -106,6 +165,19 @@ export class AppHttp {
|
|
|
106
165
|
return absoluteBaseUrl;
|
|
107
166
|
}
|
|
108
167
|
|
|
168
|
+
/**
|
|
169
|
+
* The URL a request goes to: a URL starting with http:// or https:// stays as it is,
|
|
170
|
+
* anything else is appended to BaseUrl() ("/profile/search" or "profile/search" -> "<baseUrl>/profile/search").
|
|
171
|
+
*/
|
|
172
|
+
static FullUrl(url: string): string {
|
|
173
|
+
const isAbsolute = isHttpUrl(url);
|
|
174
|
+
if (isAbsolute) {
|
|
175
|
+
return url;
|
|
176
|
+
}
|
|
177
|
+
const baseUrl = AppHttp.BaseUrl();
|
|
178
|
+
return joinUrl(baseUrl, url);
|
|
179
|
+
}
|
|
180
|
+
|
|
109
181
|
/** Adds (or with "" removes) a header sent with every request, for example the selected branch. */
|
|
110
182
|
static setHeader(headerName: string, value: string): void {
|
|
111
183
|
if (value.length === 0) {
|
|
@@ -139,22 +211,46 @@ export class AppHttp {
|
|
|
139
211
|
static async Get(url: string, params: HttpParams = {}, customHeaders: HttpHeaders = {}): Promise<unknown> {
|
|
140
212
|
const baseHeaders = AppHttp.Headers();
|
|
141
213
|
const headers = { ...baseHeaders, ...customHeaders };
|
|
142
|
-
const
|
|
214
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
215
|
+
const request = Http.Get(fullUrl, params, headers);
|
|
143
216
|
return AppHttp.withProgress(request);
|
|
144
217
|
}
|
|
145
218
|
|
|
219
|
+
/**
|
|
220
|
+
* GET once per page, shared by every copy of the library (shell + microfrontends): the same URL + params while a
|
|
221
|
+
* request is running or after it answered gives the same promise — no second call. For data that is the same for
|
|
222
|
+
* everyone on the page (languages, themes, texts, company details), not for user data. A failed request is not kept.
|
|
223
|
+
* const reply = await AppHttp.GetShared(`${MsUrl.sor}/load/LANG`, { active: true });
|
|
224
|
+
*/
|
|
225
|
+
static GetShared(url: string, params: HttpParams = {}): Promise<unknown> {
|
|
226
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
227
|
+
const key = buildSharedGetKey(fullUrl, params);
|
|
228
|
+
const cache = readSharedGetCache();
|
|
229
|
+
const cached = cache.get(key);
|
|
230
|
+
if (cached) {
|
|
231
|
+
return cached;
|
|
232
|
+
}
|
|
233
|
+
const request = AppHttp.Get(url, params);
|
|
234
|
+
cache.set(key, request);
|
|
235
|
+
const forget = forgetSharedGet.bind(null, key);
|
|
236
|
+
request.catch(forget);
|
|
237
|
+
return request;
|
|
238
|
+
}
|
|
239
|
+
|
|
146
240
|
/** POST a JSON body. */
|
|
147
241
|
static async Post(url: string, body: unknown, customHeaders: HttpHeaders = {}): Promise<unknown> {
|
|
148
242
|
const baseHeaders = AppHttp.Headers();
|
|
149
243
|
const headers = { ...baseHeaders, ...customHeaders };
|
|
150
|
-
const
|
|
244
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
245
|
+
const request = Http.Post(fullUrl, body, headers);
|
|
151
246
|
return AppHttp.withProgress(request);
|
|
152
247
|
}
|
|
153
248
|
|
|
154
249
|
/** PUT a JSON body to url/id. */
|
|
155
250
|
static async Put(url: string, id: string, body: unknown): Promise<unknown> {
|
|
156
251
|
const headers = AppHttp.Headers();
|
|
157
|
-
const
|
|
252
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
253
|
+
const itemUrl = `${fullUrl}/${id}`;
|
|
158
254
|
const request = Http.Put(itemUrl, body, headers);
|
|
159
255
|
return AppHttp.withProgress(request);
|
|
160
256
|
}
|
|
@@ -162,7 +258,8 @@ export class AppHttp {
|
|
|
162
258
|
/** DELETE url/id. */
|
|
163
259
|
static async Delete(url: string, id: string, params: HttpParams = {}): Promise<unknown> {
|
|
164
260
|
const headers = AppHttp.Headers();
|
|
165
|
-
const
|
|
261
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
262
|
+
const itemUrl = `${fullUrl}/${id}`;
|
|
166
263
|
const request = Http.Delete(itemUrl, params, headers);
|
|
167
264
|
return AppHttp.withProgress(request);
|
|
168
265
|
}
|
|
@@ -172,7 +269,8 @@ export class AppHttp {
|
|
|
172
269
|
const headers = AppHttp.Headers();
|
|
173
270
|
const bodyText = JSON.stringify(body ?? {});
|
|
174
271
|
const requestInit = { method: "PATCH", headers: headers, body: bodyText };
|
|
175
|
-
const
|
|
272
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
273
|
+
const request = fetch(fullUrl, requestInit);
|
|
176
274
|
const response = await AppHttp.withProgress(request);
|
|
177
275
|
if (!response.ok) {
|
|
178
276
|
const errorText = await response.text();
|
|
@@ -184,16 +282,17 @@ export class AppHttp {
|
|
|
184
282
|
/** Option lists from the backend loader: GET `${config.http.loadUrl}/${id}`. */
|
|
185
283
|
static async Load(id: string, params: HttpParams = {}): Promise<unknown> {
|
|
186
284
|
const savedConfig = readSavedConfig();
|
|
187
|
-
const
|
|
188
|
-
const
|
|
189
|
-
const
|
|
190
|
-
return AppHttp.Get(
|
|
285
|
+
const loadUrl = savedConfig.http?.loadUrl ?? "/load";
|
|
286
|
+
const fullLoadUrl = AppHttp.FullUrl(loadUrl);
|
|
287
|
+
const itemUrl = `${fullLoadUrl}/${id}`;
|
|
288
|
+
return AppHttp.Get(itemUrl, params);
|
|
191
289
|
}
|
|
192
290
|
|
|
193
291
|
/** PUT a file to a signed upload URL (plain PUT, no extra headers, so bucket CORS accepts it). */
|
|
194
292
|
static async CloudUpload(uploadUrl: string, file: File): Promise<Response> {
|
|
195
293
|
const requestInit = { method: "PUT", body: file };
|
|
196
|
-
const
|
|
294
|
+
const fullUrl = AppHttp.FullUrl(uploadUrl);
|
|
295
|
+
const request = fetch(fullUrl, requestInit);
|
|
197
296
|
const response = await AppHttp.withProgress(request);
|
|
198
297
|
if (!response.ok) {
|
|
199
298
|
throw new Error(`Upload failed: ${response.status}`);
|
|
@@ -206,10 +305,25 @@ export class AppHttp {
|
|
|
206
305
|
return AppHttp.CloudUpload(uploadUrl, file);
|
|
207
306
|
}
|
|
208
307
|
|
|
308
|
+
/**
|
|
309
|
+
* GET that returns the raw body as a Blob (images, video, audio, any binary file).
|
|
310
|
+
* The URL rule of every method applies (a path gets the base URL), and the same headers are sent (token,
|
|
311
|
+
* x-session-id, x-branch-id, http.headers, setHeader values; no Content-Type). No progress bar: pages often load
|
|
312
|
+
* many media files at once.
|
|
313
|
+
* const blob = await AppHttp.Media(`${MsUrl.utils}/files/${id}`);
|
|
314
|
+
* const objectUrl = URL.createObjectURL(blob); // <img src={objectUrl}>; URL.revokeObjectURL(objectUrl) later
|
|
315
|
+
*/
|
|
316
|
+
static async Media(url: string, params?: HttpParams): Promise<Blob> {
|
|
317
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
318
|
+
const headers = AppHttp.RawHeaders();
|
|
319
|
+
return Http.FileDownload(fullUrl, params, headers);
|
|
320
|
+
}
|
|
321
|
+
|
|
209
322
|
/** Downloads a file and saves it in the browser. */
|
|
210
323
|
static async DownloadFile(url: string, params: HttpParams = {}, fileName?: string): Promise<void> {
|
|
211
324
|
const headers = AppHttp.RawHeaders();
|
|
212
|
-
const
|
|
325
|
+
const fullUrl = AppHttp.FullUrl(url);
|
|
326
|
+
const request = Http.FileDownload(fullUrl, params, headers);
|
|
213
327
|
const blob = await AppHttp.withProgress(request);
|
|
214
328
|
const downloadUrl = window.URL.createObjectURL(blob);
|
|
215
329
|
const link = document.createElement("a");
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// content-util.ts
|
|
2
|
+
// Reads the content out of a content-load reply, for example sor /content-load?query=locales.en-US:
|
|
3
|
+
//
|
|
4
|
+
// readContent(reply, "locales.en-US")
|
|
5
|
+
// { "data": { "locales": { "en-US": {...} } } } -> {...}
|
|
6
|
+
// { "data": {...} } -> {...}
|
|
7
|
+
// {...} -> {...}
|
|
8
|
+
//
|
|
9
|
+
// "data" is taken off first; then each part of the query ("locales", "en-US") is followed while the object has it.
|
|
10
|
+
// So the reply may be the content itself or wrapped in the query path — both give the same result.
|
|
11
|
+
|
|
12
|
+
/** Separator of the query parts: "locales.en-US" -> ["locales", "en-US"]. */
|
|
13
|
+
const QUERY_SEPARATOR = ".";
|
|
14
|
+
|
|
15
|
+
/** true for a plain object (not null, not a list). */
|
|
16
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
17
|
+
const isObject = typeof value === "object" && value !== null;
|
|
18
|
+
return isObject && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The reply without its "data" wrapper (when it has one). */
|
|
22
|
+
function unwrapData(reply: unknown): unknown {
|
|
23
|
+
if (!isPlainObject(reply)) {
|
|
24
|
+
return reply;
|
|
25
|
+
}
|
|
26
|
+
const hasData = "data" in reply;
|
|
27
|
+
return hasData ? reply.data : reply;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Follows one query part when the value has it; otherwise the value stays. */
|
|
31
|
+
function followQueryPart(value: unknown, part: string): unknown {
|
|
32
|
+
if (isPlainObject(value) && part in value) {
|
|
33
|
+
return value[part];
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The content of a content-load reply for a query ("theme", "locales", "locales.en-US"):
|
|
40
|
+
* takes off "data", then follows the query parts the reply has.
|
|
41
|
+
*/
|
|
42
|
+
export function readContent(reply: unknown, query: string): unknown {
|
|
43
|
+
let content = unwrapData(reply);
|
|
44
|
+
const parts = query.split(QUERY_SEPARATOR);
|
|
45
|
+
for (const part of parts) {
|
|
46
|
+
content = followQueryPart(content, part);
|
|
47
|
+
}
|
|
48
|
+
return content;
|
|
49
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// error-util.ts
|
|
2
|
+
// The text to show for a failed request. AppHttp (dff-util Http) throws
|
|
3
|
+
// { url, requestId, body?, error: { ...backend reply, message, main_message } }
|
|
4
|
+
// and backends answer in different shapes, for example:
|
|
5
|
+
// { "code": "unknown", "message": "Invalid credentials, please try again", "internal_message": null }
|
|
6
|
+
// { "error": { "message": "Invalid credentials, please try again" } }
|
|
7
|
+
// { "error": "Invalid credentials, please try again" }
|
|
8
|
+
//
|
|
9
|
+
// ShowToast(readErrorMessage(error, t("login.failed")), "error"); // (two steps in real code)
|
|
10
|
+
//
|
|
11
|
+
// The backend's own message wins; internal_message is never shown (main_message / message are the user texts).
|
|
12
|
+
|
|
13
|
+
import { ConstKeys } from "dff-util";
|
|
14
|
+
|
|
15
|
+
/** Keys looked at in each object, in this order. "error" goes one level deeper. */
|
|
16
|
+
const MESSAGE_KEYS = ["main_message", "message", "error_description", "detail"];
|
|
17
|
+
|
|
18
|
+
/** How deep "error" objects are followed: { error: { error: { message } } }. */
|
|
19
|
+
const MAX_DEPTH = 4;
|
|
20
|
+
|
|
21
|
+
/** true for a plain object (not null, not a list). */
|
|
22
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
23
|
+
const isObject = typeof value === "object" && value !== null;
|
|
24
|
+
return isObject && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A text worth showing: not empty and not dff-util's placeholder key (INVALID_DATA). */
|
|
28
|
+
function isShowableText(value: unknown): value is string {
|
|
29
|
+
if (typeof value !== "string") {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const text = value.trim();
|
|
33
|
+
return text.length > 0 && text !== ConstKeys.INVALID_DATA;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The message inside one value: its "error" first (deepest wins), then main_message / message / ... */
|
|
37
|
+
function findMessage(value: unknown, depth: number): string {
|
|
38
|
+
if (isShowableText(value)) {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
if (!isPlainObject(value) || depth > MAX_DEPTH) {
|
|
42
|
+
return "";
|
|
43
|
+
}
|
|
44
|
+
const nestedMessage = findMessage(value.error, depth + 1);
|
|
45
|
+
if (nestedMessage) {
|
|
46
|
+
return nestedMessage;
|
|
47
|
+
}
|
|
48
|
+
for (const key of MESSAGE_KEYS) {
|
|
49
|
+
const text = value[key];
|
|
50
|
+
if (isShowableText(text)) {
|
|
51
|
+
return text;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return "";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The backend's message of a failed request (or an Error's message); the fallback when there is none. */
|
|
58
|
+
export function readErrorMessage(error: unknown, fallback: string = ""): string {
|
|
59
|
+
if (error instanceof Error && isShowableText(error.message)) {
|
|
60
|
+
return error.message;
|
|
61
|
+
}
|
|
62
|
+
const message = findMessage(error, 0);
|
|
63
|
+
return message || fallback;
|
|
64
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// const languages = toLanguageOptions(langResponse); // { data: [...], meta } or the plain list
|
|
5
5
|
// const defaultCode = readDefaultLanguageCode(langResponse);
|
|
6
6
|
//
|
|
7
|
-
// Usually you do not call this yourself:
|
|
7
|
+
// Usually you do not call this yourself: config.loadSupportLanguages or setLanguages() do it.
|
|
8
8
|
|
|
9
9
|
import { ConstValue } from "dff-util";
|
|
10
10
|
import type { LangSource, LanguageRowType } from "../interfaces/app-types.ts";
|
|
@@ -22,9 +22,9 @@ export function readLanguageRows(source: LangSource | null | undefined): Languag
|
|
|
22
22
|
return source.data ?? [];
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
/** The language code of a row ("en-US"), from code, langCode, languageCode or id. */
|
|
25
|
+
/** The language code of a row ("en-US"), from code, langCode, languageCode, key or id. */
|
|
26
26
|
export function readLanguageRowCode(row: LanguageRowType): string {
|
|
27
|
-
return row.code ?? row.langCode ?? row.languageCode ?? row.id ?? ConstValue.EMPTY;
|
|
27
|
+
return row.code ?? row.langCode ?? row.languageCode ?? row.key ?? row.id ?? ConstValue.EMPTY;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/** "rtl" / "ltr" from direction or dir. undefined lets the shell decide (rtl for ar, he, fa, ur). */
|
|
@@ -40,10 +40,10 @@ export function readLanguageRowDirection(row: LanguageRowType): Direction | unde
|
|
|
40
40
|
return ConstValue.LTR as Direction;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
/** Name shown in the switcher: label, name, nameLang in that language, or the code. */
|
|
43
|
+
/** Name shown in the switcher: label, name, nameLang in that language, locale (its own name), or the code. */
|
|
44
44
|
export function readLanguageRowLabel(row: LanguageRowType, languageCode: string): string {
|
|
45
45
|
const ownName = row.nameLang?.[languageCode];
|
|
46
|
-
return row.label ?? row.name ?? ownName ?? languageCode;
|
|
46
|
+
return row.label ?? row.name ?? ownName ?? row.locale ?? languageCode;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/** Sort compare: lower priority first. Same priority keeps the backend order. */
|
|
@@ -53,13 +53,13 @@ export function compareLanguagePriority(first: LanguageRowType, second: Language
|
|
|
53
53
|
return firstPriority - secondPriority;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
/** Rows that can be shown: a code is present and
|
|
56
|
+
/** Rows that can be shown: a code is present, active is not false and disabled is not true. Sorted by priority. */
|
|
57
57
|
export function readActiveLanguageRows(source: LangSource | null | undefined): LanguageRowType[] {
|
|
58
58
|
const rows = readLanguageRows(source);
|
|
59
59
|
const activeRows: LanguageRowType[] = [];
|
|
60
60
|
for (const row of rows) {
|
|
61
61
|
const languageCode = readLanguageRowCode(row);
|
|
62
|
-
const isActive = row.active !== false;
|
|
62
|
+
const isActive = row.active !== false && row.disabled !== true;
|
|
63
63
|
if (languageCode.length > 0 && isActive) {
|
|
64
64
|
activeRows.push(row);
|
|
65
65
|
}
|
package/src/utils/menu-util.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// const navItems = toNavigation(menuResponse); // { data: [...], meta } or the plain list
|
|
5
5
|
// setNavigation(navItems);
|
|
6
6
|
//
|
|
7
|
-
// Usually you do not call this yourself:
|
|
7
|
+
// Usually you do not call this yourself: config.loadMenuNavigation or setMenu(menuResponse) do it.
|
|
8
8
|
//
|
|
9
9
|
// What it does:
|
|
10
10
|
// - accepts the full reply { data, meta } or only the list
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// signal-source.ts
|
|
2
|
+
// Config values that can be a signal (loadLocale, loadThemes, loadSupportLanguages, loadBranches, loadMenuNavigation).
|
|
3
|
+
// A signal is recognised by its shape (value, peek, subscribe), so a signal from another copy of the signals package
|
|
4
|
+
// (each microfrontend bundles its own) works too.
|
|
5
|
+
|
|
6
|
+
import type { ValueSignal } from "../interfaces/ui-config.ts";
|
|
7
|
+
|
|
8
|
+
/** true when the value is a signal (or a computed). */
|
|
9
|
+
export function isSignalSource(source: unknown): source is ValueSignal<unknown> {
|
|
10
|
+
if (typeof source !== "object" || source === null) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const candidate = source as Partial<ValueSignal<unknown>>;
|
|
14
|
+
return typeof candidate.peek === "function" && typeof candidate.subscribe === "function" && "value" in candidate;
|
|
15
|
+
}
|
|
@@ -73,6 +73,12 @@ export const LOCAL_STORAGE_KEYS: string[] = [LANG, THEME, LAYOUT_CHOICE, MENU_MO
|
|
|
73
73
|
*/
|
|
74
74
|
export const PREFERENCE_EVENT = "shared-lib-web:preference";
|
|
75
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Window event fired by startSession / endSession (detail = { isSignedIn, copyId }). Every OTHER copy of the library
|
|
78
|
+
* on the page applies it (auth-service.ts) — so a sign in inside an auth microfrontend signs the shell in too.
|
|
79
|
+
*/
|
|
80
|
+
export const SESSION_EVENT = "shared-lib-web:session";
|
|
81
|
+
|
|
76
82
|
/** AppStorage prefix when config.appNameKey is missing. */
|
|
77
83
|
export const DEFAULT_APP_STORAGE_PREFIX = "UI";
|
|
78
84
|
|