@klauswuestefeld/jux-web 2024.2.5-1 → 2024.2.6-1

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.
@@ -1,10 +1,10 @@
1
1
  export declare const setBackendUrl: (backendUrl: string) => void;
2
- export declare const getGoogleAuthUrl: () => string;
3
- export declare const getMicrosoftAuthUrl: () => string;
2
+ export declare const handleJuxEvents: (ev: Event) => void;
4
3
  export declare const backendRequest: (requestType: string, url: string, postContent: any, onJsonResponse: (response: any) => any, onHelpMessage: (message: string) => any) => void;
5
4
  export declare const backendPost: (endpoint: string, postContent: any, onJsonResponse: (response: any) => any, onHelpMessage: (message: string) => any) => void;
6
- export declare const backendGet: (endpoint: string, onJsonResponse: (response: any) => any, onHelpMessage: (message: string) => any) => void;
7
- export declare const backendGetPromise: (endpoint: string) => Promise<unknown>;
5
+ export declare const backendGet: (endpoint: string, params: any, onJsonResponse: (response: any) => any, onHelpMessage: (message: string) => any) => void;
8
6
  export declare const requestMagicLink: (data: any, onJsonResponse: (response: any) => any) => void;
9
7
  export declare const openMagicLink: (magicToken: string, onJsonResponse: (response: any) => any, onHelpMessage: (message: string) => any) => void;
8
+ export declare const googleLogin: (token: string, onLogin: (response: any) => any, onLoginError: (message: string) => any) => void;
9
+ export declare const microsoftLogin: (token: string, onLogin: (response: any) => any, onLoginError: (message: string) => any) => void;
10
10
  export declare const getMXData: (domainName: string) => Promise<any>;
@@ -0,0 +1,10 @@
1
+ export declare class JuxEvent extends Event {
2
+ method: string;
3
+ endpoint: string;
4
+ onResult?: (result: any) => any;
5
+ params: any;
6
+ onError?: (error: any) => void;
7
+ constructor(method: string, endpoint: string, onResult?: (result: any) => any, params?: any, onError?: (error: any) => void);
8
+ }
9
+ export declare const query: (element: HTMLElement, endpoint: string, onResult: (result: any) => any, params?: any, onError?: ((error: any) => void) | undefined) => void;
10
+ export declare const command: (element: HTMLElement, endpoint: string, onResult?: ((result: any) => any) | undefined, params?: any, onError?: ((error: any) => void) | undefined) => void;
package/dist/jux-web.js CHANGED
@@ -144,10 +144,8 @@ var juxWeb = (function (exports) {
144
144
  const getBackendUrl = () => process.env.BACKEND_URL || localStorage.getItem('backend-url');
145
145
  //@ts-ignore
146
146
  const getSecondaryBackendUrl = () => process.env.STAGING_BACKEND_URL || getBackendUrl();
147
- let getBackendUrlToTry = () => getBackendUrl();
148
147
  const getApiUrl = () => getBackendUrl() + 'api/';
149
- const getGoogleAuthUrl = () => getBackendUrl() + 'auth-google?google-id-token=';
150
- const getMicrosoftAuthUrl = () => getBackendUrl() + 'auth-microsoft?access-token=';
148
+ let getBackendUrlToTry = () => getBackendUrl();
151
149
  const getMagicAuthUrl = () => {
152
150
  var _a;
153
151
  // @ts-ignore
@@ -160,6 +158,17 @@ var juxWeb = (function (exports) {
160
158
  const endpoint = ((_a = window === null || window === void 0 ? void 0 : window.store) === null || _a === void 0 ? void 0 : _a.magicLinkRequestEndpoint) || 'magic-link-request';
161
159
  return getBackendUrl() + endpoint + '?email=';
162
160
  };
161
+ const handleJuxEvents = (ev) => {
162
+ const { method, endpoint, onResult, params, onError } = ev;
163
+ const onJsonResponse = (res) => onResult ? onResult(res) : null;
164
+ const onHelpMessage = (err) => onError ? onError(err) : null;
165
+ if (method === 'GET') {
166
+ backendGet(endpoint, params, onJsonResponse, onHelpMessage);
167
+ }
168
+ if (method === 'POST') {
169
+ backendPost(endpoint, params, onJsonResponse, onHelpMessage);
170
+ }
171
+ };
163
172
  let timeout;
164
173
  const maxRetries = 20;
165
174
  let retries = 0;
@@ -253,19 +262,22 @@ var juxWeb = (function (exports) {
253
262
  const backendPost = (endpoint, postContent, onJsonResponse, onHelpMessage) => {
254
263
  backendRequest('POST', getApiUrl() + endpoint, postContent, onJsonResponse, onHelpMessage);
255
264
  };
256
- const backendGet = (endpoint, onJsonResponse, onHelpMessage) => {
257
- let api = getApiUrl() + endpoint;
258
- if (endpoint.includes(getGoogleAuthUrl()) || endpoint.includes(getMicrosoftAuthUrl())) {
259
- api = endpoint;
260
- }
261
- backendRequest('GET', api, undefined, onJsonResponse, onHelpMessage);
265
+ const backendGet = (endpoint, params, onJsonResponse, onHelpMessage) => {
266
+ backendRequest('GET', getApiUrl() + endpoint, params, onJsonResponse, onHelpMessage);
262
267
  };
263
- const backendGetPromise = (endpoint) => new Promise((resolve, reject) => backendGet(endpoint, resolve, reject));
264
268
  const requestMagicLink = (data, onJsonResponse) => {
265
- backendRequest('GET', getMagicAuthReqUrl() + encodeURIComponent(data.email) + '&destination=' + extractTokenFromWindowLocation('destination') + '&token=' + data.token, undefined, onJsonResponse, () => { });
269
+ backendRequest('GET', getMagicAuthReqUrl() + encodeURIComponent(data.email) + '&destination=' + extractTokenFromWindowLocation('destination') + '&token=' + data.token, null, onJsonResponse, () => { });
266
270
  };
267
271
  const openMagicLink = (magicToken, onJsonResponse, onHelpMessage) => {
268
- backendRequest('GET', getMagicAuthUrl() + magicToken, undefined, onJsonResponse, onHelpMessage);
272
+ backendRequest('GET', getMagicAuthUrl() + magicToken, null, onJsonResponse, onHelpMessage);
273
+ };
274
+ const googleLogin = (token, onLogin, onLoginError) => {
275
+ const url = getBackendUrl() + 'auth-google?google-id-token=' + token;
276
+ backendRequest('GET', url, null, onLogin, onLoginError);
277
+ };
278
+ const microsoftLogin = (token, onLogin, onLoginError) => {
279
+ const url = getBackendUrl() + 'auth-microsoft?access-token=' + token;
280
+ backendRequest('GET', url, null, onLogin, onLoginError);
269
281
  };
270
282
  const getMXData = (domainName) => __awaiter$2(void 0, void 0, void 0, function* () {
271
283
  try {
@@ -13674,27 +13686,23 @@ var juxWeb = (function (exports) {
13674
13686
  googletoken: gUser.getAuthResponse().id_token
13675
13687
  };
13676
13688
  };
13677
- const onUserChanged = (googleUser, onUserLogin) => __awaiter$2(void 0, void 0, void 0, function* () {
13689
+ const onUserChanged = (googleUser, onUserLogin) => {
13678
13690
  document.documentElement.classList.remove('progress');
13679
13691
  document.body.classList.remove('disabled');
13680
13692
  if (googleUser) {
13681
13693
  // mixpanelIdentify(googleUser); TODO
13682
- try {
13683
- yield backendGetPromise(getGoogleAuthUrl() + googleUser.googletoken)
13684
- .then(res => {
13685
- const { token } = res;
13686
- // @ts-ignore
13687
- window.store.currentUser = res;
13688
- onTokenAcquired(token, onUserLogin);
13689
- })
13690
- .catch((error) => { throw Error(error); });
13691
- }
13692
- catch (error) {
13693
- console.error(error);
13694
+ const onSuccess = (res) => {
13695
+ const { token } = res;
13696
+ // @ts-ignore
13697
+ window.store.currentUser = res;
13698
+ onTokenAcquired(token, onUserLogin);
13699
+ };
13700
+ const onError = (err) => {
13701
+ console.error(err);
13694
13702
  // logError(error); TODO
13695
13703
  // checkMixpanel(() => mixpanel.track('Login Error', { 'Login Type': 'Google', 'Error': error })); TODO
13696
- return;
13697
- }
13704
+ };
13705
+ googleLogin(googleUser.googletoken, onSuccess, onError);
13698
13706
  // checkMixpanel(() => mixpanel.track('Login', { 'Login Type': 'Google' })); TODO
13699
13707
  }
13700
13708
  else {
@@ -13702,7 +13710,7 @@ var juxWeb = (function (exports) {
13702
13710
  // checkMixpanel(() => mixpanel.reset()); TODO
13703
13711
  // displayPage(Page.LOGIN); TODO
13704
13712
  }
13705
- });
13713
+ };
13706
13714
  const onGoogleUserChanged = (gUser, onUserLogin) => {
13707
13715
  onUserChanged(gUser.isSignedIn() ? highLevelUser(gUser) : null, onUserLogin);
13708
13716
  };
@@ -14064,7 +14072,7 @@ var juxWeb = (function (exports) {
14064
14072
  if (subject !== 'email-not-sent') {
14065
14073
  // @ts-ignore
14066
14074
  const admin = ((_d = (_c = window.store) === null || _c === void 0 ? void 0 : _c.selectedTeam) === null || _d === void 0 ? void 0 : _d.admin) || window.store.currentUser.email;
14067
- backendGet(`credits-balance-for?admin=${admin}`, (res) => {
14075
+ backendGet(`credits-balance-for?admin=${admin}`, null, (res) => {
14068
14076
  const balance = res;
14069
14077
  if (balance <= 0) {
14070
14078
  form.insertBefore(captcha, btn);
@@ -14206,6 +14214,10 @@ var juxWeb = (function (exports) {
14206
14214
  Array.from(clientApp.children).forEach(el => el.remove());
14207
14215
  clientApp.appendChild(page);
14208
14216
  };
14217
+ const setJuxEventHandler = () => {
14218
+ document.removeEventListener('jux-event', handleJuxEvents);
14219
+ document.addEventListener('jux-event', handleJuxEvents);
14220
+ };
14209
14221
  const initSession = (clientApp, supportedLoginTypes, onUserLogin, backgroundImage, fetchUserBackendUrl, magicLinkRequestEndpoint, magicLinkAuthEndpoint) => {
14210
14222
  // if (startNewDemo()) {
14211
14223
  // initDemo(setBackendToken, onAuthentication);
@@ -14222,10 +14234,12 @@ var juxWeb = (function (exports) {
14222
14234
  window.store.magicLinkRequestEndpoint = magicLinkRequestEndpoint;
14223
14235
  // @ts-ignore
14224
14236
  window.store.magicLinkAuthEndpoint = magicLinkAuthEndpoint;
14225
- const magicToken = extractTokenFromWindowLocation('magic-link');
14226
- if (magicToken) {
14237
+ // TODO: use jux-events to call commands and queries
14238
+ setJuxEventHandler();
14239
+ const signInToken = extractTokenFromWindowLocation('sign-in') || extractTokenFromWindowLocation('magic-link');
14240
+ if (signInToken) {
14227
14241
  const onOpenMagicLink = () => {
14228
- openMagicLink(magicToken, (res) => onAuthentication(onUserLogin, res, 'Magic Link'), () => onAuthenticationFailure('unable-magic-login'));
14242
+ openMagicLink(signInToken, (res) => onAuthentication(onUserLogin, res, 'Magic Link'), () => onAuthenticationFailure('unable-magic-login'));
14229
14243
  };
14230
14244
  const host = extractTokenFromWindowLocation('host');
14231
14245
  if (host) {
@@ -14240,7 +14254,7 @@ var juxWeb = (function (exports) {
14240
14254
  return;
14241
14255
  }
14242
14256
  if (getBackendToken()) {
14243
- backendGet('profile', (res) => onAuthentication(onUserLogin, res, 'Token Authentication'), (_err) => onAuthenticationFailure('login-failed'));
14257
+ backendGet('profile', null, (res) => onAuthentication(onUserLogin, res, 'Token Authentication'), (_err) => onAuthenticationFailure('login-failed'));
14244
14258
  return;
14245
14259
  }
14246
14260
  displayPage(clientApp, loginPage(clientApp, backgroundImage, onUserLogin, supportedLoginTypes));
@@ -14264,15 +14278,19 @@ var juxWeb = (function (exports) {
14264
14278
  try {
14265
14279
  const loginResponse = yield msalInstance.loginPopup(loginRequest);
14266
14280
  const { accessToken } = loginResponse;
14267
- yield backendGetPromise(getMicrosoftAuthUrl() + accessToken)
14268
- .then((res) => {
14281
+ const onSuccess = (res) => {
14269
14282
  const { token } = res;
14270
14283
  // @ts-ignore
14271
14284
  window.store.currentUser = res;
14272
14285
  onTokenAcquired(token, onUserLogin);
14273
- })
14274
- .catch(console.error)
14275
- .finally(() => disableSignInLayout());
14286
+ disableSignInLayout();
14287
+ };
14288
+ const onError = (err) => {
14289
+ console.error(err);
14290
+ reportError(err);
14291
+ disableSignInLayout();
14292
+ };
14293
+ microsoftLogin(accessToken, onSuccess, onError);
14276
14294
  }
14277
14295
  catch (err) {
14278
14296
  reportError(err);
@@ -14521,19 +14539,25 @@ var juxWeb = (function (exports) {
14521
14539
  return result;
14522
14540
  };
14523
14541
 
14524
- class Query extends Event {
14525
- constructor(endpoint, onResult, params, onError) {
14526
- super(endpoint); // if needed, it is possible to modify bubbles, cancelable and composed properties
14527
- this.params = params;
14542
+ class JuxEvent extends Event {
14543
+ constructor(method, endpoint, onResult, params, onError) {
14544
+ super('jux-event'); // in the future maybe we might need to modify bubbles, cancelable and composed properties
14545
+ this.method = method;
14546
+ this.endpoint = endpoint;
14528
14547
  this.onResult = onResult;
14548
+ this.params = params;
14529
14549
  this.onError = onError;
14530
14550
  }
14531
14551
  }
14532
14552
  const query = (element, endpoint, onResult, params, onError) => {
14533
- element.dispatchEvent(new Query(endpoint, onResult, params, onError));
14553
+ element.dispatchEvent(new JuxEvent('GET', endpoint, onResult, params, onError));
14554
+ };
14555
+ const command = (element, endpoint, onResult, params, onError) => {
14556
+ element.dispatchEvent(new JuxEvent('POST', endpoint, onResult, params, onError));
14534
14557
  };
14535
14558
 
14536
14559
  exports.chosenLanguage = chosenLanguage;
14560
+ exports.command = command;
14537
14561
  exports.getTranslation = getTranslation;
14538
14562
  exports.handleMagicLinkRequest = handleMagicLinkRequest;
14539
14563
  exports.initLanguage = initLanguage;
package/dist/main.d.ts CHANGED
@@ -4,7 +4,7 @@ export { loginPage } from './login/login-page';
4
4
  export { handleMagicLinkRequest, initSession, onMicrosoftSignIn } from './login/session';
5
5
  export { requestMagicLink } from './api-client';
6
6
  export { magicLinkModal } from './login/magic-link-modal';
7
- export { query } from './jux/query';
7
+ export { command, query } from './jux/jux-event';
8
8
  declare global {
9
9
  interface Window {
10
10
  submitCaptcha: any;
package/dist/main.esm.js CHANGED
@@ -143,10 +143,8 @@ const setBackendUrl = (backendUrl) => localStorage.setItem('backend-url', backen
143
143
  const getBackendUrl = () => process.env.BACKEND_URL || localStorage.getItem('backend-url');
144
144
  //@ts-ignore
145
145
  const getSecondaryBackendUrl = () => process.env.STAGING_BACKEND_URL || getBackendUrl();
146
- let getBackendUrlToTry = () => getBackendUrl();
147
146
  const getApiUrl = () => getBackendUrl() + 'api/';
148
- const getGoogleAuthUrl = () => getBackendUrl() + 'auth-google?google-id-token=';
149
- const getMicrosoftAuthUrl = () => getBackendUrl() + 'auth-microsoft?access-token=';
147
+ let getBackendUrlToTry = () => getBackendUrl();
150
148
  const getMagicAuthUrl = () => {
151
149
  var _a;
152
150
  // @ts-ignore
@@ -159,6 +157,17 @@ const getMagicAuthReqUrl = () => {
159
157
  const endpoint = ((_a = window === null || window === void 0 ? void 0 : window.store) === null || _a === void 0 ? void 0 : _a.magicLinkRequestEndpoint) || 'magic-link-request';
160
158
  return getBackendUrl() + endpoint + '?email=';
161
159
  };
160
+ const handleJuxEvents = (ev) => {
161
+ const { method, endpoint, onResult, params, onError } = ev;
162
+ const onJsonResponse = (res) => onResult ? onResult(res) : null;
163
+ const onHelpMessage = (err) => onError ? onError(err) : null;
164
+ if (method === 'GET') {
165
+ backendGet(endpoint, params, onJsonResponse, onHelpMessage);
166
+ }
167
+ if (method === 'POST') {
168
+ backendPost(endpoint, params, onJsonResponse, onHelpMessage);
169
+ }
170
+ };
162
171
  let timeout;
163
172
  const maxRetries = 20;
164
173
  let retries = 0;
@@ -252,19 +261,22 @@ const backendRequest = (requestType, url, postContent, onJsonResponse, onHelpMes
252
261
  const backendPost = (endpoint, postContent, onJsonResponse, onHelpMessage) => {
253
262
  backendRequest('POST', getApiUrl() + endpoint, postContent, onJsonResponse, onHelpMessage);
254
263
  };
255
- const backendGet = (endpoint, onJsonResponse, onHelpMessage) => {
256
- let api = getApiUrl() + endpoint;
257
- if (endpoint.includes(getGoogleAuthUrl()) || endpoint.includes(getMicrosoftAuthUrl())) {
258
- api = endpoint;
259
- }
260
- backendRequest('GET', api, undefined, onJsonResponse, onHelpMessage);
264
+ const backendGet = (endpoint, params, onJsonResponse, onHelpMessage) => {
265
+ backendRequest('GET', getApiUrl() + endpoint, params, onJsonResponse, onHelpMessage);
261
266
  };
262
- const backendGetPromise = (endpoint) => new Promise((resolve, reject) => backendGet(endpoint, resolve, reject));
263
267
  const requestMagicLink = (data, onJsonResponse) => {
264
- backendRequest('GET', getMagicAuthReqUrl() + encodeURIComponent(data.email) + '&destination=' + extractTokenFromWindowLocation('destination') + '&token=' + data.token, undefined, onJsonResponse, () => { });
268
+ backendRequest('GET', getMagicAuthReqUrl() + encodeURIComponent(data.email) + '&destination=' + extractTokenFromWindowLocation('destination') + '&token=' + data.token, null, onJsonResponse, () => { });
265
269
  };
266
270
  const openMagicLink = (magicToken, onJsonResponse, onHelpMessage) => {
267
- backendRequest('GET', getMagicAuthUrl() + magicToken, undefined, onJsonResponse, onHelpMessage);
271
+ backendRequest('GET', getMagicAuthUrl() + magicToken, null, onJsonResponse, onHelpMessage);
272
+ };
273
+ const googleLogin = (token, onLogin, onLoginError) => {
274
+ const url = getBackendUrl() + 'auth-google?google-id-token=' + token;
275
+ backendRequest('GET', url, null, onLogin, onLoginError);
276
+ };
277
+ const microsoftLogin = (token, onLogin, onLoginError) => {
278
+ const url = getBackendUrl() + 'auth-microsoft?access-token=' + token;
279
+ backendRequest('GET', url, null, onLogin, onLoginError);
268
280
  };
269
281
  const getMXData = (domainName) => __awaiter(void 0, void 0, void 0, function* () {
270
282
  try {
@@ -354,27 +366,23 @@ const highLevelUser = (gUser) => {
354
366
  googletoken: gUser.getAuthResponse().id_token
355
367
  };
356
368
  };
357
- const onUserChanged = (googleUser, onUserLogin) => __awaiter(void 0, void 0, void 0, function* () {
369
+ const onUserChanged = (googleUser, onUserLogin) => {
358
370
  document.documentElement.classList.remove('progress');
359
371
  document.body.classList.remove('disabled');
360
372
  if (googleUser) {
361
373
  // mixpanelIdentify(googleUser); TODO
362
- try {
363
- yield backendGetPromise(getGoogleAuthUrl() + googleUser.googletoken)
364
- .then(res => {
365
- const { token } = res;
366
- // @ts-ignore
367
- window.store.currentUser = res;
368
- onTokenAcquired(token, onUserLogin);
369
- })
370
- .catch((error) => { throw Error(error); });
371
- }
372
- catch (error) {
373
- console.error(error);
374
+ const onSuccess = (res) => {
375
+ const { token } = res;
376
+ // @ts-ignore
377
+ window.store.currentUser = res;
378
+ onTokenAcquired(token, onUserLogin);
379
+ };
380
+ const onError = (err) => {
381
+ console.error(err);
374
382
  // logError(error); TODO
375
383
  // checkMixpanel(() => mixpanel.track('Login Error', { 'Login Type': 'Google', 'Error': error })); TODO
376
- return;
377
- }
384
+ };
385
+ googleLogin(googleUser.googletoken, onSuccess, onError);
378
386
  // checkMixpanel(() => mixpanel.track('Login', { 'Login Type': 'Google' })); TODO
379
387
  }
380
388
  else {
@@ -382,7 +390,7 @@ const onUserChanged = (googleUser, onUserLogin) => __awaiter(void 0, void 0, voi
382
390
  // checkMixpanel(() => mixpanel.reset()); TODO
383
391
  // displayPage(Page.LOGIN); TODO
384
392
  }
385
- });
393
+ };
386
394
  const onGoogleUserChanged = (gUser, onUserLogin) => {
387
395
  onUserChanged(gUser.isSignedIn() ? highLevelUser(gUser) : null, onUserLogin);
388
396
  };
@@ -744,7 +752,7 @@ const contactUsModal = (subject) => {
744
752
  if (subject !== 'email-not-sent') {
745
753
  // @ts-ignore
746
754
  const admin = ((_d = (_c = window.store) === null || _c === void 0 ? void 0 : _c.selectedTeam) === null || _d === void 0 ? void 0 : _d.admin) || window.store.currentUser.email;
747
- backendGet(`credits-balance-for?admin=${admin}`, (res) => {
755
+ backendGet(`credits-balance-for?admin=${admin}`, null, (res) => {
748
756
  const balance = res;
749
757
  if (balance <= 0) {
750
758
  form.insertBefore(captcha, btn);
@@ -886,6 +894,10 @@ const displayPage = (clientApp, page) => {
886
894
  Array.from(clientApp.children).forEach(el => el.remove());
887
895
  clientApp.appendChild(page);
888
896
  };
897
+ const setJuxEventHandler = () => {
898
+ document.removeEventListener('jux-event', handleJuxEvents);
899
+ document.addEventListener('jux-event', handleJuxEvents);
900
+ };
889
901
  const initSession = (clientApp, supportedLoginTypes, onUserLogin, backgroundImage, fetchUserBackendUrl, magicLinkRequestEndpoint, magicLinkAuthEndpoint) => {
890
902
  // if (startNewDemo()) {
891
903
  // initDemo(setBackendToken, onAuthentication);
@@ -902,10 +914,12 @@ const initSession = (clientApp, supportedLoginTypes, onUserLogin, backgroundImag
902
914
  window.store.magicLinkRequestEndpoint = magicLinkRequestEndpoint;
903
915
  // @ts-ignore
904
916
  window.store.magicLinkAuthEndpoint = magicLinkAuthEndpoint;
905
- const magicToken = extractTokenFromWindowLocation('magic-link');
906
- if (magicToken) {
917
+ // TODO: use jux-events to call commands and queries
918
+ setJuxEventHandler();
919
+ const signInToken = extractTokenFromWindowLocation('sign-in') || extractTokenFromWindowLocation('magic-link');
920
+ if (signInToken) {
907
921
  const onOpenMagicLink = () => {
908
- openMagicLink(magicToken, (res) => onAuthentication(onUserLogin, res, 'Magic Link'), () => onAuthenticationFailure('unable-magic-login'));
922
+ openMagicLink(signInToken, (res) => onAuthentication(onUserLogin, res, 'Magic Link'), () => onAuthenticationFailure('unable-magic-login'));
909
923
  };
910
924
  const host = extractTokenFromWindowLocation('host');
911
925
  if (host) {
@@ -920,7 +934,7 @@ const initSession = (clientApp, supportedLoginTypes, onUserLogin, backgroundImag
920
934
  return;
921
935
  }
922
936
  if (getBackendToken()) {
923
- backendGet('profile', (res) => onAuthentication(onUserLogin, res, 'Token Authentication'), (_err) => onAuthenticationFailure('login-failed'));
937
+ backendGet('profile', null, (res) => onAuthentication(onUserLogin, res, 'Token Authentication'), (_err) => onAuthenticationFailure('login-failed'));
924
938
  return;
925
939
  }
926
940
  displayPage(clientApp, loginPage(clientApp, backgroundImage, onUserLogin, supportedLoginTypes));
@@ -944,15 +958,19 @@ const onMicrosoftSignIn = (onUserLogin) => __awaiter(void 0, void 0, void 0, fun
944
958
  try {
945
959
  const loginResponse = yield msalInstance.loginPopup(loginRequest);
946
960
  const { accessToken } = loginResponse;
947
- yield backendGetPromise(getMicrosoftAuthUrl() + accessToken)
948
- .then((res) => {
961
+ const onSuccess = (res) => {
949
962
  const { token } = res;
950
963
  // @ts-ignore
951
964
  window.store.currentUser = res;
952
965
  onTokenAcquired(token, onUserLogin);
953
- })
954
- .catch(console.error)
955
- .finally(() => disableSignInLayout());
966
+ disableSignInLayout();
967
+ };
968
+ const onError = (err) => {
969
+ console.error(err);
970
+ reportError(err);
971
+ disableSignInLayout();
972
+ };
973
+ microsoftLogin(accessToken, onSuccess, onError);
956
974
  }
957
975
  catch (err) {
958
976
  reportError(err);
@@ -1201,16 +1219,21 @@ const loginPage = (clientApp, backgroundImg, onUserLogin, loginTypes) => {
1201
1219
  return result;
1202
1220
  };
1203
1221
 
1204
- class Query extends Event {
1205
- constructor(endpoint, onResult, params, onError) {
1206
- super(endpoint); // if needed, it is possible to modify bubbles, cancelable and composed properties
1207
- this.params = params;
1222
+ class JuxEvent extends Event {
1223
+ constructor(method, endpoint, onResult, params, onError) {
1224
+ super('jux-event'); // in the future maybe we might need to modify bubbles, cancelable and composed properties
1225
+ this.method = method;
1226
+ this.endpoint = endpoint;
1208
1227
  this.onResult = onResult;
1228
+ this.params = params;
1209
1229
  this.onError = onError;
1210
1230
  }
1211
1231
  }
1212
1232
  const query = (element, endpoint, onResult, params, onError) => {
1213
- element.dispatchEvent(new Query(endpoint, onResult, params, onError));
1233
+ element.dispatchEvent(new JuxEvent('GET', endpoint, onResult, params, onError));
1234
+ };
1235
+ const command = (element, endpoint, onResult, params, onError) => {
1236
+ element.dispatchEvent(new JuxEvent('POST', endpoint, onResult, params, onError));
1214
1237
  };
1215
1238
 
1216
- export { chosenLanguage, getTranslation, handleMagicLinkRequest, initLanguage, initSession, loginButton, loginPage, magicLinkModal, onMicrosoftSignIn, query, reloadWithLanguageOverride, requestMagicLink };
1239
+ export { chosenLanguage, command, getTranslation, handleMagicLinkRequest, initLanguage, initSession, loginButton, loginPage, magicLinkModal, onMicrosoftSignIn, query, reloadWithLanguageOverride, requestMagicLink };
package/dist/main.js CHANGED
@@ -167,10 +167,8 @@ const setBackendUrl = (backendUrl) => localStorage.setItem('backend-url', backen
167
167
  const getBackendUrl = () => process.env.BACKEND_URL || localStorage.getItem('backend-url');
168
168
  //@ts-ignore
169
169
  const getSecondaryBackendUrl = () => process.env.STAGING_BACKEND_URL || getBackendUrl();
170
- let getBackendUrlToTry = () => getBackendUrl();
171
170
  const getApiUrl = () => getBackendUrl() + 'api/';
172
- const getGoogleAuthUrl = () => getBackendUrl() + 'auth-google?google-id-token=';
173
- const getMicrosoftAuthUrl = () => getBackendUrl() + 'auth-microsoft?access-token=';
171
+ let getBackendUrlToTry = () => getBackendUrl();
174
172
  const getMagicAuthUrl = () => {
175
173
  var _a;
176
174
  // @ts-ignore
@@ -183,6 +181,17 @@ const getMagicAuthReqUrl = () => {
183
181
  const endpoint = ((_a = window === null || window === void 0 ? void 0 : window.store) === null || _a === void 0 ? void 0 : _a.magicLinkRequestEndpoint) || 'magic-link-request';
184
182
  return getBackendUrl() + endpoint + '?email=';
185
183
  };
184
+ const handleJuxEvents = (ev) => {
185
+ const { method, endpoint, onResult, params, onError } = ev;
186
+ const onJsonResponse = (res) => onResult ? onResult(res) : null;
187
+ const onHelpMessage = (err) => onError ? onError(err) : null;
188
+ if (method === 'GET') {
189
+ backendGet(endpoint, params, onJsonResponse, onHelpMessage);
190
+ }
191
+ if (method === 'POST') {
192
+ backendPost(endpoint, params, onJsonResponse, onHelpMessage);
193
+ }
194
+ };
186
195
  let timeout;
187
196
  const maxRetries = 20;
188
197
  let retries = 0;
@@ -276,19 +285,22 @@ const backendRequest = (requestType, url, postContent, onJsonResponse, onHelpMes
276
285
  const backendPost = (endpoint, postContent, onJsonResponse, onHelpMessage) => {
277
286
  backendRequest('POST', getApiUrl() + endpoint, postContent, onJsonResponse, onHelpMessage);
278
287
  };
279
- const backendGet = (endpoint, onJsonResponse, onHelpMessage) => {
280
- let api = getApiUrl() + endpoint;
281
- if (endpoint.includes(getGoogleAuthUrl()) || endpoint.includes(getMicrosoftAuthUrl())) {
282
- api = endpoint;
283
- }
284
- backendRequest('GET', api, undefined, onJsonResponse, onHelpMessage);
288
+ const backendGet = (endpoint, params, onJsonResponse, onHelpMessage) => {
289
+ backendRequest('GET', getApiUrl() + endpoint, params, onJsonResponse, onHelpMessage);
285
290
  };
286
- const backendGetPromise = (endpoint) => new Promise((resolve, reject) => backendGet(endpoint, resolve, reject));
287
291
  const requestMagicLink = (data, onJsonResponse) => {
288
- backendRequest('GET', getMagicAuthReqUrl() + encodeURIComponent(data.email) + '&destination=' + extractTokenFromWindowLocation('destination') + '&token=' + data.token, undefined, onJsonResponse, () => { });
292
+ backendRequest('GET', getMagicAuthReqUrl() + encodeURIComponent(data.email) + '&destination=' + extractTokenFromWindowLocation('destination') + '&token=' + data.token, null, onJsonResponse, () => { });
289
293
  };
290
294
  const openMagicLink = (magicToken, onJsonResponse, onHelpMessage) => {
291
- backendRequest('GET', getMagicAuthUrl() + magicToken, undefined, onJsonResponse, onHelpMessage);
295
+ backendRequest('GET', getMagicAuthUrl() + magicToken, null, onJsonResponse, onHelpMessage);
296
+ };
297
+ const googleLogin = (token, onLogin, onLoginError) => {
298
+ const url = getBackendUrl() + 'auth-google?google-id-token=' + token;
299
+ backendRequest('GET', url, null, onLogin, onLoginError);
300
+ };
301
+ const microsoftLogin = (token, onLogin, onLoginError) => {
302
+ const url = getBackendUrl() + 'auth-microsoft?access-token=' + token;
303
+ backendRequest('GET', url, null, onLogin, onLoginError);
292
304
  };
293
305
  const getMXData = (domainName) => __awaiter(void 0, void 0, void 0, function* () {
294
306
  try {
@@ -378,27 +390,23 @@ const highLevelUser = (gUser) => {
378
390
  googletoken: gUser.getAuthResponse().id_token
379
391
  };
380
392
  };
381
- const onUserChanged = (googleUser, onUserLogin) => __awaiter(void 0, void 0, void 0, function* () {
393
+ const onUserChanged = (googleUser, onUserLogin) => {
382
394
  document.documentElement.classList.remove('progress');
383
395
  document.body.classList.remove('disabled');
384
396
  if (googleUser) {
385
397
  // mixpanelIdentify(googleUser); TODO
386
- try {
387
- yield backendGetPromise(getGoogleAuthUrl() + googleUser.googletoken)
388
- .then(res => {
389
- const { token } = res;
390
- // @ts-ignore
391
- window.store.currentUser = res;
392
- onTokenAcquired(token, onUserLogin);
393
- })
394
- .catch((error) => { throw Error(error); });
395
- }
396
- catch (error) {
397
- console.error(error);
398
+ const onSuccess = (res) => {
399
+ const { token } = res;
400
+ // @ts-ignore
401
+ window.store.currentUser = res;
402
+ onTokenAcquired(token, onUserLogin);
403
+ };
404
+ const onError = (err) => {
405
+ console.error(err);
398
406
  // logError(error); TODO
399
407
  // checkMixpanel(() => mixpanel.track('Login Error', { 'Login Type': 'Google', 'Error': error })); TODO
400
- return;
401
- }
408
+ };
409
+ googleLogin(googleUser.googletoken, onSuccess, onError);
402
410
  // checkMixpanel(() => mixpanel.track('Login', { 'Login Type': 'Google' })); TODO
403
411
  }
404
412
  else {
@@ -406,7 +414,7 @@ const onUserChanged = (googleUser, onUserLogin) => __awaiter(void 0, void 0, voi
406
414
  // checkMixpanel(() => mixpanel.reset()); TODO
407
415
  // displayPage(Page.LOGIN); TODO
408
416
  }
409
- });
417
+ };
410
418
  const onGoogleUserChanged = (gUser, onUserLogin) => {
411
419
  onUserChanged(gUser.isSignedIn() ? highLevelUser(gUser) : null, onUserLogin);
412
420
  };
@@ -768,7 +776,7 @@ const contactUsModal = (subject) => {
768
776
  if (subject !== 'email-not-sent') {
769
777
  // @ts-ignore
770
778
  const admin = ((_d = (_c = window.store) === null || _c === void 0 ? void 0 : _c.selectedTeam) === null || _d === void 0 ? void 0 : _d.admin) || window.store.currentUser.email;
771
- backendGet(`credits-balance-for?admin=${admin}`, (res) => {
779
+ backendGet(`credits-balance-for?admin=${admin}`, null, (res) => {
772
780
  const balance = res;
773
781
  if (balance <= 0) {
774
782
  form.insertBefore(captcha, btn);
@@ -910,6 +918,10 @@ const displayPage = (clientApp, page) => {
910
918
  Array.from(clientApp.children).forEach(el => el.remove());
911
919
  clientApp.appendChild(page);
912
920
  };
921
+ const setJuxEventHandler = () => {
922
+ document.removeEventListener('jux-event', handleJuxEvents);
923
+ document.addEventListener('jux-event', handleJuxEvents);
924
+ };
913
925
  const initSession = (clientApp, supportedLoginTypes, onUserLogin, backgroundImage, fetchUserBackendUrl, magicLinkRequestEndpoint, magicLinkAuthEndpoint) => {
914
926
  // if (startNewDemo()) {
915
927
  // initDemo(setBackendToken, onAuthentication);
@@ -926,10 +938,12 @@ const initSession = (clientApp, supportedLoginTypes, onUserLogin, backgroundImag
926
938
  window.store.magicLinkRequestEndpoint = magicLinkRequestEndpoint;
927
939
  // @ts-ignore
928
940
  window.store.magicLinkAuthEndpoint = magicLinkAuthEndpoint;
929
- const magicToken = extractTokenFromWindowLocation('magic-link');
930
- if (magicToken) {
941
+ // TODO: use jux-events to call commands and queries
942
+ setJuxEventHandler();
943
+ const signInToken = extractTokenFromWindowLocation('sign-in') || extractTokenFromWindowLocation('magic-link');
944
+ if (signInToken) {
931
945
  const onOpenMagicLink = () => {
932
- openMagicLink(magicToken, (res) => onAuthentication(onUserLogin, res, 'Magic Link'), () => onAuthenticationFailure('unable-magic-login'));
946
+ openMagicLink(signInToken, (res) => onAuthentication(onUserLogin, res, 'Magic Link'), () => onAuthenticationFailure('unable-magic-login'));
933
947
  };
934
948
  const host = extractTokenFromWindowLocation('host');
935
949
  if (host) {
@@ -944,7 +958,7 @@ const initSession = (clientApp, supportedLoginTypes, onUserLogin, backgroundImag
944
958
  return;
945
959
  }
946
960
  if (getBackendToken()) {
947
- backendGet('profile', (res) => onAuthentication(onUserLogin, res, 'Token Authentication'), (_err) => onAuthenticationFailure('login-failed'));
961
+ backendGet('profile', null, (res) => onAuthentication(onUserLogin, res, 'Token Authentication'), (_err) => onAuthenticationFailure('login-failed'));
948
962
  return;
949
963
  }
950
964
  displayPage(clientApp, loginPage(clientApp, backgroundImage, onUserLogin, supportedLoginTypes));
@@ -968,15 +982,19 @@ const onMicrosoftSignIn = (onUserLogin) => __awaiter(void 0, void 0, void 0, fun
968
982
  try {
969
983
  const loginResponse = yield msalInstance.loginPopup(loginRequest);
970
984
  const { accessToken } = loginResponse;
971
- yield backendGetPromise(getMicrosoftAuthUrl() + accessToken)
972
- .then((res) => {
985
+ const onSuccess = (res) => {
973
986
  const { token } = res;
974
987
  // @ts-ignore
975
988
  window.store.currentUser = res;
976
989
  onTokenAcquired(token, onUserLogin);
977
- })
978
- .catch(console.error)
979
- .finally(() => disableSignInLayout());
990
+ disableSignInLayout();
991
+ };
992
+ const onError = (err) => {
993
+ console.error(err);
994
+ reportError(err);
995
+ disableSignInLayout();
996
+ };
997
+ microsoftLogin(accessToken, onSuccess, onError);
980
998
  }
981
999
  catch (err) {
982
1000
  reportError(err);
@@ -1225,19 +1243,25 @@ const loginPage = (clientApp, backgroundImg, onUserLogin, loginTypes) => {
1225
1243
  return result;
1226
1244
  };
1227
1245
 
1228
- class Query extends Event {
1229
- constructor(endpoint, onResult, params, onError) {
1230
- super(endpoint); // if needed, it is possible to modify bubbles, cancelable and composed properties
1231
- this.params = params;
1246
+ class JuxEvent extends Event {
1247
+ constructor(method, endpoint, onResult, params, onError) {
1248
+ super('jux-event'); // in the future maybe we might need to modify bubbles, cancelable and composed properties
1249
+ this.method = method;
1250
+ this.endpoint = endpoint;
1232
1251
  this.onResult = onResult;
1252
+ this.params = params;
1233
1253
  this.onError = onError;
1234
1254
  }
1235
1255
  }
1236
1256
  const query = (element, endpoint, onResult, params, onError) => {
1237
- element.dispatchEvent(new Query(endpoint, onResult, params, onError));
1257
+ element.dispatchEvent(new JuxEvent('GET', endpoint, onResult, params, onError));
1258
+ };
1259
+ const command = (element, endpoint, onResult, params, onError) => {
1260
+ element.dispatchEvent(new JuxEvent('POST', endpoint, onResult, params, onError));
1238
1261
  };
1239
1262
 
1240
1263
  exports.chosenLanguage = chosenLanguage;
1264
+ exports.command = command;
1241
1265
  exports.getTranslation = getTranslation;
1242
1266
  exports.handleMagicLinkRequest = handleMagicLinkRequest;
1243
1267
  exports.initLanguage = initLanguage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@klauswuestefeld/jux-web",
3
- "version": "2024.2.5-1",
3
+ "version": "2024.2.6-1",
4
4
  "author": "Jux.House",
5
5
  "license": "Copyrighted",
6
6
  "description": "",
@@ -1 +0,0 @@
1
- export declare const query: (element: HTMLElement, endpoint: string, onResult: (result: any) => void, params?: any, onError?: ((error: any) => void) | undefined) => void;