| 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 |
const ACCESS_TOKEN_CACHE_KEY = 'leadin_access_token'; |
| 97 |
const ACCESS_TOKEN_MIN_TTL_SECONDS = 300; |
| 98 |
|
| 99 |
let accessTokenRequest: Promise<any> | null = null; |
| 100 |
|
| 101 |
export function fetchAccessToken() { |
| 102 |
try { |
| 103 |
const cached = sessionStorage.getItem(ACCESS_TOKEN_CACHE_KEY); |
| 104 |
if (cached) { |
| 105 |
const { accessToken, expiresAt } = JSON.parse(cached); |
| 106 |
if ( |
| 107 |
accessToken && |
| 108 |
expiresAt > Math.floor(Date.now() / 1000) + ACCESS_TOKEN_MIN_TTL_SECONDS |
| 109 |
) { |
| 110 |
return Promise.resolve({ |
| 111 |
accessToken, |
| 112 |
expiresIn: expiresAt - Math.floor(Date.now() / 1000), |
| 113 |
}); |
| 114 |
} |
| 115 |
} |
| 116 |
} catch (_) {} |
| 117 |
|
| 118 |
if (!accessTokenRequest) { |
| 119 |
accessTokenRequest = makeRequest('get', '/access-token') |
| 120 |
.then((response: { accessToken: string; expiresIn: number }) => { |
| 121 |
try { |
| 122 |
sessionStorage.setItem( |
| 123 |
ACCESS_TOKEN_CACHE_KEY, |
| 124 |
JSON.stringify({ |
| 125 |
accessToken: response.accessToken, |
| 126 |
expiresAt: Math.floor(Date.now() / 1000) + response.expiresIn, |
| 127 |
}) |
| 128 |
); |
| 129 |
} catch (_) {} |
| 130 |
return response; |
| 131 |
}) |
| 132 |
.finally(() => { |
| 133 |
accessTokenRequest = null; |
| 134 |
}); |
| 135 |
} |
| 136 |
return accessTokenRequest; |
| 137 |
} |
| 138 |
|