@kush_hemant/react-api-monitor 1.0.27 → 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 +79 -31
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  // src/core/sender.js
2
- var LOG_ENDPOINT2 = "";
2
+ var LOG_ENDPOINT = "";
3
3
  function configureSender(url) {
4
- LOG_ENDPOINT2 = url;
4
+ LOG_ENDPOINT = url;
5
5
  }
6
- function sendLog2(data) {
6
+ function sendLog(data) {
7
7
  var _a;
8
- if (!LOG_ENDPOINT2) return;
8
+ if (!LOG_ENDPOINT) return;
9
9
  ((_a = navigator.sendBeacon) == null ? void 0 : _a.call(
10
10
  navigator,
11
- LOG_ENDPOINT2,
11
+ LOG_ENDPOINT,
12
12
  JSON.stringify(data)
13
- )) || fetch(LOG_ENDPOINT2, {
13
+ )) || fetch(LOG_ENDPOINT, {
14
14
  method: "POST",
15
15
  headers: { "Content-Type": "application/json" },
16
16
  body: JSON.stringify(data),
@@ -19,30 +19,63 @@ function sendLog2(data) {
19
19
  });
20
20
  }
21
21
 
22
+ // src/utils/metadata.js
23
+ function getMetadata() {
24
+ return {
25
+ pageUrl: location.href,
26
+ userAgent: navigator.userAgent,
27
+ language: navigator.language,
28
+ platform: navigator.platform,
29
+ screen: `${screen.width}x${screen.height}`,
30
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
31
+ };
32
+ }
33
+
22
34
  // src/network/fetchMonitor.js
35
+ var alreadyPatched = false;
23
36
  function monitorFetch() {
37
+ if (alreadyPatched) return;
38
+ alreadyPatched = true;
24
39
  const originalFetch = globalThis.fetch || window.fetch;
25
40
  const patchedFetch = async (input, init = {}) => {
26
- 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";
27
44
  if (LOG_ENDPOINT && url.startsWith(LOG_ENDPOINT)) {
28
45
  return originalFetch(input, init);
29
46
  }
30
47
  const start = Date.now();
31
- const method = init.method || "GET";
48
+ let requestBody = null;
32
49
  try {
33
- const response = await originalFetch(input, init);
34
- 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
+ );
35
61
  let responseBody = null;
36
62
  try {
37
- responseBody = await clone.text();
63
+ responseBody = await response.clone().text();
38
64
  } catch {
39
65
  }
66
+ const responseHeaders = {};
67
+ response.headers.forEach((v, k) => {
68
+ responseHeaders[k] = v;
69
+ });
40
70
  sendLog({
41
71
  type: "api_call",
42
72
  transport: "fetch",
43
73
  url,
44
74
  method,
45
75
  statusCode: response.status,
76
+ requestHeaders,
77
+ requestBody,
78
+ responseHeaders,
46
79
  responseBody,
47
80
  duration: Date.now() - start,
48
81
  ...getMetadata()
@@ -55,6 +88,7 @@ function monitorFetch() {
55
88
  url,
56
89
  method,
57
90
  error: err.message,
91
+ duration: Date.now() - start,
58
92
  ...getMetadata()
59
93
  });
60
94
  throw err;
@@ -66,18 +100,6 @@ function monitorFetch() {
66
100
  }
67
101
  }
68
102
 
69
- // src/utils/metadata.js
70
- function getMetadata2() {
71
- return {
72
- pageUrl: location.href,
73
- userAgent: navigator.userAgent,
74
- language: navigator.language,
75
- platform: navigator.platform,
76
- screen: `${screen.width}x${screen.height}`,
77
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
78
- };
79
- }
80
-
81
103
  // src/network/xhrMonitor.js
82
104
  function monitorXHR() {
83
105
  const originalOpen = XMLHttpRequest.prototype.open;
@@ -95,11 +117,11 @@ function monitorXHR() {
95
117
  };
96
118
  XMLHttpRequest.prototype.send = function(body) {
97
119
  const start = Date.now();
98
- if (this._url && LOG_ENDPOINT2 && this._url.includes(LOG_ENDPOINT2)) {
120
+ if (this._url && LOG_ENDPOINT && this._url.includes(LOG_ENDPOINT)) {
99
121
  return originalSend.call(this, body);
100
122
  }
101
123
  this.addEventListener("loadend", () => {
102
- sendLog2({
124
+ sendLog({
103
125
  type: "api_call",
104
126
  transport: "xhr",
105
127
  url: this._url,
@@ -110,13 +132,38 @@ function monitorXHR() {
110
132
  responseHeaders: this.getAllResponseHeaders(),
111
133
  responseBody: this.responseText,
112
134
  duration: Date.now() - start,
113
- ...getMetadata2()
135
+ ...getMetadata()
114
136
  });
115
137
  });
116
138
  return originalSend.call(this, body);
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;
@@ -127,14 +174,14 @@ function monitorAxios() {
127
174
  window.axios.interceptors.response.use(
128
175
  (response) => {
129
176
  var _a;
130
- sendLog2({
177
+ sendLog({
131
178
  type: "api_call",
132
179
  transport: "axios",
133
180
  url: response.config.url,
134
181
  method: (_a = response.config.method) == null ? void 0 : _a.toUpperCase(),
135
182
  statusCode: response.status,
136
183
  duration: Date.now() - response.config.__startTime,
137
- ...getMetadata2()
184
+ ...getMetadata()
138
185
  });
139
186
  return response;
140
187
  },
@@ -146,16 +193,16 @@ function monitorAxios() {
146
193
  function monitorBeacon() {
147
194
  const originalBeacon = navigator.sendBeacon;
148
195
  navigator.sendBeacon = function(url, data) {
149
- if (LOG_ENDPOINT2 && url.includes(LOG_ENDPOINT2)) {
196
+ if (LOG_ENDPOINT && url.includes(LOG_ENDPOINT)) {
150
197
  return originalBeacon.call(this, url, data);
151
198
  }
152
- sendLog2({
199
+ sendLog({
153
200
  type: "api_call",
154
201
  transport: "beacon",
155
202
  url,
156
203
  method: "POST",
157
204
  requestBody: data,
158
- ...getMetadata2()
205
+ ...getMetadata()
159
206
  });
160
207
  return originalBeacon.call(this, url, data);
161
208
  };
@@ -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.27",
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",