zavadil-ts-common 1.2.35 → 1.2.36

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.
@@ -52,6 +52,24 @@ export class RestClientWithOAuth extends RestClient {
52
52
  return Promise.reject("No valid ID token!");
53
53
  }
54
54
 
55
+ /**
56
+ * Attempt to get ID token from URL or storage, redirect to login page when not successful
57
+ */
58
+ redirectToLogin(): Promise<any> {
59
+ if (this.redirecting) return Promise.reject("Already redirecting!");
60
+ return this.getServerInfo().then(
61
+ (si) => {
62
+ const location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${this.deleteIdTokenFromUrl()}`;
63
+ console.log('Redirecting to login page:', location);
64
+ this.redirecting = true;
65
+ document.location.href = location;
66
+ }
67
+ ).catch((err) => {
68
+ console.error('Redirection failed: OAuth info not fetched:', err);
69
+ return Promise.reject(err);
70
+ });
71
+ }
72
+
55
73
  /**
56
74
  * Attempt to get ID token from URL or storage, redirect to login page when not successful
57
75
  */
@@ -64,17 +82,7 @@ export class RestClientWithOAuth extends RestClient {
64
82
  (reason) => {
65
83
  if (this.redirecting) return Promise.reject("Already redirecting!");
66
84
  console.log('OAuth initialization failed:', reason);
67
- return this.getServerInfo().then(
68
- (si) => {
69
- const location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${this.deleteIdTokenFromUrl()}`;
70
- console.log('Redirecting to login page:', location);
71
- this.redirecting = true;
72
- document.location.href = location;
73
- }
74
- ).catch((err) => {
75
- console.log('Cannot redirect: OAuth info not fetched:', reason);
76
- return Promise.reject(err);
77
- });
85
+ return this.redirectToLogin();
78
86
  }
79
87
  );
80
88
  }
@@ -96,7 +104,7 @@ export class RestClientWithOAuth extends RestClient {
96
104
  deleteIdTokenFromUrl(): string {
97
105
  const url = new URL(document.location.toString());
98
106
  url.searchParams.delete(this.tokenName);
99
- return url.toString();
107
+ return StringUtil.trimTrailingSlashes(url.toString());
100
108
  }
101
109
 
102
110
  getIdTokenFromUrl(): string | null {
@@ -3,11 +3,19 @@ import {JsonUtil} from "../src/util/JsonUtil";
3
3
  import {RestClient} from "../src";
4
4
 
5
5
  describe('testing StringUtil', () => {
6
- test('getNonEmpty', () => {
7
- expect(StringUtil.getNonEmpty('test')).toBe('test');
8
- expect(StringUtil.getNonEmpty('', 'test')).toBe('test');
9
- expect(StringUtil.getNonEmpty('', null, undefined, 'test')).toBe('test');
10
- expect(StringUtil.getNonEmpty('', 'test', undefined, 'test2')).toBe('test');
6
+ test('isEmpty', () => {
7
+ expect(StringUtil.isEmpty('test')).toBe(false);
8
+ expect(StringUtil.isEmpty(' ')).toBe(false);
9
+ expect(StringUtil.isEmpty('')).toBe(true);
10
+ expect(StringUtil.isEmpty(null)).toBe(true);
11
+ expect(StringUtil.isEmpty(undefined)).toBe(true);
12
+ });
13
+ test('isBlank', () => {
14
+ expect(StringUtil.isBlank('test')).toBe(false);
15
+ expect(StringUtil.isBlank(' ')).toBe(true);
16
+ expect(StringUtil.isBlank('')).toBe(true);
17
+ expect(StringUtil.isBlank(null)).toBe(true);
18
+ expect(StringUtil.isBlank(undefined)).toBe(true);
11
19
  });
12
20
  test('getNonEmpty', () => {
13
21
  expect(StringUtil.getNonEmpty('test')).toBe('test');