node-avo-inspector 1.1.0 → 1.1.1

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.
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.AvoInspector = void 0;
4
13
  const AvoInspectorEnv_1 = require("./AvoInspectorEnv");
@@ -102,34 +111,35 @@ class AvoInspector {
102
111
  }
103
112
  }
104
113
  trackSchemaInternal(eventName, eventSchema, eventId, eventHash) {
105
- try {
106
- const seesionId = AvoGuid_1.AvoGuid.newGuid();
107
- return this.avoNetworkCallsHandler
108
- .callInspectorWithBatchBody([
109
- this.avoNetworkCallsHandler.bodyForSessionStartedCall(seesionId),
110
- ])
111
- .then(() => {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ try {
116
+ const seesionId = AvoGuid_1.AvoGuid.newGuid();
117
+ yield this.avoNetworkCallsHandler
118
+ .callInspectorWithBatchBody([
119
+ this.avoNetworkCallsHandler.bodyForSessionStartedCall(seesionId),
120
+ ]);
112
121
  if (AvoInspector.shouldLog) {
113
122
  console.log("Avo Inspector: session started sent successfully.");
114
123
  }
115
- this.avoNetworkCallsHandler.callInspectorWithBatchBody([
116
- this.avoNetworkCallsHandler.bodyForEventSchemaCall(seesionId, eventName, eventSchema, eventId, eventHash),
117
- ]).then(() => {
124
+ try {
125
+ yield this.avoNetworkCallsHandler.callInspectorWithBatchBody([
126
+ this.avoNetworkCallsHandler.bodyForEventSchemaCall(seesionId, eventName, eventSchema, eventId, eventHash)
127
+ ]);
118
128
  if (AvoInspector.shouldLog) {
119
129
  console.log("Avo Inspector: schema sent successfully.");
120
130
  }
121
- });
122
- })
123
- .catch((err) => {
124
- if (AvoInspector.shouldLog) {
125
- console.log("Avo Inspector: schema sending failed: " + err + ".");
126
131
  }
127
- });
128
- }
129
- catch (e) {
130
- console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e);
131
- return Promise.reject();
132
- }
132
+ catch (err) {
133
+ if (AvoInspector.shouldLog) {
134
+ console.log("Avo Inspector: schema sending failed: " + err + ".");
135
+ }
136
+ }
137
+ }
138
+ catch (e) {
139
+ console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e);
140
+ return Promise.reject();
141
+ }
142
+ });
133
143
  }
134
144
  enableLogging(enable) {
135
145
  AvoInspector._shouldLog = enable;
@@ -13,6 +13,9 @@ const AvoDeduplicator_1 = require("../AvoDeduplicator");
13
13
  const utils_1 = require("../utils");
14
14
  const AvoInspector_1 = require("../AvoInspector");
15
15
  const constants_1 = require("./constants");
16
+ jest
17
+ .useFakeTimers()
18
+ .setSystemTime(new Date('2020-01-01'));
16
19
  describe("Deduplicator", () => {
17
20
  const deduplicator = new AvoDeduplicator_1.AvoDeduplicator();
18
21
  const testObject = {
@@ -14,17 +14,29 @@ const AvoNetworkCallsHandler_1 = require("../AvoNetworkCallsHandler");
14
14
  const constants_1 = require("./constants");
15
15
  describe("AvoNetworkCallsHandler", () => {
16
16
  let inspectorCallSpy;
17
+ let consoleLogSpy;
17
18
  let inspector;
18
19
  const { apiKey, env, version, appName } = constants_1.defaultOptions;
20
+ const eventName = "event name";
21
+ const properties = {
22
+ prop0: "",
23
+ prop2: false,
24
+ prop3: 0,
25
+ prop4: 0.0,
26
+ };
27
+ const happyPathInspectorCall = () => {
28
+ inspector.avoNetworkCallsHandler["samplingRate"] = 0.1;
29
+ return Promise.resolve();
30
+ };
19
31
  beforeAll(() => {
20
32
  inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
21
- inspector.enableLogging(false);
33
+ inspector.enableLogging(true);
22
34
  inspectorCallSpy = jest
23
35
  .spyOn(AvoNetworkCallsHandler_1.AvoNetworkCallsHandler.prototype, "callInspectorWithBatchBody");
24
- inspectorCallSpy.mockImplementation(() => {
25
- inspector.avoNetworkCallsHandler["samplingRate"] = 0.1;
26
- return Promise.resolve();
27
- });
36
+ consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => { });
37
+ });
38
+ beforeEach(() => {
39
+ inspectorCallSpy.mockImplementation(happyPathInspectorCall);
28
40
  });
29
41
  afterEach(() => {
30
42
  jest.clearAllMocks();
@@ -32,17 +44,9 @@ describe("AvoNetworkCallsHandler", () => {
32
44
  inspector.avoDeduplicator._clearEvents();
33
45
  });
34
46
  test("handleTrackSchema is called on trackSchemaFromEvent", () => __awaiter(void 0, void 0, void 0, function* () {
35
- const eventName = "event name";
36
- const properties = {
37
- prop0: "",
38
- prop2: false,
39
- prop3: 0,
40
- prop4: 0.0,
41
- };
42
- const schema = inspector.extractSchema(properties);
43
47
  yield inspector.trackSchemaFromEvent(eventName, properties);
44
48
  expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
45
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toBeCalledWith([
49
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
46
50
  expect.objectContaining({
47
51
  type: "sessionStarted",
48
52
  apiKey: apiKey,
@@ -76,16 +80,8 @@ describe("AvoNetworkCallsHandler", () => {
76
80
  ]);
77
81
  }));
78
82
  test("handleTrackSchema is called on _avoFunctionTrackSchemaFromEvent", () => __awaiter(void 0, void 0, void 0, function* () {
79
- const eventName = "event name";
80
- const properties = {
81
- prop0: "",
82
- prop2: false,
83
- prop3: 0,
84
- prop4: 0.0,
85
- };
86
83
  const eventId = "testId";
87
84
  const eventHash = "testHash";
88
- const schema = inspector.extractSchema(properties);
89
85
  // @ts-ignore
90
86
  yield inspector._avoFunctionTrackSchemaFromEvent(eventName, properties, eventId, eventHash);
91
87
  expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
@@ -122,4 +118,10 @@ describe("AvoNetworkCallsHandler", () => {
122
118
  }),
123
119
  ]);
124
120
  }));
121
+ test("an error is logged if event schema call fails and AvoInspector.shouldLog is true", () => __awaiter(void 0, void 0, void 0, function* () {
122
+ inspectorCallSpy.mockImplementationOnce(happyPathInspectorCall);
123
+ inspectorCallSpy.mockRejectedValueOnce('Network error');
124
+ yield inspector.trackSchemaFromEvent(eventName, properties);
125
+ expect(consoleLogSpy).toHaveBeenCalledWith('Avo Inspector: schema sending failed: Network error.');
126
+ }));
125
127
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-avo-inspector",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Avo Inspector for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "repository": "https://github.com/avohq/node-avo-inspector",
@@ -182,7 +182,7 @@ export class AvoInspector {
182
182
  }
183
183
  }
184
184
 
185
- private trackSchemaInternal(
185
+ private async trackSchemaInternal(
186
186
  eventName: string,
187
187
  eventSchema: Array<{
188
188
  propertyName: string;
@@ -194,33 +194,31 @@ export class AvoInspector {
194
194
  ): Promise<void> {
195
195
  try {
196
196
  const seesionId = AvoGuid.newGuid();
197
- return this.avoNetworkCallsHandler
197
+ await this.avoNetworkCallsHandler
198
198
  .callInspectorWithBatchBody([
199
199
  this.avoNetworkCallsHandler.bodyForSessionStartedCall(seesionId),
200
- ])
201
- .then(() => {
202
- if (AvoInspector.shouldLog) {
203
- console.log("Avo Inspector: session started sent successfully.");
204
- }
205
- this.avoNetworkCallsHandler.callInspectorWithBatchBody([
206
- this.avoNetworkCallsHandler.bodyForEventSchemaCall(
207
- seesionId,
208
- eventName,
209
- eventSchema,
210
- eventId,
211
- eventHash
212
- ),
213
- ]).then(() => {
214
- if (AvoInspector.shouldLog) {
215
- console.log("Avo Inspector: schema sent successfully.");
216
- }
217
- });
218
- })
219
- .catch((err) => {
220
- if (AvoInspector.shouldLog) {
221
- console.log("Avo Inspector: schema sending failed: " + err + ".");
222
- }
223
- });
200
+ ]);
201
+
202
+ if (AvoInspector.shouldLog) {
203
+ console.log("Avo Inspector: session started sent successfully.");
204
+ }
205
+ try {
206
+ await this.avoNetworkCallsHandler.callInspectorWithBatchBody([
207
+ this.avoNetworkCallsHandler.bodyForEventSchemaCall(
208
+ seesionId,
209
+ eventName,
210
+ eventSchema,
211
+ eventId,
212
+ eventHash
213
+ )]);
214
+ if (AvoInspector.shouldLog) {
215
+ console.log("Avo Inspector: schema sent successfully.");
216
+ }
217
+ } catch (err) {
218
+ if (AvoInspector.shouldLog) {
219
+ console.log("Avo Inspector: schema sending failed: " + err + ".");
220
+ }
221
+ }
224
222
  } catch (e) {
225
223
  console.error(
226
224
  "Avo Inspector: something went wrong. Please report to support@avo.app.",
@@ -3,6 +3,10 @@ import { deepEquals } from "../utils";
3
3
  import { AvoInspector } from "../AvoInspector";
4
4
  import { defaultOptions } from "./constants";
5
5
 
6
+ jest
7
+ .useFakeTimers()
8
+ .setSystemTime(new Date('2020-01-01'));
9
+
6
10
  describe("Deduplicator", () => {
7
11
  const deduplicator = new AvoDeduplicator();
8
12
 
@@ -5,24 +5,39 @@ import { defaultOptions } from "./constants";
5
5
 
6
6
  describe("AvoNetworkCallsHandler", () => {
7
7
  let inspectorCallSpy: jest.SpyInstance<any, unknown[]>;
8
+ let consoleLogSpy: jest.SpyInstance<any, unknown[]>;
8
9
 
9
10
  let inspector: AvoInspector;
10
11
 
11
12
  const { apiKey, env, version, appName } = defaultOptions;
12
13
 
14
+ const eventName = "event name";
15
+ const properties = {
16
+ prop0: "",
17
+ prop2: false,
18
+ prop3: 0,
19
+ prop4: 0.0,
20
+ };
21
+
22
+ const happyPathInspectorCall = () => {
23
+ inspector.avoNetworkCallsHandler["samplingRate"] = 0.1;
24
+ return Promise.resolve();
25
+ };
26
+
13
27
  beforeAll(() => {
14
28
  inspector = new AvoInspector(defaultOptions);
15
- inspector.enableLogging(false);
29
+ inspector.enableLogging(true);
16
30
 
17
31
  inspectorCallSpy = jest
18
32
  .spyOn(AvoNetworkCallsHandler.prototype as any, "callInspectorWithBatchBody");
19
33
 
20
- inspectorCallSpy.mockImplementation(() => {
21
- inspector.avoNetworkCallsHandler["samplingRate"] = 0.1;
22
- return Promise.resolve();
23
- });
34
+ consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => {});
24
35
  });
25
36
 
37
+ beforeEach(() => {
38
+ inspectorCallSpy.mockImplementation(happyPathInspectorCall);
39
+ })
40
+
26
41
  afterEach(() => {
27
42
  jest.clearAllMocks();
28
43
  // @ts-ignore
@@ -30,20 +45,10 @@ describe("AvoNetworkCallsHandler", () => {
30
45
  });
31
46
 
32
47
  test("handleTrackSchema is called on trackSchemaFromEvent", async () => {
33
- const eventName = "event name";
34
- const properties = {
35
- prop0: "",
36
- prop2: false,
37
- prop3: 0,
38
- prop4: 0.0,
39
- };
40
-
41
- const schema = inspector.extractSchema(properties);
42
-
43
48
  await inspector.trackSchemaFromEvent(eventName, properties);
44
49
 
45
50
  expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
46
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toBeCalledWith([
51
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
47
52
  expect.objectContaining({
48
53
  type: "sessionStarted",
49
54
  apiKey: apiKey,
@@ -79,18 +84,9 @@ describe("AvoNetworkCallsHandler", () => {
79
84
  });
80
85
 
81
86
  test("handleTrackSchema is called on _avoFunctionTrackSchemaFromEvent", async () => {
82
- const eventName = "event name";
83
- const properties = {
84
- prop0: "",
85
- prop2: false,
86
- prop3: 0,
87
- prop4: 0.0,
88
- };
89
87
  const eventId = "testId";
90
88
  const eventHash = "testHash";
91
89
 
92
- const schema = inspector.extractSchema(properties);
93
-
94
90
  // @ts-ignore
95
91
  await inspector._avoFunctionTrackSchemaFromEvent(
96
92
  eventName,
@@ -134,4 +130,13 @@ describe("AvoNetworkCallsHandler", () => {
134
130
  }),
135
131
  ]);
136
132
  });
133
+
134
+ test("an error is logged if event schema call fails and AvoInspector.shouldLog is true", async () => {
135
+ inspectorCallSpy.mockImplementationOnce(happyPathInspectorCall);
136
+ inspectorCallSpy.mockRejectedValueOnce('Network error');
137
+
138
+ await inspector.trackSchemaFromEvent(eventName, properties);
139
+
140
+ expect(consoleLogSpy).toHaveBeenCalledWith('Avo Inspector: schema sending failed: Network error.');
141
+ });
137
142
  });