@klauswuestefeld/jux-web 2025.5.28 → 2025.6.30
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/dist/api-client.d.ts +17 -1
- package/dist/jux/jux-event.d.ts +2 -1
- package/dist/jux-web.js +97 -45
- package/dist/main.d.ts +1 -1
- package/dist/main.esm.js +97 -46
- package/dist/main.js +97 -45
- package/package.json +1 -1
package/dist/api-client.d.ts
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
export {};
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
store: {
|
|
5
|
+
backendToken?: string;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
}
|
|
1
9
|
export declare const setBackendUrl: (backendUrl: string) => void;
|
|
2
10
|
export declare const getBackendUrl: () => string;
|
|
3
11
|
export declare const handleJuxEvent: (ev: Event) => void;
|
|
4
|
-
export
|
|
12
|
+
export interface BackendRequestOptions extends RequestInit {
|
|
13
|
+
file?: File;
|
|
14
|
+
onProgress?: (percent: number) => void;
|
|
15
|
+
}
|
|
16
|
+
export type RequestType = 'query' | 'command' | 'upload';
|
|
17
|
+
export declare const upload: (endpoint: string, file: File, onResult?: (result: any) => any, onError?: (error: any) => void, extras?: {
|
|
18
|
+
onProgress?: (percent: number) => void;
|
|
19
|
+
onStart?: () => void;
|
|
20
|
+
}) => void;
|
|
5
21
|
export declare const backendPost: (endpoint: string, postContent: any, onJsonResponse: (response: any) => any, onHelpMessage: (message: string) => any, handleUnauthorized?: any) => void;
|
|
6
22
|
export declare const backendGet: (endpoint: string, params: any, onJsonResponse: (response: any) => any, onHelpMessage: (message: string) => any, handleUnauthorized?: any) => void;
|
|
7
23
|
export declare const requestMagicLink: (data: any, onJsonResponse: (response: any) => any, onUnauthorized: () => void) => void;
|
package/dist/jux/jux-event.d.ts
CHANGED
|
@@ -6,7 +6,8 @@ export declare class JuxEvent extends Event {
|
|
|
6
6
|
params: any;
|
|
7
7
|
onError?: (error: any) => void;
|
|
8
8
|
onRedirect?: (result: any) => any;
|
|
9
|
-
|
|
9
|
+
extras?: any;
|
|
10
|
+
constructor(type: string, method: string, endpoint: string, onResult?: (result: any) => any, params?: any, onError?: (error: any) => void, onRedirect?: (result: any) => any, extras?: {});
|
|
10
11
|
}
|
|
11
12
|
export declare const query: (element: HTMLElement, endpoint: string, onResult: (result: any) => any, params?: any, onError?: (error: any) => void, onRedirect?: (result: any) => any) => void;
|
|
12
13
|
export declare const command: (element: HTMLElement, endpoint: string, onResult?: (result: any) => any, params?: any, onError?: (error: any) => void, onRedirect?: (result: any) => any) => void;
|
package/dist/jux-web.js
CHANGED
|
@@ -164,13 +164,13 @@ var juxWeb = (function (exports) {
|
|
|
164
164
|
const getMagicAuthUrl = () => {
|
|
165
165
|
var _a;
|
|
166
166
|
// @ts-ignore
|
|
167
|
-
const endpoint = ((_a = window
|
|
167
|
+
const endpoint = ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.magicLinkAuthEndpoint) || 'auth-magic';
|
|
168
168
|
return getBackendUrl() + endpoint + '?token=';
|
|
169
169
|
};
|
|
170
170
|
const getMagicAuthReqUrl = () => {
|
|
171
171
|
var _a;
|
|
172
172
|
// @ts-ignore
|
|
173
|
-
const endpoint = ((_a = window
|
|
173
|
+
const endpoint = ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.magicLinkRequestEndpoint) || 'magic-link-request';
|
|
174
174
|
return getBackendUrl() + endpoint + '?email=';
|
|
175
175
|
};
|
|
176
176
|
// TODO: use jux-events to call commands and queries inside jux-web library
|
|
@@ -213,17 +213,65 @@ var juxWeb = (function (exports) {
|
|
|
213
213
|
const slowRequestTimeout = 2000;
|
|
214
214
|
// @ts-ignore
|
|
215
215
|
window.setRequestTimeout = (newTimeout) => timeout = newTimeout;
|
|
216
|
-
const
|
|
216
|
+
const upload = (endpoint, file, onResult, onError, extras) => {
|
|
217
|
+
apiRequest(endpoint, {
|
|
218
|
+
file,
|
|
219
|
+
onProgress: (pct) => { var _a; return (_a = extras === null || extras === void 0 ? void 0 : extras.onProgress) === null || _a === void 0 ? void 0 : _a.call(extras, pct); }
|
|
220
|
+
}, (text) => onResult === null || onResult === void 0 ? void 0 : onResult(text), (err) => onError === null || onError === void 0 ? void 0 : onError(err), undefined, // no redirect handler
|
|
221
|
+
'upload', // requestType
|
|
222
|
+
undefined, undefined, undefined, extras);
|
|
223
|
+
};
|
|
224
|
+
const apiRequest = (url_1, ...args_1) => __awaiter$2(void 0, [url_1, ...args_1], void 0, function* (url, options = {}, onSuccess, onError, onRedirect, requestType = 'query', handleUnauthorized = defaultHandleUnauthorized, retrial = false, retrialNum = 0, extras) {
|
|
225
|
+
url = getApiUrl() + url;
|
|
226
|
+
backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, retrial, retrialNum, extras);
|
|
227
|
+
});
|
|
228
|
+
const uploadRequest = (url, file, onSuccess, onError, extras) => {
|
|
229
|
+
var _a, _b;
|
|
230
|
+
requestRunning = true;
|
|
231
|
+
const xhr = new XMLHttpRequest();
|
|
232
|
+
xhr.open('POST', url);
|
|
233
|
+
if ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.backendToken) {
|
|
234
|
+
xhr.setRequestHeader('auth', (_b = window.juxwebGlobal) === null || _b === void 0 ? void 0 : _b.backendToken);
|
|
235
|
+
}
|
|
236
|
+
xhr.upload.onprogress = (e) => {
|
|
237
|
+
if (e.lengthComputable && (extras === null || extras === void 0 ? void 0 : extras.onProgress)) {
|
|
238
|
+
extras.onProgress(Math.round((e.loaded / e.total) * 100));
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
xhr.onload = () => {
|
|
242
|
+
requestRunning = false;
|
|
243
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
244
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(xhr.responseText);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
onError === null || onError === void 0 ? void 0 : onError(xhr.responseText);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
xhr.onerror = () => {
|
|
251
|
+
requestRunning = false;
|
|
252
|
+
onError === null || onError === void 0 ? void 0 : onError('Error sending file. Please try again.');
|
|
253
|
+
};
|
|
254
|
+
xhr.send(file);
|
|
255
|
+
if (extras === null || extras === void 0 ? void 0 : extras.onStart) {
|
|
256
|
+
extras.onStart({
|
|
257
|
+
abortUpload: () => {
|
|
258
|
+
console.log('Aborting upload');
|
|
259
|
+
xhr.abort();
|
|
260
|
+
requestRunning = false;
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
const backendRequest = (url_1, ...args_1) => __awaiter$2(void 0, [url_1, ...args_1], void 0, function* (url, options = {}, onSuccess, onError, onRedirect, requestType = 'query', handleUnauthorized = defaultHandleUnauthorized, retrial = false, retrialNum = 0, extras) {
|
|
266
|
+
var _a;
|
|
217
267
|
if (requestRunning && !retrial) { // only allow one request at a time
|
|
218
|
-
setTimeout(() => backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, false), 300);
|
|
268
|
+
setTimeout(() => backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, false, retrialNum), 300);
|
|
219
269
|
return;
|
|
220
270
|
}
|
|
221
271
|
// @ts-ignore
|
|
222
|
-
const
|
|
223
|
-
if (backendToken) {
|
|
224
|
-
|
|
225
|
-
options.headers = {};
|
|
226
|
-
options.headers.auth = backendToken;
|
|
272
|
+
const headers = new Headers(options.headers);
|
|
273
|
+
if ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.backendToken) {
|
|
274
|
+
headers.set('auth', window.juxwebGlobal.backendToken);
|
|
227
275
|
}
|
|
228
276
|
let response;
|
|
229
277
|
requestRunning = true;
|
|
@@ -239,17 +287,19 @@ var juxWeb = (function (exports) {
|
|
|
239
287
|
const event = new CustomEvent('slow-request', { detail: requestUrl.pathname, bubbles: true });
|
|
240
288
|
window.dispatchEvent(event);
|
|
241
289
|
}, slowRequestTimeout);
|
|
242
|
-
const requestInit = Object.assign(Object.assign({}, options), { signal: controller.signal });
|
|
290
|
+
const requestInit = Object.assign(Object.assign({}, options), { headers, signal: controller.signal });
|
|
243
291
|
if (onRedirect)
|
|
244
292
|
requestInit.redirect = 'manual'; // Treat redirects manually instead of following them automatically.
|
|
293
|
+
if (requestType === 'upload') {
|
|
294
|
+
uploadRequest(url, options.file, onSuccess, onError, extras);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
245
297
|
try {
|
|
246
298
|
response = yield fetch(url, requestInit);
|
|
247
299
|
}
|
|
248
300
|
catch (err) {
|
|
249
|
-
// network errors
|
|
250
301
|
if (requestType === 'command') {
|
|
251
|
-
|
|
252
|
-
onError('There was an error performing your request. Please try again later.');
|
|
302
|
+
onError === null || onError === void 0 ? void 0 : onError('There was an error performing your request. Please try again later.');
|
|
253
303
|
requestRunning = false;
|
|
254
304
|
return;
|
|
255
305
|
}
|
|
@@ -298,20 +348,14 @@ var juxWeb = (function (exports) {
|
|
|
298
348
|
try {
|
|
299
349
|
const jsonResponse = yield response.json();
|
|
300
350
|
if (response.ok) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
return jsonResponse;
|
|
306
|
-
}
|
|
351
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(jsonResponse);
|
|
352
|
+
return;
|
|
307
353
|
}
|
|
308
354
|
else {
|
|
309
|
-
|
|
310
|
-
onError(jsonResponse);
|
|
355
|
+
onError === null || onError === void 0 ? void 0 : onError(jsonResponse);
|
|
311
356
|
}
|
|
312
357
|
}
|
|
313
358
|
catch (err) {
|
|
314
|
-
// json conversion error / json response dealing errors / onSuccess errors
|
|
315
359
|
console.error(err);
|
|
316
360
|
}
|
|
317
361
|
finally {
|
|
@@ -334,7 +378,7 @@ var juxWeb = (function (exports) {
|
|
|
334
378
|
// @ts-ignore
|
|
335
379
|
options.body = content;
|
|
336
380
|
}
|
|
337
|
-
return
|
|
381
|
+
return apiRequest(endpoint, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized);
|
|
338
382
|
};
|
|
339
383
|
const get = (endpoint, onSuccess, onError, onRedirect, handleUnauthorized) => {
|
|
340
384
|
const options = {
|
|
@@ -344,7 +388,7 @@ var juxWeb = (function (exports) {
|
|
|
344
388
|
'Content-Type': 'application/json',
|
|
345
389
|
}
|
|
346
390
|
};
|
|
347
|
-
return
|
|
391
|
+
return apiRequest(endpoint, options, onSuccess, onError, onRedirect, 'query', handleUnauthorized);
|
|
348
392
|
};
|
|
349
393
|
const backendPost = (endpoint, postContent, onJsonResponse, onHelpMessage, handleUnauthorized) => {
|
|
350
394
|
post(endpoint, postContent, onJsonResponse, onHelpMessage, null, 'command', handleUnauthorized);
|
|
@@ -362,9 +406,15 @@ var juxWeb = (function (exports) {
|
|
|
362
406
|
};
|
|
363
407
|
const requestMagicLink = (data, onJsonResponse, onUnauthorized) => {
|
|
364
408
|
const { email, token } = data;
|
|
365
|
-
const url = getMagicAuthReqUrl() +
|
|
409
|
+
const url = getMagicAuthReqUrl() +
|
|
410
|
+
encodeURIComponent(email) +
|
|
411
|
+
'&destination=' +
|
|
412
|
+
extractTokenFromWindowLocation('destination') +
|
|
413
|
+
'&token=' +
|
|
414
|
+
token;
|
|
366
415
|
const options = {};
|
|
367
|
-
backendRequest(url, options, onJsonResponse, console.error,
|
|
416
|
+
backendRequest(url, options, onJsonResponse, console.error, undefined, // ← use undefined instead of null
|
|
417
|
+
'query', onUnauthorized);
|
|
368
418
|
};
|
|
369
419
|
const openMagicLink = (magicToken, onJsonResponse, onHelpMessage) => {
|
|
370
420
|
backendRequest(getMagicAuthUrl() + magicToken, {}, onJsonResponse, onHelpMessage);
|
|
@@ -13996,7 +14046,7 @@ var juxWeb = (function (exports) {
|
|
|
13996
14046
|
const onSuccess = (res) => {
|
|
13997
14047
|
const { token } = res;
|
|
13998
14048
|
// @ts-ignore
|
|
13999
|
-
window.
|
|
14049
|
+
window.juxwebGlobal.currentUser = res;
|
|
14000
14050
|
onTokenAcquired(token, onUserLogin);
|
|
14001
14051
|
};
|
|
14002
14052
|
const onError = (err) => {
|
|
@@ -14351,9 +14401,9 @@ var juxWeb = (function (exports) {
|
|
|
14351
14401
|
var _a, _b, _c, _d, _e;
|
|
14352
14402
|
const result = document.createElement('contact-us-modal');
|
|
14353
14403
|
// @ts-ignore
|
|
14354
|
-
const currentName = window.
|
|
14404
|
+
const currentName = window.juxwebGlobal.currentUser ? window.juxwebGlobal.currentUser.name : '';
|
|
14355
14405
|
// @ts-ignore
|
|
14356
|
-
const currentEmail = window.
|
|
14406
|
+
const currentEmail = window.juxwebGlobal.currentUser ? window.juxwebGlobal.currentUser.email : (_a = document.querySelector('#magic-link-email')) === null || _a === void 0 ? void 0 : _a.textContent;
|
|
14357
14407
|
const modalBody = document.createElement('modal-body');
|
|
14358
14408
|
const form = document.createElement('form');
|
|
14359
14409
|
form.id = 'contact-form';
|
|
@@ -14373,7 +14423,7 @@ var juxWeb = (function (exports) {
|
|
|
14373
14423
|
const captcha = googleCaptcha();
|
|
14374
14424
|
if (subject !== 'email-not-sent') {
|
|
14375
14425
|
// @ts-ignore
|
|
14376
|
-
const admin = ((_d = (_c = window.
|
|
14426
|
+
const admin = ((_d = (_c = window.juxwebGlobal) === null || _c === void 0 ? void 0 : _c.selectedTeam) === null || _d === void 0 ? void 0 : _d.admin) || window.juxwebGlobal.currentUser.email;
|
|
14377
14427
|
backendGet('credits-balance-for', { admin }, (res) => {
|
|
14378
14428
|
const balance = res;
|
|
14379
14429
|
if (balance <= 0) {
|
|
@@ -14507,7 +14557,7 @@ var juxWeb = (function (exports) {
|
|
|
14507
14557
|
|
|
14508
14558
|
const setBackendToken = (token) => {
|
|
14509
14559
|
// @ts-ignore
|
|
14510
|
-
window.
|
|
14560
|
+
window.juxwebGlobal.backendToken = token;
|
|
14511
14561
|
if (token) {
|
|
14512
14562
|
setLocalStorageItem('token', token);
|
|
14513
14563
|
}
|
|
@@ -14516,7 +14566,7 @@ var juxWeb = (function (exports) {
|
|
|
14516
14566
|
var _a;
|
|
14517
14567
|
const token = (_a = getLocalStorageItem('token')) !== null && _a !== void 0 ? _a : '';
|
|
14518
14568
|
// @ts-ignore
|
|
14519
|
-
window.
|
|
14569
|
+
window.juxwebGlobal.backendToken = token;
|
|
14520
14570
|
return token;
|
|
14521
14571
|
};
|
|
14522
14572
|
const onTokenAcquired = (token, onUserLogin) => {
|
|
@@ -14527,7 +14577,7 @@ var juxWeb = (function (exports) {
|
|
|
14527
14577
|
const clearSession = () => {
|
|
14528
14578
|
removeLocalStorageItem('token');
|
|
14529
14579
|
// @ts-ignore
|
|
14530
|
-
window.
|
|
14580
|
+
window.juxwebGlobal = {};
|
|
14531
14581
|
};
|
|
14532
14582
|
const onAuthenticationFailure = (msg) => {
|
|
14533
14583
|
removeLocalStorageItem('token');
|
|
@@ -14539,7 +14589,7 @@ var juxWeb = (function (exports) {
|
|
|
14539
14589
|
clearSession();
|
|
14540
14590
|
}
|
|
14541
14591
|
// @ts-ignore
|
|
14542
|
-
window.
|
|
14592
|
+
window.juxwebGlobal.currentUser = user;
|
|
14543
14593
|
if (type === 'Token Authentication') {
|
|
14544
14594
|
user.token = getLocalStorageItem('token');
|
|
14545
14595
|
}
|
|
@@ -14579,16 +14629,16 @@ var juxWeb = (function (exports) {
|
|
|
14579
14629
|
// return;
|
|
14580
14630
|
// }
|
|
14581
14631
|
// @ts-ignore
|
|
14582
|
-
if (!window.
|
|
14632
|
+
if (!window.juxwebGlobal) {
|
|
14583
14633
|
// @ts-ignore
|
|
14584
|
-
window.
|
|
14634
|
+
window.juxwebGlobal = {};
|
|
14585
14635
|
}
|
|
14586
14636
|
// @ts-ignore
|
|
14587
|
-
window.
|
|
14637
|
+
window.juxwebGlobal.fetchUserBackendUrl = fetchUserBackendUrl;
|
|
14588
14638
|
// @ts-ignore
|
|
14589
|
-
window.
|
|
14639
|
+
window.juxwebGlobal.magicLinkRequestEndpoint = magicLinkRequestEndpoint;
|
|
14590
14640
|
// @ts-ignore
|
|
14591
|
-
window.
|
|
14641
|
+
window.juxwebGlobal.magicLinkAuthEndpoint = magicLinkAuthEndpoint;
|
|
14592
14642
|
if (urlPrefix)
|
|
14593
14643
|
setUrlPrefix(urlPrefix);
|
|
14594
14644
|
const signInToken = extractTokenFromWindowLocation('sign-in') || extractTokenFromWindowLocation('magic-link');
|
|
@@ -14599,7 +14649,7 @@ var juxWeb = (function (exports) {
|
|
|
14599
14649
|
const host = extractTokenFromWindowLocation('host');
|
|
14600
14650
|
if (host) {
|
|
14601
14651
|
// @ts-ignore
|
|
14602
|
-
window.
|
|
14652
|
+
window.juxwebGlobal.fetchUserBackendUrl({ host }, (backendUrl) => {
|
|
14603
14653
|
setBackendUrl(backendUrl);
|
|
14604
14654
|
onOpenMagicLink();
|
|
14605
14655
|
});
|
|
@@ -14644,7 +14694,7 @@ var juxWeb = (function (exports) {
|
|
|
14644
14694
|
const onSuccess = (res) => {
|
|
14645
14695
|
const { token } = res;
|
|
14646
14696
|
// @ts-ignore
|
|
14647
|
-
window.
|
|
14697
|
+
window.juxwebGlobal.currentUser = res;
|
|
14648
14698
|
onTokenAcquired(token, onUserLogin);
|
|
14649
14699
|
disableSignInLayout();
|
|
14650
14700
|
};
|
|
@@ -14677,7 +14727,7 @@ var juxWeb = (function (exports) {
|
|
|
14677
14727
|
const onSuccess = (res) => {
|
|
14678
14728
|
const { token } = res;
|
|
14679
14729
|
// @ts-ignore
|
|
14680
|
-
window.
|
|
14730
|
+
window.juxwebGlobal.currentUser = res;
|
|
14681
14731
|
onTokenAcquired(token, onUserLogin);
|
|
14682
14732
|
disableSignInLayout();
|
|
14683
14733
|
};
|
|
@@ -14712,9 +14762,9 @@ var juxWeb = (function (exports) {
|
|
|
14712
14762
|
requestMagicLink(payload, onSuccess, onUnauthorized);
|
|
14713
14763
|
};
|
|
14714
14764
|
// @ts-ignore
|
|
14715
|
-
if (window.
|
|
14765
|
+
if (window.juxwebGlobal.fetchUserBackendUrl) {
|
|
14716
14766
|
// @ts-ignore
|
|
14717
|
-
window.
|
|
14767
|
+
window.juxwebGlobal.fetchUserBackendUrl({ email }, (backendUrl) => {
|
|
14718
14768
|
setBackendUrl(backendUrl);
|
|
14719
14769
|
onRequestMagicLink();
|
|
14720
14770
|
});
|
|
@@ -14947,7 +14997,7 @@ var juxWeb = (function (exports) {
|
|
|
14947
14997
|
};
|
|
14948
14998
|
|
|
14949
14999
|
class JuxEvent extends Event {
|
|
14950
|
-
constructor(type, method, endpoint, onResult, params, onError, onRedirect) {
|
|
15000
|
+
constructor(type, method, endpoint, onResult, params, onError, onRedirect, extras) {
|
|
14951
15001
|
super('jux-event', { bubbles: true, cancelable: true, composed: true });
|
|
14952
15002
|
this.requestType = type;
|
|
14953
15003
|
this.method = method;
|
|
@@ -14956,6 +15006,7 @@ var juxWeb = (function (exports) {
|
|
|
14956
15006
|
this.params = params;
|
|
14957
15007
|
this.onError = onError;
|
|
14958
15008
|
this.onRedirect = onRedirect;
|
|
15009
|
+
this.extras = extras;
|
|
14959
15010
|
}
|
|
14960
15011
|
}
|
|
14961
15012
|
const query = (element, endpoint, onResult, params, onError, onRedirect) => {
|
|
@@ -14980,6 +15031,7 @@ var juxWeb = (function (exports) {
|
|
|
14980
15031
|
exports.query = query;
|
|
14981
15032
|
exports.reloadWithLanguageOverride = reloadWithLanguageOverride;
|
|
14982
15033
|
exports.requestMagicLink = requestMagicLink;
|
|
15034
|
+
exports.upload = upload;
|
|
14983
15035
|
|
|
14984
15036
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14985
15037
|
|
package/dist/main.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { chosenLanguage, reloadWithLanguageOverride, initLanguage, getTranslatio
|
|
|
2
2
|
export { loginButton } from './login/login-button';
|
|
3
3
|
export { loginPage } from './login/login-page';
|
|
4
4
|
export { handleMagicLinkRequest, initSession, onMicrosoftSignIn, clearSession } from './login/session';
|
|
5
|
-
export { handleJuxEvent, requestMagicLink } from './api-client';
|
|
5
|
+
export { handleJuxEvent, requestMagicLink, upload } from './api-client';
|
|
6
6
|
export { magicLinkModal } from './login/magic-link-modal';
|
|
7
7
|
export { command, query } from './jux/jux-event';
|
|
8
8
|
declare global {
|
package/dist/main.esm.js
CHANGED
|
@@ -163,13 +163,13 @@ const getApiUrl = () => getBackendUrl() + 'api/';
|
|
|
163
163
|
const getMagicAuthUrl = () => {
|
|
164
164
|
var _a;
|
|
165
165
|
// @ts-ignore
|
|
166
|
-
const endpoint = ((_a = window
|
|
166
|
+
const endpoint = ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.magicLinkAuthEndpoint) || 'auth-magic';
|
|
167
167
|
return getBackendUrl() + endpoint + '?token=';
|
|
168
168
|
};
|
|
169
169
|
const getMagicAuthReqUrl = () => {
|
|
170
170
|
var _a;
|
|
171
171
|
// @ts-ignore
|
|
172
|
-
const endpoint = ((_a = window
|
|
172
|
+
const endpoint = ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.magicLinkRequestEndpoint) || 'magic-link-request';
|
|
173
173
|
return getBackendUrl() + endpoint + '?email=';
|
|
174
174
|
};
|
|
175
175
|
// TODO: use jux-events to call commands and queries inside jux-web library
|
|
@@ -212,17 +212,65 @@ let timeout = 20000;
|
|
|
212
212
|
const slowRequestTimeout = 2000;
|
|
213
213
|
// @ts-ignore
|
|
214
214
|
window.setRequestTimeout = (newTimeout) => timeout = newTimeout;
|
|
215
|
-
const
|
|
215
|
+
const upload = (endpoint, file, onResult, onError, extras) => {
|
|
216
|
+
apiRequest(endpoint, {
|
|
217
|
+
file,
|
|
218
|
+
onProgress: (pct) => { var _a; return (_a = extras === null || extras === void 0 ? void 0 : extras.onProgress) === null || _a === void 0 ? void 0 : _a.call(extras, pct); }
|
|
219
|
+
}, (text) => onResult === null || onResult === void 0 ? void 0 : onResult(text), (err) => onError === null || onError === void 0 ? void 0 : onError(err), undefined, // no redirect handler
|
|
220
|
+
'upload', // requestType
|
|
221
|
+
undefined, undefined, undefined, extras);
|
|
222
|
+
};
|
|
223
|
+
const apiRequest = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, options = {}, onSuccess, onError, onRedirect, requestType = 'query', handleUnauthorized = defaultHandleUnauthorized, retrial = false, retrialNum = 0, extras) {
|
|
224
|
+
url = getApiUrl() + url;
|
|
225
|
+
backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, retrial, retrialNum, extras);
|
|
226
|
+
});
|
|
227
|
+
const uploadRequest = (url, file, onSuccess, onError, extras) => {
|
|
228
|
+
var _a, _b;
|
|
229
|
+
requestRunning = true;
|
|
230
|
+
const xhr = new XMLHttpRequest();
|
|
231
|
+
xhr.open('POST', url);
|
|
232
|
+
if ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.backendToken) {
|
|
233
|
+
xhr.setRequestHeader('auth', (_b = window.juxwebGlobal) === null || _b === void 0 ? void 0 : _b.backendToken);
|
|
234
|
+
}
|
|
235
|
+
xhr.upload.onprogress = (e) => {
|
|
236
|
+
if (e.lengthComputable && (extras === null || extras === void 0 ? void 0 : extras.onProgress)) {
|
|
237
|
+
extras.onProgress(Math.round((e.loaded / e.total) * 100));
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
xhr.onload = () => {
|
|
241
|
+
requestRunning = false;
|
|
242
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
243
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(xhr.responseText);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
onError === null || onError === void 0 ? void 0 : onError(xhr.responseText);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
xhr.onerror = () => {
|
|
250
|
+
requestRunning = false;
|
|
251
|
+
onError === null || onError === void 0 ? void 0 : onError('Error sending file. Please try again.');
|
|
252
|
+
};
|
|
253
|
+
xhr.send(file);
|
|
254
|
+
if (extras === null || extras === void 0 ? void 0 : extras.onStart) {
|
|
255
|
+
extras.onStart({
|
|
256
|
+
abortUpload: () => {
|
|
257
|
+
console.log('Aborting upload');
|
|
258
|
+
xhr.abort();
|
|
259
|
+
requestRunning = false;
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
const backendRequest = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, options = {}, onSuccess, onError, onRedirect, requestType = 'query', handleUnauthorized = defaultHandleUnauthorized, retrial = false, retrialNum = 0, extras) {
|
|
265
|
+
var _a;
|
|
216
266
|
if (requestRunning && !retrial) { // only allow one request at a time
|
|
217
|
-
setTimeout(() => backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, false), 300);
|
|
267
|
+
setTimeout(() => backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, false, retrialNum), 300);
|
|
218
268
|
return;
|
|
219
269
|
}
|
|
220
270
|
// @ts-ignore
|
|
221
|
-
const
|
|
222
|
-
if (backendToken) {
|
|
223
|
-
|
|
224
|
-
options.headers = {};
|
|
225
|
-
options.headers.auth = backendToken;
|
|
271
|
+
const headers = new Headers(options.headers);
|
|
272
|
+
if ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.backendToken) {
|
|
273
|
+
headers.set('auth', window.juxwebGlobal.backendToken);
|
|
226
274
|
}
|
|
227
275
|
let response;
|
|
228
276
|
requestRunning = true;
|
|
@@ -238,17 +286,19 @@ const backendRequest = (url_1, options_1, onSuccess_1, onError_1, onRedirect_1,
|
|
|
238
286
|
const event = new CustomEvent('slow-request', { detail: requestUrl.pathname, bubbles: true });
|
|
239
287
|
window.dispatchEvent(event);
|
|
240
288
|
}, slowRequestTimeout);
|
|
241
|
-
const requestInit = Object.assign(Object.assign({}, options), { signal: controller.signal });
|
|
289
|
+
const requestInit = Object.assign(Object.assign({}, options), { headers, signal: controller.signal });
|
|
242
290
|
if (onRedirect)
|
|
243
291
|
requestInit.redirect = 'manual'; // Treat redirects manually instead of following them automatically.
|
|
292
|
+
if (requestType === 'upload') {
|
|
293
|
+
uploadRequest(url, options.file, onSuccess, onError, extras);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
244
296
|
try {
|
|
245
297
|
response = yield fetch(url, requestInit);
|
|
246
298
|
}
|
|
247
299
|
catch (err) {
|
|
248
|
-
// network errors
|
|
249
300
|
if (requestType === 'command') {
|
|
250
|
-
|
|
251
|
-
onError('There was an error performing your request. Please try again later.');
|
|
301
|
+
onError === null || onError === void 0 ? void 0 : onError('There was an error performing your request. Please try again later.');
|
|
252
302
|
requestRunning = false;
|
|
253
303
|
return;
|
|
254
304
|
}
|
|
@@ -297,20 +347,14 @@ const backendRequest = (url_1, options_1, onSuccess_1, onError_1, onRedirect_1,
|
|
|
297
347
|
try {
|
|
298
348
|
const jsonResponse = yield response.json();
|
|
299
349
|
if (response.ok) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
else {
|
|
304
|
-
return jsonResponse;
|
|
305
|
-
}
|
|
350
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(jsonResponse);
|
|
351
|
+
return;
|
|
306
352
|
}
|
|
307
353
|
else {
|
|
308
|
-
|
|
309
|
-
onError(jsonResponse);
|
|
354
|
+
onError === null || onError === void 0 ? void 0 : onError(jsonResponse);
|
|
310
355
|
}
|
|
311
356
|
}
|
|
312
357
|
catch (err) {
|
|
313
|
-
// json conversion error / json response dealing errors / onSuccess errors
|
|
314
358
|
console.error(err);
|
|
315
359
|
}
|
|
316
360
|
finally {
|
|
@@ -333,7 +377,7 @@ const post = (endpoint, params, onSuccess, onError, onRedirect, requestType = "q
|
|
|
333
377
|
// @ts-ignore
|
|
334
378
|
options.body = content;
|
|
335
379
|
}
|
|
336
|
-
return
|
|
380
|
+
return apiRequest(endpoint, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized);
|
|
337
381
|
};
|
|
338
382
|
const get = (endpoint, onSuccess, onError, onRedirect, handleUnauthorized) => {
|
|
339
383
|
const options = {
|
|
@@ -343,7 +387,7 @@ const get = (endpoint, onSuccess, onError, onRedirect, handleUnauthorized) => {
|
|
|
343
387
|
'Content-Type': 'application/json',
|
|
344
388
|
}
|
|
345
389
|
};
|
|
346
|
-
return
|
|
390
|
+
return apiRequest(endpoint, options, onSuccess, onError, onRedirect, 'query', handleUnauthorized);
|
|
347
391
|
};
|
|
348
392
|
const backendPost = (endpoint, postContent, onJsonResponse, onHelpMessage, handleUnauthorized) => {
|
|
349
393
|
post(endpoint, postContent, onJsonResponse, onHelpMessage, null, 'command', handleUnauthorized);
|
|
@@ -361,9 +405,15 @@ const backendGet = (endpoint, params, onJsonResponse, onHelpMessage, handleUnaut
|
|
|
361
405
|
};
|
|
362
406
|
const requestMagicLink = (data, onJsonResponse, onUnauthorized) => {
|
|
363
407
|
const { email, token } = data;
|
|
364
|
-
const url = getMagicAuthReqUrl() +
|
|
408
|
+
const url = getMagicAuthReqUrl() +
|
|
409
|
+
encodeURIComponent(email) +
|
|
410
|
+
'&destination=' +
|
|
411
|
+
extractTokenFromWindowLocation('destination') +
|
|
412
|
+
'&token=' +
|
|
413
|
+
token;
|
|
365
414
|
const options = {};
|
|
366
|
-
backendRequest(url, options, onJsonResponse, console.error,
|
|
415
|
+
backendRequest(url, options, onJsonResponse, console.error, undefined, // ← use undefined instead of null
|
|
416
|
+
'query', onUnauthorized);
|
|
367
417
|
};
|
|
368
418
|
const openMagicLink = (magicToken, onJsonResponse, onHelpMessage) => {
|
|
369
419
|
backendRequest(getMagicAuthUrl() + magicToken, {}, onJsonResponse, onHelpMessage);
|
|
@@ -481,7 +531,7 @@ const onUserChanged = (googleUser, onUserLogin) => {
|
|
|
481
531
|
const onSuccess = (res) => {
|
|
482
532
|
const { token } = res;
|
|
483
533
|
// @ts-ignore
|
|
484
|
-
window.
|
|
534
|
+
window.juxwebGlobal.currentUser = res;
|
|
485
535
|
onTokenAcquired(token, onUserLogin);
|
|
486
536
|
};
|
|
487
537
|
const onError = (err) => {
|
|
@@ -836,9 +886,9 @@ const contactUsModal = (subject) => {
|
|
|
836
886
|
var _a, _b, _c, _d, _e;
|
|
837
887
|
const result = document.createElement('contact-us-modal');
|
|
838
888
|
// @ts-ignore
|
|
839
|
-
const currentName = window.
|
|
889
|
+
const currentName = window.juxwebGlobal.currentUser ? window.juxwebGlobal.currentUser.name : '';
|
|
840
890
|
// @ts-ignore
|
|
841
|
-
const currentEmail = window.
|
|
891
|
+
const currentEmail = window.juxwebGlobal.currentUser ? window.juxwebGlobal.currentUser.email : (_a = document.querySelector('#magic-link-email')) === null || _a === void 0 ? void 0 : _a.textContent;
|
|
842
892
|
const modalBody = document.createElement('modal-body');
|
|
843
893
|
const form = document.createElement('form');
|
|
844
894
|
form.id = 'contact-form';
|
|
@@ -858,7 +908,7 @@ const contactUsModal = (subject) => {
|
|
|
858
908
|
const captcha = googleCaptcha();
|
|
859
909
|
if (subject !== 'email-not-sent') {
|
|
860
910
|
// @ts-ignore
|
|
861
|
-
const admin = ((_d = (_c = window.
|
|
911
|
+
const admin = ((_d = (_c = window.juxwebGlobal) === null || _c === void 0 ? void 0 : _c.selectedTeam) === null || _d === void 0 ? void 0 : _d.admin) || window.juxwebGlobal.currentUser.email;
|
|
862
912
|
backendGet('credits-balance-for', { admin }, (res) => {
|
|
863
913
|
const balance = res;
|
|
864
914
|
if (balance <= 0) {
|
|
@@ -992,7 +1042,7 @@ const unauthorizedMagicLinkRequestPage = (backgroundImg, onReturn, failInfo) =>
|
|
|
992
1042
|
|
|
993
1043
|
const setBackendToken = (token) => {
|
|
994
1044
|
// @ts-ignore
|
|
995
|
-
window.
|
|
1045
|
+
window.juxwebGlobal.backendToken = token;
|
|
996
1046
|
if (token) {
|
|
997
1047
|
setLocalStorageItem('token', token);
|
|
998
1048
|
}
|
|
@@ -1001,7 +1051,7 @@ const getBackendToken = () => {
|
|
|
1001
1051
|
var _a;
|
|
1002
1052
|
const token = (_a = getLocalStorageItem('token')) !== null && _a !== void 0 ? _a : '';
|
|
1003
1053
|
// @ts-ignore
|
|
1004
|
-
window.
|
|
1054
|
+
window.juxwebGlobal.backendToken = token;
|
|
1005
1055
|
return token;
|
|
1006
1056
|
};
|
|
1007
1057
|
const onTokenAcquired = (token, onUserLogin) => {
|
|
@@ -1012,7 +1062,7 @@ const onTokenAcquired = (token, onUserLogin) => {
|
|
|
1012
1062
|
const clearSession = () => {
|
|
1013
1063
|
removeLocalStorageItem('token');
|
|
1014
1064
|
// @ts-ignore
|
|
1015
|
-
window.
|
|
1065
|
+
window.juxwebGlobal = {};
|
|
1016
1066
|
};
|
|
1017
1067
|
const onAuthenticationFailure = (msg) => {
|
|
1018
1068
|
removeLocalStorageItem('token');
|
|
@@ -1024,7 +1074,7 @@ const onAuthentication = (onUserLogin, user, type) => {
|
|
|
1024
1074
|
clearSession();
|
|
1025
1075
|
}
|
|
1026
1076
|
// @ts-ignore
|
|
1027
|
-
window.
|
|
1077
|
+
window.juxwebGlobal.currentUser = user;
|
|
1028
1078
|
if (type === 'Token Authentication') {
|
|
1029
1079
|
user.token = getLocalStorageItem('token');
|
|
1030
1080
|
}
|
|
@@ -1064,16 +1114,16 @@ const initSession = ({ clientApp, supportedLoginTypes, onUserLogin, onLoginError
|
|
|
1064
1114
|
// return;
|
|
1065
1115
|
// }
|
|
1066
1116
|
// @ts-ignore
|
|
1067
|
-
if (!window.
|
|
1117
|
+
if (!window.juxwebGlobal) {
|
|
1068
1118
|
// @ts-ignore
|
|
1069
|
-
window.
|
|
1119
|
+
window.juxwebGlobal = {};
|
|
1070
1120
|
}
|
|
1071
1121
|
// @ts-ignore
|
|
1072
|
-
window.
|
|
1122
|
+
window.juxwebGlobal.fetchUserBackendUrl = fetchUserBackendUrl;
|
|
1073
1123
|
// @ts-ignore
|
|
1074
|
-
window.
|
|
1124
|
+
window.juxwebGlobal.magicLinkRequestEndpoint = magicLinkRequestEndpoint;
|
|
1075
1125
|
// @ts-ignore
|
|
1076
|
-
window.
|
|
1126
|
+
window.juxwebGlobal.magicLinkAuthEndpoint = magicLinkAuthEndpoint;
|
|
1077
1127
|
if (urlPrefix)
|
|
1078
1128
|
setUrlPrefix(urlPrefix);
|
|
1079
1129
|
const signInToken = extractTokenFromWindowLocation('sign-in') || extractTokenFromWindowLocation('magic-link');
|
|
@@ -1084,7 +1134,7 @@ const initSession = ({ clientApp, supportedLoginTypes, onUserLogin, onLoginError
|
|
|
1084
1134
|
const host = extractTokenFromWindowLocation('host');
|
|
1085
1135
|
if (host) {
|
|
1086
1136
|
// @ts-ignore
|
|
1087
|
-
window.
|
|
1137
|
+
window.juxwebGlobal.fetchUserBackendUrl({ host }, (backendUrl) => {
|
|
1088
1138
|
setBackendUrl(backendUrl);
|
|
1089
1139
|
onOpenMagicLink();
|
|
1090
1140
|
});
|
|
@@ -1129,7 +1179,7 @@ const onMicrosoftSignIn = (onUserLogin) => __awaiter(void 0, void 0, void 0, fun
|
|
|
1129
1179
|
const onSuccess = (res) => {
|
|
1130
1180
|
const { token } = res;
|
|
1131
1181
|
// @ts-ignore
|
|
1132
|
-
window.
|
|
1182
|
+
window.juxwebGlobal.currentUser = res;
|
|
1133
1183
|
onTokenAcquired(token, onUserLogin);
|
|
1134
1184
|
disableSignInLayout();
|
|
1135
1185
|
};
|
|
@@ -1162,7 +1212,7 @@ const onAuthPasswordLogin = (credentials, onUserLogin, onLoginError, onReturn, c
|
|
|
1162
1212
|
const onSuccess = (res) => {
|
|
1163
1213
|
const { token } = res;
|
|
1164
1214
|
// @ts-ignore
|
|
1165
|
-
window.
|
|
1215
|
+
window.juxwebGlobal.currentUser = res;
|
|
1166
1216
|
onTokenAcquired(token, onUserLogin);
|
|
1167
1217
|
disableSignInLayout();
|
|
1168
1218
|
};
|
|
@@ -1197,9 +1247,9 @@ const handleMagicLinkRequest = (token, onReturn, backgroundImage, currentPage, c
|
|
|
1197
1247
|
requestMagicLink(payload, onSuccess, onUnauthorized);
|
|
1198
1248
|
};
|
|
1199
1249
|
// @ts-ignore
|
|
1200
|
-
if (window.
|
|
1250
|
+
if (window.juxwebGlobal.fetchUserBackendUrl) {
|
|
1201
1251
|
// @ts-ignore
|
|
1202
|
-
window.
|
|
1252
|
+
window.juxwebGlobal.fetchUserBackendUrl({ email }, (backendUrl) => {
|
|
1203
1253
|
setBackendUrl(backendUrl);
|
|
1204
1254
|
onRequestMagicLink();
|
|
1205
1255
|
});
|
|
@@ -1432,7 +1482,7 @@ const loginPage = (clientApp, backgroundImg, onUserLogin, onLoginError, loginTyp
|
|
|
1432
1482
|
};
|
|
1433
1483
|
|
|
1434
1484
|
class JuxEvent extends Event {
|
|
1435
|
-
constructor(type, method, endpoint, onResult, params, onError, onRedirect) {
|
|
1485
|
+
constructor(type, method, endpoint, onResult, params, onError, onRedirect, extras) {
|
|
1436
1486
|
super('jux-event', { bubbles: true, cancelable: true, composed: true });
|
|
1437
1487
|
this.requestType = type;
|
|
1438
1488
|
this.method = method;
|
|
@@ -1441,6 +1491,7 @@ class JuxEvent extends Event {
|
|
|
1441
1491
|
this.params = params;
|
|
1442
1492
|
this.onError = onError;
|
|
1443
1493
|
this.onRedirect = onRedirect;
|
|
1494
|
+
this.extras = extras;
|
|
1444
1495
|
}
|
|
1445
1496
|
}
|
|
1446
1497
|
const query = (element, endpoint, onResult, params, onError, onRedirect) => {
|
|
@@ -1450,4 +1501,4 @@ const command = (element, endpoint, onResult, params, onError, onRedirect) => {
|
|
|
1450
1501
|
element.dispatchEvent(new JuxEvent('command', 'POST', endpoint, onResult, params, onError, onRedirect));
|
|
1451
1502
|
};
|
|
1452
1503
|
|
|
1453
|
-
export { chosenLanguage, clearSession, command, getTranslation, handleJuxEvent, handleMagicLinkRequest, initLanguage, initSession, loginButton, loginPage, magicLinkModal, onMicrosoftSignIn, query, reloadWithLanguageOverride, requestMagicLink };
|
|
1504
|
+
export { chosenLanguage, clearSession, command, getTranslation, handleJuxEvent, handleMagicLinkRequest, initLanguage, initSession, loginButton, loginPage, magicLinkModal, onMicrosoftSignIn, query, reloadWithLanguageOverride, requestMagicLink, upload };
|
package/dist/main.js
CHANGED
|
@@ -187,13 +187,13 @@ const getApiUrl = () => getBackendUrl() + 'api/';
|
|
|
187
187
|
const getMagicAuthUrl = () => {
|
|
188
188
|
var _a;
|
|
189
189
|
// @ts-ignore
|
|
190
|
-
const endpoint = ((_a = window
|
|
190
|
+
const endpoint = ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.magicLinkAuthEndpoint) || 'auth-magic';
|
|
191
191
|
return getBackendUrl() + endpoint + '?token=';
|
|
192
192
|
};
|
|
193
193
|
const getMagicAuthReqUrl = () => {
|
|
194
194
|
var _a;
|
|
195
195
|
// @ts-ignore
|
|
196
|
-
const endpoint = ((_a = window
|
|
196
|
+
const endpoint = ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.magicLinkRequestEndpoint) || 'magic-link-request';
|
|
197
197
|
return getBackendUrl() + endpoint + '?email=';
|
|
198
198
|
};
|
|
199
199
|
// TODO: use jux-events to call commands and queries inside jux-web library
|
|
@@ -236,17 +236,65 @@ let timeout = 20000;
|
|
|
236
236
|
const slowRequestTimeout = 2000;
|
|
237
237
|
// @ts-ignore
|
|
238
238
|
window.setRequestTimeout = (newTimeout) => timeout = newTimeout;
|
|
239
|
-
const
|
|
239
|
+
const upload = (endpoint, file, onResult, onError, extras) => {
|
|
240
|
+
apiRequest(endpoint, {
|
|
241
|
+
file,
|
|
242
|
+
onProgress: (pct) => { var _a; return (_a = extras === null || extras === void 0 ? void 0 : extras.onProgress) === null || _a === void 0 ? void 0 : _a.call(extras, pct); }
|
|
243
|
+
}, (text) => onResult === null || onResult === void 0 ? void 0 : onResult(text), (err) => onError === null || onError === void 0 ? void 0 : onError(err), undefined, // no redirect handler
|
|
244
|
+
'upload', // requestType
|
|
245
|
+
undefined, undefined, undefined, extras);
|
|
246
|
+
};
|
|
247
|
+
const apiRequest = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, options = {}, onSuccess, onError, onRedirect, requestType = 'query', handleUnauthorized = defaultHandleUnauthorized, retrial = false, retrialNum = 0, extras) {
|
|
248
|
+
url = getApiUrl() + url;
|
|
249
|
+
backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, retrial, retrialNum, extras);
|
|
250
|
+
});
|
|
251
|
+
const uploadRequest = (url, file, onSuccess, onError, extras) => {
|
|
252
|
+
var _a, _b;
|
|
253
|
+
requestRunning = true;
|
|
254
|
+
const xhr = new XMLHttpRequest();
|
|
255
|
+
xhr.open('POST', url);
|
|
256
|
+
if ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.backendToken) {
|
|
257
|
+
xhr.setRequestHeader('auth', (_b = window.juxwebGlobal) === null || _b === void 0 ? void 0 : _b.backendToken);
|
|
258
|
+
}
|
|
259
|
+
xhr.upload.onprogress = (e) => {
|
|
260
|
+
if (e.lengthComputable && (extras === null || extras === void 0 ? void 0 : extras.onProgress)) {
|
|
261
|
+
extras.onProgress(Math.round((e.loaded / e.total) * 100));
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
xhr.onload = () => {
|
|
265
|
+
requestRunning = false;
|
|
266
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
267
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(xhr.responseText);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
onError === null || onError === void 0 ? void 0 : onError(xhr.responseText);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
xhr.onerror = () => {
|
|
274
|
+
requestRunning = false;
|
|
275
|
+
onError === null || onError === void 0 ? void 0 : onError('Error sending file. Please try again.');
|
|
276
|
+
};
|
|
277
|
+
xhr.send(file);
|
|
278
|
+
if (extras === null || extras === void 0 ? void 0 : extras.onStart) {
|
|
279
|
+
extras.onStart({
|
|
280
|
+
abortUpload: () => {
|
|
281
|
+
console.log('Aborting upload');
|
|
282
|
+
xhr.abort();
|
|
283
|
+
requestRunning = false;
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
const backendRequest = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, options = {}, onSuccess, onError, onRedirect, requestType = 'query', handleUnauthorized = defaultHandleUnauthorized, retrial = false, retrialNum = 0, extras) {
|
|
289
|
+
var _a;
|
|
240
290
|
if (requestRunning && !retrial) { // only allow one request at a time
|
|
241
|
-
setTimeout(() => backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, false), 300);
|
|
291
|
+
setTimeout(() => backendRequest(url, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized, false, retrialNum), 300);
|
|
242
292
|
return;
|
|
243
293
|
}
|
|
244
294
|
// @ts-ignore
|
|
245
|
-
const
|
|
246
|
-
if (backendToken) {
|
|
247
|
-
|
|
248
|
-
options.headers = {};
|
|
249
|
-
options.headers.auth = backendToken;
|
|
295
|
+
const headers = new Headers(options.headers);
|
|
296
|
+
if ((_a = window.juxwebGlobal) === null || _a === void 0 ? void 0 : _a.backendToken) {
|
|
297
|
+
headers.set('auth', window.juxwebGlobal.backendToken);
|
|
250
298
|
}
|
|
251
299
|
let response;
|
|
252
300
|
requestRunning = true;
|
|
@@ -262,17 +310,19 @@ const backendRequest = (url_1, options_1, onSuccess_1, onError_1, onRedirect_1,
|
|
|
262
310
|
const event = new CustomEvent('slow-request', { detail: requestUrl.pathname, bubbles: true });
|
|
263
311
|
window.dispatchEvent(event);
|
|
264
312
|
}, slowRequestTimeout);
|
|
265
|
-
const requestInit = Object.assign(Object.assign({}, options), { signal: controller.signal });
|
|
313
|
+
const requestInit = Object.assign(Object.assign({}, options), { headers, signal: controller.signal });
|
|
266
314
|
if (onRedirect)
|
|
267
315
|
requestInit.redirect = 'manual'; // Treat redirects manually instead of following them automatically.
|
|
316
|
+
if (requestType === 'upload') {
|
|
317
|
+
uploadRequest(url, options.file, onSuccess, onError, extras);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
268
320
|
try {
|
|
269
321
|
response = yield fetch(url, requestInit);
|
|
270
322
|
}
|
|
271
323
|
catch (err) {
|
|
272
|
-
// network errors
|
|
273
324
|
if (requestType === 'command') {
|
|
274
|
-
|
|
275
|
-
onError('There was an error performing your request. Please try again later.');
|
|
325
|
+
onError === null || onError === void 0 ? void 0 : onError('There was an error performing your request. Please try again later.');
|
|
276
326
|
requestRunning = false;
|
|
277
327
|
return;
|
|
278
328
|
}
|
|
@@ -321,20 +371,14 @@ const backendRequest = (url_1, options_1, onSuccess_1, onError_1, onRedirect_1,
|
|
|
321
371
|
try {
|
|
322
372
|
const jsonResponse = yield response.json();
|
|
323
373
|
if (response.ok) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
}
|
|
327
|
-
else {
|
|
328
|
-
return jsonResponse;
|
|
329
|
-
}
|
|
374
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(jsonResponse);
|
|
375
|
+
return;
|
|
330
376
|
}
|
|
331
377
|
else {
|
|
332
|
-
|
|
333
|
-
onError(jsonResponse);
|
|
378
|
+
onError === null || onError === void 0 ? void 0 : onError(jsonResponse);
|
|
334
379
|
}
|
|
335
380
|
}
|
|
336
381
|
catch (err) {
|
|
337
|
-
// json conversion error / json response dealing errors / onSuccess errors
|
|
338
382
|
console.error(err);
|
|
339
383
|
}
|
|
340
384
|
finally {
|
|
@@ -357,7 +401,7 @@ const post = (endpoint, params, onSuccess, onError, onRedirect, requestType = "q
|
|
|
357
401
|
// @ts-ignore
|
|
358
402
|
options.body = content;
|
|
359
403
|
}
|
|
360
|
-
return
|
|
404
|
+
return apiRequest(endpoint, options, onSuccess, onError, onRedirect, requestType, handleUnauthorized);
|
|
361
405
|
};
|
|
362
406
|
const get = (endpoint, onSuccess, onError, onRedirect, handleUnauthorized) => {
|
|
363
407
|
const options = {
|
|
@@ -367,7 +411,7 @@ const get = (endpoint, onSuccess, onError, onRedirect, handleUnauthorized) => {
|
|
|
367
411
|
'Content-Type': 'application/json',
|
|
368
412
|
}
|
|
369
413
|
};
|
|
370
|
-
return
|
|
414
|
+
return apiRequest(endpoint, options, onSuccess, onError, onRedirect, 'query', handleUnauthorized);
|
|
371
415
|
};
|
|
372
416
|
const backendPost = (endpoint, postContent, onJsonResponse, onHelpMessage, handleUnauthorized) => {
|
|
373
417
|
post(endpoint, postContent, onJsonResponse, onHelpMessage, null, 'command', handleUnauthorized);
|
|
@@ -385,9 +429,15 @@ const backendGet = (endpoint, params, onJsonResponse, onHelpMessage, handleUnaut
|
|
|
385
429
|
};
|
|
386
430
|
const requestMagicLink = (data, onJsonResponse, onUnauthorized) => {
|
|
387
431
|
const { email, token } = data;
|
|
388
|
-
const url = getMagicAuthReqUrl() +
|
|
432
|
+
const url = getMagicAuthReqUrl() +
|
|
433
|
+
encodeURIComponent(email) +
|
|
434
|
+
'&destination=' +
|
|
435
|
+
extractTokenFromWindowLocation('destination') +
|
|
436
|
+
'&token=' +
|
|
437
|
+
token;
|
|
389
438
|
const options = {};
|
|
390
|
-
backendRequest(url, options, onJsonResponse, console.error,
|
|
439
|
+
backendRequest(url, options, onJsonResponse, console.error, undefined, // ← use undefined instead of null
|
|
440
|
+
'query', onUnauthorized);
|
|
391
441
|
};
|
|
392
442
|
const openMagicLink = (magicToken, onJsonResponse, onHelpMessage) => {
|
|
393
443
|
backendRequest(getMagicAuthUrl() + magicToken, {}, onJsonResponse, onHelpMessage);
|
|
@@ -505,7 +555,7 @@ const onUserChanged = (googleUser, onUserLogin) => {
|
|
|
505
555
|
const onSuccess = (res) => {
|
|
506
556
|
const { token } = res;
|
|
507
557
|
// @ts-ignore
|
|
508
|
-
window.
|
|
558
|
+
window.juxwebGlobal.currentUser = res;
|
|
509
559
|
onTokenAcquired(token, onUserLogin);
|
|
510
560
|
};
|
|
511
561
|
const onError = (err) => {
|
|
@@ -860,9 +910,9 @@ const contactUsModal = (subject) => {
|
|
|
860
910
|
var _a, _b, _c, _d, _e;
|
|
861
911
|
const result = document.createElement('contact-us-modal');
|
|
862
912
|
// @ts-ignore
|
|
863
|
-
const currentName = window.
|
|
913
|
+
const currentName = window.juxwebGlobal.currentUser ? window.juxwebGlobal.currentUser.name : '';
|
|
864
914
|
// @ts-ignore
|
|
865
|
-
const currentEmail = window.
|
|
915
|
+
const currentEmail = window.juxwebGlobal.currentUser ? window.juxwebGlobal.currentUser.email : (_a = document.querySelector('#magic-link-email')) === null || _a === void 0 ? void 0 : _a.textContent;
|
|
866
916
|
const modalBody = document.createElement('modal-body');
|
|
867
917
|
const form = document.createElement('form');
|
|
868
918
|
form.id = 'contact-form';
|
|
@@ -882,7 +932,7 @@ const contactUsModal = (subject) => {
|
|
|
882
932
|
const captcha = googleCaptcha();
|
|
883
933
|
if (subject !== 'email-not-sent') {
|
|
884
934
|
// @ts-ignore
|
|
885
|
-
const admin = ((_d = (_c = window.
|
|
935
|
+
const admin = ((_d = (_c = window.juxwebGlobal) === null || _c === void 0 ? void 0 : _c.selectedTeam) === null || _d === void 0 ? void 0 : _d.admin) || window.juxwebGlobal.currentUser.email;
|
|
886
936
|
backendGet('credits-balance-for', { admin }, (res) => {
|
|
887
937
|
const balance = res;
|
|
888
938
|
if (balance <= 0) {
|
|
@@ -1016,7 +1066,7 @@ const unauthorizedMagicLinkRequestPage = (backgroundImg, onReturn, failInfo) =>
|
|
|
1016
1066
|
|
|
1017
1067
|
const setBackendToken = (token) => {
|
|
1018
1068
|
// @ts-ignore
|
|
1019
|
-
window.
|
|
1069
|
+
window.juxwebGlobal.backendToken = token;
|
|
1020
1070
|
if (token) {
|
|
1021
1071
|
setLocalStorageItem('token', token);
|
|
1022
1072
|
}
|
|
@@ -1025,7 +1075,7 @@ const getBackendToken = () => {
|
|
|
1025
1075
|
var _a;
|
|
1026
1076
|
const token = (_a = getLocalStorageItem('token')) !== null && _a !== void 0 ? _a : '';
|
|
1027
1077
|
// @ts-ignore
|
|
1028
|
-
window.
|
|
1078
|
+
window.juxwebGlobal.backendToken = token;
|
|
1029
1079
|
return token;
|
|
1030
1080
|
};
|
|
1031
1081
|
const onTokenAcquired = (token, onUserLogin) => {
|
|
@@ -1036,7 +1086,7 @@ const onTokenAcquired = (token, onUserLogin) => {
|
|
|
1036
1086
|
const clearSession = () => {
|
|
1037
1087
|
removeLocalStorageItem('token');
|
|
1038
1088
|
// @ts-ignore
|
|
1039
|
-
window.
|
|
1089
|
+
window.juxwebGlobal = {};
|
|
1040
1090
|
};
|
|
1041
1091
|
const onAuthenticationFailure = (msg) => {
|
|
1042
1092
|
removeLocalStorageItem('token');
|
|
@@ -1048,7 +1098,7 @@ const onAuthentication = (onUserLogin, user, type) => {
|
|
|
1048
1098
|
clearSession();
|
|
1049
1099
|
}
|
|
1050
1100
|
// @ts-ignore
|
|
1051
|
-
window.
|
|
1101
|
+
window.juxwebGlobal.currentUser = user;
|
|
1052
1102
|
if (type === 'Token Authentication') {
|
|
1053
1103
|
user.token = getLocalStorageItem('token');
|
|
1054
1104
|
}
|
|
@@ -1088,16 +1138,16 @@ const initSession = ({ clientApp, supportedLoginTypes, onUserLogin, onLoginError
|
|
|
1088
1138
|
// return;
|
|
1089
1139
|
// }
|
|
1090
1140
|
// @ts-ignore
|
|
1091
|
-
if (!window.
|
|
1141
|
+
if (!window.juxwebGlobal) {
|
|
1092
1142
|
// @ts-ignore
|
|
1093
|
-
window.
|
|
1143
|
+
window.juxwebGlobal = {};
|
|
1094
1144
|
}
|
|
1095
1145
|
// @ts-ignore
|
|
1096
|
-
window.
|
|
1146
|
+
window.juxwebGlobal.fetchUserBackendUrl = fetchUserBackendUrl;
|
|
1097
1147
|
// @ts-ignore
|
|
1098
|
-
window.
|
|
1148
|
+
window.juxwebGlobal.magicLinkRequestEndpoint = magicLinkRequestEndpoint;
|
|
1099
1149
|
// @ts-ignore
|
|
1100
|
-
window.
|
|
1150
|
+
window.juxwebGlobal.magicLinkAuthEndpoint = magicLinkAuthEndpoint;
|
|
1101
1151
|
if (urlPrefix)
|
|
1102
1152
|
setUrlPrefix(urlPrefix);
|
|
1103
1153
|
const signInToken = extractTokenFromWindowLocation('sign-in') || extractTokenFromWindowLocation('magic-link');
|
|
@@ -1108,7 +1158,7 @@ const initSession = ({ clientApp, supportedLoginTypes, onUserLogin, onLoginError
|
|
|
1108
1158
|
const host = extractTokenFromWindowLocation('host');
|
|
1109
1159
|
if (host) {
|
|
1110
1160
|
// @ts-ignore
|
|
1111
|
-
window.
|
|
1161
|
+
window.juxwebGlobal.fetchUserBackendUrl({ host }, (backendUrl) => {
|
|
1112
1162
|
setBackendUrl(backendUrl);
|
|
1113
1163
|
onOpenMagicLink();
|
|
1114
1164
|
});
|
|
@@ -1153,7 +1203,7 @@ const onMicrosoftSignIn = (onUserLogin) => __awaiter(void 0, void 0, void 0, fun
|
|
|
1153
1203
|
const onSuccess = (res) => {
|
|
1154
1204
|
const { token } = res;
|
|
1155
1205
|
// @ts-ignore
|
|
1156
|
-
window.
|
|
1206
|
+
window.juxwebGlobal.currentUser = res;
|
|
1157
1207
|
onTokenAcquired(token, onUserLogin);
|
|
1158
1208
|
disableSignInLayout();
|
|
1159
1209
|
};
|
|
@@ -1186,7 +1236,7 @@ const onAuthPasswordLogin = (credentials, onUserLogin, onLoginError, onReturn, c
|
|
|
1186
1236
|
const onSuccess = (res) => {
|
|
1187
1237
|
const { token } = res;
|
|
1188
1238
|
// @ts-ignore
|
|
1189
|
-
window.
|
|
1239
|
+
window.juxwebGlobal.currentUser = res;
|
|
1190
1240
|
onTokenAcquired(token, onUserLogin);
|
|
1191
1241
|
disableSignInLayout();
|
|
1192
1242
|
};
|
|
@@ -1221,9 +1271,9 @@ const handleMagicLinkRequest = (token, onReturn, backgroundImage, currentPage, c
|
|
|
1221
1271
|
requestMagicLink(payload, onSuccess, onUnauthorized);
|
|
1222
1272
|
};
|
|
1223
1273
|
// @ts-ignore
|
|
1224
|
-
if (window.
|
|
1274
|
+
if (window.juxwebGlobal.fetchUserBackendUrl) {
|
|
1225
1275
|
// @ts-ignore
|
|
1226
|
-
window.
|
|
1276
|
+
window.juxwebGlobal.fetchUserBackendUrl({ email }, (backendUrl) => {
|
|
1227
1277
|
setBackendUrl(backendUrl);
|
|
1228
1278
|
onRequestMagicLink();
|
|
1229
1279
|
});
|
|
@@ -1456,7 +1506,7 @@ const loginPage = (clientApp, backgroundImg, onUserLogin, onLoginError, loginTyp
|
|
|
1456
1506
|
};
|
|
1457
1507
|
|
|
1458
1508
|
class JuxEvent extends Event {
|
|
1459
|
-
constructor(type, method, endpoint, onResult, params, onError, onRedirect) {
|
|
1509
|
+
constructor(type, method, endpoint, onResult, params, onError, onRedirect, extras) {
|
|
1460
1510
|
super('jux-event', { bubbles: true, cancelable: true, composed: true });
|
|
1461
1511
|
this.requestType = type;
|
|
1462
1512
|
this.method = method;
|
|
@@ -1465,6 +1515,7 @@ class JuxEvent extends Event {
|
|
|
1465
1515
|
this.params = params;
|
|
1466
1516
|
this.onError = onError;
|
|
1467
1517
|
this.onRedirect = onRedirect;
|
|
1518
|
+
this.extras = extras;
|
|
1468
1519
|
}
|
|
1469
1520
|
}
|
|
1470
1521
|
const query = (element, endpoint, onResult, params, onError, onRedirect) => {
|
|
@@ -1489,3 +1540,4 @@ exports.onMicrosoftSignIn = onMicrosoftSignIn;
|
|
|
1489
1540
|
exports.query = query;
|
|
1490
1541
|
exports.reloadWithLanguageOverride = reloadWithLanguageOverride;
|
|
1491
1542
|
exports.requestMagicLink = requestMagicLink;
|
|
1543
|
+
exports.upload = upload;
|