@maxzima/wa-communicator 0.0.16 → 1.0.2

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.
Files changed (73) hide show
  1. package/README.md +92 -43
  2. package/dist/engine/AccountTracker/Enums/AccountTrackerEnum.d.ts +15 -0
  3. package/dist/engine/AccountTracker/Enums/AccountTrackerEnum.js +1 -0
  4. package/dist/engine/AccountTracker/Public/AccountTracker.d.ts +9 -0
  5. package/dist/engine/AccountTracker/Public/AccountTracker.js +35 -0
  6. package/dist/engine/AccountTracker/Types/TAccountTracker.d.ts +15 -0
  7. package/dist/engine/AccountTracker/Types/TAccountTracker.js +1 -0
  8. package/dist/engine/AccountTracker/index.d.ts +1 -0
  9. package/dist/engine/AccountTracker/index.js +1 -0
  10. package/dist/engine/Communicator/Constants/Communicator.d.ts +2 -0
  11. package/dist/engine/Communicator/Constants/Communicator.js +10 -0
  12. package/dist/engine/Communicator/Enums/CommunicatorEnum.d.ts +12 -0
  13. package/dist/engine/Communicator/Enums/CommunicatorEnum.js +1 -0
  14. package/dist/engine/Communicator/Public/Communicator.d.ts +16 -0
  15. package/dist/engine/Communicator/Public/Communicator.js +158 -0
  16. package/dist/engine/Communicator/Types/TCommunicator.d.ts +10 -0
  17. package/dist/engine/Communicator/Types/TCommunicator.js +1 -0
  18. package/dist/engine/Communicator/index.d.ts +1 -0
  19. package/dist/engine/Communicator/index.js +1 -0
  20. package/dist/engine/index.d.ts +2 -0
  21. package/dist/engine/index.js +2 -0
  22. package/dist/enums/CommunicatorErrorEnum.d.ts +15 -0
  23. package/dist/enums/CommunicatorErrorEnum.js +1 -0
  24. package/dist/enums/index.d.ts +1 -0
  25. package/dist/enums/index.js +1 -0
  26. package/dist/index.d.ts +3 -6
  27. package/dist/index.js +1 -6
  28. package/dist/types/TCommunicator.d.ts +110 -0
  29. package/dist/types/TCommunicator.js +1 -0
  30. package/dist/types/index.d.ts +1 -59
  31. package/dist/types/index.js +1 -1
  32. package/package.json +19 -26
  33. package/src/engine/AccountTracker/Enums/AccountTrackerEnum.ts +18 -0
  34. package/src/engine/AccountTracker/Public/AccountTracker.ts +45 -0
  35. package/src/engine/AccountTracker/Types/TAccountTracker.ts +16 -0
  36. package/src/engine/AccountTracker/index.ts +1 -0
  37. package/src/engine/Communicator/Constants/Communicator.ts +14 -0
  38. package/src/engine/Communicator/Enums/CommunicatorEnum.ts +13 -0
  39. package/src/engine/Communicator/Public/Communicator.ts +195 -0
  40. package/src/engine/Communicator/Types/TCommunicator.ts +11 -0
  41. package/src/engine/Communicator/index.ts +1 -0
  42. package/src/engine/index.ts +2 -0
  43. package/src/enums/CommunicatorErrorEnum.ts +22 -0
  44. package/src/enums/index.ts +1 -0
  45. package/src/index.ts +18 -11
  46. package/src/types/TCommunicator.ts +179 -0
  47. package/src/types/index.ts +1 -72
  48. package/test/utiles.spec.ts +2 -66
  49. package/tsconfig.json +5 -0
  50. package/dist/engine/CommunicatorReceiver.d.ts +0 -14
  51. package/dist/engine/CommunicatorReceiver.js +0 -114
  52. package/dist/engine/CommunicatorSender.d.ts +0 -7
  53. package/dist/engine/CommunicatorSender.js +0 -20
  54. package/dist/engine/utils.d.ts +0 -5
  55. package/dist/engine/utils.js +0 -41
  56. package/dist/enums/CommunicatorActionEnum.d.ts +0 -10
  57. package/dist/enums/CommunicatorActionEnum.js +0 -15
  58. package/dist/enums/CommunicatorTargetEnum.d.ts +0 -5
  59. package/dist/enums/CommunicatorTargetEnum.js +0 -8
  60. package/dist/receiver.d.ts +0 -6
  61. package/dist/receiver.js +0 -6
  62. package/dist/sender.d.ts +0 -5
  63. package/dist/sender.js +0 -5
  64. package/src/engine/CommunicatorReceiver.ts +0 -182
  65. package/src/engine/CommunicatorSender.ts +0 -34
  66. package/src/engine/utils.ts +0 -53
  67. package/src/enums/CommunicatorActionEnum.ts +0 -16
  68. package/src/enums/CommunicatorTargetEnum.ts +0 -8
  69. package/src/receiver.ts +0 -13
  70. package/src/sender.ts +0 -11
  71. package/test/testUnits/message.ts +0 -44
  72. package/test/testUnits/queue.ts +0 -165
  73. package/test/testUnits/scenario.ts +0 -31
@@ -1,182 +0,0 @@
1
- import {
2
- TCommunicatorMessage,
3
- TReceiverMessageQueue,
4
- TReceiverProps,
5
- TReceiverSubscribeParams,
6
- TReceiverTargetScenario,
7
- TReceiverUnsubscribeMessageParams,
8
- } from '../types';
9
- import {isValidMessage, modifyOrigin, sortMessageQueue} from "./utils";
10
- import {CommunicatorTargetEnum, CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
11
- import {
12
- CommunicatorActionEnum,
13
- CommunicatorActionEnum_isCorrect,
14
- CommunicatorActionEnum_scenario
15
- } from '../enums/CommunicatorActionEnum';
16
-
17
- export class CommunicatorReceiver {
18
- private readonly senderOrigin: string;
19
- private queue: TReceiverMessageQueue = [];
20
- private scenario: TReceiverTargetScenario = {};
21
-
22
- constructor(props: TReceiverProps) {
23
- this.senderOrigin = modifyOrigin(props.senderOrigin);
24
-
25
- if (this.senderOrigin) {
26
- // Watch Messages
27
- window.addEventListener('message', this.onMessage, false);
28
- } else {
29
- throw new Error('WAC: senderOrigin is broken!');
30
- }
31
- }
32
-
33
- /**
34
- * Message Receive Callback
35
- * @param event
36
- */
37
- private onMessage = (event: MessageEvent) => {
38
- if (!event || !event.origin || !event.data) {
39
- return;
40
- }
41
-
42
- if (this.senderOrigin !== event.origin) {
43
- return;
44
- }
45
-
46
- let message: TCommunicatorMessage;
47
-
48
- try {
49
- message = JSON.parse(event.data);
50
- } catch (error) {
51
- return;
52
- }
53
-
54
- if (!isValidMessage(message)) {
55
- return;
56
- }
57
-
58
- const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
59
-
60
- // default sort queue
61
- const defaultScenario = CommunicatorActionEnum_scenario();
62
- let sortedQueue = sortMessageQueue(queue, defaultScenario);
63
-
64
- // sort queue
65
- const scenario = this.getMessageScenario(message.target);
66
- if (defaultScenario !== scenario) {
67
- sortedQueue = sortMessageQueue(queue, scenario);
68
- }
69
-
70
- if (sortedQueue.length) {
71
- sortedQueue.forEach(item => {
72
- if (typeof item.params.callback === 'function') {
73
- item.params.callback(message);
74
- }
75
- });
76
- }
77
- };
78
-
79
- /**
80
- * Subscribe by target and action
81
- * @param params
82
- */
83
- public subscribe = (params: TReceiverSubscribeParams): void => {
84
- if (params.code && typeof params.code !== 'string') {
85
- throw new Error('WAC: code can only be a string!');
86
- }
87
-
88
- if (
89
- (!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
90
- (!params.action || !CommunicatorActionEnum_isCorrect(params.action))
91
- ) {
92
- throw new Error('WAC: target or action is not allowed!');
93
- }
94
-
95
- if (this.queue.find(item => {
96
- return (!params.code || item.code === params.code) &&
97
- item.target === params.target &&
98
- item.action === params.action;
99
- })) {
100
- console.log(`WAB: subscribe is exists!`);
101
- return;
102
- }
103
-
104
- const newQueueItem = {
105
- target: params.target,
106
- action: params.action,
107
- params: {
108
- callback: params.callback,
109
- }
110
- };
111
-
112
- if (params.code) {
113
- newQueueItem['code'] = params.code;
114
- }
115
-
116
- this.queue.push(newQueueItem);
117
- };
118
-
119
- /**
120
- * Subscribe by code or target or action
121
- * @param params
122
- */
123
- public unsubscribe = (params?: TReceiverUnsubscribeMessageParams): boolean => {
124
- if (!params || Object.keys(params).length === 0) {
125
- this.queue = [];
126
- return true;
127
- }
128
-
129
- const queueForRemove = this.queue.filter(item => {
130
- return (!params.code || item.code === params.code) &&
131
- (!params.target || item.target === params.target) &&
132
- (!params.action || item.action === params.action);
133
- });
134
-
135
- if (queueForRemove.length) {
136
- const toRemove = new Set(queueForRemove);
137
- this.queue = this.queue.filter(x => !toRemove.has(x));
138
- return true;
139
- }
140
-
141
- return false;
142
- };
143
-
144
- /**
145
- * Get scenario for target
146
- * @param target
147
- */
148
- private getMessageScenario = (target: CommunicatorTargetEnum) => {
149
- const scenario = this.scenario[target];
150
-
151
- if (scenario) {
152
- return scenario;
153
- }
154
-
155
- return CommunicatorActionEnum_scenario();
156
- };
157
-
158
- /**
159
- * Change scenario for target
160
- * @param target
161
- * @param scenario
162
- */
163
- public setMessageScenario = (target: CommunicatorTargetEnum, scenario: CommunicatorActionEnum[]) => {
164
- if (
165
- !CommunicatorTargetEnum_isCorrect(target) ||
166
- !Array.isArray(scenario) ||
167
- !scenario.length) {
168
- return;
169
- }
170
-
171
- scenario = [...new Set(scenario)];
172
-
173
- scenario.forEach(item => {
174
- if (!CommunicatorActionEnum_isCorrect(item)) {
175
- throw new Error('WAC: failed scenario action!');
176
- }
177
- });
178
-
179
- this.scenario[target] = scenario;
180
- };
181
- }
182
-
@@ -1,34 +0,0 @@
1
- import {TCommunicatorMessage, TSenderProps} from '../types';
2
- import {isValidMessage, modifyOrigin} from './utils';
3
-
4
- export class CommunicatorSender {
5
- private readonly otherWindow: Window;
6
- private readonly receiverOrigin: string;
7
-
8
- constructor(props: TSenderProps) {
9
- this.otherWindow = props.otherWindow;
10
- this.receiverOrigin = modifyOrigin(props.receiverOrigin);
11
-
12
- if (!this.receiverOrigin) {
13
- throw new Error('WAC: receiverOrigin is broken!');
14
- }
15
- }
16
-
17
- /**
18
- * Send PostMessage
19
- * @param message
20
- */
21
- public sendMessage(message: TCommunicatorMessage) {
22
- if (!this.receiverOrigin) {
23
- return;
24
- }
25
-
26
- if (!isValidMessage(message)) {
27
- throw new Error(`WAC: Message not valid!`);
28
- }
29
-
30
- const jsonMessage = JSON.stringify(message);
31
- this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
32
- }
33
- }
34
-
@@ -1,53 +0,0 @@
1
- import {TCommunicatorMessage, TReceiverMessageQueue} from "../types";
2
- import {CommunicatorTargetEnum_isCorrect} from "../enums/CommunicatorTargetEnum";
3
- import {CommunicatorActionEnum, CommunicatorActionEnum_isCorrect} from "../enums/CommunicatorActionEnum";
4
-
5
- /**
6
- * @param address
7
- */
8
- export function modifyOrigin(address: string): string {
9
- let url: URL;
10
- try {
11
- let targetOrigin = address.trim();
12
- targetOrigin = (targetOrigin.indexOf('://') === -1) ? 'https://' + targetOrigin : targetOrigin;
13
- url = new URL(targetOrigin);
14
- } catch (_) {
15
- return null;
16
- }
17
- return url.origin !== 'null' ? url.origin : null;
18
- }
19
-
20
- /**
21
- * Check the message for validity
22
- * @param message
23
- */
24
- export function isValidMessage(message: TCommunicatorMessage) {
25
- const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
26
- const actions = message.action;
27
- let isValidActions = typeof actions === 'object';
28
-
29
- if (isValidTarget && isValidActions) {
30
- for (const prop in actions) {
31
- if(actions.hasOwnProperty(prop)) {
32
- isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
33
- if (!isValidActions) {
34
- break;
35
- }
36
- }
37
- }
38
- }
39
-
40
- return isValidTarget && isValidActions;
41
- }
42
-
43
- export function sortMessageQueue(queue: TReceiverMessageQueue, scenario: CommunicatorActionEnum[]) {
44
- return queue.sort((a, b) => {
45
- if (scenario.indexOf(a.action) > scenario.indexOf(b.action)) {
46
- return 1;
47
- }
48
- if (scenario.indexOf(a.action) < scenario.indexOf(b.action)) {
49
- return -1;
50
- }
51
- return 0;
52
- });
53
- }
@@ -1,16 +0,0 @@
1
- export enum CommunicatorActionEnum {
2
- LOADED = 'loaded',
3
- TOKEN = 'token',
4
- RESIZE = 'resize',
5
- CLOSE = 'close',
6
- OPEN = 'open',
7
- GOTO = 'goto',
8
- }
9
-
10
- export function CommunicatorActionEnum_isCorrect(item: string): boolean {
11
- return Object.values(CommunicatorActionEnum).includes(item as CommunicatorActionEnum);
12
- }
13
-
14
- export function CommunicatorActionEnum_scenario(): CommunicatorActionEnum[] {
15
- return Object.values(CommunicatorActionEnum) as CommunicatorActionEnum[];
16
- }
@@ -1,8 +0,0 @@
1
- export enum CommunicatorTargetEnum {
2
- SIGN_UP = 'sign_up',
3
- SIGN_IN = 'sign_in',
4
- }
5
-
6
- export function CommunicatorTargetEnum_isCorrect(item: string): boolean {
7
- return Object.values(CommunicatorTargetEnum).includes(item as CommunicatorTargetEnum);
8
- }
package/src/receiver.ts DELETED
@@ -1,13 +0,0 @@
1
- import {CommunicatorReceiver} from "./engine/CommunicatorReceiver";
2
- import {CommunicatorTargetEnum} from "./enums/CommunicatorTargetEnum";
3
- import {CommunicatorActionEnum} from "./enums/CommunicatorActionEnum";
4
- import * as types from "./types";
5
- import {isValidMessage as communicatorMessageIsValid} from "./engine/utils";
6
-
7
- export {
8
- CommunicatorReceiver,
9
- CommunicatorTargetEnum,
10
- CommunicatorActionEnum,
11
- types,
12
- communicatorMessageIsValid
13
- };
package/src/sender.ts DELETED
@@ -1,11 +0,0 @@
1
- import {CommunicatorSender} from "./engine/CommunicatorSender";
2
- import {CommunicatorTargetEnum} from "./enums/CommunicatorTargetEnum";
3
- import {CommunicatorActionEnum} from "./enums/CommunicatorActionEnum";
4
- import * as types from "./types";
5
-
6
- export {
7
- CommunicatorSender,
8
- CommunicatorTargetEnum,
9
- CommunicatorActionEnum,
10
- types,
11
- };
@@ -1,44 +0,0 @@
1
- import {TCommunicatorMessage} from '../../src/types';
2
- import {CommunicatorActionEnum, CommunicatorTargetEnum} from '../../src';
3
-
4
- const normalMessage: TCommunicatorMessage = {
5
- target: CommunicatorTargetEnum.SIGN_UP,
6
- action: {
7
- [CommunicatorActionEnum.OPEN]: CommunicatorTargetEnum.SIGN_IN,
8
- [CommunicatorActionEnum.CLOSE]: true,
9
- [CommunicatorActionEnum.GOTO]: {
10
- url: 'google.com',
11
- },
12
- }
13
- };
14
-
15
- const messageWithInvalidTarget: TCommunicatorMessage = {
16
- target: 'Invalid' as CommunicatorTargetEnum,
17
- action: {
18
- [CommunicatorActionEnum.OPEN]: CommunicatorTargetEnum.SIGN_IN,
19
- [CommunicatorActionEnum.CLOSE]: true,
20
- }
21
- };
22
-
23
- const messageWithInvalidAction: TCommunicatorMessage = {
24
- target: CommunicatorTargetEnum.SIGN_UP,
25
- action: {
26
- ['Invalid' as CommunicatorActionEnum]: CommunicatorTargetEnum.SIGN_IN,
27
- [CommunicatorActionEnum.CLOSE]: true,
28
- }
29
- };
30
-
31
- const messageWithInvalidActionParams: TCommunicatorMessage = {
32
- target: 'Invalid' as CommunicatorTargetEnum,
33
- action: {
34
- [CommunicatorActionEnum.OPEN]: 'failed' as CommunicatorTargetEnum,
35
- [CommunicatorActionEnum.CLOSE]: true,
36
- }
37
- };
38
-
39
- export {
40
- normalMessage,
41
- messageWithInvalidTarget,
42
- messageWithInvalidAction,
43
- messageWithInvalidActionParams
44
- };
@@ -1,165 +0,0 @@
1
- import {TReceiverMessageQueue} from '../../src/types';
2
- import {CommunicatorActionEnum, CommunicatorTargetEnum} from '../../src';
3
-
4
- const queueWithoutSorting: TReceiverMessageQueue = [
5
- {
6
- target: CommunicatorTargetEnum.SIGN_IN,
7
- action: CommunicatorActionEnum.OPEN,
8
- params: {
9
- callback: () => {}
10
- }
11
- },
12
- {
13
- target: CommunicatorTargetEnum.SIGN_IN,
14
- action: CommunicatorActionEnum.CLOSE,
15
- params: {
16
- callback: () => {}
17
- }
18
- },
19
- {
20
- target: CommunicatorTargetEnum.SIGN_IN,
21
- action: CommunicatorActionEnum.GOTO,
22
- params: {
23
- callback: () => {}
24
- }
25
- },
26
- {
27
- target: CommunicatorTargetEnum.SIGN_IN,
28
- action: CommunicatorActionEnum.TOKEN,
29
- params: {
30
- callback: () => {}
31
- }
32
- },
33
- ];
34
-
35
- const queueWithDefaultScenario: TReceiverMessageQueue = [
36
- {
37
- target: CommunicatorTargetEnum.SIGN_IN,
38
- action: CommunicatorActionEnum.TOKEN,
39
- params: {
40
- callback: () => {}
41
- }
42
- },
43
- {
44
- target: CommunicatorTargetEnum.SIGN_IN,
45
- action: CommunicatorActionEnum.CLOSE,
46
- params: {
47
- callback: () => {}
48
- }
49
- },
50
- {
51
- target: CommunicatorTargetEnum.SIGN_IN,
52
- action: CommunicatorActionEnum.OPEN,
53
- params: {
54
- callback: () => {}
55
- }
56
- },
57
- {
58
- target: CommunicatorTargetEnum.SIGN_IN,
59
- action: CommunicatorActionEnum.GOTO,
60
- params: {
61
- callback: () => {}
62
- }
63
- },
64
- ];
65
-
66
- const queueWithCustomScenario: TReceiverMessageQueue = [
67
- {
68
- target: CommunicatorTargetEnum.SIGN_IN,
69
- action: CommunicatorActionEnum.OPEN,
70
- params: {
71
- callback: () => {}
72
- }
73
- },
74
- {
75
- target: CommunicatorTargetEnum.SIGN_IN,
76
- action: CommunicatorActionEnum.CLOSE,
77
- params: {
78
- callback: () => {}
79
- }
80
- },
81
- {
82
- target: CommunicatorTargetEnum.SIGN_IN,
83
- action: CommunicatorActionEnum.TOKEN,
84
- params: {
85
- callback: () => {}
86
- }
87
- },
88
- {
89
- target: CommunicatorTargetEnum.SIGN_IN,
90
- action: CommunicatorActionEnum.GOTO,
91
- params: {
92
- callback: () => {}
93
- }
94
- },
95
- ];
96
-
97
- const queueWithCustomV2Scenario: TReceiverMessageQueue = [
98
- {
99
- target: CommunicatorTargetEnum.SIGN_IN,
100
- action: CommunicatorActionEnum.CLOSE,
101
- params: {
102
- callback: () => {}
103
- }
104
- },
105
- {
106
- target: CommunicatorTargetEnum.SIGN_IN,
107
- action: CommunicatorActionEnum.TOKEN,
108
- params: {
109
- callback: () => {}
110
- }
111
- },
112
- {
113
- target: CommunicatorTargetEnum.SIGN_IN,
114
- action: CommunicatorActionEnum.OPEN,
115
- params: {
116
- callback: () => {}
117
- }
118
- },
119
- {
120
- target: CommunicatorTargetEnum.SIGN_IN,
121
- action: CommunicatorActionEnum.GOTO,
122
- params: {
123
- callback: () => {}
124
- }
125
- },
126
- ];
127
-
128
- const queueWithCustomV3Scenario: TReceiverMessageQueue = [
129
- {
130
- target: CommunicatorTargetEnum.SIGN_IN,
131
- action: CommunicatorActionEnum.OPEN,
132
- params: {
133
- callback: () => {}
134
- }
135
- },
136
- {
137
- target: CommunicatorTargetEnum.SIGN_IN,
138
- action: CommunicatorActionEnum.CLOSE,
139
- params: {
140
- callback: () => {}
141
- }
142
- },
143
- {
144
- target: CommunicatorTargetEnum.SIGN_IN,
145
- action: CommunicatorActionEnum.TOKEN,
146
- params: {
147
- callback: () => {}
148
- }
149
- },
150
- {
151
- target: CommunicatorTargetEnum.SIGN_IN,
152
- action: CommunicatorActionEnum.GOTO,
153
- params: {
154
- callback: () => {}
155
- }
156
- },
157
- ];
158
-
159
- export {
160
- queueWithoutSorting,
161
- queueWithDefaultScenario,
162
- queueWithCustomScenario,
163
- queueWithCustomV2Scenario,
164
- queueWithCustomV3Scenario,
165
- };
@@ -1,31 +0,0 @@
1
- import {CommunicatorActionEnum, CommunicatorActionEnum_scenario} from '../../src/enums/CommunicatorActionEnum';
2
-
3
- const defaultScenario = CommunicatorActionEnum_scenario();
4
-
5
- const scenarioCustom = [
6
- CommunicatorActionEnum.CLOSE,
7
- CommunicatorActionEnum.TOKEN,
8
- CommunicatorActionEnum.GOTO,
9
- ];
10
-
11
- const scenarioCustomV2 = [
12
- CommunicatorActionEnum.CLOSE,
13
- CommunicatorActionEnum.TOKEN,
14
- CommunicatorActionEnum.OPEN,
15
- CommunicatorActionEnum.GOTO,
16
- ];
17
-
18
- const scenarioCustomV3 = [
19
- CommunicatorActionEnum.OPEN,
20
- CommunicatorActionEnum.CLOSE,
21
- CommunicatorActionEnum.TOKEN,
22
- CommunicatorActionEnum.OPEN,
23
- CommunicatorActionEnum.GOTO,
24
- ];
25
-
26
- export {
27
- defaultScenario,
28
- scenarioCustom,
29
- scenarioCustomV2,
30
- scenarioCustomV3
31
- };