Create NexusDataSender class to send data to the website
This commit is contained in:
parent
49ab3a4d6e
commit
4578c6af8d
|
@ -0,0 +1,55 @@
|
|||
/* exported NexusDataSender */
|
||||
/* global NexusData, Preferences */
|
||||
'use strict'
|
||||
|
||||
class NexusDataSender {
|
||||
constructor(preferences) {
|
||||
if (!(preferences instanceof Preferences)) {
|
||||
window.console.error('[NexusDataSender] constructor: argument is not an instance of Preferences!')
|
||||
}
|
||||
this._preferences = preferences
|
||||
}
|
||||
|
||||
_formatDate(value) {
|
||||
return value instanceof Date ? value.toISOString() : null
|
||||
}
|
||||
|
||||
send(nexusData) {
|
||||
if (!(nexusData instanceof NexusData)) {
|
||||
window.console.error('[NexusDataSender] send: argument is not an instance of NexusData!')
|
||||
}
|
||||
|
||||
if (false === this._preferences.isConfigured()) {
|
||||
window.console.error('[NexusDataSender] send: preferences are not configured!')
|
||||
return
|
||||
}
|
||||
|
||||
const jsonData = {
|
||||
sessionId: nexusData.sessionId,
|
||||
requestId: nexusData.requestId,
|
||||
previousRequestId: nexusData.previousRequestId,
|
||||
requestStartedAt: this._formatDate(nexusData.requestStartedAt),
|
||||
responseCompletedAt: this._formatDate(nexusData.responseCompletedAt),
|
||||
method: nexusData.method,
|
||||
url: nexusData.url,
|
||||
formData: nexusData.formData,
|
||||
responseBody: nexusData.responseBody,
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('userAccessToken', this._preferences.userAccessToken)
|
||||
formData.append('jsonData', JSON.stringify(jsonData, null, 2))
|
||||
|
||||
const fetchOptions = {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
mode: 'no-cors',
|
||||
}
|
||||
|
||||
window.fetch(this._preferences.trackerSubmitUrl, fetchOptions).catch(
|
||||
error => {
|
||||
window.console.error(`[NexusDataSender] Failed to send data: ${error}`)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue