PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.75
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.75
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / scripts / iframe / messageMiddleware.ts

messageMiddleware.ts in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.3.75, at scripts/iframe/messageMiddleware.ts

263 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { MessageType, PluginMessages } from './integratedMessages';
2 import {
3 fetchDisableInternalTracking,
4 trackConsent,
5 disableInternalTracking,
6 getBusinessUnitId,
7 setBusinessUnitId,
8 skipReview,
9 refreshProxyMappingsCache,
10 fetchProxyMappingsEnabled,
11 toggleProxyMappingsEnabled,
12 } from '../api/wordpressApiClient';
13 import { removeQueryParamFromLocation } from '../utils/queryParams';
14 import { startActivation, startInstall } from '../utils/contentEmbedInstaller';
15
16 export type Message = { key: MessageType; payload?: any };
17
18 /*
19 * We cannot postMessage error objects. We will run into serialization errors
20 * Extract some properties we care about from the errors and create an error object from these
21 */
22 function createSafeErrorPayload(
23 error: any,
24 defaultMessage: string = 'An error occurred'
25 ) {
26 const safePayload: any = {
27 status: (error && error.status) || 500,
28 statusText: (error && error.statusText) || 'Error',
29 message:
30 (error && error.responseJSON && error.responseJSON.message) ||
31 (error && error.message) ||
32 defaultMessage,
33 code: error && error.responseJSON && error.responseJSON.code,
34 };
35
36 if (error && error.responseJSON && error.responseJSON.data) {
37 safePayload.data = error.responseJSON.data;
38 }
39
40 return safePayload;
41 }
42
43 const messageMapper: Map<MessageType, Function> = new Map([
44 [
45 PluginMessages.TrackConsent,
46 (message: Message) => {
47 trackConsent(message.payload);
48 },
49 ],
50 [
51 PluginMessages.InternalTrackingChangeRequest,
52 (message: Message, embedder: any) => {
53 disableInternalTracking(message.payload)
54 .then(() => {
55 embedder.postMessage({
56 key: PluginMessages.InternalTrackingFetchResponse,
57 payload: message.payload,
58 });
59 })
60 .catch(error => {
61 // Extract only serializable properties from error. You cannot postMessage raw error obj with prototype methods
62 embedder.postMessage({
63 key: PluginMessages.InternalTrackingChangeError,
64 payload: createSafeErrorPayload(error),
65 });
66 });
67 },
68 ],
69 [
70 PluginMessages.InternalTrackingFetchRequest,
71 (__message: Message, embedder: any) => {
72 fetchDisableInternalTracking()
73 .then(({ message: payload }) => {
74 embedder.postMessage({
75 key: PluginMessages.InternalTrackingFetchResponse,
76 payload,
77 });
78 })
79 .catch(error => {
80 embedder.postMessage({
81 key: PluginMessages.InternalTrackingFetchError,
82 payload: createSafeErrorPayload(error, 'Fetch error occurred'),
83 });
84 });
85 },
86 ],
87 [
88 PluginMessages.BusinessUnitFetchRequest,
89 (__message: Message, embedder: any) => {
90 getBusinessUnitId()
91 .then(payload => {
92 embedder.postMessage({
93 key: PluginMessages.BusinessUnitFetchResponse,
94 payload,
95 });
96 })
97 .catch(error => {
98 embedder.postMessage({
99 key: PluginMessages.BusinessUnitFetchError,
100 payload: createSafeErrorPayload(error, 'Business unit fetch error'),
101 });
102 });
103 },
104 ],
105 [
106 PluginMessages.BusinessUnitChangeRequest,
107 (message: Message, embedder: any) => {
108 setBusinessUnitId(message.payload)
109 .then(payload => {
110 embedder.postMessage({
111 key: PluginMessages.BusinessUnitFetchResponse,
112 payload,
113 });
114 })
115 .catch(error => {
116 embedder.postMessage({
117 key: PluginMessages.BusinessUnitChangeError,
118 payload: createSafeErrorPayload(
119 error,
120 'Business unit change error'
121 ),
122 });
123 });
124 },
125 ],
126 [
127 PluginMessages.SkipReviewRequest,
128 (__message: Message, embedder: any) => {
129 skipReview()
130 .then(payload => {
131 embedder.postMessage({
132 key: PluginMessages.SkipReviewResponse,
133 payload,
134 });
135 })
136 .catch(error => {
137 embedder.postMessage({
138 key: PluginMessages.SkipReviewError,
139 payload: createSafeErrorPayload(error, 'Skip review error'),
140 });
141 });
142 },
143 ],
144 [
145 PluginMessages.RemoveParentQueryParam,
146 (message: Message) => {
147 removeQueryParamFromLocation(message.payload);
148 },
149 ],
150 [
151 PluginMessages.ContentEmbedInstallRequest,
152 (message: Message, embedder: any) => {
153 startInstall(message.payload.nonce)
154 .then(payload => {
155 embedder.postMessage({
156 key: PluginMessages.ContentEmbedInstallResponse,
157 payload: payload,
158 });
159 })
160 .catch(error => {
161 embedder.postMessage({
162 key: PluginMessages.ContentEmbedInstallError,
163 payload: createSafeErrorPayload(
164 error,
165 'Content embed install error'
166 ),
167 });
168 });
169 },
170 ],
171 [
172 PluginMessages.ContentEmbedActivationRequest,
173 (message: Message, embedder: any) => {
174 startActivation(message.payload.activateAjaxUrl)
175 .then(payload => {
176 embedder.postMessage({
177 key: PluginMessages.ContentEmbedActivationResponse,
178 payload: payload,
179 });
180 })
181 .catch(error => {
182 embedder.postMessage({
183 key: PluginMessages.ContentEmbedActivationError,
184 payload: createSafeErrorPayload(
185 error,
186 'Content embed activation error'
187 ),
188 });
189 });
190 },
191 ],
192 [
193 PluginMessages.RefreshProxyMappingsRequest,
194 (__message: Message, embedder: any) => {
195 refreshProxyMappingsCache()
196 .then(() => {
197 embedder.postMessage({
198 key: PluginMessages.RefreshProxyMappingsResponse,
199 payload: {},
200 });
201 })
202 .catch(error => {
203 embedder.postMessage({
204 key: PluginMessages.RefreshProxyMappingsError,
205 payload: createSafeErrorPayload(
206 error,
207 'Refresh proxy mappings error'
208 ),
209 });
210 });
211 },
212 ],
213 [
214 PluginMessages.ProxyMappingsEnabledRequest,
215 (__message: Message, embedder: any) => {
216 fetchProxyMappingsEnabled()
217 .then(payload => {
218 embedder.postMessage({
219 key: PluginMessages.ProxyMappingsEnabledResponse,
220 payload,
221 });
222 })
223 .catch(error => {
224 embedder.postMessage({
225 key: PluginMessages.ProxyMappingsEnabledError,
226 payload: createSafeErrorPayload(
227 error,
228 'Proxy mappings enabled fetch error'
229 ),
230 });
231 });
232 },
233 ],
234 [
235 PluginMessages.ProxyMappingsEnabledChangeRequest,
236 ({ payload }: Message, embedder: any) => {
237 toggleProxyMappingsEnabled(payload)
238 .then(() => {
239 embedder.postMessage({
240 key: PluginMessages.ProxyMappingsEnabledResponse,
241 payload,
242 });
243 })
244 .catch(error => {
245 embedder.postMessage({
246 key: PluginMessages.ProxyMappingsEnabledChangeError,
247 payload: createSafeErrorPayload(
248 error,
249 'Proxy mappings enabled change error'
250 ),
251 });
252 });
253 },
254 ],
255 ]);
256
257 export const messageMiddleware = (embedder: any) => (message: Message) => {
258 const next = messageMapper.get(message.key);
259 if (next) {
260 next(message, embedder);
261 }
262 };
263