@kush_hemant/react-api-monitor 1.0.19 → 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 +42 -112
  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,83 +147,34 @@ function monitorXHR() {
168
147
  };
169
148
  }
170
149
 
171
- // src/network/monitorAxios.js
172
- function monitorAxios(axiosInstance) {
173
- if (!(axiosInstance == null ? void 0 : axiosInstance.interceptors)) return;
174
- axiosInstance.interceptors.request.use((config) => {
175
- config.__monitorStart = Date.now();
176
- return config;
177
- });
178
- axiosInstance.interceptors.response.use(
179
- (response) => {
180
- var _a, _b, _c, _d, _e;
181
- const url = ((_a = response.config) == null ? void 0 : _a.url) || "";
182
- if (LOG_ENDPOINT && url.includes(LOG_ENDPOINT)) {
183
- return response;
184
- }
185
- sendLog({
186
- type: "api_call",
187
- transport: "axios",
188
- url,
189
- method: (_c = (_b = response.config) == null ? void 0 : _b.method) == null ? void 0 : _c.toUpperCase(),
190
- statusCode: response.status,
191
- requestHeaders: (_d = response.config) == null ? void 0 : _d.headers,
192
- requestBody: (_e = response.config) == null ? void 0 : _e.data,
193
- responseHeaders: response.headers,
194
- responseBody: typeof response.data === "string" ? response.data : JSON.stringify(response.data),
195
- duration: Date.now() - (response.config.__monitorStart || Date.now()),
196
- ...getMetadata()
197
- });
198
- return response;
199
- },
200
- (error) => {
201
- var _a, _b;
202
- const config = error.config || {};
203
- const url = (config == null ? void 0 : config.url) || "";
204
- sendLog({
205
- type: "api_error",
206
- transport: "axios",
207
- url,
208
- method: (_a = config == null ? void 0 : config.method) == null ? void 0 : _a.toUpperCase(),
209
- statusCode: ((_b = error.response) == null ? void 0 : _b.status) || 0,
210
- duration: Date.now() - (config.__monitorStart || Date.now()),
211
- error: error.message,
212
- ...getMetadata()
213
- });
214
- return Promise.reject(error);
215
- }
216
- );
217
- }
218
-
219
150
  // src/network/resourceMonitor.js
220
151
  function monitorResources() {
221
- setInterval(() => {
222
- const entries = performance.getEntriesByType("resource");
223
- for (const e of entries) {
224
- if (e.initiatorType === "fetch" || e.initiatorType === "xmlhttprequest") {
225
- if (LOG_ENDPOINT && e.name.includes(LOG_ENDPOINT)) continue;
226
- sendLog({
227
- type: "api_call",
228
- transport: "resource",
229
- url: e.name,
230
- duration: e.duration,
231
- statusCode: 0,
232
- // not available
233
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
234
- });
235
- }
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
+ });
236
167
  }
237
- performance.clearResourceTimings();
238
- }, 3e3);
168
+ );
169
+ observer.observe({
170
+ entryTypes: ["resource"]
171
+ });
239
172
  }
240
173
 
241
174
  // src/network/auto.js
242
- function startNetworkMonitoring(options = {}) {
175
+ function startNetworkMonitoring() {
243
176
  monitorFetch();
244
177
  monitorXHR();
245
- if (options.axios) {
246
- monitorAxios(options.axios);
247
- }
248
178
  monitorResources();
249
179
  }
250
180
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kush_hemant/react-api-monitor",
3
- "version": "1.0.19",
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",