@kush_hemant/react-api-monitor 1.0.46 → 1.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +42 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -12,8 +12,17 @@ function sendLog(data) {
12
12
  const payload = JSON.stringify({
13
13
  ...data,
14
14
  sessionId: SESSION_ID
15
- // ⭐ KEY CHANGE
16
15
  });
16
+ if (navigator.sendBeacon) {
17
+ const blob = new Blob([payload], {
18
+ type: "application/json"
19
+ });
20
+ const ok = navigator.sendBeacon(
21
+ LOG_ENDPOINT,
22
+ blob
23
+ );
24
+ if (ok) return;
25
+ }
17
26
  fetch(LOG_ENDPOINT, {
18
27
  method: "POST",
19
28
  headers: {
@@ -56,15 +65,24 @@ function monitorFetch() {
56
65
  }
57
66
  const requestHeaders = {};
58
67
  request.headers.forEach((v, k) => requestHeaders[k] = v);
59
- const response = await originalFetch.apply(this, args);
60
- const responseClone = response.clone();
61
- let responseBody = null;
68
+ let response;
62
69
  try {
63
- responseBody = await responseClone.text();
64
- } catch {
70
+ response = await originalFetch.apply(this, args);
71
+ } catch (err) {
72
+ sendLog({
73
+ type: "api_call",
74
+ transport: "fetch",
75
+ url,
76
+ method,
77
+ statusCode: 0,
78
+ requestHeaders,
79
+ requestBody,
80
+ error: err.message,
81
+ duration: Date.now() - start,
82
+ ...getMetadata()
83
+ });
84
+ throw err;
65
85
  }
66
- const responseHeaders = {};
67
- response.headers.forEach((v, k) => responseHeaders[k] = v);
68
86
  sendLog({
69
87
  type: "api_call",
70
88
  transport: "fetch",
@@ -73,11 +91,25 @@ function monitorFetch() {
73
91
  statusCode: response.status,
74
92
  requestHeaders,
75
93
  requestBody,
76
- responseHeaders,
77
- responseBody,
78
94
  duration: Date.now() - start,
79
95
  ...getMetadata()
80
96
  });
97
+ try {
98
+ const responseClone = response.clone();
99
+ responseClone.text().then((responseBody) => {
100
+ sendLog({
101
+ type: "api_call_response",
102
+ transport: "fetch",
103
+ url,
104
+ method,
105
+ statusCode: response.status,
106
+ responseBody,
107
+ ...getMetadata()
108
+ });
109
+ }).catch(() => {
110
+ });
111
+ } catch {
112
+ }
81
113
  return response;
82
114
  };
83
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kush_hemant/react-api-monitor",
3
- "version": "1.0.46",
3
+ "version": "1.0.48",
4
4
  "description": "Automatic API monitoring SDK for React apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",