@kush_hemant/react-api-monitor 1.0.32 → 1.0.34
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 +40 -54
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
// src/core/sender.js
|
|
2
2
|
var LOG_ENDPOINT = "";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
var API_KEY = "";
|
|
4
|
+
function configureSender({ endpoint, apiKey }) {
|
|
5
|
+
LOG_ENDPOINT = endpoint;
|
|
6
|
+
API_KEY = apiKey;
|
|
5
7
|
}
|
|
6
8
|
function sendLog(data) {
|
|
7
|
-
var _a;
|
|
8
9
|
if (!LOG_ENDPOINT) return;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
LOG_ENDPOINT,
|
|
12
|
-
JSON.stringify(data)
|
|
13
|
-
)) || fetch(LOG_ENDPOINT, {
|
|
10
|
+
const payload = JSON.stringify(data);
|
|
11
|
+
fetch(LOG_ENDPOINT, {
|
|
14
12
|
method: "POST",
|
|
15
|
-
headers: {
|
|
16
|
-
|
|
13
|
+
headers: {
|
|
14
|
+
"Content-Type": "application/json",
|
|
15
|
+
"x-api-key": API_KEY
|
|
16
|
+
},
|
|
17
|
+
body: payload,
|
|
17
18
|
keepalive: true
|
|
18
19
|
}).catch(() => {
|
|
19
20
|
});
|
|
@@ -34,13 +35,13 @@ function getMetadata() {
|
|
|
34
35
|
// src/network/fetchMonitor.js
|
|
35
36
|
function monitorFetch() {
|
|
36
37
|
const originalFetch = window.fetch;
|
|
37
|
-
window.fetch = async function(
|
|
38
|
+
window.fetch = async function(...args) {
|
|
38
39
|
const start = Date.now();
|
|
39
|
-
const request =
|
|
40
|
+
const request = args[0] instanceof Request ? args[0] : new Request(args[0], args[1]);
|
|
40
41
|
const url = request.url;
|
|
41
42
|
const method = request.method || "GET";
|
|
42
43
|
if (LOG_ENDPOINT && url.startsWith(LOG_ENDPOINT)) {
|
|
43
|
-
return originalFetch.apply(this,
|
|
44
|
+
return originalFetch.apply(this, args);
|
|
44
45
|
}
|
|
45
46
|
let requestBody = null;
|
|
46
47
|
try {
|
|
@@ -48,48 +49,30 @@ function monitorFetch() {
|
|
|
48
49
|
} catch {
|
|
49
50
|
}
|
|
50
51
|
const requestHeaders = {};
|
|
51
|
-
request.headers.forEach((v, k) =>
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
request.headers.forEach((v, k) => requestHeaders[k] = v);
|
|
53
|
+
const response = await originalFetch.apply(this, args);
|
|
54
|
+
const responseClone = response.clone();
|
|
55
|
+
let responseBody = null;
|
|
54
56
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
responseBody = await response.clone().text();
|
|
59
|
-
} catch {
|
|
60
|
-
}
|
|
61
|
-
const responseHeaders = {};
|
|
62
|
-
response.headers.forEach((v, k) => {
|
|
63
|
-
responseHeaders[k] = v;
|
|
64
|
-
});
|
|
65
|
-
sendLog({
|
|
66
|
-
type: "api_call",
|
|
67
|
-
transport: "fetch",
|
|
68
|
-
url,
|
|
69
|
-
method,
|
|
70
|
-
statusCode: response.status,
|
|
71
|
-
requestHeaders,
|
|
72
|
-
requestBody,
|
|
73
|
-
responseHeaders,
|
|
74
|
-
responseBody,
|
|
75
|
-
duration: Date.now() - start,
|
|
76
|
-
...getMetadata()
|
|
77
|
-
});
|
|
78
|
-
return response;
|
|
79
|
-
} catch (err) {
|
|
80
|
-
sendLog({
|
|
81
|
-
type: "api_error",
|
|
82
|
-
transport: "fetch",
|
|
83
|
-
url,
|
|
84
|
-
method,
|
|
85
|
-
requestHeaders,
|
|
86
|
-
requestBody,
|
|
87
|
-
error: err.message,
|
|
88
|
-
duration: Date.now() - start,
|
|
89
|
-
...getMetadata()
|
|
90
|
-
});
|
|
91
|
-
throw err;
|
|
57
|
+
responseBody = await responseClone.text();
|
|
58
|
+
} catch {
|
|
92
59
|
}
|
|
60
|
+
const responseHeaders = {};
|
|
61
|
+
response.headers.forEach((v, k) => responseHeaders[k] = v);
|
|
62
|
+
sendLog({
|
|
63
|
+
type: "api_call",
|
|
64
|
+
transport: "fetch",
|
|
65
|
+
url,
|
|
66
|
+
method,
|
|
67
|
+
statusCode: response.status,
|
|
68
|
+
requestHeaders,
|
|
69
|
+
requestBody,
|
|
70
|
+
responseHeaders,
|
|
71
|
+
responseBody,
|
|
72
|
+
duration: Date.now() - start,
|
|
73
|
+
...getMetadata()
|
|
74
|
+
});
|
|
75
|
+
return response;
|
|
93
76
|
};
|
|
94
77
|
}
|
|
95
78
|
|
|
@@ -186,7 +169,10 @@ function startNetworkMonitoring() {
|
|
|
186
169
|
|
|
187
170
|
// src/index.js
|
|
188
171
|
function initMonitoring(options) {
|
|
189
|
-
configureSender(
|
|
172
|
+
configureSender({
|
|
173
|
+
endpoint: options.endpoint,
|
|
174
|
+
apiKey: options.apiKey
|
|
175
|
+
});
|
|
190
176
|
startNetworkMonitoring();
|
|
191
177
|
}
|
|
192
178
|
export {
|