PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.58
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.58
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 / api / wordpressApiClient.ts

wordpressApiClient.ts in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.3.58, at scripts/api/wordpressApiClient.ts

107 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import $ from 'jquery';
2
3 import Raven from '../lib/Raven';
4 import { restNonce, restUrl } from '../constants/leadinConfig';
5 import { addQueryObjectToUrl } from '../utils/queryParams';
6
7 function makeRequest(
8 method: string,
9 path: string,
10 data: any = {},
11 queryParams = {}
12 ): Promise<any> {
13 // eslint-disable-next-line compat/compat
14 const restApiUrl = new URL(`${restUrl}leadin/v1${path}`);
15 addQueryObjectToUrl(restApiUrl, queryParams);
16
17 return new Promise((resolve, reject) => {
18 const payload: { [key: string]: any } = {
19 url: restApiUrl.toString(),
20 method,
21 contentType: 'application/json',
22 beforeSend: (xhr: any) => xhr.setRequestHeader('X-WP-Nonce', restNonce),
23 success: resolve,
24 error: (response: any) => {
25 Raven.captureMessage(
26 `HTTP Request to ${restApiUrl} failed with error ${response.status}: ${response.responseText}`,
27 {
28 fingerprint: [
29 '{{ default }}',
30 path,
31 response.status,
32 response.responseText,
33 ],
34 }
35 );
36 reject(response);
37 },
38 };
39
40 if (method !== 'get') {
41 payload.data = JSON.stringify(data);
42 }
43
44 $.ajax(payload);
45 });
46 }
47
48 export function healthcheckRestApi() {
49 return makeRequest('get', '/healthcheck');
50 }
51
52 export function disableInternalTracking(value: boolean) {
53 return makeRequest('put', '/internal-tracking', value ? '1' : '0');
54 }
55
56 export function fetchDisableInternalTracking() {
57 return makeRequest('get', '/internal-tracking').then(message => ({
58 message,
59 }));
60 }
61
62 export function updateHublet(hublet: string) {
63 return makeRequest('put', '/hublet', { hublet });
64 }
65
66 export function skipReview() {
67 return makeRequest('post', '/skip-review');
68 }
69
70 export function trackConsent(canTrack: boolean) {
71 return makeRequest('post', '/track-consent', { canTrack }).then(message => ({
72 message,
73 }));
74 }
75
76 export function setBusinessUnitId(businessUnitId: number) {
77 return makeRequest('put', '/business-unit', { businessUnitId });
78 }
79
80 export function getBusinessUnitId() {
81 return makeRequest('get', '/business-unit');
82 }
83
84 export function refreshProxyMappingsCache() {
85 return makeRequest('post', '/wp-mappings-cache-reset');
86 }
87
88 export function fetchProxyMappingsEnabled() {
89 return makeRequest('get', '/wp-mappings-proxy-enabled');
90 }
91
92 export function toggleProxyMappingsEnabled(value: boolean) {
93 return makeRequest('put', '/wp-mappings-proxy-enabled', value);
94 }
95
96 let refreshTokenRequest: Promise<any> | null = null;
97
98 export function fetchRefreshToken() {
99 if (!refreshTokenRequest) {
100 refreshTokenRequest = makeRequest('get', '/refresh-token').catch(err => {
101 refreshTokenRequest = null;
102 throw err;
103 });
104 }
105 return refreshTokenRequest;
106 }
107