@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.
- package/dist/index.js +42 -112
- 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,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
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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
|
-
|
|
238
|
-
|
|
168
|
+
);
|
|
169
|
+
observer.observe({
|
|
170
|
+
entryTypes: ["resource"]
|
|
171
|
+
});
|
|
239
172
|
}
|
|
240
173
|
|
|
241
174
|
// src/network/auto.js
|
|
242
|
-
function startNetworkMonitoring(
|
|
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
|
|