shared-lib-web 0.0.3 → 0.0.5

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 (58) hide show
  1. package/README.md +193 -783
  2. package/dist/components/otp-modal.d.ts +5 -0
  3. package/dist/components/otp-modal.d.ts.map +1 -0
  4. package/dist/components/otp-modal.js +77 -0
  5. package/dist/components/otp-modal.js.map +1 -0
  6. package/dist/components/settings-menu.d.ts +1 -1
  7. package/dist/components/settings-menu.d.ts.map +1 -1
  8. package/dist/components/settings-menu.js +1 -1
  9. package/dist/components/settings-menu.js.map +1 -1
  10. package/dist/components/user-menu.d.ts.map +1 -1
  11. package/dist/components/user-menu.js +22 -5
  12. package/dist/components/user-menu.js.map +1 -1
  13. package/dist/i18n/default-messages.d.ts.map +1 -1
  14. package/dist/i18n/default-messages.js +12 -0
  15. package/dist/i18n/default-messages.js.map +1 -1
  16. package/dist/index.d.ts +5 -3
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +4 -2
  19. package/dist/index.js.map +1 -1
  20. package/dist/interfaces/ui-config.d.ts +25 -1
  21. package/dist/interfaces/ui-config.d.ts.map +1 -1
  22. package/dist/state/auth-service.d.ts +5 -0
  23. package/dist/state/auth-service.d.ts.map +1 -1
  24. package/dist/state/auth-service.js +25 -2
  25. package/dist/state/auth-service.js.map +1 -1
  26. package/dist/state/layout-choice-service.d.ts +3 -3
  27. package/dist/state/layout-choice-service.d.ts.map +1 -1
  28. package/dist/state/layout-choice-service.js +8 -6
  29. package/dist/state/layout-choice-service.js.map +1 -1
  30. package/dist/state/otp-service.d.ts +69 -0
  31. package/dist/state/otp-service.d.ts.map +1 -0
  32. package/dist/state/otp-service.js +212 -0
  33. package/dist/state/otp-service.js.map +1 -0
  34. package/dist/types/field-wrapper.d.ts +2 -0
  35. package/dist/types/field-wrapper.d.ts.map +1 -1
  36. package/dist/types/field-wrapper.js +3 -2
  37. package/dist/types/field-wrapper.js.map +1 -1
  38. package/dist/types/type.otp.d.ts +2 -0
  39. package/dist/types/type.otp.d.ts.map +1 -1
  40. package/dist/types/type.otp.js +2 -2
  41. package/dist/types/type.otp.js.map +1 -1
  42. package/dist/utils/app-http.d.ts +12 -0
  43. package/dist/utils/app-http.d.ts.map +1 -1
  44. package/dist/utils/app-http.js +75 -1
  45. package/dist/utils/app-http.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/components/otp-modal.tsx +154 -0
  48. package/src/components/settings-menu.tsx +2 -2
  49. package/src/components/user-menu.tsx +41 -12
  50. package/src/i18n/default-messages.ts +12 -0
  51. package/src/index.ts +31 -1
  52. package/src/interfaces/ui-config.ts +26 -1
  53. package/src/state/auth-service.ts +28 -2
  54. package/src/state/layout-choice-service.ts +8 -6
  55. package/src/state/otp-service.ts +283 -0
  56. package/src/types/field-wrapper.tsx +5 -2
  57. package/src/types/type.otp.tsx +11 -2
  58. package/src/utils/app-http.ts +80 -1
@@ -0,0 +1,283 @@
1
+ // otp-service.ts
2
+ // State and HTTP calls of the one-time code dialog (components/otp-modal.tsx). The URLs come from config.http.otp:
3
+ //
4
+ // // config.ts
5
+ // http: { baseUrl, otp: { sendUrl: `${MsUrl.sor}/otp-send`, verifyUrl: `${MsUrl.sor}/otp-verify`, resendSeconds: 180 } }
6
+ //
7
+ // // page: render <OtpModal /> once, then open it (the code is sent on open)
8
+ // openOtpModal({ userId: email, title: t("profile.verifyEmail"), message: t("profile.verifyEmailText"),
9
+ // onVerified: handleEmailVerified });
10
+ // export function handleEmailVerified(result: OtpVerifiedResult): void { ... result.oid, result.uid, result.otp ... }
11
+ //
12
+ // Send: POST sendUrl { userid } -> { data: { message, oid, uid } }
13
+ // Verify: POST verifyUrl { otp, oid, uid } -> { data: { message, oid, uid } }
14
+ // The backend message is shown as a toast; a failed call shows the backend error (readErrorMessage).
15
+
16
+ import { signal } from "@preact/signals-react";
17
+ import { ConstValue } from "dff-util";
18
+ import { translate } from "../i18n/translator.ts";
19
+ import type { OtpConfig } from "../interfaces/ui-config.ts";
20
+ import { readAppConfig } from "../utils/app-config.ts";
21
+ import { AppHttp } from "../utils/app-http.ts";
22
+ import { readErrorMessage } from "../utils/error-util.ts";
23
+ import { ShowToast } from "./toast-service.ts";
24
+
25
+ /** Default number of characters of the code. */
26
+ export const DEFAULT_OTP_LENGTH = 6;
27
+
28
+ /** Default seconds before "Resend code" works again. */
29
+ export const DEFAULT_OTP_RESEND_SECONDS = 180;
30
+
31
+ const OTP_CONFIG_MISSING = "shared-lib-web: config.http.otp (sendUrl, verifyUrl) is missing.";
32
+
33
+ /** oid + uid of the code that was sent: the verify call needs them. */
34
+ export interface OtpSession {
35
+ oid: string;
36
+ uid: string;
37
+ }
38
+
39
+ /** What onVerified receives. */
40
+ export interface OtpVerifiedResult extends OtpSession {
41
+ otp: string;
42
+ }
43
+
44
+ /** The form of the dialog. */
45
+ export interface OtpFormType {
46
+ otp: string;
47
+ }
48
+
49
+ /** What openOtpModal needs. */
50
+ export interface OtpOpenOptions {
51
+ /** Sent as "userid" and shown in the dialog (email, mobile or user id). */
52
+ userId: string;
53
+ /** Heading (already translated). Default t("ui.otpTitle"). */
54
+ title?: string;
55
+ /** Text under the user id (already translated). Default t("ui.otpText"). */
56
+ message?: string;
57
+ /** Called after a successful verify (the dialog is closed by then). */
58
+ onVerified?: (result: OtpVerifiedResult) => void;
59
+ /** Called after Cancel, ✕, Escape or a backdrop click. */
60
+ onCancel?: () => void;
61
+ }
62
+
63
+ /** What the dialog shows. */
64
+ export interface OtpModalState {
65
+ isOpen: boolean;
66
+ title: string;
67
+ message: string;
68
+ userId: string;
69
+ }
70
+
71
+ /** The reply fields of the send / verify services. */
72
+ interface OtpReply {
73
+ message?: string;
74
+ oid?: string;
75
+ uid?: string;
76
+ }
77
+
78
+ type OtpCallbacks = Pick<OtpOpenOptions, "onVerified" | "onCancel">;
79
+
80
+ const CLOSED_OTP_MODAL: OtpModalState = {
81
+ isOpen: false,
82
+ title: ConstValue.EMPTY,
83
+ message: ConstValue.EMPTY,
84
+ userId: ConstValue.EMPTY,
85
+ };
86
+
87
+ /** The dialog: open, title and user id. */
88
+ export const otpModalSignal = signal<OtpModalState>(CLOSED_OTP_MODAL);
89
+
90
+ /** oid + uid of the sent code; null = no code sent yet. */
91
+ export const otpSessionSignal = signal<OtpSession | null>(null);
92
+
93
+ /** true while the send call runs. */
94
+ export const otpIsSendingSignal = signal(false);
95
+
96
+ /** true while the verify call runs. */
97
+ export const otpIsVerifyingSignal = signal(false);
98
+
99
+ /** Seconds left before "Resend code" works again (0 = now). */
100
+ export const otpResendSecondsSignal = signal(0);
101
+
102
+ let otpCallbacks: OtpCallbacks = {};
103
+
104
+ let resendTimer: ReturnType<typeof setInterval> | null = null;
105
+
106
+ /** config.http.otp, or an error when it is not set. */
107
+ export function readOtpConfig(): OtpConfig {
108
+ const config = readAppConfig();
109
+ const otpConfig = config?.http?.otp;
110
+ if (!otpConfig) {
111
+ throw new Error(OTP_CONFIG_MISSING);
112
+ }
113
+ return otpConfig;
114
+ }
115
+
116
+ /** Number of characters of the code: config.http.otp.length, default 6. */
117
+ export function readOtpLength(): number {
118
+ const config = readAppConfig();
119
+ return config?.http?.otp?.length ?? DEFAULT_OTP_LENGTH;
120
+ }
121
+
122
+ /** true for a plain object. */
123
+ function isRecord(value: unknown): value is Record<string, unknown> {
124
+ return typeof value === "object" && value !== null && !Array.isArray(value);
125
+ }
126
+
127
+ /** The reply fields from { data: { message, oid, uid } } or { message, oid, uid }. */
128
+ function readOtpReply(reply: unknown): OtpReply {
129
+ if (!isRecord(reply)) {
130
+ return {};
131
+ }
132
+ const source = isRecord(reply.data) ? reply.data : reply;
133
+ return source as OtpReply;
134
+ }
135
+
136
+ /** 125 -> "2:05". */
137
+ export function formatOtpCountdown(seconds: number): string {
138
+ const minutes = Math.floor(seconds / 60);
139
+ const rest = seconds % 60;
140
+ const restText = String(rest);
141
+ const paddedRest = restText.padStart(2, "0");
142
+ return `${minutes}:${paddedRest}`;
143
+ }
144
+
145
+ /** Stops the resend countdown (0 = resend works). */
146
+ export function stopOtpResendCountdown(): void {
147
+ if (resendTimer) {
148
+ clearInterval(resendTimer);
149
+ resendTimer = null;
150
+ }
151
+ otpResendSecondsSignal.value = 0;
152
+ }
153
+
154
+ /** One second of the resend countdown. */
155
+ function tickOtpResendCountdown(): void {
156
+ const nextSeconds = otpResendSecondsSignal.value - 1;
157
+ if (nextSeconds <= 0) {
158
+ stopOtpResendCountdown();
159
+ return;
160
+ }
161
+ otpResendSecondsSignal.value = nextSeconds;
162
+ }
163
+
164
+ /** Starts the resend countdown (one timer for the whole page). */
165
+ function startOtpResendCountdown(seconds: number): void {
166
+ stopOtpResendCountdown();
167
+ if (seconds <= 0) {
168
+ return;
169
+ }
170
+ otpResendSecondsSignal.value = seconds;
171
+ resendTimer = setInterval(tickOtpResendCountdown, 1000);
172
+ }
173
+
174
+ /** The backend message, else the translated fallback. */
175
+ function readReplyMessage(otpReply: OtpReply, fallbackKey: string): string {
176
+ const fallback = translate(fallbackKey);
177
+ return otpReply.message || fallback;
178
+ }
179
+
180
+ /** Shows the backend error of a failed call, else the translated fallback. */
181
+ function showOtpError(error: unknown, fallbackKey: string): void {
182
+ const fallback = translate(fallbackKey);
183
+ const message = readErrorMessage(error, fallback);
184
+ ShowToast(message, "error");
185
+ }
186
+
187
+ /** POST sendUrl { userid }: keeps oid + uid and starts the resend countdown. Returns true when sent. */
188
+ export async function otpSendCall(): Promise<boolean> {
189
+ const userId = otpModalSignal.value.userId;
190
+ if (otpIsSendingSignal.value || userId.length === 0) {
191
+ return false;
192
+ }
193
+ otpIsSendingSignal.value = true;
194
+ try {
195
+ const otpConfig = readOtpConfig();
196
+ const body = { userid: userId };
197
+ const reply = await AppHttp.Post(otpConfig.sendUrl, body);
198
+ const isStillOpen = otpModalSignal.value.isOpen && otpModalSignal.value.userId === userId;
199
+ if (!isStillOpen) {
200
+ return false;
201
+ }
202
+ const otpReply = readOtpReply(reply);
203
+ otpSessionSignal.value = { oid: otpReply.oid ?? ConstValue.EMPTY, uid: otpReply.uid ?? ConstValue.EMPTY };
204
+ const message = readReplyMessage(otpReply, "ui.otpSent");
205
+ ShowToast(message, "success");
206
+ const resendSeconds = otpConfig.resendSeconds ?? DEFAULT_OTP_RESEND_SECONDS;
207
+ startOtpResendCountdown(resendSeconds);
208
+ return true;
209
+ } catch (error: unknown) {
210
+ showOtpError(error, "ui.otpSendFailed");
211
+ return false;
212
+ } finally {
213
+ otpIsSendingSignal.value = false;
214
+ }
215
+ }
216
+
217
+ /** "Resend code" button: sends a new code once the countdown is over. */
218
+ export function handleOtpResend(): void {
219
+ if (otpResendSecondsSignal.value > 0) {
220
+ return;
221
+ }
222
+ void otpSendCall();
223
+ }
224
+
225
+ /** POST verifyUrl { otp, oid, uid }: closes the dialog and calls onVerified. For form.handleSubmit. */
226
+ export async function handleOtpVerify(values: OtpFormType): Promise<void> {
227
+ const session = otpSessionSignal.value;
228
+ if (otpIsVerifyingSignal.value) {
229
+ return;
230
+ }
231
+ if (!session) {
232
+ const message = translate("ui.otpSessionMissing");
233
+ ShowToast(message, "error");
234
+ return;
235
+ }
236
+ otpIsVerifyingSignal.value = true;
237
+ try {
238
+ const otpConfig = readOtpConfig();
239
+ const body = { otp: values.otp, oid: session.oid, uid: session.uid };
240
+ const reply = await AppHttp.Post(otpConfig.verifyUrl, body);
241
+ const otpReply = readOtpReply(reply);
242
+ const message = readReplyMessage(otpReply, "ui.otpVerified");
243
+ ShowToast(message, "success");
244
+ const result: OtpVerifiedResult = {
245
+ oid: otpReply.oid || session.oid,
246
+ uid: otpReply.uid || session.uid,
247
+ otp: values.otp,
248
+ };
249
+ const onVerified = otpCallbacks.onVerified;
250
+ closeOtpModal();
251
+ onVerified?.(result);
252
+ } catch (error: unknown) {
253
+ showOtpError(error, "ui.otpInvalid");
254
+ } finally {
255
+ otpIsVerifyingSignal.value = false;
256
+ }
257
+ }
258
+
259
+ /** Opens the dialog for a user id and sends the code. */
260
+ export function openOtpModal(options: OtpOpenOptions): void {
261
+ stopOtpResendCountdown();
262
+ otpCallbacks = { onVerified: options.onVerified, onCancel: options.onCancel };
263
+ otpSessionSignal.value = null;
264
+ const title = options.title ?? translate("ui.otpTitle");
265
+ const message = options.message ?? translate("ui.otpText");
266
+ otpModalSignal.value = { isOpen: true, title: title, message: message, userId: options.userId };
267
+ void otpSendCall();
268
+ }
269
+
270
+ /** Closes the dialog without calling onCancel (also used after a successful verify). */
271
+ export function closeOtpModal(): void {
272
+ stopOtpResendCountdown();
273
+ otpCallbacks = {};
274
+ otpSessionSignal.value = null;
275
+ otpModalSignal.value = CLOSED_OTP_MODAL;
276
+ }
277
+
278
+ /** Cancel, ✕, Escape or a backdrop click: closes the dialog and calls onCancel. */
279
+ export function cancelOtpModal(): void {
280
+ const onCancel = otpCallbacks.onCancel;
281
+ closeOtpModal();
282
+ onCancel?.();
283
+ }
@@ -10,6 +10,8 @@ export interface FieldWrapperProps {
10
10
  errorText?: string;
11
11
  hint?: string;
12
12
  className?: string;
13
+ /** Keep the label for screen readers only (not shown). Default false. */
14
+ isLabelHidden?: boolean;
13
15
  children?: ReactNode;
14
16
  }
15
17
 
@@ -19,16 +21,17 @@ export function readMessageId(inputId: string): string {
19
21
  }
20
22
 
21
23
  export function FieldWrapper(props: FieldWrapperProps) {
22
- const { inputId, label, errorText, hint, className, children } = props;
24
+ const { inputId, label, errorText, hint, className, isLabelHidden = false, children } = props;
23
25
  const messageId = readMessageId(inputId);
24
26
  const messageText = errorText ?? hint;
25
27
  const messageClassName = errorText ? "label text-error" : "label";
26
28
  const classList = ["fieldset", className];
27
29
  const wrapperClassName = joinClassNames(classList);
30
+ const labelClassName = isLabelHidden ? "sr-only" : "fieldset-legend";
28
31
 
29
32
  return (
30
33
  <fieldset className={wrapperClassName}>
31
- <label className="fieldset-legend" htmlFor={inputId}>
34
+ <label className={labelClassName} htmlFor={inputId}>
32
35
  {label}
33
36
  </label>
34
37
  {children}
@@ -18,6 +18,8 @@ import type { TypeFieldProps } from "./field-props.ts";
18
18
  export interface TypeOtpProps<TFieldValues extends FieldValues> extends TypeFieldProps<TFieldValues> {
19
19
  /** Number of characters. Default 6. */
20
20
  length?: number;
21
+ /** Keep the label for screen readers only (not shown). Default false. */
22
+ isLabelHidden?: boolean;
21
23
  }
22
24
 
23
25
  /** Builds the boxes that show one character each. The box at the cursor is highlighted. */
@@ -43,7 +45,7 @@ function buildOtpBoxes(value: string, length: number, hasError: boolean): ReactE
43
45
  }
44
46
 
45
47
  export function TypeOtp<TFieldValues extends FieldValues>(props: TypeOtpProps<TFieldValues>) {
46
- const { control, name, label, rules, error, hint, disabled, className, length = 6 } = props;
48
+ const { control, name, label, rules, error, hint, disabled, className, length = 6, isLabelHidden = false } = props;
47
49
  const { t } = useUi();
48
50
  const inputId = useId();
49
51
  const controllerOptions = { control: control, name: name, rules: rules, disabled: disabled };
@@ -57,7 +59,14 @@ export function TypeOtp<TFieldValues extends FieldValues>(props: TypeOtpProps<TF
57
59
  const boxes = buildOtpBoxes(value, length, hasError);
58
60
 
59
61
  return (
60
- <FieldWrapper inputId={inputId} label={label} errorText={errorText} hint={hint} className={className}>
62
+ <FieldWrapper
63
+ inputId={inputId}
64
+ label={label}
65
+ errorText={errorText}
66
+ hint={hint}
67
+ className={className}
68
+ isLabelHidden={isLabelHidden}
69
+ >
61
70
  <div className="relative flex w-fit gap-2" dir="ltr">
62
71
  <input
63
72
  id={inputId}
@@ -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);