@gandalan/weblibs 1.3.5 → 1.3.7
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/api/RESTClient.js +2 -0
- package/api/authUtils.js +5 -1
- package/api/fluentApi.js +7 -7
- package/api/fluentAuthManager.js +14 -5
- package/api/fluentAuthUtils.js +1 -0
- package/api/fluentEnvUtils.js +1 -0
- package/api/fluentRestClient.js +22 -8
- package/package.json +1 -1
package/api/RESTClient.js
CHANGED
|
@@ -70,6 +70,7 @@ export class RESTClient
|
|
|
70
70
|
fileName = response.headers["content-disposition"].split(";")[1];
|
|
71
71
|
fileName = fileName.replace("filename=", "").trim();
|
|
72
72
|
}
|
|
73
|
+
|
|
73
74
|
this.lastError = "";
|
|
74
75
|
return { data: response.data, filename: fileName, contentType: "application/pdf" };
|
|
75
76
|
}
|
|
@@ -92,6 +93,7 @@ export class RESTClient
|
|
|
92
93
|
{
|
|
93
94
|
this.handleError(error);
|
|
94
95
|
}
|
|
96
|
+
|
|
95
97
|
return response;
|
|
96
98
|
}
|
|
97
99
|
|
package/api/authUtils.js
CHANGED
|
@@ -51,14 +51,17 @@ export async function initIDAS(appToken)
|
|
|
51
51
|
{
|
|
52
52
|
mandantGuid = urlParams.get("m");
|
|
53
53
|
}
|
|
54
|
+
|
|
54
55
|
if (urlParams.has("a"))
|
|
55
56
|
{
|
|
56
57
|
apiBaseurl = urlParams.get("a");
|
|
57
58
|
}
|
|
59
|
+
|
|
58
60
|
if (urlParams.has("j"))
|
|
59
61
|
{
|
|
60
62
|
jwtToken = urlParams.get("j");
|
|
61
63
|
}
|
|
64
|
+
|
|
62
65
|
if (urlParams.has("t"))
|
|
63
66
|
{
|
|
64
67
|
jwtRefreshToken = urlParams.get("t");
|
|
@@ -87,7 +90,7 @@ export async function initIDAS(appToken)
|
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
/**
|
|
90
|
-
* sets up authentication
|
|
93
|
+
* sets up authentication
|
|
91
94
|
*
|
|
92
95
|
* @export
|
|
93
96
|
* @async
|
|
@@ -121,6 +124,7 @@ export async function setup(settings)
|
|
|
121
124
|
currentRefreshToken = refreshToken;
|
|
122
125
|
startRefreshTimer(settings);
|
|
123
126
|
}
|
|
127
|
+
|
|
124
128
|
let mandantGuid = decoded["mandantGuid"] || "";
|
|
125
129
|
if (mandantGuid)
|
|
126
130
|
{
|
package/api/fluentApi.js
CHANGED
|
@@ -3,13 +3,13 @@ import { restClient } from "./fluentRestClient";
|
|
|
3
3
|
/**
|
|
4
4
|
* @typedef {Object} FluentApi
|
|
5
5
|
* @property {string} baseUrl - The base URL for API requests.
|
|
6
|
-
* @property {FluentAuthManager} authManager - The authentication manager.
|
|
6
|
+
* @property {import("./fluentAuthManager").FluentAuthManager} authManager - The authentication manager.
|
|
7
7
|
* @property {function(string) : FluentApi} useBaseUrl - Sets the base URL for API requests and returns the FluentApi object.
|
|
8
|
-
* @property {function(
|
|
9
|
-
* @property {function(string) : object|Array<any
|
|
10
|
-
* @property {function(string, object|null) : object|Array<any
|
|
11
|
-
* @property {function(string, object|null) : object|Array<any
|
|
12
|
-
* @property {function(string) : object|Array<any
|
|
8
|
+
* @property {function(fluentAuthManager) : FluentApi} useAuthManager - Sets the auth manager and returns the FluentApi object.
|
|
9
|
+
* @property {function(string) : Promise<object|Array<any>>} get - Async function to perform GET requests.
|
|
10
|
+
* @property {function(string, object|null) : Promise<object|Array<any>>} put - Async function to perform PUT requests with a payload.
|
|
11
|
+
* @property {function(string, object|null) : Promise<object|Array<any>>} post - Async function to perform POST requests with a payload.
|
|
12
|
+
* @property {function(string) : Promise<object|Array<any>>} delete - Async function to perform DELETE requests.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -102,7 +102,7 @@ export function createApi() {
|
|
|
102
102
|
* Creates the REST client instance with the current configuration.
|
|
103
103
|
*
|
|
104
104
|
* @private
|
|
105
|
-
* @returns {
|
|
105
|
+
* @returns {import("./fluentRestClient").FluentRESTClient}
|
|
106
106
|
*/
|
|
107
107
|
createRestClient() {
|
|
108
108
|
return restClient().useBaseUrl(this.baseUrl).useToken(this.authManager?.token);
|
package/api/fluentAuthManager.js
CHANGED
|
@@ -20,6 +20,8 @@ import { popRefreshTokenFromUrl } from "./fluentAuthUtils";
|
|
|
20
20
|
* @property {function} tryRefreshToken - Attempts to refresh the authentication token using the refresh token.
|
|
21
21
|
* @property {function} updateUserSession - Updates the user session with the new token.
|
|
22
22
|
* @property {function} redirectToLogin - Redirects to the login page.
|
|
23
|
+
* @property {function(string) : boolean} hasRight - Checks if the user has the specific right.
|
|
24
|
+
* @property {function(string) : boolean} hasRole - Checks if the user has the specific role.
|
|
23
25
|
*/
|
|
24
26
|
|
|
25
27
|
/**
|
|
@@ -91,8 +93,9 @@ export function createAuthManager() {
|
|
|
91
93
|
* @private
|
|
92
94
|
*/
|
|
93
95
|
async ensureAuthenticated() {
|
|
94
|
-
if (this.token && isTokenValid(this.token))
|
|
96
|
+
if (this.token && isTokenValid(this.token)) {
|
|
95
97
|
return;
|
|
98
|
+
}
|
|
96
99
|
|
|
97
100
|
try {
|
|
98
101
|
await this.authenticate();
|
|
@@ -112,11 +115,13 @@ export function createAuthManager() {
|
|
|
112
115
|
async authenticate() { // benutzt bei existierendem JWT oder RefreshToken, wenn keins vorhanden ERROR
|
|
113
116
|
console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken, this.appToken);
|
|
114
117
|
|
|
115
|
-
if (this.token && isTokenValid(this.token))
|
|
118
|
+
if (this.token && isTokenValid(this.token)) {
|
|
116
119
|
return;
|
|
120
|
+
}
|
|
117
121
|
|
|
118
|
-
if (this.token && !this.refreshToken)
|
|
122
|
+
if (this.token && !this.refreshToken) {
|
|
119
123
|
this.refreshToken = getRefreshToken(this.token);
|
|
124
|
+
}
|
|
120
125
|
|
|
121
126
|
if (!this.refreshToken) {
|
|
122
127
|
throw new Error("not authenticated");
|
|
@@ -161,6 +166,7 @@ export function createAuthManager() {
|
|
|
161
166
|
this.redirectToLogin();
|
|
162
167
|
throw "Redirect to login...";
|
|
163
168
|
}
|
|
169
|
+
|
|
164
170
|
return this;
|
|
165
171
|
},
|
|
166
172
|
|
|
@@ -178,6 +184,7 @@ export function createAuthManager() {
|
|
|
178
184
|
this.updateUserSession((await res.json()));
|
|
179
185
|
return;
|
|
180
186
|
}
|
|
187
|
+
|
|
181
188
|
throw new Error("not authenticated");
|
|
182
189
|
},
|
|
183
190
|
|
|
@@ -279,15 +286,17 @@ export function getRefreshToken(token) {
|
|
|
279
286
|
* check if the token is still valid
|
|
280
287
|
* - checks the expiry date and the JWT_SAFE_RENEWAL buffer
|
|
281
288
|
*
|
|
282
|
-
*
|
|
289
|
+
* @export
|
|
283
290
|
* @param {string} token
|
|
284
291
|
* @returns {boolean}
|
|
285
292
|
*/
|
|
286
293
|
export function isTokenValid(token) {
|
|
287
294
|
try {
|
|
288
295
|
const decoded = jwtDecode(token);
|
|
289
|
-
if (!decoded || !decoded.exp)
|
|
296
|
+
if (!decoded || !decoded.exp) {
|
|
290
297
|
throw new Error("Invalid token");
|
|
298
|
+
}
|
|
299
|
+
|
|
291
300
|
return (decoded.exp - JWT_SAFE_RENEWAL > Date.now() / 1000);
|
|
292
301
|
}
|
|
293
302
|
catch {
|
package/api/fluentAuthUtils.js
CHANGED
package/api/fluentEnvUtils.js
CHANGED
package/api/fluentRestClient.js
CHANGED
|
@@ -51,8 +51,10 @@ export function restClient() {
|
|
|
51
51
|
const finalUrl = `${this.baseUrl}${url}`;
|
|
52
52
|
const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
|
|
53
53
|
const res = await fetch(finalUrl, { method: "GET", headers });
|
|
54
|
-
if (res.ok)
|
|
54
|
+
if (res.ok) {
|
|
55
55
|
return await this._parseReponse(res);
|
|
56
|
+
}
|
|
57
|
+
|
|
56
58
|
throw new Error(`GET ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
57
59
|
},
|
|
58
60
|
|
|
@@ -68,8 +70,10 @@ export function restClient() {
|
|
|
68
70
|
const finalUrl = `${this.baseUrl}${url}`;
|
|
69
71
|
const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
|
|
70
72
|
const res = await fetch(finalUrl, { method: "PUT", body: JSON.stringify(payload), headers });
|
|
71
|
-
if (res.ok)
|
|
73
|
+
if (res.ok) {
|
|
72
74
|
return await this._parseReponse(res);
|
|
75
|
+
}
|
|
76
|
+
|
|
73
77
|
throw new Error(`PUT ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
74
78
|
},
|
|
75
79
|
|
|
@@ -95,8 +99,10 @@ export function restClient() {
|
|
|
95
99
|
}
|
|
96
100
|
|
|
97
101
|
const res = await fetch(finalUrl, { method: "POST", body, headers });
|
|
98
|
-
if (res.ok)
|
|
102
|
+
if (res.ok) {
|
|
99
103
|
return await this._parseReponse(res);
|
|
104
|
+
}
|
|
105
|
+
|
|
100
106
|
throw new Error(`POST ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
101
107
|
},
|
|
102
108
|
|
|
@@ -111,18 +117,26 @@ export function restClient() {
|
|
|
111
117
|
const finalUrl = `${this.baseUrl}${url}`;
|
|
112
118
|
const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
|
|
113
119
|
const res = await fetch(finalUrl, { method: "DELETE", headers });
|
|
114
|
-
if (res.ok)
|
|
120
|
+
if (res.ok) {
|
|
115
121
|
return await this._parseReponse(res);
|
|
122
|
+
}
|
|
123
|
+
|
|
116
124
|
throw new Error(`DELETE ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
117
125
|
},
|
|
118
126
|
|
|
119
127
|
async _parseReponse(res) {
|
|
120
128
|
// check if repsonse is JSON, then return parsed JSON, otherwise return text
|
|
121
129
|
const contentType = res.headers.get("content-type");
|
|
122
|
-
if (contentType
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
130
|
+
if (contentType) {
|
|
131
|
+
if (contentType.includes("application/json")) {
|
|
132
|
+
return await res.json();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (contentType.includes("image") || contentType.includes("application/pdf")) {
|
|
136
|
+
return await res.blob();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
126
140
|
return await res.text();
|
|
127
141
|
}
|
|
128
142
|
};
|