@kush_hemant/react-api-monitor 1.0.28 → 1.0.30

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 +53 -5
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -32,29 +32,50 @@ function getMetadata() {
32
32
  }
33
33
 
34
34
  // src/network/fetchMonitor.js
35
+ var alreadyPatched = false;
35
36
  function monitorFetch() {
37
+ if (alreadyPatched) return;
38
+ alreadyPatched = true;
36
39
  const originalFetch = globalThis.fetch || window.fetch;
37
40
  const patchedFetch = async (input, init = {}) => {
38
- const url = typeof input === "string" ? input : (input == null ? void 0 : input.url) || "";
41
+ const request = input instanceof Request ? input : new Request(input, init);
42
+ const url = request.url;
43
+ const method = request.method || "GET";
39
44
  if (LOG_ENDPOINT && url.startsWith(LOG_ENDPOINT)) {
40
45
  return originalFetch(input, init);
41
46
  }
42
47
  const start = Date.now();
43
- const method = init.method || "GET";
48
+ let requestBody = null;
44
49
  try {
45
- const response = await originalFetch(input, init);
46
- const clone = response.clone();
50
+ requestBody = await request.clone().text();
51
+ } catch {
52
+ }
53
+ const requestHeaders = {};
54
+ request.headers.forEach((v, k) => {
55
+ requestHeaders[k] = v;
56
+ });
57
+ try {
58
+ const response = await originalFetch(
59
+ request
60
+ );
47
61
  let responseBody = null;
48
62
  try {
49
- responseBody = await clone.text();
63
+ responseBody = await response.clone().text();
50
64
  } catch {
51
65
  }
66
+ const responseHeaders = {};
67
+ response.headers.forEach((v, k) => {
68
+ responseHeaders[k] = v;
69
+ });
52
70
  sendLog({
53
71
  type: "api_call",
54
72
  transport: "fetch",
55
73
  url,
56
74
  method,
57
75
  statusCode: response.status,
76
+ requestHeaders,
77
+ requestBody,
78
+ responseHeaders,
58
79
  responseBody,
59
80
  duration: Date.now() - start,
60
81
  ...getMetadata()
@@ -67,6 +88,7 @@ function monitorFetch() {
67
88
  url,
68
89
  method,
69
90
  error: err.message,
91
+ duration: Date.now() - start,
70
92
  ...getMetadata()
71
93
  });
72
94
  throw err;
@@ -117,6 +139,31 @@ function monitorXHR() {
117
139
  };
118
140
  }
119
141
 
142
+ // src/network/resourceMonitor.js
143
+ function monitorResources() {
144
+ if (!window.PerformanceObserver) return;
145
+ const observer = new PerformanceObserver((list) => {
146
+ list.getEntries().forEach((entry) => {
147
+ if (entry.initiatorType === "fetch" || entry.initiatorType === "xmlhttprequest" || entry.initiatorType === "beacon") {
148
+ return;
149
+ }
150
+ if (LOG_ENDPOINT && entry.name.includes(LOG_ENDPOINT)) {
151
+ return;
152
+ }
153
+ sendLog({
154
+ type: "resource",
155
+ transport: "resource",
156
+ url: entry.name,
157
+ method: "GET",
158
+ duration: Math.round(entry.duration),
159
+ statusCode: 0,
160
+ ...getMetadata()
161
+ });
162
+ });
163
+ });
164
+ observer.observe({ entryTypes: ["resource"] });
165
+ }
166
+
120
167
  // src/network/monitorAxios.js
121
168
  function monitorAxios() {
122
169
  if (!window.axios) return;
@@ -167,6 +214,7 @@ function startNetworkMonitoring() {
167
214
  monitorXHR();
168
215
  monitorAxios();
169
216
  monitorBeacon();
217
+ monitorResources();
170
218
  }
171
219
 
172
220
  // src/index.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kush_hemant/react-api-monitor",
3
- "version": "1.0.28",
3
+ "version": "1.0.30",
4
4
  "description": "Automatic API monitoring SDK for React apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",