@maxzima/wa-communicator 0.0.9 → 0.0.10

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
@@ -56,7 +56,7 @@ const communicator = new CommunicatorReceiver({
56
56
  });
57
57
 
58
58
  // subscribe
59
- communicator.follow('one', {
59
+ communicator.subscribe({
60
60
  target: CommunicatorTargetEnum.SIGN_UP,
61
61
  action: CommunicatorActionEnum.CLOSE,
62
62
  callback: message => {
@@ -65,8 +65,7 @@ communicator.follow('one', {
65
65
  });
66
66
 
67
67
  // unsubscribe
68
- receiver.unfollow({
69
- code: 'one',
68
+ receiver.unsubscribe({
70
69
  action: CommunicatorActionEnum.CLOSE
71
70
  });
72
71
 
@@ -1,11 +1,9 @@
1
- import { TCommunicatorMessage, TReceiverProps, TReceiverSubscribeParams, TReceiverUnsubscribeMessageParams } from '../types';
1
+ import { TReceiverProps, TReceiverSubscribeParams, TReceiverUnsubscribeMessageParams } from '../types';
2
2
  export declare class CommunicatorReceiver {
3
3
  private readonly senderOrigin;
4
4
  private queue;
5
5
  constructor(props: TReceiverProps);
6
6
  private onMessage;
7
- private prepareCode;
8
- isValidMessage: (message: TCommunicatorMessage) => boolean;
9
- subscribe: (code: string, params: TReceiverSubscribeParams) => void;
7
+ subscribe: (params: TReceiverSubscribeParams) => void;
10
8
  unsubscribe: (params?: TReceiverUnsubscribeMessageParams) => boolean;
11
9
  }
@@ -1,4 +1,4 @@
1
- import { modifyOrigin } from "./utils";
1
+ import { isValidMessage, modifyOrigin } from "./utils";
2
2
  import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
3
3
  import { CommunicatorActionEnum_isCorrect } from '../enums/CommunicatorActionEnum';
4
4
  export class CommunicatorReceiver {
@@ -8,18 +8,17 @@ export class CommunicatorReceiver {
8
8
  if (!event || !event.origin || !event.data) {
9
9
  return;
10
10
  }
11
+ if (this.senderOrigin !== event.origin) {
12
+ return;
13
+ }
11
14
  let message;
12
15
  try {
13
16
  message = JSON.parse(event.data);
14
17
  }
15
18
  catch (error) {
16
- console.log('PostMessage read error');
17
19
  return;
18
20
  }
19
- if (this.senderOrigin !== event.origin) {
20
- return;
21
- }
22
- if (!this.isValidMessage(message)) {
21
+ if (!isValidMessage(message)) {
23
22
  return;
24
23
  }
25
24
  const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
@@ -31,44 +30,32 @@ export class CommunicatorReceiver {
31
30
  });
32
31
  }
33
32
  };
34
- this.prepareCode = (code) => {
35
- if (typeof code === 'string' || code === "") {
36
- throw new Error('WAC: code is not valid!');
33
+ this.subscribe = (params) => {
34
+ if (params.code && typeof params.code !== 'string') {
35
+ throw new Error('WAC: code can only be a string!');
37
36
  }
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
37
  if ((!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
58
38
  (!params.action || !CommunicatorActionEnum_isCorrect(params.action))) {
59
39
  throw new Error('WAC: target or action is not allowed!');
60
40
  }
61
- if (this.queue.find(item => item.code === this.prepareCode(code) && item.target === params.target && item.action === params.action)) {
41
+ if (this.queue.find(item => {
42
+ return (!params.code || item.code === params.code) &&
43
+ item.target === params.target &&
44
+ item.action === params.action;
45
+ })) {
62
46
  console.log(`WAB: subscribe is exists!`);
63
47
  }
64
- this.queue.push({
65
- code: this.prepareCode(code),
48
+ const newQueueItem = {
66
49
  target: params.target,
67
50
  action: params.action,
68
51
  params: {
69
52
  callback: params.callback,
70
53
  }
71
- });
54
+ };
55
+ if (params.code) {
56
+ newQueueItem['code'] = params.code;
57
+ }
58
+ this.queue.push(newQueueItem);
72
59
  };
73
60
  this.unsubscribe = (params) => {
74
61
  if (!params) {
@@ -76,7 +63,7 @@ export class CommunicatorReceiver {
76
63
  return true;
77
64
  }
78
65
  const queueForRemove = this.queue.filter(item => {
79
- return (!params.code || item.code === this.prepareCode(params.code)) &&
66
+ return (!params.code || item.code === params.code) &&
80
67
  (!params.target || item.target === params.target) &&
81
68
  (!params.action || item.action === params.action);
82
69
  });
@@ -1,5 +1,4 @@
1
- import { modifyOrigin } from './utils';
2
- import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
1
+ import { isValidMessage, modifyOrigin } from './utils';
3
2
  export class CommunicatorSender {
4
3
  constructor(props) {
5
4
  this.otherWindow = props.otherWindow;
@@ -12,12 +11,8 @@ export class CommunicatorSender {
12
11
  if (!this.receiverOrigin) {
13
12
  return;
14
13
  }
15
- if (!CommunicatorTargetEnum_isCorrect(message.target)) {
16
- throw new Error(`WAC: Target "${message.target}" is not allowed!`);
17
- }
18
- if (!this.receiverOrigin ||
19
- !CommunicatorTargetEnum_isCorrect(message.target)) {
20
- return;
14
+ if (!isValidMessage(message)) {
15
+ throw new Error(`WAC: Message not valid!`);
21
16
  }
22
17
  const jsonMessage = JSON.stringify(message);
23
18
  this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
@@ -1 +1,3 @@
1
+ import { TCommunicatorMessage } from "../types";
1
2
  export declare function modifyOrigin(address: string): string;
3
+ export declare function isValidMessage(message: TCommunicatorMessage): boolean;
@@ -1,3 +1,5 @@
1
+ import { CommunicatorTargetEnum_isCorrect } from "../enums/CommunicatorTargetEnum";
2
+ import { CommunicatorActionEnum_isCorrect } from "../enums/CommunicatorActionEnum";
1
3
  export function modifyOrigin(address) {
2
4
  let url;
3
5
  try {
@@ -10,3 +12,19 @@ export function modifyOrigin(address) {
10
12
  }
11
13
  return url.origin;
12
14
  }
15
+ export function isValidMessage(message) {
16
+ const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
17
+ const actions = message.action;
18
+ let isValidActions = typeof actions === 'object';
19
+ if (isValidTarget && isValidActions) {
20
+ for (const prop in actions) {
21
+ if (actions.hasOwnProperty(prop)) {
22
+ isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
23
+ if (!isValidActions) {
24
+ break;
25
+ }
26
+ }
27
+ }
28
+ }
29
+ return isValidTarget && isValidActions;
30
+ }
@@ -2,6 +2,7 @@ export declare enum CommunicatorActionEnum {
2
2
  CLOSE = "close",
3
3
  OPEN = "open",
4
4
  GOTO = "goto",
5
- RESIZE = "resize"
5
+ RESIZE = "resize",
6
+ TOKEN = "token"
6
7
  }
7
8
  export declare function CommunicatorActionEnum_isCorrect(item: string): boolean;
@@ -4,6 +4,7 @@ export var CommunicatorActionEnum;
4
4
  CommunicatorActionEnum["OPEN"] = "open";
5
5
  CommunicatorActionEnum["GOTO"] = "goto";
6
6
  CommunicatorActionEnum["RESIZE"] = "resize";
7
+ CommunicatorActionEnum["TOKEN"] = "token";
7
8
  })(CommunicatorActionEnum || (CommunicatorActionEnum = {}));
8
9
  export function CommunicatorActionEnum_isCorrect(item) {
9
10
  return Object.values(CommunicatorActionEnum).includes(item);
@@ -2,4 +2,5 @@ import { CommunicatorReceiver } from "./engine/CommunicatorReceiver";
2
2
  import { CommunicatorTargetEnum } from "./enums/CommunicatorTargetEnum";
3
3
  import { CommunicatorActionEnum } from "./enums/CommunicatorActionEnum";
4
4
  import * as types from "./types";
5
- export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
5
+ import { isValidMessage as communicatorMessageIsValid } from "./engine/utils";
6
+ export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, communicatorMessageIsValid };
package/dist/receiver.js CHANGED
@@ -2,4 +2,5 @@ import { CommunicatorReceiver } from "./engine/CommunicatorReceiver";
2
2
  import { CommunicatorTargetEnum } from "./enums/CommunicatorTargetEnum";
3
3
  import { CommunicatorActionEnum } from "./enums/CommunicatorActionEnum";
4
4
  import * as types from "./types";
5
- export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
5
+ import { isValidMessage as communicatorMessageIsValid } from "./engine/utils";
6
+ export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, communicatorMessageIsValid };
@@ -21,19 +21,21 @@ export declare type TCommunicatorSenderActionMap = {
21
21
  [CommunicatorActionEnum.GOTO]?: TCommunicatorActionGotoParams;
22
22
  [CommunicatorActionEnum.CLOSE]?: true;
23
23
  [CommunicatorActionEnum.OPEN]?: CommunicatorTargetEnum;
24
+ [CommunicatorActionEnum.TOKEN]?: string;
24
25
  };
25
26
  export declare type TCommunicatorMessage = {
26
27
  target: CommunicatorTargetEnum;
27
28
  action: TCommunicatorSenderActionMap;
28
29
  };
29
30
  export declare type TReceiverSubscribeParams = {
31
+ code?: string;
30
32
  target: CommunicatorTargetEnum;
31
33
  action: CommunicatorActionEnum;
32
34
  callback: TReceiverCallback;
33
35
  };
34
36
  export declare type TReceiverMessageQueue = TReceiverMessageQueueItem[];
35
37
  export declare type TReceiverMessageQueueItem = {
36
- code: string;
38
+ code?: string;
37
39
  target: CommunicatorTargetEnum;
38
40
  action: CommunicatorActionEnum;
39
41
  params?: {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.9",
2
+ "version": "0.0.10",
3
3
  "name": "@maxzima/wa-communicator",
4
4
  "description": "",
5
5
  "author": "Noname",
@@ -5,7 +5,7 @@ import {
5
5
  TReceiverSubscribeParams,
6
6
  TReceiverUnsubscribeMessageParams,
7
7
  } from '../types';
8
- import {modifyOrigin} from "./utils";
8
+ import {isValidMessage, modifyOrigin} from "./utils";
9
9
  import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
10
10
  import {CommunicatorActionEnum_isCorrect} from '../enums/CommunicatorActionEnum';
11
11
 
@@ -33,20 +33,19 @@ export class CommunicatorReceiver {
33
33
  return;
34
34
  }
35
35
 
36
+ if (this.senderOrigin !== event.origin) {
37
+ return;
38
+ }
39
+
36
40
  let message: TCommunicatorMessage;
37
41
 
38
42
  try {
39
43
  message = JSON.parse(event.data);
40
44
  } catch (error) {
41
- console.log('PostMessage read error');
42
- return;
43
- }
44
-
45
- if (this.senderOrigin !== event.origin) {
46
45
  return;
47
46
  }
48
47
 
49
- if (!this.isValidMessage(message)) {
48
+ if (!isValidMessage(message)) {
50
49
  return;
51
50
  }
52
51
 
@@ -62,46 +61,15 @@ export class CommunicatorReceiver {
62
61
  }
63
62
  };
64
63
 
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
64
  /**
100
65
  * Subscribe by target and action
101
- * @param code
102
66
  * @param params
103
67
  */
104
- public subscribe = (code: string, params: TReceiverSubscribeParams): void => {
68
+ public subscribe = (params: TReceiverSubscribeParams): void => {
69
+ if (params.code && typeof params.code !== 'string') {
70
+ throw new Error('WAC: code can only be a string!');
71
+ }
72
+
105
73
  if (
106
74
  (!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
107
75
  (!params.action || !CommunicatorActionEnum_isCorrect(params.action))
@@ -109,18 +77,27 @@ export class CommunicatorReceiver {
109
77
  throw new Error('WAC: target or action is not allowed!');
110
78
  }
111
79
 
112
- if (this.queue.find(item => item.code === this.prepareCode(code) && item.target === params.target && item.action === params.action)) {
80
+ if (this.queue.find(item => {
81
+ return (!params.code || item.code === params.code) &&
82
+ item.target === params.target &&
83
+ item.action === params.action;
84
+ })) {
113
85
  console.log(`WAB: subscribe is exists!`);
114
86
  }
115
87
 
116
- this.queue.push({
117
- code: this.prepareCode(code),
88
+ const newQueueItem = {
118
89
  target: params.target,
119
90
  action: params.action,
120
91
  params: {
121
92
  callback: params.callback,
122
93
  }
123
- });
94
+ };
95
+
96
+ if (params.code) {
97
+ newQueueItem['code'] = params.code;
98
+ }
99
+
100
+ this.queue.push(newQueueItem);
124
101
  }
125
102
 
126
103
  /**
@@ -134,7 +111,7 @@ export class CommunicatorReceiver {
134
111
  }
135
112
 
136
113
  const queueForRemove = this.queue.filter(item => {
137
- return (!params.code || item.code === this.prepareCode(params.code)) &&
114
+ return (!params.code || item.code === params.code) &&
138
115
  (!params.target || item.target === params.target) &&
139
116
  (!params.action || item.action === params.action);
140
117
  });
@@ -1,6 +1,5 @@
1
1
  import {TCommunicatorMessage, TSenderProps} from '../types';
2
- import {modifyOrigin} from './utils';
3
- import {CommunicatorTargetEnum_isCorrect} from '../enums/CommunicatorTargetEnum';
2
+ import {isValidMessage, modifyOrigin} from './utils';
4
3
 
5
4
  export class CommunicatorSender {
6
5
  private readonly otherWindow: Window;
@@ -24,15 +23,8 @@ export class CommunicatorSender {
24
23
  return;
25
24
  }
26
25
 
27
- if (!CommunicatorTargetEnum_isCorrect(message.target)) {
28
- throw new Error(`WAC: Target "${message.target}" is not allowed!`);
29
- }
30
-
31
- if (
32
- !this.receiverOrigin ||
33
- !CommunicatorTargetEnum_isCorrect(message.target)
34
- ) {
35
- return;
26
+ if (!isValidMessage(message)) {
27
+ throw new Error(`WAC: Message not valid!`);
36
28
  }
37
29
 
38
30
  const jsonMessage = JSON.stringify(message);
@@ -1,3 +1,11 @@
1
+ import {TCommunicatorMessage} from "../types";
2
+ import {CommunicatorTargetEnum_isCorrect} from "../enums/CommunicatorTargetEnum";
3
+ import {CommunicatorActionEnum_isCorrect} from "../enums/CommunicatorActionEnum";
4
+
5
+ /**
6
+ *
7
+ * @param address
8
+ */
1
9
  export function modifyOrigin(address: string): string {
2
10
  let url: URL;
3
11
  try {
@@ -9,3 +17,26 @@ export function modifyOrigin(address: string): string {
9
17
  }
10
18
  return url.origin;
11
19
  }
20
+
21
+ /**
22
+ * Check the message for validity
23
+ * @param message
24
+ */
25
+ export function isValidMessage(message: TCommunicatorMessage) {
26
+ const isValidTarget = message.target && CommunicatorTargetEnum_isCorrect(message.target);
27
+ const actions = message.action;
28
+ let isValidActions = typeof actions === 'object';
29
+
30
+ if (isValidTarget && isValidActions) {
31
+ for (const prop in actions) {
32
+ if(actions.hasOwnProperty(prop)) {
33
+ isValidActions = actions.hasOwnProperty(prop) && CommunicatorActionEnum_isCorrect(prop);
34
+ if (!isValidActions) {
35
+ break;
36
+ }
37
+ }
38
+ }
39
+ }
40
+
41
+ return isValidTarget && isValidActions;
42
+ }
@@ -3,6 +3,7 @@ export enum CommunicatorActionEnum {
3
3
  OPEN = 'open',
4
4
  GOTO = 'goto',
5
5
  RESIZE = 'resize',
6
+ TOKEN = 'token',
6
7
  }
7
8
 
8
9
  export function CommunicatorActionEnum_isCorrect(item: string): boolean {
package/src/receiver.ts CHANGED
@@ -2,10 +2,12 @@ import {CommunicatorReceiver} from "./engine/CommunicatorReceiver"
2
2
  import {CommunicatorTargetEnum} from "./enums/CommunicatorTargetEnum"
3
3
  import {CommunicatorActionEnum} from "./enums/CommunicatorActionEnum"
4
4
  import * as types from "./types";
5
+ import {isValidMessage as communicatorMessageIsValid} from "./engine/utils";
5
6
 
6
7
  export {
7
8
  CommunicatorReceiver,
8
9
  CommunicatorTargetEnum,
9
10
  CommunicatorActionEnum,
10
11
  types,
12
+ communicatorMessageIsValid
11
13
  }
@@ -27,6 +27,7 @@ export type TCommunicatorSenderActionMap = {
27
27
  [CommunicatorActionEnum.GOTO]?: TCommunicatorActionGotoParams,
28
28
  [CommunicatorActionEnum.CLOSE]?: true,
29
29
  [CommunicatorActionEnum.OPEN]?: CommunicatorTargetEnum,
30
+ [CommunicatorActionEnum.TOKEN]?: string,
30
31
  }
31
32
 
32
33
  export type TCommunicatorMessage = {
@@ -35,6 +36,7 @@ export type TCommunicatorMessage = {
35
36
  }
36
37
 
37
38
  export type TReceiverSubscribeParams = {
39
+ code?: string;
38
40
  target: CommunicatorTargetEnum,
39
41
  action: CommunicatorActionEnum,
40
42
  callback: TReceiverCallback,
@@ -43,7 +45,7 @@ export type TReceiverSubscribeParams = {
43
45
  export type TReceiverMessageQueue = TReceiverMessageQueueItem[]
44
46
 
45
47
  export type TReceiverMessageQueueItem = {
46
- code: string;
48
+ code?: string;
47
49
  target: CommunicatorTargetEnum,
48
50
  action: CommunicatorActionEnum,
49
51
  params?: {