@maxzima/wa-communicator 0.0.5 → 0.0.8

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
@@ -1,8 +1,38 @@
1
- # Sender
1
+ # Import communicator modules
2
+
3
+ Import all
4
+ ```javascript
5
+ import {
6
+ CommunicatorReceiver,
7
+ CommunicatorSender,
8
+ CommunicatorTargetEnum,
9
+ CommunicatorActionEnum,
10
+ } from '@maxzima/wa-communicator';
11
+ ```
12
+
13
+ Import only sender
14
+ ```javascript
15
+ import {
16
+ CommunicatorSender,
17
+ CommunicatorTargetEnum,
18
+ CommunicatorActionEnum,
19
+ } from '@maxzima/wa-communicator/dit/sender';
20
+ ```
21
+
22
+ Import only receiver
23
+ ```javascript
24
+ import {
25
+ CommunicatorReceiver,
26
+ CommunicatorTargetEnum,
27
+ CommunicatorActionEnum,
28
+ } from '@maxzima/wa-communicator';
29
+ ```
30
+
31
+ # Usage Sender
2
32
 
3
33
  ```javascript
4
34
  const communicator = new CommunicatorSender({
5
- targetOrigin: 'google.com',
35
+ receiverOrigin: 'google.com',
6
36
  otherWindow: window.parent,
7
37
  });
8
38
 
@@ -18,10 +48,11 @@ communicator.sendMessage({
18
48
  });
19
49
  ```
20
50
 
21
- # Receiver
51
+ # Usage Receiver
22
52
 
23
53
  ```javascript
24
54
  const communicator = new CommunicatorReceiver({
55
+ senderOrigin: 'clients.google.com',
25
56
  callback: (message) => {
26
57
  if (message.target === CommunicatorTargetEnum.SIGN_UP && message.action[CommunicatorActionEnum.GOTO]) {
27
58
  window.location.href = message.action[CommunicatorActionEnum.GOTO].url;
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../tsconfig",
3
+ "compilerOptions": {
4
+ "module": "ESNext",
5
+ "outDir": "../dist",
6
+ },
7
+ "exclude": [
8
+ "../src/types/index.ts",
9
+ ]
10
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../tsconfig",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "emitDeclarationOnly": true,
6
+ "outDir": "../dist"
7
+ }
8
+ }
@@ -1,14 +1,8 @@
1
1
  import { TReceiverProps } from '../types';
2
2
  export declare class CommunicatorReceiver {
3
3
  private readonly callback;
4
+ private readonly senderOrigin;
4
5
  constructor(props: TReceiverProps);
5
- /**
6
- * Watch Messages
7
- */
8
6
  watch(): void;
9
- /**
10
- * Message Receive Callback
11
- * @param event
12
- */
13
7
  private onMessage;
14
8
  }
@@ -1,9 +1,6 @@
1
+ import { modifyOrigin } from "./utils";
1
2
  export class CommunicatorReceiver {
2
3
  constructor(props) {
3
- /**
4
- * Message Receive Callback
5
- * @param event
6
- */
7
4
  this.onMessage = (event) => {
8
5
  if (!event || !event.origin || !event.data) {
9
6
  return;
@@ -16,7 +13,7 @@ export class CommunicatorReceiver {
16
13
  console.log('PostMessage read error');
17
14
  return;
18
15
  }
19
- if (window.origin !== event.origin) {
16
+ if (this.senderOrigin !== event.origin) {
20
17
  return;
21
18
  }
22
19
  if (typeof this.callback !== 'function') {
@@ -25,10 +22,8 @@ export class CommunicatorReceiver {
25
22
  this.callback(message);
26
23
  };
27
24
  this.callback = props.callback;
25
+ this.senderOrigin = modifyOrigin(props.senderOrigin);
28
26
  }
29
- /**
30
- * Watch Messages
31
- */
32
27
  watch() {
33
28
  window.addEventListener('message', this.onMessage, false);
34
29
  }
@@ -1,11 +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
- /**
7
- * Send PostMessage
8
- * @param message
9
- */
10
6
  sendMessage(message: TCommunicatorMessage): void;
11
7
  }
@@ -1,20 +1,16 @@
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
7
  }
8
- /**
9
- * Send PostMessage
10
- * @param message
11
- */
12
8
  sendMessage(message) {
13
- if (!this.targetOrigin ||
9
+ if (!this.receiverOrigin ||
14
10
  !CommunicatorTargetEnum_isCorrect(message.target)) {
15
11
  return;
16
12
  }
17
13
  const jsonMessage = JSON.stringify(message);
18
- this.otherWindow.postMessage(jsonMessage, this.targetOrigin);
14
+ this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
19
15
  }
20
16
  }
@@ -0,0 +1 @@
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
  }
@@ -0,0 +1,5 @@
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
+ export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
@@ -0,0 +1,5 @@
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
+ export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
@@ -0,0 +1,5 @@
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
+ export { CommunicatorSender, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
package/dist/sender.js ADDED
@@ -0,0 +1,5 @@
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
+ export { CommunicatorSender, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
@@ -1,11 +1,12 @@
1
- import { CommunicatorActionEnum } from "./enums/CommunicatorActionEnum";
2
- import { CommunicatorTargetEnum } from "./enums/CommunicatorTargetEnum";
1
+ import { CommunicatorActionEnum } from "../enums/CommunicatorActionEnum";
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
8
  callback: TReceiverCallback;
9
+ senderOrigin: string;
9
10
  };
10
11
  export declare type TReceiverCallback = (message: TCommunicatorMessage) => void;
11
12
  export declare type TCommunicatorActionResizeParams = {
@@ -0,0 +1 @@
1
+ import { CommunicatorActionEnum } from "../enums/CommunicatorActionEnum";
package/package.json CHANGED
@@ -1,20 +1,28 @@
1
1
  {
2
- "version": "0.0.5",
2
+ "version": "0.0.8",
3
3
  "name": "@maxzima/wa-communicator",
4
4
  "description": "",
5
5
  "author": "Noname",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
- "types": "dist/index.d.ts",
8
+ "types": "dist/index.d.js",
9
9
  "engines": {
10
10
  "node": ">=14"
11
11
  },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": ""
15
+ },
12
16
  "scripts": {
13
- "prepublish": "rm -rf dist",
14
- "postinstall": "npm run build",
15
- "build": "tsc --project tsconfig.json"
17
+ "prepublishOnly": "npm run build",
18
+ "clean": "node tools/cleanup",
19
+ "build": "npm run clean && run-p build:*",
20
+ "build:esm": "tsc -p config/tsconfig.esm.json",
21
+ "build:types": "tsc -p config/tsconfig.types.json",
22
+ "package": "npm run build"
16
23
  },
17
24
  "devDependencies": {
25
+ "npm-run-all": "^4.1.5",
18
26
  "typescript": "^4.7.4"
19
27
  }
20
28
  }
@@ -1,10 +1,13 @@
1
1
  import {TCommunicatorMessage, TReceiverCallback, TReceiverProps} from '../types';
2
+ import {modifyOrigin} from "./utils";
2
3
 
3
4
  export class CommunicatorReceiver {
4
5
  private readonly callback: TReceiverCallback;
6
+ private readonly senderOrigin: string;
5
7
 
6
8
  constructor(props: TReceiverProps) {
7
9
  this.callback = props.callback;
10
+ this.senderOrigin = modifyOrigin(props.senderOrigin);
8
11
  }
9
12
 
10
13
  /**
@@ -32,7 +35,7 @@ export class CommunicatorReceiver {
32
35
  return;
33
36
  }
34
37
 
35
- if (window.origin !== event.origin) {
38
+ if (this.senderOrigin !== event.origin) {
36
39
  return;
37
40
  }
38
41
 
@@ -1,14 +1,14 @@
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
12
  }
13
13
 
14
14
  /**
@@ -17,14 +17,14 @@ export class CommunicatorSender {
17
17
  */
18
18
  public sendMessage(message: TCommunicatorMessage) {
19
19
  if (
20
- !this.targetOrigin ||
20
+ !this.receiverOrigin ||
21
21
  !CommunicatorTargetEnum_isCorrect(message.target)
22
22
  ) {
23
23
  return;
24
24
  }
25
25
 
26
26
  const jsonMessage = JSON.stringify(message);
27
- this.otherWindow.postMessage(jsonMessage, this.targetOrigin);
27
+ this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
28
28
  }
29
29
  }
30
30
 
@@ -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
  }
@@ -0,0 +1,11 @@
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
+
6
+ export {
7
+ CommunicatorReceiver,
8
+ CommunicatorTargetEnum,
9
+ CommunicatorActionEnum,
10
+ types,
11
+ }
package/src/sender.ts ADDED
@@ -0,0 +1,11 @@
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,13 +1,14 @@
1
- import {CommunicatorActionEnum} from "./enums/CommunicatorActionEnum";
2
- import {CommunicatorTargetEnum} from "./enums/CommunicatorTargetEnum";
1
+ import {CommunicatorActionEnum} from "../enums/CommunicatorActionEnum";
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
10
  callback: TReceiverCallback,
11
+ senderOrigin: string,
11
12
  }
12
13
 
13
14
  export type TReceiverCallback = (message: TCommunicatorMessage) => void;
@@ -0,0 +1,26 @@
1
+ /* eslint-disable */
2
+ const fs = require('fs')
3
+ const Path = require('path')
4
+ /* eslint-enable */
5
+
6
+ const deleteFolderRecursive = (path) => {
7
+ if (fs.existsSync(path)) {
8
+ fs.readdirSync(path).forEach((file) => {
9
+ const curPath = Path.join(path, file)
10
+ if (fs.lstatSync(curPath).isDirectory()) {
11
+ deleteFolderRecursive(curPath)
12
+ } else {
13
+ fs.unlinkSync(curPath)
14
+ }
15
+ })
16
+ fs.rmdirSync(path)
17
+ }
18
+ }
19
+
20
+ const folder = process.argv.slice(2)[0]
21
+
22
+ if (folder) {
23
+ deleteFolderRecursive(Path.join(__dirname, '../dist', folder))
24
+ } else {
25
+ deleteFolderRecursive(Path.join(__dirname, '../dist'))
26
+ }
package/tsconfig.json CHANGED
@@ -7,17 +7,15 @@
7
7
  "ESNext"
8
8
  ],
9
9
  "target": "ES6",
10
- "module": "esnext",
11
10
  "moduleResolution": "node",
12
11
  "strictNullChecks": false,
13
12
  "esModuleInterop": true,
14
13
  "skipLibCheck": true,
15
14
  "forceConsistentCasingInFileNames": true,
16
- "baseUrl": "./src",
15
+ "baseUrl": "./",
17
16
  "outDir": "dist",
18
- "declaration": true,
19
17
  "alwaysStrict": true,
20
- "removeComments": false,
18
+ "removeComments": true,
21
19
  "noImplicitReturns": true,
22
20
  "noEmit": false,
23
21
  "noFallthroughCasesInSwitch": true,
@@ -25,13 +23,10 @@
25
23
  "noUnusedParameters": true,
26
24
  "importHelpers": true,
27
25
  "sourceMap": false,
28
- "typeRoots": [
29
- "./node_modules/@types",
30
- "./src/types"
31
- ],
32
26
  },
33
27
  "exclude": [
34
28
  "dist",
35
29
  "node_modules",
36
- ]
30
+ ],
31
+ "include": ["src/**/*"]
37
32
  }
@@ -1,8 +0,0 @@
1
- export default abstract class BaseEnum {
2
- static array: string[];
3
- static isCorrect(value: string): boolean;
4
- /**
5
- * Gets array of all possible values
6
- */
7
- static getAsArray(): string[];
8
- }
@@ -1,31 +0,0 @@
1
- export default class BaseEnum {
2
- static isCorrect(value) {
3
- return value && value.trim() && this.getAsArray().indexOf(value) !== -1;
4
- }
5
- /**
6
- * Gets array of all possible values
7
- */
8
- static getAsArray() {
9
- if (!this.array) {
10
- let props = Object.getOwnPropertyNames(this);
11
- let obj = Object.getPrototypeOf(this);
12
- this.array = [];
13
- while (Object.prototype.isPrototypeOf.call(BaseEnum, obj)) {
14
- props = [...props, ...Object.getOwnPropertyNames(obj)];
15
- obj = Object.getPrototypeOf(obj);
16
- }
17
- for (const prop of props) {
18
- if (['caller', 'arguments', 'name', 'length', 'prototype'].indexOf(prop) !== -1) {
19
- continue;
20
- }
21
- const self = this;
22
- const propValue = self[prop];
23
- if (typeof propValue === 'string' && prop.indexOf('CONTEXT') === -1) {
24
- this.array.push(propValue);
25
- }
26
- }
27
- }
28
- return this.array;
29
- }
30
- }
31
- BaseEnum.array = null;
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- import { CommunicatorActionEnum } from "./enums/CommunicatorActionEnum";
package/dist/utils.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function modifyUrl(address: string): string;