@kush_hemant/react-api-monitor 1.0.18 → 1.0.20

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 +44 -108
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -33,60 +33,35 @@ function getMetadata() {
33
33
  }
34
34
 
35
35
  // src/network/fetchMonitor.js
36
- var isPatched = false;
36
+ var alreadyPatched = false;
37
37
  function monitorFetch() {
38
- if (isPatched) return;
39
- isPatched = true;
40
- const originalFetch = window.fetch;
41
- window.fetch = async function(...args) {
42
- const input = args[0];
43
- const init = args[1] || {};
44
- const url = typeof input === "string" ? input : (input == null ? void 0 : input.url) || "";
45
- const method = init.method || input instanceof Request && input.method || "GET";
46
- if (LOG_ENDPOINT && url.includes(LOG_ENDPOINT)) {
47
- return originalFetch.apply(this, args);
38
+ if (alreadyPatched) return;
39
+ alreadyPatched = true;
40
+ 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";
46
+ if (LOG_ENDPOINT && url.startsWith(LOG_ENDPOINT)) {
47
+ return originalFetch(input, init);
48
48
  }
49
49
  const start = Date.now();
50
- let requestHeaders = {};
50
+ const requestHeaders = {};
51
51
  try {
52
- const headers = init.headers || input instanceof Request && input.headers;
53
- if (headers instanceof Headers) {
54
- headers.forEach((v, k) => {
55
- requestHeaders[k] = v;
56
- });
57
- } else if (headers) {
58
- requestHeaders = { ...headers };
59
- }
52
+ request.headers.forEach((v, k) => {
53
+ requestHeaders[k] = v;
54
+ });
60
55
  } catch {
61
56
  }
62
57
  let requestBody = null;
63
58
  try {
64
- if (init.body) {
65
- requestBody = typeof init.body === "string" ? init.body : JSON.stringify(init.body);
66
- }
59
+ requestBody = await request.clone().text();
67
60
  } catch {
68
61
  }
69
- const signal = init.signal || input instanceof Request && input.signal;
70
- if (signal) {
71
- signal.addEventListener(
72
- "abort",
73
- () => {
74
- sendLog({
75
- type: "api_abort",
76
- transport: "fetch",
77
- url,
78
- method,
79
- duration: Date.now() - start,
80
- ...getMetadata()
81
- });
82
- },
83
- { once: true }
84
- );
85
- }
86
62
  try {
87
- const response = await originalFetch.apply(
88
- this,
89
- args
63
+ const response = await originalFetch(
64
+ request
90
65
  );
91
66
  const responseHeaders = {};
92
67
  try {
@@ -127,6 +102,10 @@ function monitorFetch() {
127
102
  throw err;
128
103
  }
129
104
  };
105
+ globalThis.fetch = patchedFetch;
106
+ if (typeof window !== "undefined") {
107
+ window.fetch = patchedFetch;
108
+ }
130
109
  }
131
110
 
132
111
  // src/network/xhrMonitor.js
@@ -168,86 +147,43 @@ function monitorXHR() {
168
147
  };
169
148
  }
170
149
 
171
- // src/network/monitorAxios.js
172
- function monitorAxios() {
173
- if (!window.axios) return;
174
- window.axios.interceptors.request.use(
175
- (config) => {
176
- config.__startTime = Date.now();
177
- return config;
178
- }
179
- );
180
- window.axios.interceptors.response.use(
181
- (response) => {
182
- const url = response.config.url || "";
183
- if (LOG_ENDPOINT && url.startsWith(LOG_ENDPOINT)) {
184
- return response;
185
- }
186
- sendLog({
187
- type: "api_call",
188
- transport: "axios",
189
- url,
190
- method: response.config.method,
191
- statusCode: response.status,
192
- requestHeaders: response.config.headers,
193
- requestBody: response.config.data,
194
- responseHeaders: response.headers,
195
- responseBody: response.data,
196
- duration: Date.now() - response.config.__startTime,
197
- ...getMetadata()
198
- });
199
- return response;
200
- },
201
- (error) => {
202
- const config = error.config || {};
203
- sendLog({
204
- type: "api_error",
205
- transport: "axios",
206
- url: config.url,
207
- method: config.method,
208
- error: error.message,
209
- duration: Date.now() - (config.__startTime || 0),
210
- ...getMetadata()
211
- });
212
- return Promise.reject(error);
213
- }
214
- );
215
- }
216
-
217
150
  // src/network/resourceMonitor.js
218
151
  function monitorResources() {
219
- setInterval(() => {
220
- const entries = performance.getEntriesByType("resource");
221
- for (const e of entries) {
222
- if (e.initiatorType === "fetch" || e.initiatorType === "xmlhttprequest") {
223
- if (LOG_ENDPOINT && e.name.includes(LOG_ENDPOINT)) continue;
224
- sendLog({
225
- type: "api_call",
226
- transport: "resource",
227
- url: e.name,
228
- duration: e.duration,
229
- statusCode: 0,
230
- // not available
231
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
232
- });
233
- }
152
+ 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
+ }
166
+ });
234
167
  }
235
- performance.clearResourceTimings();
236
- }, 3e3);
168
+ );
169
+ observer.observe({
170
+ entryTypes: ["resource"]
171
+ });
237
172
  }
238
173
 
239
174
  // src/network/auto.js
240
175
  function startNetworkMonitoring() {
241
176
  monitorFetch();
242
177
  monitorXHR();
243
- monitorAxios();
244
178
  monitorResources();
245
179
  }
246
180
 
247
181
  // src/index.js
248
182
  function initMonitoring(options) {
249
183
  configureSender(options.endpoint);
250
- startNetworkMonitoring();
184
+ startNetworkMonitoring({
185
+ axios: options.axios
186
+ });
251
187
  }
252
188
  export {
253
189
  initMonitoring
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kush_hemant/react-api-monitor",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "Automatic API monitoring SDK for React apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",