@maxzima/wa-communicator 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Noname
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Sender
2
+
3
+ ```javascript
4
+ const communicator = new CommunicatorSender({
5
+ targetOrigin: 'google.com',
6
+ otherWindow: window.parent,
7
+ });
8
+
9
+ communicator.sendMessage({
10
+ target: 'sign_up',
11
+ action: [
12
+ {
13
+ key: 'goto',
14
+ params: {
15
+ url: 'clients.google.com',
16
+ isTargetBlank: true
17
+ }
18
+ }
19
+ ]
20
+ });
21
+ ```
22
+
23
+ # Receiver
24
+
25
+ ```javascript
26
+ const communicator = new CommunicatorReceiver({
27
+ callback: (message) => {
28
+ if (message.target === 'sign_up' && message.action.length) {
29
+ message.action.forEach((action) => {
30
+ if (action.key === CommunicatorActionEnum.GOTO) {
31
+ window.location = action.params['url'];
32
+ }
33
+ })
34
+ }
35
+ }
36
+ });
37
+
38
+ communicator.watch();
39
+ ```
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "version": "0.0.2",
3
+ "name": "@maxzima/wa-communicator",
4
+ "description": "",
5
+ "author": "Noname",
6
+ "license": "MIT",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "engines": {
10
+ "node": ">=14"
11
+ },
12
+ "scripts": {
13
+ "postinstall": "npm run build",
14
+ "build": "tsc --project tsconfig.json"
15
+ },
16
+ "devDependencies": {
17
+ "typescript": "^4.7.4"
18
+ }
19
+ }
@@ -0,0 +1,50 @@
1
+ import {TCommunicatorMessage, TReceiverProps} from '../types';
2
+
3
+ export class CommunicatorReceiver {
4
+ private readonly callback: any;
5
+
6
+ constructor(props: TReceiverProps) {
7
+ this.callback = props.callback;
8
+ }
9
+
10
+ /**
11
+ * Watch Messages
12
+ */
13
+ public watch() {
14
+ window.addEventListener('message', this.onMessageReceive, false);
15
+ }
16
+
17
+ /**
18
+ *
19
+ * @param event
20
+ */
21
+ private onMessageReceive = (event: MessageEvent) => {
22
+ if (!event || !event.origin || !event.data) {
23
+ return;
24
+ }
25
+
26
+ let message: TCommunicatorMessage;
27
+
28
+ try {
29
+ message = JSON.parse(event.data);
30
+ } catch (error) {
31
+ console.log('PostMessage read error');
32
+ return;
33
+ }
34
+
35
+ if (window.origin !== event.origin) {
36
+ return;
37
+ }
38
+
39
+ if (!Array.isArray(message.action)) {
40
+ message.action = [message.action];
41
+ }
42
+
43
+ if (typeof this.callback !== 'function') {
44
+ return;
45
+ }
46
+
47
+ this.callback(message);
48
+ };
49
+ }
50
+
@@ -0,0 +1,26 @@
1
+ import {TCommunicatorMessage, TSenderProps} from '../types';
2
+ import {modifyUrl} from '../utils';
3
+ import {CommunicatorTargetEnum} from '../enums/CommunicatorTargetEnum';
4
+
5
+ export class CommunicatorSender {
6
+ private readonly otherWindow: Window;
7
+ private readonly targetOrigin: string;
8
+
9
+ constructor(props: TSenderProps) {
10
+ this.otherWindow = props.otherWindow;
11
+ this.targetOrigin = modifyUrl(props.targetOrigin);
12
+ }
13
+
14
+ public sendMessage(message: TCommunicatorMessage) {
15
+ if (
16
+ !this.targetOrigin ||
17
+ !CommunicatorTargetEnum.isCorrect(message.target)
18
+ ) {
19
+ return;
20
+ }
21
+
22
+ const jsonMessage = JSON.stringify(message);
23
+ this.otherWindow.postMessage(jsonMessage, this.targetOrigin);
24
+ }
25
+ }
26
+
@@ -0,0 +1,36 @@
1
+ export default abstract class BaseEnum {
2
+ public static array: string[] = null;
3
+
4
+ public static isCorrect(value: string): boolean {
5
+ return value && value.trim() && this.getAsArray().indexOf(value) !== -1;
6
+ }
7
+
8
+ /**
9
+ * Gets array of all possible values
10
+ */
11
+ public static getAsArray(): string[] {
12
+ if (!this.array) {
13
+ let props = Object.getOwnPropertyNames(this);
14
+ let obj = Object.getPrototypeOf(this);
15
+
16
+ this.array = [];
17
+
18
+ while (Object.prototype.isPrototypeOf.call(BaseEnum, obj)) {
19
+ props = [...props, ...Object.getOwnPropertyNames(obj)];
20
+ obj = Object.getPrototypeOf(obj);
21
+ }
22
+
23
+ for (const prop of props) {
24
+ if (['caller', 'arguments', 'name', 'length', 'prototype'].indexOf(prop) !== -1) {
25
+ continue;
26
+ }
27
+ const self = this as {[index: string]: any};
28
+ const propValue = self[prop];
29
+ if (typeof propValue === 'string' && prop.indexOf('CONTEXT') === -1) {
30
+ this.array.push(propValue);
31
+ }
32
+ }
33
+ }
34
+ return this.array;
35
+ }
36
+ }
@@ -0,0 +1,9 @@
1
+ import {TCommunicatorActionKey} from '../types';
2
+ import BaseEnum from './BaseEnum';
3
+
4
+ export class CommunicatorActionEnum extends BaseEnum {
5
+ public static CLOSE: TCommunicatorActionKey = 'close';
6
+ public static OPEN: TCommunicatorActionKey = 'open';
7
+ public static GOTO: TCommunicatorActionKey = 'goto';
8
+ public static RESIZE: TCommunicatorActionKey = 'resize';
9
+ }
@@ -0,0 +1,7 @@
1
+ import {TCommunicatorTargetKey} from '../types';
2
+ import BaseEnum from './BaseEnum';
3
+
4
+ export class CommunicatorTargetEnum extends BaseEnum {
5
+ public static SIGN_UP: TCommunicatorTargetKey = 'sign_up';
6
+ public static SIGN_IN: TCommunicatorTargetKey = 'sign_in';
7
+ }
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ import {CommunicatorReceiver} from "./engine/CommunicatorReceiver"
2
+ import {CommunicatorSender} from "./engine/CommunicatorSender"
3
+ import {CommunicatorTargetEnum} from "./enums/CommunicatorTargetEnum"
4
+ import {CommunicatorActionEnum} from "./enums/CommunicatorActionEnum"
5
+ import * as types from "./types";
6
+
7
+ export {
8
+ CommunicatorReceiver,
9
+ CommunicatorSender,
10
+ CommunicatorTargetEnum,
11
+ CommunicatorActionEnum,
12
+ types,
13
+ }
package/src/types.ts ADDED
@@ -0,0 +1,43 @@
1
+ export type TSenderProps = {
2
+ targetOrigin: string,
3
+ otherWindow: Window,
4
+ }
5
+
6
+ export type TReceiverProps = {
7
+ callback: (message: TCommunicatorMessageReceiver) => void,
8
+ }
9
+
10
+ export type TCommunicatorTargetKey = 'sign_up' | 'sign_in';
11
+ export type TCommunicatorActionKey = keyof TCommunicatorSenderActionMap;
12
+
13
+ export type TCommunicatorActionResizeParams = {
14
+ width?: number,
15
+ height?: number,
16
+ }
17
+
18
+ export type TCommunicatorActionGotoParams = {
19
+ url: string,
20
+ isTargetBlank?: boolean,
21
+ }
22
+
23
+ export type TCommunicatorSenderActionMap = {
24
+ 'resize': TCommunicatorActionResizeParams,
25
+ 'goto': TCommunicatorActionGotoParams,
26
+ 'close': null,
27
+ 'open': TCommunicatorTargetKey,
28
+ }
29
+
30
+ export type TCommunicatorMessage = {
31
+ target: TCommunicatorTargetKey,
32
+ action: TCommunicatorActionItem | TCommunicatorActionItem[]
33
+ }
34
+
35
+ export type TCommunicatorMessageReceiver = {
36
+ target: TCommunicatorTargetKey,
37
+ action: TCommunicatorActionItem[]
38
+ }
39
+
40
+ export type TCommunicatorActionItem = {
41
+ key: TCommunicatorActionKey,
42
+ params?: TCommunicatorSenderActionMap[TCommunicatorActionKey]
43
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,11 @@
1
+ export function modifyUrl(address: string): string {
2
+ let url: URL;
3
+ try {
4
+ let targetOrigin = address.trim();
5
+ targetOrigin = (targetOrigin.indexOf('://') === -1) ? 'https://' + targetOrigin : targetOrigin;
6
+ url = new URL(targetOrigin);
7
+ } catch (_) {
8
+ return null;
9
+ }
10
+ return url.toString();
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "lib": [
5
+ "DOM",
6
+ "ES6",
7
+ "ESNext"
8
+ ],
9
+ "target": "ES6",
10
+ "module": "esnext",
11
+ "moduleResolution": "node",
12
+ "strictNullChecks": false,
13
+ "esModuleInterop": true,
14
+ "skipLibCheck": true,
15
+ "forceConsistentCasingInFileNames": true,
16
+ "baseUrl": "./src",
17
+ "outDir": "dist",
18
+ "declaration": true,
19
+ "alwaysStrict": true,
20
+ "removeComments": false,
21
+ "noImplicitReturns": true,
22
+ "noEmit": false,
23
+ "noFallthroughCasesInSwitch": true,
24
+ "noUnusedLocals": true,
25
+ "noUnusedParameters": true,
26
+ "importHelpers": true,
27
+ "sourceMap": false,
28
+ "typeRoots": [
29
+ "./node_modules/@types",
30
+ "./src/types"
31
+ ],
32
+ },
33
+ "exclude": [
34
+ "dist",
35
+ "node_modules",
36
+ ]
37
+ }