shared-lib-web 0.0.2 → 0.0.4

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.
Files changed (206) hide show
  1. package/README.md +192 -528
  2. package/dist/components/app-brand.d.ts.map +1 -1
  3. package/dist/components/app-brand.js +9 -5
  4. package/dist/components/app-brand.js.map +1 -1
  5. package/dist/components/coming-soon.d.ts.map +1 -1
  6. package/dist/components/coming-soon.js +3 -2
  7. package/dist/components/coming-soon.js.map +1 -1
  8. package/dist/components/language-switcher.d.ts +5 -1
  9. package/dist/components/language-switcher.d.ts.map +1 -1
  10. package/dist/components/language-switcher.js +20 -7
  11. package/dist/components/language-switcher.js.map +1 -1
  12. package/dist/components/otp-modal.d.ts +5 -0
  13. package/dist/components/otp-modal.d.ts.map +1 -0
  14. package/dist/components/otp-modal.js +77 -0
  15. package/dist/components/otp-modal.js.map +1 -0
  16. package/dist/components/settings-menu.d.ts +4 -0
  17. package/dist/components/settings-menu.d.ts.map +1 -1
  18. package/dist/components/settings-menu.js +15 -2
  19. package/dist/components/settings-menu.js.map +1 -1
  20. package/dist/context/use-ui.d.ts.map +1 -1
  21. package/dist/context/use-ui.js +4 -3
  22. package/dist/context/use-ui.js.map +1 -1
  23. package/dist/i18n/browser-language.d.ts +9 -0
  24. package/dist/i18n/browser-language.d.ts.map +1 -0
  25. package/dist/i18n/browser-language.js +89 -0
  26. package/dist/i18n/browser-language.js.map +1 -0
  27. package/dist/i18n/default-messages.d.ts.map +1 -1
  28. package/dist/i18n/default-messages.js +15 -0
  29. package/dist/i18n/default-messages.js.map +1 -1
  30. package/dist/i18n/locale-groups.d.ts +18 -0
  31. package/dist/i18n/locale-groups.d.ts.map +1 -0
  32. package/dist/i18n/locale-groups.js +74 -0
  33. package/dist/i18n/locale-groups.js.map +1 -0
  34. package/dist/i18n/locale-service.d.ts +22 -4
  35. package/dist/i18n/locale-service.d.ts.map +1 -1
  36. package/dist/i18n/locale-service.js +95 -27
  37. package/dist/i18n/locale-service.js.map +1 -1
  38. package/dist/i18n/locale-signal.d.ts +18 -0
  39. package/dist/i18n/locale-signal.d.ts.map +1 -0
  40. package/dist/i18n/locale-signal.js +142 -0
  41. package/dist/i18n/locale-signal.js.map +1 -0
  42. package/dist/i18n/translator.d.ts +8 -1
  43. package/dist/i18n/translator.d.ts.map +1 -1
  44. package/dist/i18n/translator.js +36 -12
  45. package/dist/i18n/translator.js.map +1 -1
  46. package/dist/index.d.ts +24 -11
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +21 -8
  49. package/dist/index.js.map +1 -1
  50. package/dist/interfaces/app-types.d.ts +6 -0
  51. package/dist/interfaces/app-types.d.ts.map +1 -1
  52. package/dist/interfaces/ui-config.d.ts +184 -15
  53. package/dist/interfaces/ui-config.d.ts.map +1 -1
  54. package/dist/interfaces/ui-context.d.ts +1 -1
  55. package/dist/interfaces/ui-context.d.ts.map +1 -1
  56. package/dist/layouts/auth-layout.js +1 -1
  57. package/dist/layouts/auth-layout.js.map +1 -1
  58. package/dist/providers/provider-effects.d.ts +2 -2
  59. package/dist/providers/provider-effects.d.ts.map +1 -1
  60. package/dist/providers/provider-effects.js +4 -1
  61. package/dist/providers/provider-effects.js.map +1 -1
  62. package/dist/providers/register-config.d.ts.map +1 -1
  63. package/dist/providers/register-config.js +2 -0
  64. package/dist/providers/register-config.js.map +1 -1
  65. package/dist/providers/shared-web-ui-provider.d.ts +7 -19
  66. package/dist/providers/shared-web-ui-provider.d.ts.map +1 -1
  67. package/dist/providers/shared-web-ui-provider.js +21 -22
  68. package/dist/providers/shared-web-ui-provider.js.map +1 -1
  69. package/dist/query/query-param.d.ts +4 -1
  70. package/dist/query/query-param.d.ts.map +1 -1
  71. package/dist/query/query-param.js +6 -3
  72. package/dist/query/query-param.js.map +1 -1
  73. package/dist/state/api-loader.d.ts +19 -0
  74. package/dist/state/api-loader.d.ts.map +1 -0
  75. package/dist/state/api-loader.js +187 -0
  76. package/dist/state/api-loader.js.map +1 -0
  77. package/dist/state/auth-service.d.ts +9 -0
  78. package/dist/state/auth-service.d.ts.map +1 -1
  79. package/dist/state/auth-service.js +70 -3
  80. package/dist/state/auth-service.js.map +1 -1
  81. package/dist/state/branch-service.d.ts +1 -1
  82. package/dist/state/branch-service.js +3 -3
  83. package/dist/state/branch-service.js.map +1 -1
  84. package/dist/state/company-service.d.ts +16 -0
  85. package/dist/state/company-service.d.ts.map +1 -0
  86. package/dist/state/company-service.js +97 -0
  87. package/dist/state/company-service.js.map +1 -0
  88. package/dist/state/language-service.d.ts +5 -2
  89. package/dist/state/language-service.d.ts.map +1 -1
  90. package/dist/state/language-service.js +12 -7
  91. package/dist/state/language-service.js.map +1 -1
  92. package/dist/state/layout-choice-service.d.ts +10 -0
  93. package/dist/state/layout-choice-service.d.ts.map +1 -0
  94. package/dist/state/layout-choice-service.js +34 -0
  95. package/dist/state/layout-choice-service.js.map +1 -0
  96. package/dist/state/menu-service.d.ts +1 -1
  97. package/dist/state/menu-service.js +2 -2
  98. package/dist/state/menu-service.js.map +1 -1
  99. package/dist/state/otp-service.d.ts +69 -0
  100. package/dist/state/otp-service.d.ts.map +1 -0
  101. package/dist/state/otp-service.js +212 -0
  102. package/dist/state/otp-service.js.map +1 -0
  103. package/dist/state/preference-sync.d.ts +1 -1
  104. package/dist/state/preference-sync.d.ts.map +1 -1
  105. package/dist/state/preference-sync.js +11 -3
  106. package/dist/state/preference-sync.js.map +1 -1
  107. package/dist/state/signals.d.ts +9 -1
  108. package/dist/state/signals.d.ts.map +1 -1
  109. package/dist/state/signals.js +28 -4
  110. package/dist/state/signals.js.map +1 -1
  111. package/dist/theme/custom-themes.d.ts +28 -0
  112. package/dist/theme/custom-themes.d.ts.map +1 -0
  113. package/dist/theme/custom-themes.js +218 -0
  114. package/dist/theme/custom-themes.js.map +1 -0
  115. package/dist/theme/dark-themes.d.ts +9 -1
  116. package/dist/theme/dark-themes.d.ts.map +1 -1
  117. package/dist/theme/dark-themes.js +74 -2
  118. package/dist/theme/dark-themes.js.map +1 -1
  119. package/dist/theme/logo.d.ts +6 -3
  120. package/dist/theme/logo.d.ts.map +1 -1
  121. package/dist/theme/logo.js +17 -3
  122. package/dist/theme/logo.js.map +1 -1
  123. package/dist/theme/theme-service.d.ts +14 -3
  124. package/dist/theme/theme-service.d.ts.map +1 -1
  125. package/dist/theme/theme-service.js +64 -10
  126. package/dist/theme/theme-service.js.map +1 -1
  127. package/dist/types/field-wrapper.d.ts +2 -0
  128. package/dist/types/field-wrapper.d.ts.map +1 -1
  129. package/dist/types/field-wrapper.js +3 -2
  130. package/dist/types/field-wrapper.js.map +1 -1
  131. package/dist/types/type.otp.d.ts +2 -0
  132. package/dist/types/type.otp.d.ts.map +1 -1
  133. package/dist/types/type.otp.js +2 -2
  134. package/dist/types/type.otp.js.map +1 -1
  135. package/dist/utils/app-http.d.ts +41 -1
  136. package/dist/utils/app-http.d.ts.map +1 -1
  137. package/dist/utils/app-http.js +188 -12
  138. package/dist/utils/app-http.js.map +1 -1
  139. package/dist/utils/content-util.d.ts +6 -0
  140. package/dist/utils/content-util.d.ts.map +1 -0
  141. package/dist/utils/content-util.js +45 -0
  142. package/dist/utils/content-util.js.map +1 -0
  143. package/dist/utils/error-util.d.ts +3 -0
  144. package/dist/utils/error-util.d.ts.map +1 -0
  145. package/dist/utils/error-util.js +58 -0
  146. package/dist/utils/error-util.js.map +1 -0
  147. package/dist/utils/language-util.d.ts +3 -3
  148. package/dist/utils/language-util.d.ts.map +1 -1
  149. package/dist/utils/language-util.js +7 -7
  150. package/dist/utils/language-util.js.map +1 -1
  151. package/dist/utils/menu-util.js +1 -1
  152. package/dist/utils/menu-util.js.map +1 -1
  153. package/dist/utils/signal-source.d.ts +4 -0
  154. package/dist/utils/signal-source.d.ts.map +1 -0
  155. package/dist/utils/signal-source.js +13 -0
  156. package/dist/utils/signal-source.js.map +1 -0
  157. package/dist/utils/storage-keys.d.ts +5 -0
  158. package/dist/utils/storage-keys.d.ts.map +1 -1
  159. package/dist/utils/storage-keys.js +5 -0
  160. package/dist/utils/storage-keys.js.map +1 -1
  161. package/package.json +3 -3
  162. package/src/components/app-brand.tsx +9 -5
  163. package/src/components/coming-soon.tsx +3 -2
  164. package/src/components/language-switcher.tsx +26 -7
  165. package/src/components/otp-modal.tsx +154 -0
  166. package/src/components/settings-menu.tsx +28 -2
  167. package/src/context/use-ui.ts +4 -3
  168. package/src/i18n/browser-language.ts +98 -0
  169. package/src/i18n/default-messages.ts +15 -0
  170. package/src/i18n/locale-groups.ts +87 -0
  171. package/src/i18n/locale-service.ts +99 -27
  172. package/src/i18n/locale-signal.ts +173 -0
  173. package/src/i18n/translator.ts +38 -12
  174. package/src/index.ts +102 -4
  175. package/src/interfaces/app-types.ts +6 -0
  176. package/src/interfaces/ui-config.ts +191 -15
  177. package/src/interfaces/ui-context.ts +1 -1
  178. package/src/layouts/auth-layout.tsx +1 -1
  179. package/src/providers/provider-effects.ts +5 -2
  180. package/src/providers/register-config.ts +2 -0
  181. package/src/providers/shared-web-ui-provider.tsx +26 -43
  182. package/src/query/query-param.ts +6 -2
  183. package/src/state/api-loader.ts +205 -0
  184. package/src/state/auth-service.ts +84 -3
  185. package/src/state/branch-service.ts +3 -3
  186. package/src/state/company-service.ts +107 -0
  187. package/src/state/language-service.ts +12 -7
  188. package/src/state/layout-choice-service.ts +40 -0
  189. package/src/state/menu-service.ts +2 -2
  190. package/src/state/otp-service.ts +283 -0
  191. package/src/state/preference-sync.ts +11 -3
  192. package/src/state/signals.ts +36 -4
  193. package/src/theme/custom-themes.ts +236 -0
  194. package/src/theme/dark-themes.ts +80 -3
  195. package/src/theme/logo.ts +19 -4
  196. package/src/theme/org-themes.json +70 -0
  197. package/src/theme/theme-service.ts +70 -10
  198. package/src/types/field-wrapper.tsx +5 -2
  199. package/src/types/type.otp.tsx +11 -2
  200. package/src/utils/app-http.ts +205 -12
  201. package/src/utils/content-util.ts +49 -0
  202. package/src/utils/error-util.ts +64 -0
  203. package/src/utils/language-util.ts +7 -7
  204. package/src/utils/menu-util.ts +1 -1
  205. package/src/utils/signal-source.ts +15 -0
  206. package/src/utils/storage-keys.ts +6 -0
@@ -13,19 +13,28 @@
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);
19
22
  // const saved = await AppHttp.Post(saveUrl, body);
20
23
  // AppHttp.setHeader("x-custom", value);
21
24
  //
25
+ // // JWT regenerate: set http.regenerateUrl. Before every request: JwtValid(token) false -> GET regenerateUrl once
26
+ // // (shared page-wide in readSharedGetCache) -> new token ({ token } or { data: { token } }) saved in sessionStorage;
27
+ // // an error or no token -> endSession() (log out, goes to http.logoutUrl).
28
+ // http: { baseUrl, logoutUrl: "/login", regenerateUrl: `${MsUrl.utils}/jwt-regenerate` }
29
+ //
22
30
  // // file upload to a signed URL
23
31
  // const signed = await AppHttp.Post(`${MsUrl.utils}/s3/upload-url`, { fileName: file.name });
24
32
  // const uploaded = await uploadToSignedUrl(file, signed.data.upload, signed.data.download);
25
33
  // // uploaded = { filename, mimetype, url }
26
34
 
27
- import { Http, type FileType } from "dff-util";
35
+ import { Http, JwtValid, type FileType } from "dff-util";
28
36
  import type { UiConfig } from "../interfaces/ui-config.ts";
37
+ import { endSession, refreshSessionToken } from "../state/auth-service.ts";
29
38
  import { ShowProgress } from "../state/progress-service.ts";
30
39
  import { readAppConfig } from "./app-config.ts";
31
40
  import { AppStorage } from "./app-storage.ts";
@@ -37,6 +46,7 @@ export type HttpParams = Record<string, string | number | boolean | null | undef
37
46
  export type HttpHeaders = Record<string, string>;
38
47
 
39
48
  const CONFIG_MISSING = "shared-lib-web: config is missing. Render <SharedWebUiProvider config={config}> or call setAppConfig(config) first.";
49
+ const REGENERATE_URL_MISSING = "shared-lib-web: the token is not valid and config.http.regenerateUrl is empty.";
40
50
  const BASE_URL_MISSING = "shared-lib-web: no API base URL. Set config.http.baseUrl or window.env.REACT_APP_<APPNAMEKEY>_API_BASE_URL.";
41
51
 
42
52
  /** window.env name (without prefix) of the API base URL. */
@@ -61,6 +71,50 @@ function isProgressEnabled(): boolean {
61
71
  }
62
72
 
63
73
  /** JWT from AppStorage (config.http.tokenKey, default TOKEN, sessionStorage). "" when missing. */
74
+ /**
75
+ * Requests shared by every copy of the library on the page (a shell and its microfrontends each bundle their own
76
+ * copy): the key is global (Symbol.for), so the cache lives on window once for all copies.
77
+ */
78
+ const SHARED_GET_KEY = Symbol.for("shared-lib-web:shared-get");
79
+
80
+ type SharedGetCache = Map<string, Promise<unknown>>;
81
+
82
+ /** The page-wide cache of GetShared (created by the first copy that needs it). */
83
+ function readSharedGetCache(): SharedGetCache {
84
+ const holder = globalThis as unknown as Record<symbol, SharedGetCache | undefined>;
85
+ let cache = holder[SHARED_GET_KEY];
86
+ if (!cache) {
87
+ cache = new Map();
88
+ holder[SHARED_GET_KEY] = cache;
89
+ }
90
+ return cache;
91
+ }
92
+
93
+ /** Cache key of a request: the full URL + the params sorted by name. */
94
+ export function buildSharedGetKey(fullUrl: string, params: HttpParams): string {
95
+ const names = Object.keys(params);
96
+ names.sort();
97
+ const parts: string[] = [];
98
+ for (const name of names) {
99
+ const value = params[name];
100
+ parts.push(`${name}=${String(value)}`);
101
+ }
102
+ const paramText = parts.join("&");
103
+ return `${fullUrl}?${paramText}`;
104
+ }
105
+
106
+ /** A failed shared request is removed, so the next call tries again. */
107
+ function forgetSharedGet(key: string): void {
108
+ const cache = readSharedGetCache();
109
+ cache.delete(key);
110
+ }
111
+
112
+ /** Empties the shared request cache (for example after the texts or themes changed on the server, or in tests). */
113
+ export function clearSharedGetCache(): void {
114
+ const cache = readSharedGetCache();
115
+ cache.clear();
116
+ }
117
+
64
118
  export function readJwtToken(): string {
65
119
  const config = readAppConfig();
66
120
  const tokenKey = config?.http?.tokenKey ?? TOKEN;
@@ -79,6 +133,18 @@ export function toAbsoluteUrl(url: string): string {
79
133
  return fullUrl.replace(/\/+$/, "");
80
134
  }
81
135
 
136
+ /** true for "http://..." and "https://..." (any case). */
137
+ export function isHttpUrl(url: string): boolean {
138
+ return /^https?:\/\//i.test(url);
139
+ }
140
+
141
+ /** Joins a base URL and a path with exactly one "/" between them. */
142
+ export function joinUrl(baseUrl: string, path: string): string {
143
+ const trimmedBase = baseUrl.replace(/\/+$/, "");
144
+ const trimmedPath = path.replace(/^\/+/, "");
145
+ return trimmedPath.length > 0 ? `${trimmedBase}/${trimmedPath}` : trimmedBase;
146
+ }
147
+
82
148
  /** Turns a Response body into JSON or text. */
83
149
  async function readResponseBody(response: Response): Promise<unknown> {
84
150
  const contentType = response.headers.get("content-type") ?? "";
@@ -88,6 +154,71 @@ async function readResponseBody(response: Response): Promise<unknown> {
88
154
  return response.text();
89
155
  }
90
156
 
157
+ /** config.http.regenerateUrl, or "" when not set. */
158
+ export function readRegenerateUrl(): string {
159
+ const config = readAppConfig();
160
+ return config?.http?.regenerateUrl ?? "";
161
+ }
162
+
163
+ /**
164
+ * GET http.regenerateUrl. Returns the new token from { token } or { data: { token } }.
165
+ * Throws when regenerateUrl is empty or the reply has no token.
166
+ */
167
+ export async function jwtRegenerateCall(): Promise<string> {
168
+ const regenerateUrl = readRegenerateUrl();
169
+ if (regenerateUrl.length === 0) {
170
+ throw new Error(REGENERATE_URL_MISSING);
171
+ }
172
+ const fullUrl = AppHttp.FullUrl(regenerateUrl);
173
+ const headers = AppHttp.Headers();
174
+ const reply = (await Http.Get(fullUrl, {}, headers)) as { token?: string; data?: { token?: string } } | undefined;
175
+ const token = reply?.token ?? reply?.data?.token;
176
+ if (!token) {
177
+ throw new Error("jwt-regenerate: no token");
178
+ }
179
+ refreshSessionToken(token);
180
+ return token;
181
+ }
182
+
183
+ /** The regenerate failed: log out (endSession goes to http.logoutUrl). */
184
+ function handleRegenerateError(): void {
185
+ endSession();
186
+ }
187
+
188
+ /**
189
+ * Runs the regenerate once: the running call is kept in readSharedGetCache under the config regenerateUrl,
190
+ * so every request (shell and MFEs) waits for the same one.
191
+ */
192
+ function regenerateOnce(regenerateUrl: string): Promise<unknown> {
193
+ const cache = readSharedGetCache();
194
+ const running = cache.get(regenerateUrl);
195
+ if (running) {
196
+ return running;
197
+ }
198
+ const request = jwtRegenerateCall();
199
+ const handled = request.catch(handleRegenerateError);
200
+ const forget = forgetSharedGet.bind(null, regenerateUrl);
201
+ const regenerate = handled.finally(forget);
202
+ cache.set(regenerateUrl, regenerate);
203
+ return regenerate;
204
+ }
205
+
206
+ /**
207
+ * Before a request: no token or JwtValid(token) -> nothing. Otherwise the token is regenerated (once);
208
+ * an empty config.http.regenerateUrl throws only then (at the time of the call).
209
+ */
210
+ export async function ensureFreshToken(): Promise<void> {
211
+ const token = readJwtToken();
212
+ if (token.length === 0 || JwtValid(token)) {
213
+ return;
214
+ }
215
+ const regenerateUrl = readRegenerateUrl();
216
+ if (regenerateUrl.length === 0) {
217
+ throw new Error(REGENERATE_URL_MISSING);
218
+ }
219
+ await regenerateOnce(regenerateUrl);
220
+ }
221
+
91
222
  export class AppHttp {
92
223
  /**
93
224
  * API base URL: window.env REACT_APP_<APPNAMEKEY>_API_BASE_URL first, then config.http.baseUrl.
@@ -106,6 +237,19 @@ export class AppHttp {
106
237
  return absoluteBaseUrl;
107
238
  }
108
239
 
240
+ /**
241
+ * The URL a request goes to: a URL starting with http:// or https:// stays as it is,
242
+ * anything else is appended to BaseUrl() ("/profile/search" or "profile/search" -> "<baseUrl>/profile/search").
243
+ */
244
+ static FullUrl(url: string): string {
245
+ const isAbsolute = isHttpUrl(url);
246
+ if (isAbsolute) {
247
+ return url;
248
+ }
249
+ const baseUrl = AppHttp.BaseUrl();
250
+ return joinUrl(baseUrl, url);
251
+ }
252
+
109
253
  /** Adds (or with "" removes) a header sent with every request, for example the selected branch. */
110
254
  static setHeader(headerName: string, value: string): void {
111
255
  if (value.length === 0) {
@@ -137,42 +281,73 @@ export class AppHttp {
137
281
 
138
282
  /** GET with query params. */
139
283
  static async Get(url: string, params: HttpParams = {}, customHeaders: HttpHeaders = {}): Promise<unknown> {
284
+ await ensureFreshToken();
140
285
  const baseHeaders = AppHttp.Headers();
141
286
  const headers = { ...baseHeaders, ...customHeaders };
142
- const request = Http.Get(url, params, headers);
287
+ const fullUrl = AppHttp.FullUrl(url);
288
+ const request = Http.Get(fullUrl, params, headers);
143
289
  return AppHttp.withProgress(request);
144
290
  }
145
291
 
292
+ /**
293
+ * GET once per page, shared by every copy of the library (shell + microfrontends): the same URL + params while a
294
+ * request is running or after it answered gives the same promise — no second call. For data that is the same for
295
+ * everyone on the page (languages, themes, texts, company details), not for user data. A failed request is not kept.
296
+ * const reply = await AppHttp.GetShared(`${MsUrl.sor}/load/LANG`, { active: true });
297
+ */
298
+ static GetShared(url: string, params: HttpParams = {}): Promise<unknown> {
299
+ const fullUrl = AppHttp.FullUrl(url);
300
+ const key = buildSharedGetKey(fullUrl, params);
301
+ const cache = readSharedGetCache();
302
+ const cached = cache.get(key);
303
+ if (cached) {
304
+ return cached;
305
+ }
306
+ const request = AppHttp.Get(url, params);
307
+ cache.set(key, request);
308
+ const forget = forgetSharedGet.bind(null, key);
309
+ request.catch(forget);
310
+ return request;
311
+ }
312
+
146
313
  /** POST a JSON body. */
147
314
  static async Post(url: string, body: unknown, customHeaders: HttpHeaders = {}): Promise<unknown> {
315
+ await ensureFreshToken();
148
316
  const baseHeaders = AppHttp.Headers();
149
317
  const headers = { ...baseHeaders, ...customHeaders };
150
- const request = Http.Post(url, body, headers);
318
+ const fullUrl = AppHttp.FullUrl(url);
319
+ const request = Http.Post(fullUrl, body, headers);
151
320
  return AppHttp.withProgress(request);
152
321
  }
153
322
 
154
323
  /** PUT a JSON body to url/id. */
155
324
  static async Put(url: string, id: string, body: unknown): Promise<unknown> {
325
+ await ensureFreshToken();
156
326
  const headers = AppHttp.Headers();
157
- const itemUrl = `${url}/${id}`;
327
+ const fullUrl = AppHttp.FullUrl(url);
328
+ const itemUrl = `${fullUrl}/${id}`;
158
329
  const request = Http.Put(itemUrl, body, headers);
159
330
  return AppHttp.withProgress(request);
160
331
  }
161
332
 
162
333
  /** DELETE url/id. */
163
334
  static async Delete(url: string, id: string, params: HttpParams = {}): Promise<unknown> {
335
+ await ensureFreshToken();
164
336
  const headers = AppHttp.Headers();
165
- const itemUrl = `${url}/${id}`;
337
+ const fullUrl = AppHttp.FullUrl(url);
338
+ const itemUrl = `${fullUrl}/${id}`;
166
339
  const request = Http.Delete(itemUrl, params, headers);
167
340
  return AppHttp.withProgress(request);
168
341
  }
169
342
 
170
343
  /** PATCH a JSON body. */
171
344
  static async Patch(url: string, body: unknown): Promise<unknown> {
345
+ await ensureFreshToken();
172
346
  const headers = AppHttp.Headers();
173
347
  const bodyText = JSON.stringify(body ?? {});
174
348
  const requestInit = { method: "PATCH", headers: headers, body: bodyText };
175
- const request = fetch(url, requestInit);
349
+ const fullUrl = AppHttp.FullUrl(url);
350
+ const request = fetch(fullUrl, requestInit);
176
351
  const response = await AppHttp.withProgress(request);
177
352
  if (!response.ok) {
178
353
  const errorText = await response.text();
@@ -184,16 +359,17 @@ export class AppHttp {
184
359
  /** Option lists from the backend loader: GET `${config.http.loadUrl}/${id}`. */
185
360
  static async Load(id: string, params: HttpParams = {}): Promise<unknown> {
186
361
  const savedConfig = readSavedConfig();
187
- const baseUrl = AppHttp.BaseUrl();
188
- const loadUrl = savedConfig.http?.loadUrl ?? `${baseUrl}/load`;
189
- const fullUrl = `${loadUrl}/${id}`;
190
- return AppHttp.Get(fullUrl, params);
362
+ const loadUrl = savedConfig.http?.loadUrl ?? "/load";
363
+ const fullLoadUrl = AppHttp.FullUrl(loadUrl);
364
+ const itemUrl = `${fullLoadUrl}/${id}`;
365
+ return AppHttp.Get(itemUrl, params);
191
366
  }
192
367
 
193
368
  /** PUT a file to a signed upload URL (plain PUT, no extra headers, so bucket CORS accepts it). */
194
369
  static async CloudUpload(uploadUrl: string, file: File): Promise<Response> {
195
370
  const requestInit = { method: "PUT", body: file };
196
- const request = fetch(uploadUrl, requestInit);
371
+ const fullUrl = AppHttp.FullUrl(uploadUrl);
372
+ const request = fetch(fullUrl, requestInit);
197
373
  const response = await AppHttp.withProgress(request);
198
374
  if (!response.ok) {
199
375
  throw new Error(`Upload failed: ${response.status}`);
@@ -206,10 +382,27 @@ export class AppHttp {
206
382
  return AppHttp.CloudUpload(uploadUrl, file);
207
383
  }
208
384
 
385
+ /**
386
+ * GET that returns the raw body as a Blob (images, video, audio, any binary file).
387
+ * The URL rule of every method applies (a path gets the base URL), and the same headers are sent (token,
388
+ * x-session-id, x-branch-id, http.headers, setHeader values; no Content-Type). No progress bar: pages often load
389
+ * many media files at once.
390
+ * const blob = await AppHttp.Media(`${MsUrl.utils}/files/${id}`);
391
+ * const objectUrl = URL.createObjectURL(blob); // <img src={objectUrl}>; URL.revokeObjectURL(objectUrl) later
392
+ */
393
+ static async Media(url: string, params?: HttpParams): Promise<Blob> {
394
+ await ensureFreshToken();
395
+ const fullUrl = AppHttp.FullUrl(url);
396
+ const headers = AppHttp.RawHeaders();
397
+ return Http.FileDownload(fullUrl, params, headers);
398
+ }
399
+
209
400
  /** Downloads a file and saves it in the browser. */
210
401
  static async DownloadFile(url: string, params: HttpParams = {}, fileName?: string): Promise<void> {
402
+ await ensureFreshToken();
211
403
  const headers = AppHttp.RawHeaders();
212
- const request = Http.FileDownload(url, params, headers);
404
+ const fullUrl = AppHttp.FullUrl(url);
405
+ const request = Http.FileDownload(fullUrl, params, headers);
213
406
  const blob = await AppHttp.withProgress(request);
214
407
  const downloadUrl = window.URL.createObjectURL(blob);
215
408
  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: <SharedWebUiProvider lang={langResponse}> or setLanguages() do it.
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 active is not false. Sorted by priority. */
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
  }
@@ -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: <SharedWebUiProvider nav={menuResponse}> or setMenu(menuResponse) do it.
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