Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/reports-service.js
// @flow | // @flow | ||||
import { fetchWithTimeout } from './fetch-utils.js'; | import { fetchWithTimeout } from './fetch-utils.js'; | ||||
import { | import { | ||||
reportsServiceURL, | reportsServiceURL, | ||||
sendReportEndpoint, | sendReportEndpoint, | ||||
} from '../facts/reports-service.js'; | } from '../facts/reports-service.js'; | ||||
import type { | import type { | ||||
ReportsServiceSendReportsRequest, | ReportsServiceSendReportsRequest, | ||||
ReportsServiceSendReportsResponse, | ReportsServiceSendReportsResponse, | ||||
ReportCreationRequest, | |||||
ClientReportCreationRequest, | |||||
} from '../types/report-types.js'; | } from '../types/report-types.js'; | ||||
const REQUEST_TIMEOUT = 60 * 1000; // 60s | const REQUEST_TIMEOUT = 60 * 1000; // 60s | ||||
async function sendReports( | async function sendReports( | ||||
reports: ReportsServiceSendReportsRequest, | reports: ReportsServiceSendReportsRequest, | ||||
): Promise<ReportsServiceSendReportsResponse> { | ): Promise<ReportsServiceSendReportsResponse> { | ||||
const reportsArray = Array.isArray(reports) ? reports : [reports]; | const reportsArray: $ReadOnlyArray<ClientReportCreationRequest> = | ||||
Array.isArray(reports) ? reports : [reports]; | |||||
if (reportsArray.length === 0) { | if (reportsArray.length === 0) { | ||||
return { reportIDs: [] }; | return { reportIDs: [] }; | ||||
} | } | ||||
const requestBody = reportsArray.map(clientReport => { | const requestBody: $ReadOnlyArray<ReportCreationRequest> = reportsArray.map( | ||||
clientReport => { | |||||
const { id, ...report } = clientReport; | const { id, ...report } = clientReport; | ||||
return report; | return report; | ||||
}); | }, | ||||
); | |||||
const url = `${reportsServiceURL}${sendReportEndpoint.path}`; | const url = `${reportsServiceURL}${sendReportEndpoint.path}`; | ||||
const response = await fetchWithTimeout(url, { | const response = await fetchWithTimeout(url, { | ||||
method: sendReportEndpoint.method, | method: sendReportEndpoint.method, | ||||
body: JSON.stringify(requestBody), | body: JSON.stringify(requestBody), | ||||
headers: { | headers: { | ||||
'Content-Type': 'application/json', | 'Content-Type': 'application/json', | ||||
}, | }, | ||||
timeout: REQUEST_TIMEOUT, | timeout: REQUEST_TIMEOUT, | ||||
Show All 19 Lines |