Page MenuHomePhabricator

D9042.diff
No OneTemporary

D9042.diff

diff --git a/lib/utils/reports-service.js b/lib/utils/reports-service.js
new file mode 100644
--- /dev/null
+++ b/lib/utils/reports-service.js
@@ -0,0 +1,50 @@
+// @flow
+
+import {
+ reportsServiceURL,
+ sendReportEndpoint,
+} from '../facts/reports-service.js';
+import type {
+ ReportsServiceSendReportsRequest,
+ ReportsServiceSendReportsResponse,
+} from '../types/report-types.js';
+
+async function sendReports(
+ reports: ReportsServiceSendReportsRequest,
+): Promise<ReportsServiceSendReportsResponse> {
+ const reportsArray = Array.isArray(reports) ? reports : [reports];
+
+ if (reportsArray.length === 0) {
+ return { reportIDs: [] };
+ }
+
+ const requestBody = reportsArray.map(clientReport => {
+ const { id, ...report } = clientReport;
+ return report;
+ });
+ const url = `${reportsServiceURL}${sendReportEndpoint.path}`;
+ const response = await fetch(url, {
+ method: sendReportEndpoint.method,
+ body: JSON.stringify(requestBody),
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ if (!response.ok) {
+ const { status, statusText } = response;
+ let responseText, errorMessage;
+ try {
+ responseText = await response.text();
+ } finally {
+ errorMessage = responseText || statusText || '-';
+ }
+ // we cannot throw error inside `finally` block because eslint complains
+ throw new Error(`Server responded with HTTP ${status}: ${errorMessage}`);
+ }
+
+ const { reportIDs } = await response.json();
+ return { reportIDs };
+}
+
+export { sendReports };

File Metadata

Mime Type
text/plain
Expires
Mon, Dec 2, 1:42 PM (21 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2607721
Default Alt Text
D9042.diff (1 KB)

Event Timeline