@kush_hemant/react-api-monitor 1.0.16 → 1.0.18

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 +54 -27
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -33,42 +33,73 @@ function getMetadata() {
33
33
  }
34
34
 
35
35
  // src/network/fetchMonitor.js
36
- var alreadyPatched = false;
36
+ var isPatched = false;
37
37
  function monitorFetch() {
38
- if (alreadyPatched) return;
39
- alreadyPatched = true;
40
- const originalFetch = globalThis.fetch || window.fetch;
41
- const patchedFetch = async (input, init = {}) => {
42
- const request = input instanceof Request ? input : new Request(input, init);
43
- const url = request.url;
44
- console.log("SDK FETCH INTERCEPTED:", url);
45
- const method = request.method || "GET";
46
- if (LOG_ENDPOINT && url.startsWith(LOG_ENDPOINT)) {
47
- return originalFetch(input, init);
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);
48
48
  }
49
49
  const start = Date.now();
50
+ let requestHeaders = {};
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
+ }
60
+ } catch {
61
+ }
50
62
  let requestBody = null;
51
63
  try {
52
- requestBody = await request.clone().text();
64
+ if (init.body) {
65
+ requestBody = typeof init.body === "string" ? init.body : JSON.stringify(init.body);
66
+ }
53
67
  } catch {
54
68
  }
55
- const requestHeaders = {};
56
- request.headers.forEach((v, k) => {
57
- requestHeaders[k] = v;
58
- });
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
+ }
59
86
  try {
60
- const response = await originalFetch(
61
- request
87
+ const response = await originalFetch.apply(
88
+ this,
89
+ args
62
90
  );
91
+ const responseHeaders = {};
92
+ try {
93
+ response.headers.forEach((v, k) => {
94
+ responseHeaders[k] = v;
95
+ });
96
+ } catch {
97
+ }
63
98
  let responseBody = null;
64
99
  try {
65
100
  responseBody = await response.clone().text();
66
101
  } catch {
67
102
  }
68
- const responseHeaders = {};
69
- response.headers.forEach((v, k) => {
70
- responseHeaders[k] = v;
71
- });
72
103
  sendLog({
73
104
  type: "api_call",
74
105
  transport: "fetch",
@@ -89,17 +120,13 @@ function monitorFetch() {
89
120
  transport: "fetch",
90
121
  url,
91
122
  method,
92
- error: err.message,
123
+ error: (err == null ? void 0 : err.message) || "Fetch failed",
93
124
  duration: Date.now() - start,
94
125
  ...getMetadata()
95
126
  });
96
127
  throw err;
97
128
  }
98
129
  };
99
- globalThis.fetch = patchedFetch;
100
- if (typeof window !== "undefined") {
101
- window.fetch = patchedFetch;
102
- }
103
130
  }
104
131
 
105
132
  // src/network/xhrMonitor.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kush_hemant/react-api-monitor",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Automatic API monitoring SDK for React apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",