@maxzima/wa-communicator 0.0.6 → 0.0.9

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/README.md CHANGED
@@ -32,7 +32,7 @@ import {
32
32
 
33
33
  ```javascript
34
34
  const communicator = new CommunicatorSender({
35
- targetOrigin: 'google.com',
35
+ receiverOrigin: 'google.com',
36
36
  otherWindow: window.parent,
37
37
  });
38
38
 
@@ -52,12 +52,22 @@ communicator.sendMessage({
52
52
 
53
53
  ```javascript
54
54
  const communicator = new CommunicatorReceiver({
55
- callback: (message) => {
56
- if (message.target === CommunicatorTargetEnum.SIGN_UP && message.action[CommunicatorActionEnum.GOTO]) {
57
- window.location.href = message.action[CommunicatorActionEnum.GOTO].url;
58
- }
59
- }
55
+ senderOrigin: 'clients.google.com'
56
+ });
57
+
58
+ // subscribe
59
+ communicator.follow('one', {
60
+ target: CommunicatorTargetEnum.SIGN_UP,
61
+ action: CommunicatorActionEnum.CLOSE,
62
+ callback: message => {
63
+ console.log('close', message);
64
+ },
65
+ });
66
+
67
+ // unsubscribe
68
+ receiver.unfollow({
69
+ code: 'one',
70
+ action: CommunicatorActionEnum.CLOSE
60
71
  });
61
72
 
62
- communicator.watch();
63
73
  ```
@@ -1,7 +1,11 @@
1
- import { TReceiverProps } from '../types';
1
+ import { TCommunicatorMessage, TReceiverProps, TReceiverSubscribeParams, TReceiverUnsubscribeMessageParams } from '../types';
2
2
  export declare class CommunicatorReceiver {
3
- private readonly callback;
3
+ private readonly senderOrigin;
4
+ private queue;
4
5
  constructor(props: TReceiverProps);
5
- watch(): void;
6
6
  private onMessage;
7
+ private prepareCode;
8
+ isValidMessage: (message: TCommunicatorMessage) => boolean;
9
+ subscribe: (code: string, params: TReceiverSubscribeParams) => void;
10
+ unsubscribe: (params?: TReceiverUnsubscribeMessageParams) => boolean;
7
11
  }
@@ -1,5 +1,9 @@
1
+ import { modifyOrigin } from "./utils";
2
+ import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
3
+ import { CommunicatorActionEnum_isCorrect } from '../enums/CommunicatorActionEnum';
1
4
  export class CommunicatorReceiver {
2
5
  constructor(props) {
6
+ this.queue = [];
3
7
  this.onMessage = (event) => {
4
8
  if (!event || !event.origin || !event.data) {
5
9
  return;
@@ -12,17 +16,83 @@ export class CommunicatorReceiver {
12
16
  console.log('PostMessage read error');
13
17
  return;
14
18
  }
15
- if (window.origin !== event.origin) {
19
+ if (this.senderOrigin !== event.origin) {
16
20
  return;
17
21
  }
18
- if (typeof this.callback !== 'function') {
22
+ if (!this.isValidMessage(message)) {
19
23
  return;
20
24
  }
21
- this.callback(message);
25
+ const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
26
+ if (queue.length) {
27
+ queue.forEach(item => {
28
+ if (typeof item.params.callback === 'function') {
29
+ item.params.callback(message);
30
+ }
31
+ });
32
+ }
22
33
  };
23
- this.callback = props.callback;
24
- }
25
- watch() {
26
- window.addEventListener('message', this.onMessage, false);
34
+ this.prepareCode = (code) => {
35
+ if (typeof code === 'string' || code === "") {
36
+ throw new Error('WAC: code is not valid!');
37
+ }
38
+ return `wab_${code}`;
39
+ };
40
+ this.isValidMessage = (message) => {
41
+ const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
42
+ const actions = message.action;
43
+ let isValidActions = typeof actions === 'object';
44
+ if (isValidTarget && isValidActions) {
45
+ for (const prop in actions) {
46
+ if (actions.hasOwnProperty(prop)) {
47
+ isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
48
+ if (!isValidActions) {
49
+ break;
50
+ }
51
+ }
52
+ }
53
+ }
54
+ return isValidTarget && isValidActions;
55
+ };
56
+ this.subscribe = (code, params) => {
57
+ if ((!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
58
+ (!params.action || !CommunicatorActionEnum_isCorrect(params.action))) {
59
+ throw new Error('WAC: target or action is not allowed!');
60
+ }
61
+ if (this.queue.find(item => item.code === this.prepareCode(code) && item.target === params.target && item.action === params.action)) {
62
+ console.log(`WAB: subscribe is exists!`);
63
+ }
64
+ this.queue.push({
65
+ code: this.prepareCode(code),
66
+ target: params.target,
67
+ action: params.action,
68
+ params: {
69
+ callback: params.callback,
70
+ }
71
+ });
72
+ };
73
+ this.unsubscribe = (params) => {
74
+ if (!params) {
75
+ this.queue = [];
76
+ return true;
77
+ }
78
+ const queueForRemove = this.queue.filter(item => {
79
+ return (!params.code || item.code === this.prepareCode(params.code)) &&
80
+ (!params.target || item.target === params.target) &&
81
+ (!params.action || item.action === params.action);
82
+ });
83
+ if (queueForRemove.length) {
84
+ const toRemove = new Set(queueForRemove);
85
+ this.queue = this.queue.filter(x => !toRemove.has(x));
86
+ return true;
87
+ }
88
+ return false;
89
+ };
90
+ this.senderOrigin = modifyOrigin(props.senderOrigin);
91
+ if (this.senderOrigin) {
92
+ window.addEventListener('message', this.onMessage, false);
93
+ }
94
+ else {
95
+ throw new Error('WAC: senderOrigin is broken!');
96
+ }
27
97
  }
28
98
  }
@@ -1,7 +1,7 @@
1
1
  import { TCommunicatorMessage, TSenderProps } from '../types';
2
2
  export declare class CommunicatorSender {
3
3
  private readonly otherWindow;
4
- private readonly targetOrigin;
4
+ private readonly receiverOrigin;
5
5
  constructor(props: TSenderProps);
6
6
  sendMessage(message: TCommunicatorMessage): void;
7
7
  }
@@ -1,16 +1,25 @@
1
- import { modifyUrl } from './utils';
1
+ import { modifyOrigin } from './utils';
2
2
  import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
3
3
  export class CommunicatorSender {
4
4
  constructor(props) {
5
5
  this.otherWindow = props.otherWindow;
6
- this.targetOrigin = modifyUrl(props.targetOrigin);
6
+ this.receiverOrigin = modifyOrigin(props.receiverOrigin);
7
+ if (!this.receiverOrigin) {
8
+ throw new Error('WAC: receiverOrigin is broken!');
9
+ }
7
10
  }
8
11
  sendMessage(message) {
9
- if (!this.targetOrigin ||
12
+ if (!this.receiverOrigin) {
13
+ return;
14
+ }
15
+ if (!CommunicatorTargetEnum_isCorrect(message.target)) {
16
+ throw new Error(`WAC: Target "${message.target}" is not allowed!`);
17
+ }
18
+ if (!this.receiverOrigin ||
10
19
  !CommunicatorTargetEnum_isCorrect(message.target)) {
11
20
  return;
12
21
  }
13
22
  const jsonMessage = JSON.stringify(message);
14
- this.otherWindow.postMessage(jsonMessage, this.targetOrigin);
23
+ this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
15
24
  }
16
25
  }
@@ -1 +1 @@
1
- export declare function modifyUrl(address: string): string;
1
+ export declare function modifyOrigin(address: string): string;
@@ -1,4 +1,4 @@
1
- export function modifyUrl(address) {
1
+ export function modifyOrigin(address) {
2
2
  let url;
3
3
  try {
4
4
  let targetOrigin = address.trim();
@@ -8,5 +8,5 @@ export function modifyUrl(address) {
8
8
  catch (_) {
9
9
  return null;
10
10
  }
11
- return url.toString();
11
+ return url.origin;
12
12
  }
@@ -1,11 +1,11 @@
1
1
  import { CommunicatorActionEnum } from "../enums/CommunicatorActionEnum";
2
2
  import { CommunicatorTargetEnum } from "../enums/CommunicatorTargetEnum";
3
3
  export declare type TSenderProps = {
4
- targetOrigin: string;
4
+ receiverOrigin: string;
5
5
  otherWindow: Window;
6
6
  };
7
7
  export declare type TReceiverProps = {
8
- callback: TReceiverCallback;
8
+ senderOrigin: string;
9
9
  };
10
10
  export declare type TReceiverCallback = (message: TCommunicatorMessage) => void;
11
11
  export declare type TCommunicatorActionResizeParams = {
@@ -26,3 +26,22 @@ export declare type TCommunicatorMessage = {
26
26
  target: CommunicatorTargetEnum;
27
27
  action: TCommunicatorSenderActionMap;
28
28
  };
29
+ export declare type TReceiverSubscribeParams = {
30
+ target: CommunicatorTargetEnum;
31
+ action: CommunicatorActionEnum;
32
+ callback: TReceiverCallback;
33
+ };
34
+ export declare type TReceiverMessageQueue = TReceiverMessageQueueItem[];
35
+ export declare type TReceiverMessageQueueItem = {
36
+ code: string;
37
+ target: CommunicatorTargetEnum;
38
+ action: CommunicatorActionEnum;
39
+ params?: {
40
+ callback: TReceiverCallback;
41
+ };
42
+ };
43
+ export declare type TReceiverUnsubscribeMessageParams = {
44
+ code?: string;
45
+ target?: CommunicatorTargetEnum;
46
+ action?: CommunicatorActionEnum;
47
+ };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.6",
2
+ "version": "0.0.9",
3
3
  "name": "@maxzima/wa-communicator",
4
4
  "description": "",
5
5
  "author": "Noname",
@@ -1,17 +1,27 @@
1
- import {TCommunicatorMessage, TReceiverCallback, TReceiverProps} from '../types';
1
+ import {
2
+ TCommunicatorMessage,
3
+ TReceiverMessageQueue,
4
+ TReceiverProps,
5
+ TReceiverSubscribeParams,
6
+ TReceiverUnsubscribeMessageParams,
7
+ } from '../types';
8
+ import {modifyOrigin} from "./utils";
9
+ import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
10
+ import {CommunicatorActionEnum_isCorrect} from '../enums/CommunicatorActionEnum';
2
11
 
3
12
  export class CommunicatorReceiver {
4
- private readonly callback: TReceiverCallback;
13
+ private readonly senderOrigin: string;
14
+ private queue: TReceiverMessageQueue = [];
5
15
 
6
16
  constructor(props: TReceiverProps) {
7
- this.callback = props.callback;
8
- }
17
+ this.senderOrigin = modifyOrigin(props.senderOrigin);
9
18
 
10
- /**
11
- * Watch Messages
12
- */
13
- public watch() {
14
- window.addEventListener('message', this.onMessage, false);
19
+ if (this.senderOrigin) {
20
+ // Watch Messages
21
+ window.addEventListener('message', this.onMessage, false);
22
+ } else {
23
+ throw new Error('WAC: senderOrigin is broken!');
24
+ }
15
25
  }
16
26
 
17
27
  /**
@@ -32,15 +42,110 @@ export class CommunicatorReceiver {
32
42
  return;
33
43
  }
34
44
 
35
- if (window.origin !== event.origin) {
45
+ if (this.senderOrigin !== event.origin) {
36
46
  return;
37
47
  }
38
48
 
39
- if (typeof this.callback !== 'function') {
49
+ if (!this.isValidMessage(message)) {
40
50
  return;
41
51
  }
42
52
 
43
- this.callback(message);
53
+ // Find queue by target and action
54
+ const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
55
+
56
+ if (queue.length) {
57
+ queue.forEach(item => {
58
+ if (typeof item.params.callback === 'function') {
59
+ item.params.callback(message);
60
+ }
61
+ })
62
+ }
44
63
  };
64
+
65
+ /**
66
+ * Prepare code for queue
67
+ * @param code
68
+ */
69
+ private prepareCode = (code: string) => {
70
+ if (typeof code === 'string' || code === "") {
71
+ throw new Error('WAC: code is not valid!');
72
+ }
73
+ return `wab_${code}`
74
+ }
75
+
76
+ /**
77
+ * Check the message for validity
78
+ * @param message
79
+ */
80
+ public isValidMessage = (message: TCommunicatorMessage) => {
81
+ const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
82
+ const actions = message.action;
83
+ let isValidActions = typeof actions === 'object';
84
+
85
+ if (isValidTarget && isValidActions) {
86
+ for (const prop in actions) {
87
+ if(actions.hasOwnProperty(prop)) {
88
+ isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
89
+ if (!isValidActions) {
90
+ break;
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ return isValidTarget && isValidActions;
97
+ }
98
+
99
+ /**
100
+ * Subscribe by target and action
101
+ * @param code
102
+ * @param params
103
+ */
104
+ public subscribe = (code: string, params: TReceiverSubscribeParams): void => {
105
+ if (
106
+ (!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
107
+ (!params.action || !CommunicatorActionEnum_isCorrect(params.action))
108
+ ) {
109
+ throw new Error('WAC: target or action is not allowed!');
110
+ }
111
+
112
+ if (this.queue.find(item => item.code === this.prepareCode(code) && item.target === params.target && item.action === params.action)) {
113
+ console.log(`WAB: subscribe is exists!`);
114
+ }
115
+
116
+ this.queue.push({
117
+ code: this.prepareCode(code),
118
+ target: params.target,
119
+ action: params.action,
120
+ params: {
121
+ callback: params.callback,
122
+ }
123
+ });
124
+ }
125
+
126
+ /**
127
+ * Subscribe by code or target or action
128
+ * @param params
129
+ */
130
+ public unsubscribe = (params?: TReceiverUnsubscribeMessageParams): boolean => {
131
+ if (!params) {
132
+ this.queue = [];
133
+ return true;
134
+ }
135
+
136
+ const queueForRemove = this.queue.filter(item => {
137
+ return (!params.code || item.code === this.prepareCode(params.code)) &&
138
+ (!params.target || item.target === params.target) &&
139
+ (!params.action || item.action === params.action);
140
+ });
141
+
142
+ if (queueForRemove.length) {
143
+ const toRemove = new Set(queueForRemove);
144
+ this.queue = this.queue.filter(x => !toRemove.has(x));
145
+ return true;
146
+ }
147
+
148
+ return false;
149
+ }
45
150
  }
46
151
 
@@ -1,14 +1,18 @@
1
1
  import {TCommunicatorMessage, TSenderProps} from '../types';
2
- import {modifyUrl} from './utils';
2
+ import {modifyOrigin} from './utils';
3
3
  import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
4
4
 
5
5
  export class CommunicatorSender {
6
6
  private readonly otherWindow: Window;
7
- private readonly targetOrigin: string;
7
+ private readonly receiverOrigin: string;
8
8
 
9
9
  constructor(props: TSenderProps) {
10
10
  this.otherWindow = props.otherWindow;
11
- this.targetOrigin = modifyUrl(props.targetOrigin);
11
+ this.receiverOrigin = modifyOrigin(props.receiverOrigin);
12
+
13
+ if (!this.receiverOrigin) {
14
+ throw new Error('WAC: receiverOrigin is broken!');
15
+ }
12
16
  }
13
17
 
14
18
  /**
@@ -16,15 +20,23 @@ export class CommunicatorSender {
16
20
  * @param message
17
21
  */
18
22
  public sendMessage(message: TCommunicatorMessage) {
23
+ if (!this.receiverOrigin) {
24
+ return;
25
+ }
26
+
27
+ if (!CommunicatorTargetEnum_isCorrect(message.target)) {
28
+ throw new Error(`WAC: Target "${message.target}" is not allowed!`);
29
+ }
30
+
19
31
  if (
20
- !this.targetOrigin ||
32
+ !this.receiverOrigin ||
21
33
  !CommunicatorTargetEnum_isCorrect(message.target)
22
34
  ) {
23
35
  return;
24
36
  }
25
37
 
26
38
  const jsonMessage = JSON.stringify(message);
27
- this.otherWindow.postMessage(jsonMessage, this.targetOrigin);
39
+ this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
28
40
  }
29
41
  }
30
42
 
@@ -1,4 +1,4 @@
1
- export function modifyUrl(address: string): string {
1
+ export function modifyOrigin(address: string): string {
2
2
  let url: URL;
3
3
  try {
4
4
  let targetOrigin = address.trim();
@@ -7,5 +7,5 @@ export function modifyUrl(address: string): string {
7
7
  } catch (_) {
8
8
  return null;
9
9
  }
10
- return url.toString();
10
+ return url.origin;
11
11
  }
@@ -2,12 +2,12 @@ import {CommunicatorActionEnum} from "../enums/CommunicatorActionEnum";
2
2
  import {CommunicatorTargetEnum} from "../enums/CommunicatorTargetEnum";
3
3
 
4
4
  export type TSenderProps = {
5
- targetOrigin: string,
5
+ receiverOrigin: string,
6
6
  otherWindow: Window,
7
7
  }
8
8
 
9
9
  export type TReceiverProps = {
10
- callback: TReceiverCallback,
10
+ senderOrigin: string,
11
11
  }
12
12
 
13
13
  export type TReceiverCallback = (message: TCommunicatorMessage) => void;
@@ -33,3 +33,26 @@ export type TCommunicatorMessage = {
33
33
  target: CommunicatorTargetEnum,
34
34
  action: TCommunicatorSenderActionMap
35
35
  }
36
+
37
+ export type TReceiverSubscribeParams = {
38
+ target: CommunicatorTargetEnum,
39
+ action: CommunicatorActionEnum,
40
+ callback: TReceiverCallback,
41
+ }
42
+
43
+ export type TReceiverMessageQueue = TReceiverMessageQueueItem[]
44
+
45
+ export type TReceiverMessageQueueItem = {
46
+ code: string;
47
+ target: CommunicatorTargetEnum,
48
+ action: CommunicatorActionEnum,
49
+ params?: {
50
+ callback: TReceiverCallback,
51
+ }
52
+ }
53
+
54
+ export type TReceiverUnsubscribeMessageParams = {
55
+ code?: string;
56
+ target?: CommunicatorTargetEnum,
57
+ action?: CommunicatorActionEnum,
58
+ }
package/tsconfig.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "outDir": "dist",
17
17
  "alwaysStrict": true,
18
18
  "removeComments": true,
19
- "noImplicitReturns": true,
19
+ "noImplicitReturns": false,
20
20
  "noEmit": false,
21
21
  "noFallthroughCasesInSwitch": true,
22
22
  "noUnusedLocals": true,