@kush_hemant/react-api-monitor 1.0.20 → 1.0.22

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 +73 -78
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  // src/core/sender.js
2
- var LOG_ENDPOINT = "";
2
+ var LOG_ENDPOINT2 = "";
3
3
  function configureSender(url) {
4
- LOG_ENDPOINT = url;
4
+ LOG_ENDPOINT2 = url;
5
5
  }
6
- function sendLog(data) {
6
+ function sendLog2(data) {
7
7
  var _a;
8
- if (!LOG_ENDPOINT) return;
8
+ if (!LOG_ENDPOINT2) return;
9
9
  ((_a = navigator.sendBeacon) == null ? void 0 : _a.call(
10
10
  navigator,
11
- LOG_ENDPOINT,
11
+ LOG_ENDPOINT2,
12
12
  JSON.stringify(data)
13
- )) || fetch(LOG_ENDPOINT, {
13
+ )) || fetch(LOG_ENDPOINT2, {
14
14
  method: "POST",
15
15
  headers: { "Content-Type": "application/json" },
16
16
  body: JSON.stringify(data),
@@ -19,60 +19,22 @@ function sendLog(data) {
19
19
  });
20
20
  }
21
21
 
22
- // src/utils/metadata.js
23
- function getMetadata() {
24
- return {
25
- pageUrl: window.location.href,
26
- userAgent: navigator.userAgent,
27
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
28
- // Useful extras
29
- language: navigator.language,
30
- platform: navigator.platform,
31
- screen: `${screen.width}x${screen.height}`
32
- };
33
- }
34
-
35
22
  // src/network/fetchMonitor.js
36
- var alreadyPatched = false;
37
23
  function monitorFetch() {
38
- if (alreadyPatched) return;
39
- alreadyPatched = true;
40
24
  const originalFetch = globalThis.fetch || window.fetch;
41
- if (!originalFetch) return;
42
- const patchedFetch = async function(input, init = {}) {
43
- const request = input instanceof Request ? input : new Request(input, init);
44
- const url = request.url;
45
- const method = request.method || "GET";
25
+ const patchedFetch = async (input, init = {}) => {
26
+ const url = typeof input === "string" ? input : (input == null ? void 0 : input.url) || "";
46
27
  if (LOG_ENDPOINT && url.startsWith(LOG_ENDPOINT)) {
47
28
  return originalFetch(input, init);
48
29
  }
49
30
  const start = Date.now();
50
- const requestHeaders = {};
51
- try {
52
- request.headers.forEach((v, k) => {
53
- requestHeaders[k] = v;
54
- });
55
- } catch {
56
- }
57
- let requestBody = null;
31
+ const method = init.method || "GET";
58
32
  try {
59
- requestBody = await request.clone().text();
60
- } catch {
61
- }
62
- try {
63
- const response = await originalFetch(
64
- request
65
- );
66
- const responseHeaders = {};
67
- try {
68
- response.headers.forEach((v, k) => {
69
- responseHeaders[k] = v;
70
- });
71
- } catch {
72
- }
33
+ const response = await originalFetch(input, init);
34
+ const clone = response.clone();
73
35
  let responseBody = null;
74
36
  try {
75
- responseBody = await response.clone().text();
37
+ responseBody = await clone.text();
76
38
  } catch {
77
39
  }
78
40
  sendLog({
@@ -81,9 +43,6 @@ function monitorFetch() {
81
43
  url,
82
44
  method,
83
45
  statusCode: response.status,
84
- requestHeaders,
85
- requestBody,
86
- responseHeaders,
87
46
  responseBody,
88
47
  duration: Date.now() - start,
89
48
  ...getMetadata()
@@ -95,8 +54,7 @@ function monitorFetch() {
95
54
  transport: "fetch",
96
55
  url,
97
56
  method,
98
- error: (err == null ? void 0 : err.message) || "Fetch failed",
99
- duration: Date.now() - start,
57
+ error: err.message,
100
58
  ...getMetadata()
101
59
  });
102
60
  throw err;
@@ -108,6 +66,18 @@ function monitorFetch() {
108
66
  }
109
67
  }
110
68
 
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
+
111
81
  // src/network/xhrMonitor.js
112
82
  function monitorXHR() {
113
83
  const originalOpen = XMLHttpRequest.prototype.open;
@@ -125,11 +95,11 @@ function monitorXHR() {
125
95
  };
126
96
  XMLHttpRequest.prototype.send = function(body) {
127
97
  const start = Date.now();
128
- if (this._url && LOG_ENDPOINT && this._url.includes(LOG_ENDPOINT)) {
98
+ if (this._url && LOG_ENDPOINT2 && this._url.includes(LOG_ENDPOINT2)) {
129
99
  return originalSend.call(this, body);
130
100
  }
131
101
  this.addEventListener("loadend", () => {
132
- sendLog({
102
+ sendLog2({
133
103
  type: "api_call",
134
104
  transport: "xhr",
135
105
  url: this._url,
@@ -140,7 +110,7 @@ function monitorXHR() {
140
110
  responseHeaders: this.getAllResponseHeaders(),
141
111
  responseBody: this.responseText,
142
112
  duration: Date.now() - start,
143
- ...getMetadata()
113
+ ...getMetadata2()
144
114
  });
145
115
  });
146
116
  return originalSend.call(this, body);
@@ -150,40 +120,65 @@ function monitorXHR() {
150
120
  // src/network/resourceMonitor.js
151
121
  function monitorResources() {
152
122
  if (!window.PerformanceObserver) return;
153
- const observer = new PerformanceObserver(
154
- (list) => {
155
- list.getEntries().forEach((entry) => {
156
- if (entry.initiatorType === "fetch" || entry.initiatorType === "xmlhttprequest") {
157
- sendLog({
158
- type: "api_call",
159
- transport: "resource",
160
- url: entry.name,
161
- duration: Math.round(entry.duration),
162
- statusCode: 0,
163
- ...getMetadata()
164
- });
165
- }
123
+ const observer = new PerformanceObserver((list) => {
124
+ list.getEntries().forEach((entry) => {
125
+ if (entry.initiatorType === "fetch" || entry.initiatorType === "xmlhttprequest" || entry.initiatorType === "beacon") {
126
+ return;
127
+ }
128
+ if (LOG_ENDPOINT2 && entry.name.includes(LOG_ENDPOINT2)) {
129
+ return;
130
+ }
131
+ sendLog2({
132
+ type: "resource",
133
+ transport: "resource",
134
+ url: entry.name,
135
+ method: "GET",
136
+ duration: Math.round(entry.duration),
137
+ statusCode: 0,
138
+ ...getMetadata2()
166
139
  });
167
- }
168
- );
169
- observer.observe({
170
- entryTypes: ["resource"]
140
+ });
171
141
  });
142
+ observer.observe({ entryTypes: ["resource"] });
143
+ }
144
+
145
+ // src/network/monitorAxios.js
146
+ function monitorAxios() {
147
+ if (!window.axios) return;
148
+ window.axios.interceptors.request.use((config) => {
149
+ config.__startTime = Date.now();
150
+ return config;
151
+ });
152
+ window.axios.interceptors.response.use(
153
+ (response) => {
154
+ var _a;
155
+ sendLog2({
156
+ type: "api_call",
157
+ transport: "axios",
158
+ url: response.config.url,
159
+ method: (_a = response.config.method) == null ? void 0 : _a.toUpperCase(),
160
+ statusCode: response.status,
161
+ duration: Date.now() - response.config.__startTime,
162
+ ...getMetadata2()
163
+ });
164
+ return response;
165
+ },
166
+ (error) => Promise.reject(error)
167
+ );
172
168
  }
173
169
 
174
170
  // src/network/auto.js
175
171
  function startNetworkMonitoring() {
176
172
  monitorFetch();
177
173
  monitorXHR();
174
+ monitorAxios();
178
175
  monitorResources();
179
176
  }
180
177
 
181
178
  // src/index.js
182
179
  function initMonitoring(options) {
183
180
  configureSender(options.endpoint);
184
- startNetworkMonitoring({
185
- axios: options.axios
186
- });
181
+ startNetworkMonitoring();
187
182
  }
188
183
  export {
189
184
  initMonitoring
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kush_hemant/react-api-monitor",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "Automatic API monitoring SDK for React apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",