| 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 |
|