shared-lib-web 0.0.3 → 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.
- package/README.md +193 -783
- package/dist/components/otp-modal.d.ts +5 -0
- package/dist/components/otp-modal.d.ts.map +1 -0
- package/dist/components/otp-modal.js +77 -0
- package/dist/components/otp-modal.js.map +1 -0
- package/dist/i18n/default-messages.d.ts.map +1 -1
- package/dist/i18n/default-messages.js +12 -0
- package/dist/i18n/default-messages.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/interfaces/ui-config.d.ts +22 -0
- package/dist/interfaces/ui-config.d.ts.map +1 -1
- package/dist/state/auth-service.d.ts +5 -0
- package/dist/state/auth-service.d.ts.map +1 -1
- package/dist/state/auth-service.js +25 -2
- package/dist/state/auth-service.js.map +1 -1
- package/dist/state/otp-service.d.ts +69 -0
- package/dist/state/otp-service.d.ts.map +1 -0
- package/dist/state/otp-service.js +212 -0
- package/dist/state/otp-service.js.map +1 -0
- package/dist/types/field-wrapper.d.ts +2 -0
- package/dist/types/field-wrapper.d.ts.map +1 -1
- package/dist/types/field-wrapper.js +3 -2
- package/dist/types/field-wrapper.js.map +1 -1
- package/dist/types/type.otp.d.ts +2 -0
- package/dist/types/type.otp.d.ts.map +1 -1
- package/dist/types/type.otp.js +2 -2
- package/dist/types/type.otp.js.map +1 -1
- package/dist/utils/app-http.d.ts +12 -0
- package/dist/utils/app-http.d.ts.map +1 -1
- package/dist/utils/app-http.js +75 -1
- package/dist/utils/app-http.js.map +1 -1
- package/package.json +1 -1
- package/src/components/otp-modal.tsx +154 -0
- package/src/i18n/default-messages.ts +12 -0
- package/src/index.ts +31 -1
- package/src/interfaces/ui-config.ts +23 -0
- package/src/state/auth-service.ts +28 -2
- package/src/state/otp-service.ts +283 -0
- package/src/types/field-wrapper.tsx +5 -2
- package/src/types/type.otp.tsx +11 -2
- package/src/utils/app-http.ts +80 -1
package/src/utils/app-http.ts
CHANGED
|
@@ -22,13 +22,19 @@
|
|
|
22
22
|
// const saved = await AppHttp.Post(saveUrl, body);
|
|
23
23
|
// AppHttp.setHeader("x-custom", value);
|
|
24
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
|
+
//
|
|
25
30
|
// // file upload to a signed URL
|
|
26
31
|
// const signed = await AppHttp.Post(`${MsUrl.utils}/s3/upload-url`, { fileName: file.name });
|
|
27
32
|
// const uploaded = await uploadToSignedUrl(file, signed.data.upload, signed.data.download);
|
|
28
33
|
// // uploaded = { filename, mimetype, url }
|
|
29
34
|
|
|
30
|
-
import { Http, type FileType } from "dff-util";
|
|
35
|
+
import { Http, JwtValid, type FileType } from "dff-util";
|
|
31
36
|
import type { UiConfig } from "../interfaces/ui-config.ts";
|
|
37
|
+
import { endSession, refreshSessionToken } from "../state/auth-service.ts";
|
|
32
38
|
import { ShowProgress } from "../state/progress-service.ts";
|
|
33
39
|
import { readAppConfig } from "./app-config.ts";
|
|
34
40
|
import { AppStorage } from "./app-storage.ts";
|
|
@@ -40,6 +46,7 @@ export type HttpParams = Record<string, string | number | boolean | null | undef
|
|
|
40
46
|
export type HttpHeaders = Record<string, string>;
|
|
41
47
|
|
|
42
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.";
|
|
43
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.";
|
|
44
51
|
|
|
45
52
|
/** window.env name (without prefix) of the API base URL. */
|
|
@@ -147,6 +154,71 @@ async function readResponseBody(response: Response): Promise<unknown> {
|
|
|
147
154
|
return response.text();
|
|
148
155
|
}
|
|
149
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
|
+
|
|
150
222
|
export class AppHttp {
|
|
151
223
|
/**
|
|
152
224
|
* API base URL: window.env REACT_APP_<APPNAMEKEY>_API_BASE_URL first, then config.http.baseUrl.
|
|
@@ -209,6 +281,7 @@ export class AppHttp {
|
|
|
209
281
|
|
|
210
282
|
/** GET with query params. */
|
|
211
283
|
static async Get(url: string, params: HttpParams = {}, customHeaders: HttpHeaders = {}): Promise<unknown> {
|
|
284
|
+
await ensureFreshToken();
|
|
212
285
|
const baseHeaders = AppHttp.Headers();
|
|
213
286
|
const headers = { ...baseHeaders, ...customHeaders };
|
|
214
287
|
const fullUrl = AppHttp.FullUrl(url);
|
|
@@ -239,6 +312,7 @@ export class AppHttp {
|
|
|
239
312
|
|
|
240
313
|
/** POST a JSON body. */
|
|
241
314
|
static async Post(url: string, body: unknown, customHeaders: HttpHeaders = {}): Promise<unknown> {
|
|
315
|
+
await ensureFreshToken();
|
|
242
316
|
const baseHeaders = AppHttp.Headers();
|
|
243
317
|
const headers = { ...baseHeaders, ...customHeaders };
|
|
244
318
|
const fullUrl = AppHttp.FullUrl(url);
|
|
@@ -248,6 +322,7 @@ export class AppHttp {
|
|
|
248
322
|
|
|
249
323
|
/** PUT a JSON body to url/id. */
|
|
250
324
|
static async Put(url: string, id: string, body: unknown): Promise<unknown> {
|
|
325
|
+
await ensureFreshToken();
|
|
251
326
|
const headers = AppHttp.Headers();
|
|
252
327
|
const fullUrl = AppHttp.FullUrl(url);
|
|
253
328
|
const itemUrl = `${fullUrl}/${id}`;
|
|
@@ -257,6 +332,7 @@ export class AppHttp {
|
|
|
257
332
|
|
|
258
333
|
/** DELETE url/id. */
|
|
259
334
|
static async Delete(url: string, id: string, params: HttpParams = {}): Promise<unknown> {
|
|
335
|
+
await ensureFreshToken();
|
|
260
336
|
const headers = AppHttp.Headers();
|
|
261
337
|
const fullUrl = AppHttp.FullUrl(url);
|
|
262
338
|
const itemUrl = `${fullUrl}/${id}`;
|
|
@@ -266,6 +342,7 @@ export class AppHttp {
|
|
|
266
342
|
|
|
267
343
|
/** PATCH a JSON body. */
|
|
268
344
|
static async Patch(url: string, body: unknown): Promise<unknown> {
|
|
345
|
+
await ensureFreshToken();
|
|
269
346
|
const headers = AppHttp.Headers();
|
|
270
347
|
const bodyText = JSON.stringify(body ?? {});
|
|
271
348
|
const requestInit = { method: "PATCH", headers: headers, body: bodyText };
|
|
@@ -314,6 +391,7 @@ export class AppHttp {
|
|
|
314
391
|
* const objectUrl = URL.createObjectURL(blob); // <img src={objectUrl}>; URL.revokeObjectURL(objectUrl) later
|
|
315
392
|
*/
|
|
316
393
|
static async Media(url: string, params?: HttpParams): Promise<Blob> {
|
|
394
|
+
await ensureFreshToken();
|
|
317
395
|
const fullUrl = AppHttp.FullUrl(url);
|
|
318
396
|
const headers = AppHttp.RawHeaders();
|
|
319
397
|
return Http.FileDownload(fullUrl, params, headers);
|
|
@@ -321,6 +399,7 @@ export class AppHttp {
|
|
|
321
399
|
|
|
322
400
|
/** Downloads a file and saves it in the browser. */
|
|
323
401
|
static async DownloadFile(url: string, params: HttpParams = {}, fileName?: string): Promise<void> {
|
|
402
|
+
await ensureFreshToken();
|
|
324
403
|
const headers = AppHttp.RawHeaders();
|
|
325
404
|
const fullUrl = AppHttp.FullUrl(url);
|
|
326
405
|
const request = Http.FileDownload(fullUrl, params, headers);
|