@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.
- package/README.md +92 -43
- package/dist/engine/AccountTracker/Enums/AccountTrackerEnum.d.ts +15 -0
- package/dist/engine/AccountTracker/Enums/AccountTrackerEnum.js +1 -0
- package/dist/engine/AccountTracker/Public/AccountTracker.d.ts +9 -0
- package/dist/engine/AccountTracker/Public/AccountTracker.js +35 -0
- package/dist/engine/AccountTracker/Types/TAccountTracker.d.ts +15 -0
- package/dist/engine/AccountTracker/Types/TAccountTracker.js +1 -0
- package/dist/engine/AccountTracker/index.d.ts +1 -0
- package/dist/engine/AccountTracker/index.js +1 -0
- package/dist/engine/Communicator/Constants/Communicator.d.ts +2 -0
- package/dist/engine/Communicator/Constants/Communicator.js +10 -0
- package/dist/engine/Communicator/Enums/CommunicatorEnum.d.ts +12 -0
- package/dist/engine/Communicator/Enums/CommunicatorEnum.js +1 -0
- package/dist/engine/Communicator/Public/Communicator.d.ts +16 -0
- package/dist/engine/Communicator/Public/Communicator.js +158 -0
- package/dist/engine/Communicator/Types/TCommunicator.d.ts +10 -0
- package/dist/engine/Communicator/Types/TCommunicator.js +1 -0
- package/dist/engine/Communicator/index.d.ts +1 -0
- package/dist/engine/Communicator/index.js +1 -0
- package/dist/engine/index.d.ts +2 -0
- package/dist/engine/index.js +2 -0
- package/dist/enums/CommunicatorErrorEnum.d.ts +15 -0
- package/dist/enums/CommunicatorErrorEnum.js +1 -0
- package/dist/enums/index.d.ts +1 -0
- package/dist/enums/index.js +1 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.js +1 -6
- package/dist/types/TCommunicator.d.ts +110 -0
- package/dist/types/TCommunicator.js +1 -0
- package/dist/types/index.d.ts +1 -59
- package/dist/types/index.js +1 -1
- package/package.json +19 -26
- package/src/engine/AccountTracker/Enums/AccountTrackerEnum.ts +18 -0
- package/src/engine/AccountTracker/Public/AccountTracker.ts +45 -0
- package/src/engine/AccountTracker/Types/TAccountTracker.ts +16 -0
- package/src/engine/AccountTracker/index.ts +1 -0
- package/src/engine/Communicator/Constants/Communicator.ts +14 -0
- package/src/engine/Communicator/Enums/CommunicatorEnum.ts +13 -0
- package/src/engine/Communicator/Public/Communicator.ts +195 -0
- package/src/engine/Communicator/Types/TCommunicator.ts +11 -0
- package/src/engine/Communicator/index.ts +1 -0
- package/src/engine/index.ts +2 -0
- package/src/enums/CommunicatorErrorEnum.ts +22 -0
- package/src/enums/index.ts +1 -0
- package/src/index.ts +18 -11
- package/src/types/TCommunicator.ts +179 -0
- package/src/types/index.ts +1 -72
- package/test/utiles.spec.ts +2 -66
- package/tsconfig.json +5 -0
- package/dist/engine/CommunicatorReceiver.d.ts +0 -14
- package/dist/engine/CommunicatorReceiver.js +0 -114
- package/dist/engine/CommunicatorSender.d.ts +0 -7
- package/dist/engine/CommunicatorSender.js +0 -20
- package/dist/engine/utils.d.ts +0 -5
- package/dist/engine/utils.js +0 -41
- package/dist/enums/CommunicatorActionEnum.d.ts +0 -10
- package/dist/enums/CommunicatorActionEnum.js +0 -15
- package/dist/enums/CommunicatorTargetEnum.d.ts +0 -5
- package/dist/enums/CommunicatorTargetEnum.js +0 -8
- package/dist/receiver.d.ts +0 -6
- package/dist/receiver.js +0 -6
- package/dist/sender.d.ts +0 -5
- package/dist/sender.js +0 -5
- package/src/engine/CommunicatorReceiver.ts +0 -182
- package/src/engine/CommunicatorSender.ts +0 -34
- package/src/engine/utils.ts +0 -53
- package/src/enums/CommunicatorActionEnum.ts +0 -16
- package/src/enums/CommunicatorTargetEnum.ts +0 -8
- package/src/receiver.ts +0 -13
- package/src/sender.ts +0 -11
- package/test/testUnits/message.ts +0 -44
- package/test/testUnits/queue.ts +0 -165
- package/test/testUnits/scenario.ts +0 -31
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
type TChallenge = 'email_otp' | 'mobile_otp' | 'pin';
|
|
2
|
+
type TAuditSource = 'Backend' | 'SelfService';
|
|
3
|
+
type TRegistrationRequestBodySourceDeviceType = 'mobile' | 'regular';
|
|
4
|
+
|
|
5
|
+
type TRequestProps = {
|
|
6
|
+
url?: string,
|
|
7
|
+
method?: RequestInit['method'],
|
|
8
|
+
body?: any,
|
|
9
|
+
headers?: HeadersInit,
|
|
10
|
+
mode?: RequestMode,
|
|
11
|
+
queryParams?: TRequestQueryParams,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type TRequestQueryParams = {
|
|
15
|
+
[key: string]: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type TResponse = {
|
|
19
|
+
json: any;
|
|
20
|
+
status: number;
|
|
21
|
+
ok: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* region signIn */
|
|
25
|
+
type TSignInStartRequestBody = {
|
|
26
|
+
captcha_response?: string,
|
|
27
|
+
meta: TAccountMeta,
|
|
28
|
+
mobile?: string,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type TAccountMeta = {
|
|
32
|
+
audit_source_type: TAuditSource,
|
|
33
|
+
browser_page_resolution: string,
|
|
34
|
+
cookies_enabled: boolean,
|
|
35
|
+
ip: string,
|
|
36
|
+
js_enabled: boolean,
|
|
37
|
+
language: string,
|
|
38
|
+
user_agent: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type TSignInByMobileRequestData = {
|
|
42
|
+
phoneNumber: string,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type TSignInByMobileResponse = {
|
|
46
|
+
auth_token: string,
|
|
47
|
+
challenges: TChallenge[],
|
|
48
|
+
}
|
|
49
|
+
/* endregion signIn */
|
|
50
|
+
|
|
51
|
+
/* region signUp */
|
|
52
|
+
type TSignUpStartRequestBody = {
|
|
53
|
+
locale: string,
|
|
54
|
+
mobile: string,
|
|
55
|
+
timezone_name: string,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type TSignUpByMobileRequestData = {
|
|
59
|
+
locale: string,
|
|
60
|
+
phoneNumber: string,
|
|
61
|
+
timezoneName: string,
|
|
62
|
+
}
|
|
63
|
+
/* endregion signUp */
|
|
64
|
+
|
|
65
|
+
/* region setupChallenge */
|
|
66
|
+
type TSetupChallengeRequestData = {
|
|
67
|
+
type: TChallenge,
|
|
68
|
+
authToken: string,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type TSetupChallengeResponse = {
|
|
72
|
+
attempts_left: number,
|
|
73
|
+
masked_email: string,
|
|
74
|
+
masked_mobile: string,
|
|
75
|
+
next_attempt_after: number,
|
|
76
|
+
}
|
|
77
|
+
/* endregion setupChallenge */
|
|
78
|
+
|
|
79
|
+
/* region verifyChallenge */
|
|
80
|
+
type TVerifyChallengeRequestBody = {
|
|
81
|
+
type: TChallenge,
|
|
82
|
+
value: string,
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type TVerifyChallengeRequestData = {
|
|
86
|
+
authToken: string,
|
|
87
|
+
type: TChallenge,
|
|
88
|
+
code: string,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type TVerifyChallengeResponse = {
|
|
92
|
+
auth_token: string,
|
|
93
|
+
challenges: TChallenge[],
|
|
94
|
+
}
|
|
95
|
+
/* endregion verifyChallenge */
|
|
96
|
+
|
|
97
|
+
/* region emailUpdate */
|
|
98
|
+
type TEmailUpdateRequestData = {
|
|
99
|
+
authToken: string,
|
|
100
|
+
email: string,
|
|
101
|
+
}
|
|
102
|
+
/* endregion emailUpdate */
|
|
103
|
+
|
|
104
|
+
/* region createSession */
|
|
105
|
+
type TCreateSessionRequestData = {
|
|
106
|
+
authToken: string,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type TCreateSessionResponse = {
|
|
110
|
+
access_token: string,
|
|
111
|
+
refresh_token: string,
|
|
112
|
+
}
|
|
113
|
+
/* endregion createSession */
|
|
114
|
+
|
|
115
|
+
/* region getCurrentUserProfile */
|
|
116
|
+
/**
|
|
117
|
+
* Has other properties witch not important for processing on frontend,
|
|
118
|
+
* see https://api-frontend-qa.goolge.com/v1/doc/#operation/GetCurrentUserProfile
|
|
119
|
+
*/
|
|
120
|
+
type TGetCurrentUserProfileResponseUser = {
|
|
121
|
+
profiles: (TGetCurrentUserProfileResponseUserProfiles & Record<string, unknown>)[],
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Has other properties witch not important for processing on frontend,
|
|
126
|
+
* see https://api-frontend-qa.goolge.com/v1/doc/#operation/GetCurrentUserProfile
|
|
127
|
+
*/
|
|
128
|
+
type TGetCurrentUserProfileResponseUserProfiles = {
|
|
129
|
+
target_entity_name: string,
|
|
130
|
+
target_entity_id: string,
|
|
131
|
+
}
|
|
132
|
+
/* endregion getCurrentUserProfile */
|
|
133
|
+
|
|
134
|
+
/* region registration */
|
|
135
|
+
type TRegistrationRequestBodySource = {
|
|
136
|
+
hash: string,
|
|
137
|
+
device_type: TRegistrationRequestBodySourceDeviceType,
|
|
138
|
+
ga_id: string,
|
|
139
|
+
origin: string,
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
type TRegistrationRequestBody = {
|
|
143
|
+
name: string,
|
|
144
|
+
source: TRegistrationRequestBodySource,
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
type TRegistrationRequestData = {
|
|
148
|
+
authToken: string,
|
|
149
|
+
name: TRegistrationRequestBody['name'],
|
|
150
|
+
deviceType: TRegistrationRequestBodySource['device_type']
|
|
151
|
+
hash: TRegistrationRequestBodySource['hash'],
|
|
152
|
+
gaId: TRegistrationRequestBodySource['ga_id'],
|
|
153
|
+
origin: TRegistrationRequestBodySource['origin'],
|
|
154
|
+
}
|
|
155
|
+
/* endregion registration */
|
|
156
|
+
|
|
157
|
+
export {
|
|
158
|
+
TChallenge,
|
|
159
|
+
TAuditSource,
|
|
160
|
+
TRequestProps,
|
|
161
|
+
TRequestQueryParams,
|
|
162
|
+
TResponse,
|
|
163
|
+
TSignInStartRequestBody,
|
|
164
|
+
TAccountMeta,
|
|
165
|
+
TSignInByMobileRequestData,
|
|
166
|
+
TSignInByMobileResponse,
|
|
167
|
+
TSignUpStartRequestBody,
|
|
168
|
+
TSignUpByMobileRequestData,
|
|
169
|
+
TSetupChallengeRequestData,
|
|
170
|
+
TSetupChallengeResponse,
|
|
171
|
+
TVerifyChallengeRequestBody,
|
|
172
|
+
TVerifyChallengeRequestData,
|
|
173
|
+
TVerifyChallengeResponse,
|
|
174
|
+
TEmailUpdateRequestData,
|
|
175
|
+
TCreateSessionRequestData,
|
|
176
|
+
TCreateSessionResponse,
|
|
177
|
+
TGetCurrentUserProfileResponseUser,
|
|
178
|
+
TRegistrationRequestData
|
|
179
|
+
};
|
package/src/types/index.ts
CHANGED
|
@@ -1,72 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import {CommunicatorTargetEnum} from "../enums/CommunicatorTargetEnum";
|
|
3
|
-
|
|
4
|
-
export type TSenderProps = {
|
|
5
|
-
receiverOrigin: string,
|
|
6
|
-
otherWindow: Window,
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export type TReceiverProps = {
|
|
10
|
-
senderOrigin: string,
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type TReceiverCallback = (message: TCommunicatorMessage) => void;
|
|
14
|
-
|
|
15
|
-
export type TCommunicatorActionTokenParams = {
|
|
16
|
-
name: string,
|
|
17
|
-
value: string,
|
|
18
|
-
domain: string,
|
|
19
|
-
expires?: number,
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type TCommunicatorActionResizeParams = {
|
|
23
|
-
width?: number,
|
|
24
|
-
height?: number,
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type TCommunicatorActionGotoParams = {
|
|
28
|
-
url: string,
|
|
29
|
-
isTargetBlank?: boolean,
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type TCommunicatorSenderActionMap = {
|
|
33
|
-
[CommunicatorActionEnum.LOADED]?: true,
|
|
34
|
-
[CommunicatorActionEnum.TOKEN]?: TCommunicatorActionTokenParams,
|
|
35
|
-
[CommunicatorActionEnum.RESIZE]?: TCommunicatorActionResizeParams,
|
|
36
|
-
[CommunicatorActionEnum.GOTO]?: TCommunicatorActionGotoParams,
|
|
37
|
-
[CommunicatorActionEnum.CLOSE]?: true,
|
|
38
|
-
[CommunicatorActionEnum.OPEN]?: CommunicatorTargetEnum,
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type TCommunicatorMessage = {
|
|
42
|
-
target: CommunicatorTargetEnum,
|
|
43
|
-
action: TCommunicatorSenderActionMap
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type TReceiverSubscribeParams = {
|
|
47
|
-
code?: string;
|
|
48
|
-
target: CommunicatorTargetEnum,
|
|
49
|
-
action: CommunicatorActionEnum,
|
|
50
|
-
callback: TReceiverCallback,
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export type TReceiverMessageQueue = TReceiverMessageQueueItem[]
|
|
54
|
-
|
|
55
|
-
export type TReceiverMessageQueueItem = {
|
|
56
|
-
code?: string;
|
|
57
|
-
target: CommunicatorTargetEnum,
|
|
58
|
-
action: CommunicatorActionEnum,
|
|
59
|
-
params?: {
|
|
60
|
-
callback: TReceiverCallback,
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type TReceiverUnsubscribeMessageParams = {
|
|
65
|
-
code?: string;
|
|
66
|
-
target?: CommunicatorTargetEnum,
|
|
67
|
-
action?: CommunicatorActionEnum,
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export type TReceiverTargetScenario = {
|
|
71
|
-
[key in CommunicatorTargetEnum]?: CommunicatorActionEnum[];
|
|
72
|
-
}
|
|
1
|
+
export * from "./TCommunicator";
|
package/test/utiles.spec.ts
CHANGED
|
@@ -1,69 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
queueWithCustomScenario,
|
|
4
|
-
queueWithCustomV2Scenario, queueWithCustomV3Scenario,
|
|
5
|
-
queueWithDefaultScenario,
|
|
6
|
-
queueWithoutSorting
|
|
7
|
-
} from './testUnits/queue';
|
|
8
|
-
import {messageWithInvalidAction, messageWithInvalidTarget, normalMessage} from './testUnits/message';
|
|
9
|
-
import {scenarioCustom, defaultScenario, scenarioCustomV2, scenarioCustomV3} from './testUnits/scenario';
|
|
1
|
+
// TODO: There should be unit tests
|
|
10
2
|
|
|
11
3
|
test('modifyOrigin', () => {
|
|
12
|
-
expect(
|
|
13
|
-
expect(modifyOrigin('google.com/test')).toBe('https://google.com');
|
|
14
|
-
expect(modifyOrigin('https://google.com/')).toBe('https://google.com');
|
|
15
|
-
expect(modifyOrigin(' https://google.com/')).toBe('https://google.com');
|
|
16
|
-
expect(modifyOrigin('htts://google.com')).toBe(null);
|
|
4
|
+
expect(true).toBe(true);
|
|
17
5
|
});
|
|
18
|
-
|
|
19
|
-
describe('isValidMessage', () => {
|
|
20
|
-
it('message is valid', () => {
|
|
21
|
-
expect(isValidMessage(normalMessage)).toBeTruthy();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('message has invalid target', () => {
|
|
25
|
-
expect(
|
|
26
|
-
isValidMessage(messageWithInvalidTarget)
|
|
27
|
-
).toBeFalsy();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('message has invalid action', () => {
|
|
31
|
-
expect(
|
|
32
|
-
isValidMessage(messageWithInvalidAction)
|
|
33
|
-
).toBeFalsy();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// TODO: need validation action params
|
|
37
|
-
// it('Invalid Action Params', () => {
|
|
38
|
-
// expect(
|
|
39
|
-
// isValidMessage(messageWithInvalidActionParams)
|
|
40
|
-
// ).toBeFalsy();
|
|
41
|
-
// });
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
describe('sortMessageQueue', () => {
|
|
45
|
-
it('Default Sorting (Default scenario)', () => {
|
|
46
|
-
expect(
|
|
47
|
-
JSON.stringify(sortMessageQueue(queueWithoutSorting, defaultScenario))
|
|
48
|
-
).toBe(JSON.stringify(queueWithDefaultScenario));
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('Custom Sorting (Not full scenario)', () => {
|
|
52
|
-
expect(
|
|
53
|
-
JSON.stringify(sortMessageQueue(queueWithoutSorting, scenarioCustom))
|
|
54
|
-
).toBe(JSON.stringify(queueWithCustomScenario));
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('Custom Sorting (Full scenario)', () => {
|
|
58
|
-
expect(
|
|
59
|
-
JSON.stringify(sortMessageQueue(queueWithoutSorting, scenarioCustomV2))
|
|
60
|
-
).toBe(JSON.stringify(queueWithCustomV2Scenario));
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('Custom Sorting (Full scenario with Doubling)', () => {
|
|
64
|
-
expect(
|
|
65
|
-
JSON.stringify(sortMessageQueue(queueWithoutSorting, scenarioCustomV3))
|
|
66
|
-
).toBe(JSON.stringify(queueWithCustomV3Scenario));
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { TReceiverProps, TReceiverSubscribeParams, TReceiverUnsubscribeMessageParams } from '../types';
|
|
2
|
-
import { CommunicatorTargetEnum } from '../enums/CommunicatorTargetEnum';
|
|
3
|
-
import { CommunicatorActionEnum } from '../enums/CommunicatorActionEnum';
|
|
4
|
-
export declare class CommunicatorReceiver {
|
|
5
|
-
private readonly senderOrigin;
|
|
6
|
-
private queue;
|
|
7
|
-
private scenario;
|
|
8
|
-
constructor(props: TReceiverProps);
|
|
9
|
-
private onMessage;
|
|
10
|
-
subscribe: (params: TReceiverSubscribeParams) => void;
|
|
11
|
-
unsubscribe: (params?: TReceiverUnsubscribeMessageParams) => boolean;
|
|
12
|
-
private getMessageScenario;
|
|
13
|
-
setMessageScenario: (target: CommunicatorTargetEnum, scenario: CommunicatorActionEnum[]) => void;
|
|
14
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { isValidMessage, modifyOrigin, sortMessageQueue } from "./utils";
|
|
2
|
-
import { CommunicatorTargetEnum_isCorrect } from '../enums/CommunicatorTargetEnum';
|
|
3
|
-
import { CommunicatorActionEnum_isCorrect, CommunicatorActionEnum_scenario } from '../enums/CommunicatorActionEnum';
|
|
4
|
-
export class CommunicatorReceiver {
|
|
5
|
-
constructor(props) {
|
|
6
|
-
this.queue = [];
|
|
7
|
-
this.scenario = {};
|
|
8
|
-
this.onMessage = (event) => {
|
|
9
|
-
if (!event || !event.origin || !event.data) {
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
if (this.senderOrigin !== event.origin) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
let message;
|
|
16
|
-
try {
|
|
17
|
-
message = JSON.parse(event.data);
|
|
18
|
-
}
|
|
19
|
-
catch (error) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
if (!isValidMessage(message)) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
const queue = this.queue.filter(item => item.target === message.target && message.action.hasOwnProperty(item.action));
|
|
26
|
-
const defaultScenario = CommunicatorActionEnum_scenario();
|
|
27
|
-
let sortedQueue = sortMessageQueue(queue, defaultScenario);
|
|
28
|
-
const scenario = this.getMessageScenario(message.target);
|
|
29
|
-
if (defaultScenario !== scenario) {
|
|
30
|
-
sortedQueue = sortMessageQueue(queue, scenario);
|
|
31
|
-
}
|
|
32
|
-
if (sortedQueue.length) {
|
|
33
|
-
sortedQueue.forEach(item => {
|
|
34
|
-
if (typeof item.params.callback === 'function') {
|
|
35
|
-
item.params.callback(message);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
this.subscribe = (params) => {
|
|
41
|
-
if (params.code && typeof params.code !== 'string') {
|
|
42
|
-
throw new Error('WAC: code can only be a string!');
|
|
43
|
-
}
|
|
44
|
-
if ((!params.target || !CommunicatorTargetEnum_isCorrect(params.target)) ||
|
|
45
|
-
(!params.action || !CommunicatorActionEnum_isCorrect(params.action))) {
|
|
46
|
-
throw new Error('WAC: target or action is not allowed!');
|
|
47
|
-
}
|
|
48
|
-
if (this.queue.find(item => {
|
|
49
|
-
return (!params.code || item.code === params.code) &&
|
|
50
|
-
item.target === params.target &&
|
|
51
|
-
item.action === params.action;
|
|
52
|
-
})) {
|
|
53
|
-
console.log(`WAB: subscribe is exists!`);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
const newQueueItem = {
|
|
57
|
-
target: params.target,
|
|
58
|
-
action: params.action,
|
|
59
|
-
params: {
|
|
60
|
-
callback: params.callback,
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
if (params.code) {
|
|
64
|
-
newQueueItem['code'] = params.code;
|
|
65
|
-
}
|
|
66
|
-
this.queue.push(newQueueItem);
|
|
67
|
-
};
|
|
68
|
-
this.unsubscribe = (params) => {
|
|
69
|
-
if (!params || Object.keys(params).length === 0) {
|
|
70
|
-
this.queue = [];
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
const queueForRemove = this.queue.filter(item => {
|
|
74
|
-
return (!params.code || item.code === params.code) &&
|
|
75
|
-
(!params.target || item.target === params.target) &&
|
|
76
|
-
(!params.action || item.action === params.action);
|
|
77
|
-
});
|
|
78
|
-
if (queueForRemove.length) {
|
|
79
|
-
const toRemove = new Set(queueForRemove);
|
|
80
|
-
this.queue = this.queue.filter(x => !toRemove.has(x));
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
return false;
|
|
84
|
-
};
|
|
85
|
-
this.getMessageScenario = (target) => {
|
|
86
|
-
const scenario = this.scenario[target];
|
|
87
|
-
if (scenario) {
|
|
88
|
-
return scenario;
|
|
89
|
-
}
|
|
90
|
-
return CommunicatorActionEnum_scenario();
|
|
91
|
-
};
|
|
92
|
-
this.setMessageScenario = (target, scenario) => {
|
|
93
|
-
if (!CommunicatorTargetEnum_isCorrect(target) ||
|
|
94
|
-
!Array.isArray(scenario) ||
|
|
95
|
-
!scenario.length) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
scenario = [...new Set(scenario)];
|
|
99
|
-
scenario.forEach(item => {
|
|
100
|
-
if (!CommunicatorActionEnum_isCorrect(item)) {
|
|
101
|
-
throw new Error('WAC: failed scenario action!');
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
this.scenario[target] = scenario;
|
|
105
|
-
};
|
|
106
|
-
this.senderOrigin = modifyOrigin(props.senderOrigin);
|
|
107
|
-
if (this.senderOrigin) {
|
|
108
|
-
window.addEventListener('message', this.onMessage, false);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
throw new Error('WAC: senderOrigin is broken!');
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { isValidMessage, modifyOrigin } from './utils';
|
|
2
|
-
export class CommunicatorSender {
|
|
3
|
-
constructor(props) {
|
|
4
|
-
this.otherWindow = props.otherWindow;
|
|
5
|
-
this.receiverOrigin = modifyOrigin(props.receiverOrigin);
|
|
6
|
-
if (!this.receiverOrigin) {
|
|
7
|
-
throw new Error('WAC: receiverOrigin is broken!');
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
sendMessage(message) {
|
|
11
|
-
if (!this.receiverOrigin) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
if (!isValidMessage(message)) {
|
|
15
|
-
throw new Error(`WAC: Message not valid!`);
|
|
16
|
-
}
|
|
17
|
-
const jsonMessage = JSON.stringify(message);
|
|
18
|
-
this.otherWindow.postMessage(jsonMessage, this.receiverOrigin);
|
|
19
|
-
}
|
|
20
|
-
}
|
package/dist/engine/utils.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { TCommunicatorMessage, TReceiverMessageQueue } from "../types";
|
|
2
|
-
import { CommunicatorActionEnum } from "../enums/CommunicatorActionEnum";
|
|
3
|
-
export declare function modifyOrigin(address: string): string;
|
|
4
|
-
export declare function isValidMessage(message: TCommunicatorMessage): boolean;
|
|
5
|
-
export declare function sortMessageQueue(queue: TReceiverMessageQueue, scenario: CommunicatorActionEnum[]): TReceiverMessageQueue;
|
package/dist/engine/utils.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { CommunicatorTargetEnum_isCorrect } from "../enums/CommunicatorTargetEnum";
|
|
2
|
-
import { CommunicatorActionEnum_isCorrect } from "../enums/CommunicatorActionEnum";
|
|
3
|
-
export function modifyOrigin(address) {
|
|
4
|
-
let url;
|
|
5
|
-
try {
|
|
6
|
-
let targetOrigin = address.trim();
|
|
7
|
-
targetOrigin = (targetOrigin.indexOf('://') === -1) ? 'https://' + targetOrigin : targetOrigin;
|
|
8
|
-
url = new URL(targetOrigin);
|
|
9
|
-
}
|
|
10
|
-
catch (_) {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
return url.origin !== 'null' ? url.origin : null;
|
|
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
|
-
}
|
|
31
|
-
export function sortMessageQueue(queue, scenario) {
|
|
32
|
-
return queue.sort((a, b) => {
|
|
33
|
-
if (scenario.indexOf(a.action) > scenario.indexOf(b.action)) {
|
|
34
|
-
return 1;
|
|
35
|
-
}
|
|
36
|
-
if (scenario.indexOf(a.action) < scenario.indexOf(b.action)) {
|
|
37
|
-
return -1;
|
|
38
|
-
}
|
|
39
|
-
return 0;
|
|
40
|
-
});
|
|
41
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export declare enum CommunicatorActionEnum {
|
|
2
|
-
LOADED = "loaded",
|
|
3
|
-
TOKEN = "token",
|
|
4
|
-
RESIZE = "resize",
|
|
5
|
-
CLOSE = "close",
|
|
6
|
-
OPEN = "open",
|
|
7
|
-
GOTO = "goto"
|
|
8
|
-
}
|
|
9
|
-
export declare function CommunicatorActionEnum_isCorrect(item: string): boolean;
|
|
10
|
-
export declare function CommunicatorActionEnum_scenario(): CommunicatorActionEnum[];
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export var CommunicatorActionEnum;
|
|
2
|
-
(function (CommunicatorActionEnum) {
|
|
3
|
-
CommunicatorActionEnum["LOADED"] = "loaded";
|
|
4
|
-
CommunicatorActionEnum["TOKEN"] = "token";
|
|
5
|
-
CommunicatorActionEnum["RESIZE"] = "resize";
|
|
6
|
-
CommunicatorActionEnum["CLOSE"] = "close";
|
|
7
|
-
CommunicatorActionEnum["OPEN"] = "open";
|
|
8
|
-
CommunicatorActionEnum["GOTO"] = "goto";
|
|
9
|
-
})(CommunicatorActionEnum || (CommunicatorActionEnum = {}));
|
|
10
|
-
export function CommunicatorActionEnum_isCorrect(item) {
|
|
11
|
-
return Object.values(CommunicatorActionEnum).includes(item);
|
|
12
|
-
}
|
|
13
|
-
export function CommunicatorActionEnum_scenario() {
|
|
14
|
-
return Object.values(CommunicatorActionEnum);
|
|
15
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export var CommunicatorTargetEnum;
|
|
2
|
-
(function (CommunicatorTargetEnum) {
|
|
3
|
-
CommunicatorTargetEnum["SIGN_UP"] = "sign_up";
|
|
4
|
-
CommunicatorTargetEnum["SIGN_IN"] = "sign_in";
|
|
5
|
-
})(CommunicatorTargetEnum || (CommunicatorTargetEnum = {}));
|
|
6
|
-
export function CommunicatorTargetEnum_isCorrect(item) {
|
|
7
|
-
return Object.values(CommunicatorTargetEnum).includes(item);
|
|
8
|
-
}
|
package/dist/receiver.d.ts
DELETED
|
@@ -1,6 +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
|
-
export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, communicatorMessageIsValid };
|
package/dist/receiver.js
DELETED
|
@@ -1,6 +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
|
-
export { CommunicatorReceiver, CommunicatorTargetEnum, CommunicatorActionEnum, types, communicatorMessageIsValid };
|
package/dist/sender.d.ts
DELETED
|
@@ -1,5 +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
|
-
export { CommunicatorSender, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
|
package/dist/sender.js
DELETED
|
@@ -1,5 +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
|
-
export { CommunicatorSender, CommunicatorTargetEnum, CommunicatorActionEnum, types, };
|