chayns-api 2.2.21 → 2.3.0-beta.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.
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.addAppStorageListener = addAppStorageListener;
7
+ exports.clearAppStorage = clearAppStorage;
8
+ exports.getAppStorageItem = getAppStorageItem;
9
+ exports.isAppStorageAvailable = isAppStorageAvailable;
10
+ exports.removeAppStorageItem = removeAppStorageItem;
11
+ exports.setAppStorageItem = setAppStorageItem;
12
+ var _apiListenerHelper = require("../helper/apiListenerHelper");
13
+ var _IChaynsReact = require("../types/IChaynsReact");
14
+ function isAppStorageAvailable() {
15
+ var _this$values$device$a, _this$values$device$a2;
16
+ if (((_this$values$device$a = this.values.device.app) === null || _this$values$device$a === void 0 ? void 0 : _this$values$device$a.name) !== _IChaynsReact.AppName.Team) {
17
+ return false;
18
+ }
19
+ return ((_this$values$device$a2 = this.values.device.app) === null || _this$values$device$a2 === void 0 ? void 0 : _this$values$device$a2.appVersion) >= (['iOS', 'Mac OS'].includes(this.values.device.os) ? 1046 : 1031);
20
+ }
21
+ function getAppStorageItem(storeName, key) {
22
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
23
+ const callbackName = `chaynsApiV5Callback_${this.counter++}`;
24
+ return new Promise(resolve => {
25
+ window[callbackName] = (_key, _storeName, value) => {
26
+ resolve(value);
27
+ delete window[callbackName];
28
+ };
29
+ window.webkit.messageHandlers.chaynsDataGetItem.postMessage({
30
+ storeName,
31
+ key,
32
+ callback: callbackName
33
+ });
34
+ });
35
+ }
36
+ const result = window.chaynsWebViewStorage.chaynsDataGetItem(storeName, key);
37
+ try {
38
+ return Promise.resolve(result ? JSON.parse(result) : result);
39
+ } catch {
40
+ return Promise.resolve(result);
41
+ }
42
+ }
43
+ function setAppStorageItem(storeName, key, value) {
44
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
45
+ const callbackName = `chaynsApiV5Callback_${this.counter++}`;
46
+ return new Promise((resolve, reject) => {
47
+ window[callbackName] = () => {
48
+ resolve();
49
+ delete window[callbackName];
50
+ };
51
+ window.webkit.messageHandlers.chaynsDataSetItem.postMessage({
52
+ storeName,
53
+ key,
54
+ value,
55
+ callback: callbackName
56
+ });
57
+ });
58
+ }
59
+ window.chaynsWebViewStorage.chaynsDataSetItem(storeName, key, JSON.stringify(value));
60
+ return Promise.resolve();
61
+ }
62
+ function removeAppStorageItem(storeName, key) {
63
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
64
+ window.webkit.messageHandlers.chaynsDataRemoveItem.postMessage({
65
+ storeName,
66
+ key
67
+ });
68
+ } else {
69
+ window.chaynsWebViewStorage.chaynsDataRemoveItem(storeName, key);
70
+ }
71
+ }
72
+ function clearAppStorage(storeName) {
73
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
74
+ window.webkit.messageHandlers.chaynsDataErase.postMessage({
75
+ storeName
76
+ });
77
+ } else {
78
+ window.chaynsWebViewStorage.chaynsDataErase(storeName);
79
+ }
80
+ }
81
+ async function addAppStorageListener(storeName, prefix, callback) {
82
+ const callbackName = `chaynsApiV5Callback_${this.counter++}`;
83
+ const {
84
+ shouldInitialize
85
+ } = (0, _apiListenerHelper.addApiListener)(`appStorageListener/${storeName}`, callback);
86
+ const handleValue = value => {
87
+ var _value$key;
88
+ if (value.action === 0 && (_value$key = value.key) !== null && _value$key !== void 0 && _value$key.startsWith(prefix)) {
89
+ let data = value.value;
90
+ try {
91
+ if (typeof data === 'string') {
92
+ data = JSON.parse(data);
93
+ }
94
+ } catch {}
95
+ callback(data);
96
+ void removeAppStorageItem.call(this, storeName, value.key);
97
+ }
98
+ };
99
+ if (shouldInitialize) {
100
+ const result = await getAppStorageItem.call(this, storeName);
101
+ result === null || result === void 0 || result.forEach(value => {
102
+ handleValue(value);
103
+ });
104
+ }
105
+ window[callbackName] = handleValue;
106
+ this.appCall(307, {
107
+ storeName,
108
+ callback: callbackName,
109
+ action: 1
110
+ }, {
111
+ awaitResult: false
112
+ });
113
+ }
@@ -5,24 +5,25 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.AppWrapper = void 0;
7
7
  var _lodash = _interopRequireDefault(require("lodash.throttle"));
8
+ var _getUserInfo = _interopRequireDefault(require("../calls/getUserInfo"));
9
+ var _sendMessage = require("../calls/sendMessage");
10
+ var _languages = require("../constants/languages");
8
11
  var _DialogHandler = _interopRequireDefault(require("../handler/DialogHandler"));
12
+ var _apiListenerHelper = require("../helper/apiListenerHelper");
9
13
  var _IChaynsReact = require("../types/IChaynsReact");
10
14
  var _appCall = _interopRequireDefault(require("../util/appCall"));
15
+ var _appStorage = require("../util/appStorage");
11
16
  var _deviceHelper = _interopRequireWildcard(require("../util/deviceHelper"));
12
- var _getUserInfo = _interopRequireDefault(require("../calls/getUserInfo"));
13
- var _sendMessage = require("../calls/sendMessage");
14
- var _apiListenerHelper = require("../helper/apiListenerHelper");
15
- var _languages = require("../constants/languages");
16
17
  var _is = require("../util/is");
17
18
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
18
19
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
19
20
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
20
- let appWrapperDialogId = 0;
21
21
  class AppWrapper {
22
22
  values = null;
23
23
  accessToken = '';
24
24
  listeners = [];
25
25
  customFunctions = {};
26
+ nextDialogEventId = 0;
26
27
  async loadStyleSettings(siteId) {
27
28
  try {
28
29
  var _AbortSignal$timeout, _AbortSignal;
@@ -575,7 +576,8 @@ class AppWrapper {
575
576
  return new _DialogHandler.default(config, this.functions.openDialog, this.functions.closeDialog, this.functions.dispatchEventToDialogClient, this.functions.addDialogClientEventListener);
576
577
  },
577
578
  openDialog: async (config, callback) => {
578
- const currentDialogId = appWrapperDialogId++;
579
+ const currentDialogId = crypto.randomUUID();
580
+ this.nextDialogEventId = 0;
579
581
  const isSupported = (0, _is.isAppCallSupported)({
580
582
  minAndroidVersion: 7137,
581
583
  minIOSVersion: 6934
@@ -583,7 +585,8 @@ class AppWrapper {
583
585
  this.appCall(184, {
584
586
  dialogContent: {
585
587
  apiVersion: 5,
586
- config
588
+ config,
589
+ dialogId: currentDialogId
587
590
  },
588
591
  externalDialogUrl: isSupported ? undefined : 'https://tapp.chayns-static.space/api/dialog-v2/v1/index.html'
589
592
  }, {
@@ -600,10 +603,37 @@ class AppWrapper {
600
603
  buttonType,
601
604
  result
602
605
  });
606
+ (0, _appStorage.clearAppStorage)(window._currentDialogId);
607
+ }
608
+ },
609
+ dispatchEventToDialogClient: (dialogId, data) => {
610
+ if (_appStorage.isAppStorageAvailable.call(this)) {
611
+ void _appStorage.setAppStorageItem.call(this, dialogId, `client/${this.nextDialogEventId++}`, data);
612
+ } else {
613
+ this.notImplemented('dispatchEventToDialogClient');
614
+ }
615
+ },
616
+ addDialogClientEventListener: (dialogId, listener) => {
617
+ if (_appStorage.isAppStorageAvailable.call(this)) {
618
+ _appStorage.addAppStorageListener.call(this, dialogId, 'host/', listener);
619
+ } else {
620
+ this.notImplemented('addDialogClientEventListener');
621
+ }
622
+ },
623
+ dispatchEventToDialogHost: data => {
624
+ if (_appStorage.isAppStorageAvailable.call(this)) {
625
+ void _appStorage.setAppStorageItem.call(this, window._currentDialogId, `host/${this.nextDialogEventId++}`, data);
626
+ } else {
627
+ this.notImplemented('dispatchEventToDialogHost');
628
+ }
629
+ },
630
+ addDialogHostEventListener: callback => {
631
+ if (_appStorage.isAppStorageAvailable.call(this)) {
632
+ void _appStorage.addAppStorageListener.call(this, window._currentDialogId, 'client/', callback);
633
+ } else {
634
+ this.notImplemented('addDialogHostEventListener');
603
635
  }
604
636
  },
605
- dispatchEventToDialogClient: () => this.notImplemented('dispatchEventToDialogClient'),
606
- addDialogClientEventListener: () => this.notImplemented('addDialogClientEventListener'),
607
637
  addAnonymousAccount: async () => {
608
638
  return this.appCall(302);
609
639
  },
@@ -0,0 +1,102 @@
1
+ import { addApiListener } from '../helper/apiListenerHelper';
2
+ import { AppName } from '../types/IChaynsReact';
3
+ export function isAppStorageAvailable() {
4
+ var _this$values$device$a, _this$values$device$a2;
5
+ if (((_this$values$device$a = this.values.device.app) === null || _this$values$device$a === void 0 ? void 0 : _this$values$device$a.name) !== AppName.Team) {
6
+ return false;
7
+ }
8
+ return ((_this$values$device$a2 = this.values.device.app) === null || _this$values$device$a2 === void 0 ? void 0 : _this$values$device$a2.appVersion) >= (['iOS', 'Mac OS'].includes(this.values.device.os) ? 1046 : 1031);
9
+ }
10
+ export function getAppStorageItem(storeName, key) {
11
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
12
+ const callbackName = `chaynsApiV5Callback_${this.counter++}`;
13
+ return new Promise(resolve => {
14
+ window[callbackName] = (_key, _storeName, value) => {
15
+ resolve(value);
16
+ delete window[callbackName];
17
+ };
18
+ window.webkit.messageHandlers.chaynsDataGetItem.postMessage({
19
+ storeName,
20
+ key,
21
+ callback: callbackName
22
+ });
23
+ });
24
+ }
25
+ const result = window.chaynsWebViewStorage.chaynsDataGetItem(storeName, key);
26
+ try {
27
+ return Promise.resolve(result ? JSON.parse(result) : result);
28
+ } catch {
29
+ return Promise.resolve(result);
30
+ }
31
+ }
32
+ export function setAppStorageItem(storeName, key, value) {
33
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
34
+ const callbackName = `chaynsApiV5Callback_${this.counter++}`;
35
+ return new Promise((resolve, reject) => {
36
+ window[callbackName] = () => {
37
+ resolve();
38
+ delete window[callbackName];
39
+ };
40
+ window.webkit.messageHandlers.chaynsDataSetItem.postMessage({
41
+ storeName,
42
+ key,
43
+ value,
44
+ callback: callbackName
45
+ });
46
+ });
47
+ }
48
+ window.chaynsWebViewStorage.chaynsDataSetItem(storeName, key, JSON.stringify(value));
49
+ return Promise.resolve();
50
+ }
51
+ export function removeAppStorageItem(storeName, key) {
52
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
53
+ window.webkit.messageHandlers.chaynsDataRemoveItem.postMessage({
54
+ storeName,
55
+ key
56
+ });
57
+ } else {
58
+ window.chaynsWebViewStorage.chaynsDataRemoveItem(storeName, key);
59
+ }
60
+ }
61
+ export function clearAppStorage(storeName) {
62
+ if (this.values.device.os === 'iOS' || this.values.device.os === 'Mac OS') {
63
+ window.webkit.messageHandlers.chaynsDataErase.postMessage({
64
+ storeName
65
+ });
66
+ } else {
67
+ window.chaynsWebViewStorage.chaynsDataErase(storeName);
68
+ }
69
+ }
70
+ export async function addAppStorageListener(storeName, prefix, callback) {
71
+ const callbackName = `chaynsApiV5Callback_${this.counter++}`;
72
+ const {
73
+ shouldInitialize
74
+ } = addApiListener(`appStorageListener/${storeName}`, callback);
75
+ const handleValue = value => {
76
+ var _value$key;
77
+ if (value.action === 0 && (_value$key = value.key) !== null && _value$key !== void 0 && _value$key.startsWith(prefix)) {
78
+ let data = value.value;
79
+ try {
80
+ if (typeof data === 'string') {
81
+ data = JSON.parse(data);
82
+ }
83
+ } catch {}
84
+ callback(data);
85
+ void removeAppStorageItem.call(this, storeName, value.key);
86
+ }
87
+ };
88
+ if (shouldInitialize) {
89
+ const result = await getAppStorageItem.call(this, storeName);
90
+ result === null || result === void 0 || result.forEach(value => {
91
+ handleValue(value);
92
+ });
93
+ }
94
+ window[callbackName] = handleValue;
95
+ this.appCall(307, {
96
+ storeName,
97
+ callback: callbackName,
98
+ action: 1
99
+ }, {
100
+ awaitResult: false
101
+ });
102
+ }
@@ -2,16 +2,16 @@ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object
2
2
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
3
3
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
4
4
  import throttle from 'lodash.throttle';
5
+ import getUserInfo from '../calls/getUserInfo';
6
+ import { sendMessageToGroup, sendMessageToPage, sendMessageToUser } from '../calls/sendMessage';
7
+ import { DeviceLanguage } from '../constants/languages';
5
8
  import DialogHandler from '../handler/DialogHandler';
9
+ import { addApiListener, dispatchApiEvent, removeApiListener } from '../helper/apiListenerHelper';
6
10
  import { Environment, Font, Gender, IconType, RuntimeEnviroment, TappEvent } from '../types/IChaynsReact';
7
11
  import invokeAppCall from '../util/appCall';
12
+ import { addAppStorageListener, clearAppStorage, isAppStorageAvailable, setAppStorageItem } from '../util/appStorage';
8
13
  import getDeviceInfo, { getScreenSize } from '../util/deviceHelper';
9
- import getUserInfo from '../calls/getUserInfo';
10
- import { sendMessageToGroup, sendMessageToPage, sendMessageToUser } from '../calls/sendMessage';
11
- import { addApiListener, dispatchApiEvent, removeApiListener } from '../helper/apiListenerHelper';
12
- import { DeviceLanguage } from '../constants/languages';
13
14
  import { isAppCallSupported } from '../util/is';
14
- let appWrapperDialogId = 0;
15
15
  export class AppWrapper {
16
16
  async loadStyleSettings(siteId) {
17
17
  try {
@@ -142,6 +142,7 @@ export class AppWrapper {
142
142
  _defineProperty(this, "accessToken", '');
143
143
  _defineProperty(this, "listeners", []);
144
144
  _defineProperty(this, "customFunctions", {});
145
+ _defineProperty(this, "nextDialogEventId", 0);
145
146
  _defineProperty(this, "counter", 0);
146
147
  _defineProperty(this, "functions", {
147
148
  getAccessToken: async () => ({
@@ -539,7 +540,8 @@ export class AppWrapper {
539
540
  return new DialogHandler(config, this.functions.openDialog, this.functions.closeDialog, this.functions.dispatchEventToDialogClient, this.functions.addDialogClientEventListener);
540
541
  },
541
542
  openDialog: async (config, callback) => {
542
- const currentDialogId = appWrapperDialogId++;
543
+ const currentDialogId = crypto.randomUUID();
544
+ this.nextDialogEventId = 0;
543
545
  const isSupported = isAppCallSupported({
544
546
  minAndroidVersion: 7137,
545
547
  minIOSVersion: 6934
@@ -547,7 +549,8 @@ export class AppWrapper {
547
549
  this.appCall(184, {
548
550
  dialogContent: {
549
551
  apiVersion: 5,
550
- config
552
+ config,
553
+ dialogId: currentDialogId
551
554
  },
552
555
  externalDialogUrl: isSupported ? undefined : 'https://tapp.chayns-static.space/api/dialog-v2/v1/index.html'
553
556
  }, {
@@ -565,10 +568,37 @@ export class AppWrapper {
565
568
  buttonType,
566
569
  result
567
570
  });
571
+ clearAppStorage(window._currentDialogId);
572
+ }
573
+ },
574
+ dispatchEventToDialogClient: (dialogId, data) => {
575
+ if (isAppStorageAvailable.call(this)) {
576
+ void setAppStorageItem.call(this, dialogId, `client/${this.nextDialogEventId++}`, data);
577
+ } else {
578
+ this.notImplemented('dispatchEventToDialogClient');
579
+ }
580
+ },
581
+ addDialogClientEventListener: (dialogId, listener) => {
582
+ if (isAppStorageAvailable.call(this)) {
583
+ addAppStorageListener.call(this, dialogId, 'host/', listener);
584
+ } else {
585
+ this.notImplemented('addDialogClientEventListener');
586
+ }
587
+ },
588
+ dispatchEventToDialogHost: data => {
589
+ if (isAppStorageAvailable.call(this)) {
590
+ void setAppStorageItem.call(this, window._currentDialogId, `host/${this.nextDialogEventId++}`, data);
591
+ } else {
592
+ this.notImplemented('dispatchEventToDialogHost');
593
+ }
594
+ },
595
+ addDialogHostEventListener: callback => {
596
+ if (isAppStorageAvailable.call(this)) {
597
+ void addAppStorageListener.call(this, window._currentDialogId, 'client/', callback);
598
+ } else {
599
+ this.notImplemented('addDialogHostEventListener');
568
600
  }
569
601
  },
570
- dispatchEventToDialogClient: () => this.notImplemented('dispatchEventToDialogClient'),
571
- addDialogClientEventListener: () => this.notImplemented('addDialogClientEventListener'),
572
602
  addAnonymousAccount: async () => {
573
603
  return this.appCall(302);
574
604
  },
@@ -419,11 +419,17 @@ export interface ChaynsReactFunctions {
419
419
  openDialog: (value: any, callback: (data: any) => any) => Promise<any>;
420
420
  closeDialog: (dialogId: number) => Promise<void>;
421
421
  setDialogResult: (buttonType: DialogButtonType, result: any) => Promise<void>;
422
+ /** Sends event from opener to dialog */
422
423
  dispatchEventToDialogClient: (dialogId: number, data: object) => Promise<void>;
424
+ /** Allows the opener of a dialog to add event listeners for events sent from the dialog */
423
425
  addDialogClientEventListener: (dialogId: number, callback: (data: object) => void) => Promise<number>;
426
+ /** Removes listeners which were added via addDialogClientEventListener */
424
427
  removeDialogClientEventListener: (dialogId: number, id: number) => Promise<void>;
428
+ /** Sends event from dialog to opener */
425
429
  dispatchEventToDialogHost: (data: object) => Promise<void>;
430
+ /** Allows the dialog to add event listeners for events sent from the opener */
426
431
  addDialogHostEventListener: (callback: (data: object) => void) => Promise<number>;
432
+ /** Removes listeners which were added via addDialogHostEventListener */
427
433
  removeDialogHostEventListener: (id: number) => Promise<void>;
428
434
  addAnonymousAccount: () => Promise<AnonymousAccountResult>;
429
435
  addAccessTokenChangeListener: (options: {
@@ -0,0 +1,7 @@
1
+ import { AppWrapper } from '../wrapper/AppWrapper';
2
+ export declare function isAppStorageAvailable(this: AppWrapper): boolean;
3
+ export declare function getAppStorageItem<T extends unknown>(this: AppWrapper, storeName: string, key?: string): Promise<T>;
4
+ export declare function setAppStorageItem<T extends string | object>(this: AppWrapper, storeName: string, key: string, value: T): Promise<void>;
5
+ export declare function removeAppStorageItem(this: AppWrapper, storeName: string, key: string): void;
6
+ export declare function clearAppStorage(this: AppWrapper, storeName: string): void;
7
+ export declare function addAppStorageListener<T extends string | object>(this: AppWrapper, storeName: string, prefix: string, callback: (value: T | undefined) => void): Promise<void>;
@@ -4,6 +4,7 @@ export declare class AppWrapper implements IChaynsReact {
4
4
  accessToken: string;
5
5
  listeners: (() => void)[];
6
6
  customFunctions: {};
7
+ nextDialogEventId: number;
7
8
  loadStyleSettings(siteId: string): Promise<ChaynsStyleSettings | undefined>;
8
9
  mapOldApiToNew(retVal: any): ChaynsReactValues;
9
10
  constructor();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chayns-api",
3
- "version": "2.2.21",
3
+ "version": "2.3.0-beta.1",
4
4
  "description": "new chayns api",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",