Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3396613
D9042.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D9042.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D9042: [lib] Add function to send reports
Attached
Detach File
Event Timeline
Log In to Comment