@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.
Files changed (2) hide show
  1. package/dist/index.js +40 -54
  2. 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
- function configureSender(url) {
4
- LOG_ENDPOINT = url;
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
- ((_a = navigator.sendBeacon) == null ? void 0 : _a.call(
10
- navigator,
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: { "Content-Type": "application/json" },
16
- body: JSON.stringify(data),
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(input, init = {}) {
38
+ window.fetch = async function(...args) {
38
39
  const start = Date.now();
39
- const request = input instanceof Request ? input : new Request(input, init);
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, arguments);
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
- requestHeaders[k] = v;
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
- const response = await originalFetch.apply(this, arguments);
56
- let responseBody = null;
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(options.endpoint);
172
+ configureSender({
173
+ endpoint: options.endpoint,
174
+ apiKey: options.apiKey
175
+ });
190
176
  startNetworkMonitoring();
191
177
  }
192
178
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kush_hemant/react-api-monitor",
3
- "version": "1.0.32",
3
+ "version": "1.0.34",
4
4
  "description": "Automatic API monitoring SDK for React apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",