@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.
- package/dist/index.js +44 -108
- 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
|
|
36
|
+
var alreadyPatched = false;
|
|
37
37
|
function monitorFetch() {
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
const originalFetch = window.fetch;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
const url =
|
|
45
|
-
const method =
|
|
46
|
-
if (LOG_ENDPOINT && url.
|
|
47
|
-
return originalFetch
|
|
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
|
-
|
|
50
|
+
const requestHeaders = {};
|
|
51
51
|
try {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
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
|
|
88
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
|
|
236
|
-
|
|
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
|