node-avo-inspector 1.0.1 → 1.0.3-beta.0

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/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Avo Inspector SDK for Node.js
2
2
 
3
- This is a quick start guide. For more information about the Inspector project please read [Avo Inspector SDK Reference](https://www.avo.app/docs/implementation/avo-inspector-sdk-reference) and the [Avo Inspector Setup Guide](https://www.avo.app/docs/implementation/setup-inspector-sdk).
3
+ [![npm version](https://badge.fury.io/js/node-avo-inspector.svg)](https://badge.fury.io/js/node-avo-inspector)
4
+
5
+ This is a quick start guide. For more information about the Inspector project please read the [Inspector SDK Reference](https://www.avo.app/docs/implementation/avo-inspector-sdk-reference) and the [Inspector Setup Guide](https://www.avo.app/docs/implementation/setup-inspector-sdk).
4
6
 
5
7
  # Installation
6
8
 
@@ -29,12 +31,19 @@ let inspector = new Inspector.AvoInspector({
29
31
  });
30
32
  ```
31
33
 
32
- # Sending event schemas
34
+ # Integrating with Avo Codegen
35
+
36
+ The setup is lightweight and is covered [in this guide](https://www.avo.app/docs/implementation/start-using-inspector-with-avo-functions).
37
+
38
+ Every event sent with your Codegen after this integration will automatically be sent to Inspector.
39
+
40
+ # Sending event schemas for events reported outside of Codegen
41
+
42
+ Whenever you send tracking event call the following methods:
33
43
 
34
- Whenever you send tracking event call the following method:
35
- Read more in the [Avo documentation](https://www.avo.app/docs/implementation/inspector/sdk/node)
44
+ Read more in the [Avo documentation](https://www.avo.app/docs/implementation/devs-101#inspecting-events)
36
45
 
37
- This method gets actual tracking event parameters, extracts schema automatically and sends it to the Avo Inspector backend.
46
+ This method gets actual tracking event parameters, extracts schema automatically and sends it to the Inspector backend.
38
47
  It is the easiest way to use the library, just call this method at the same place you call your analytics tools' track methods with the same parameters.
39
48
 
40
49
  ```javascript
@@ -1,9 +1,9 @@
1
1
  import { AvoInspectorEnvValueType } from "./AvoInspectorEnv";
2
- import { AvoBatcher } from "./AvoBatcher";
2
+ import { AvoNetworkCallsHandler } from "./AvoNetworkCallsHandler";
3
3
  import { AvoDeduplicator } from "./AvoDeduplicator";
4
4
  export declare class AvoInspector {
5
5
  environment: AvoInspectorEnvValueType;
6
- avoBatcher: AvoBatcher;
6
+ avoNetworkCallsHandler: AvoNetworkCallsHandler;
7
7
  avoDeduplicator: AvoDeduplicator;
8
8
  apiKey: string;
9
9
  version: string;
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AvoInspector = void 0;
4
4
  const AvoInspectorEnv_1 = require("./AvoInspectorEnv");
5
5
  const AvoSchemaParser_1 = require("./AvoSchemaParser");
6
- const AvoBatcher_1 = require("./AvoBatcher");
7
6
  const AvoNetworkCallsHandler_1 = require("./AvoNetworkCallsHandler");
8
7
  const AvoDeduplicator_1 = require("./AvoDeduplicator");
9
8
  const utils_1 = require("./utils");
9
+ const AvoGuid_1 = require("./AvoGuid");
10
10
  const libVersion = require("../package.json").version;
11
11
  class AvoInspector {
12
12
  constructor(options) {
@@ -40,8 +40,7 @@ class AvoInspector {
40
40
  else {
41
41
  AvoInspector._shouldLog = false;
42
42
  }
43
- let avoNetworkCallsHandler = new AvoNetworkCallsHandler_1.AvoNetworkCallsHandler(this.apiKey, this.environment.toString(), options.appName || "", this.version, libVersion);
44
- this.avoBatcher = new AvoBatcher_1.AvoBatcher(avoNetworkCallsHandler);
43
+ this.avoNetworkCallsHandler = new AvoNetworkCallsHandler_1.AvoNetworkCallsHandler(this.apiKey, this.environment.toString(), options.appName || "", this.version, libVersion);
45
44
  this.avoDeduplicator = new AvoDeduplicator_1.AvoDeduplicator();
46
45
  }
47
46
  static get shouldLog() {
@@ -104,7 +103,28 @@ class AvoInspector {
104
103
  }
105
104
  trackSchemaInternal(eventName, eventSchema, eventId, eventHash) {
106
105
  try {
107
- return this.avoBatcher.handleTrackSchema(eventName, eventSchema, eventId, eventHash);
106
+ const seesionId = AvoGuid_1.AvoGuid.newGuid();
107
+ return this.avoNetworkCallsHandler
108
+ .callInspectorWithBatchBody([
109
+ this.avoNetworkCallsHandler.bodyForSessionStartedCall(seesionId),
110
+ ])
111
+ .then(() => {
112
+ if (AvoInspector.shouldLog) {
113
+ console.log("Avo Inspector: session started sent successfully.");
114
+ }
115
+ this.avoNetworkCallsHandler.callInspectorWithBatchBody([
116
+ this.avoNetworkCallsHandler.bodyForEventSchemaCall(seesionId, eventName, eventSchema, eventId, eventHash),
117
+ ]).then(() => {
118
+ if (AvoInspector.shouldLog) {
119
+ console.log("Avo Inspector: schema sent successfully.");
120
+ }
121
+ });
122
+ })
123
+ .catch((err) => {
124
+ if (AvoInspector.shouldLog) {
125
+ console.log("Avo Inspector: schema sending failed: " + err + ".");
126
+ }
127
+ });
108
128
  }
109
129
  catch (e) {
110
130
  console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e);
@@ -118,7 +138,7 @@ class AvoInspector {
118
138
  try {
119
139
  if (this.avoDeduplicator.hasSeenEventParams(eventProperties, true)) {
120
140
  if (shouldLogIfEnabled && AvoInspector.shouldLog) {
121
- console.warn("Avo Inspector: WARNING! You are trying to extract schema shape that was just reported by your Avo functions. " +
141
+ console.warn("Avo Inspector: WARNING! You are trying to extract schema shape that was just reported by your Codegen. " +
122
142
  "This is an indicator of duplicate inspector reporting. " +
123
143
  "Please reach out to support@avo.app for advice if you are not sure how to handle this.");
124
144
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AvoNetworkCallsHandler = void 0;
4
4
  const AvoGuid_1 = require("./AvoGuid");
5
5
  const AvoInspector_1 = require("./AvoInspector");
6
+ const https_1 = require("https");
6
7
  class AvoNetworkCallsHandler {
7
8
  constructor(apiKey, envName, appName, appVersion, libVersion) {
8
9
  this.samplingRate = 1.0;
@@ -49,7 +50,7 @@ class AvoNetworkCallsHandler {
49
50
  "Content-Length": Buffer.byteLength(data),
50
51
  },
51
52
  };
52
- var req = require("https").request(options, (res) => {
53
+ var req = https_1.request(options, (res) => {
53
54
  const chunks = [];
54
55
  res.on("data", (data) => chunks.push(data));
55
56
  res.on("end", () => {
@@ -3,7 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const AvoInspector_1 = require("../AvoInspector");
4
4
  const AvoInspectorEnv_1 = require("../AvoInspectorEnv");
5
5
  const constants_1 = require("./constants");
6
+ const AvoNetworkCallsHandler_1 = require("../AvoNetworkCallsHandler");
7
+ jest.mock("../AvoNetworkCallsHandler");
6
8
  describe("Initialization", () => {
9
+ test("Network Handler is initialized on Inspector init", () => {
10
+ const inspectorVersion = process.env.npm_package_version || "";
11
+ const { apiKey, env, version, appName } = constants_1.defaultOptions;
12
+ let inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
13
+ inspector.enableLogging(true);
14
+ expect(AvoNetworkCallsHandler_1.AvoNetworkCallsHandler).toHaveBeenCalledTimes(1);
15
+ expect(AvoNetworkCallsHandler_1.AvoNetworkCallsHandler).toHaveBeenCalledWith(apiKey, env, "my-test-app", version, inspectorVersion);
16
+ });
7
17
  test("Api Key is set", () => {
8
18
  // Given
9
19
  const apiKey = "api-key-xxx";
@@ -53,8 +53,7 @@ describe("Deduplicator", () => {
53
53
  const avoTrackedSchema = yield inspector._avoFunctionTrackSchemaFromEvent("test", testObject, "eventId", "eventhash");
54
54
  const manuallyTrackedSchemaAgain = yield inspector.trackSchemaFromEvent("test", testObject);
55
55
  expect(manuallyTrackedSchema.length).toBe(4);
56
- expect(avoTrackedSchema.length).toBe(0);
57
- expect(manuallyTrackedSchemaAgain.length).toBe(4);
56
+ expect(manuallyTrackedSchema.length + avoTrackedSchema.length + manuallyTrackedSchemaAgain.length).toBe(8);
58
57
  }));
59
58
  test(`Inspector deduplicates only one event when track in avo, manually and then in avo again`, () => __awaiter(void 0, void 0, void 0, function* () {
60
59
  const inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
@@ -39,14 +39,14 @@ describe("NetworkCallsHandler", () => {
39
39
  const body = networkHandler.bodyForSessionStartedCall(constants_1.mockedReturns.SESSION_ID);
40
40
  expect(body).toEqual(Object.assign(Object.assign({}, baseBody), { type: "sessionStarted" }));
41
41
  });
42
- test("bodyForEventSchemaCall returns base body + event schema used for event sending from non avo functions", () => {
42
+ test("bodyForEventSchemaCall returns base body + event schema used for event sending from non Codegen", () => {
43
43
  const eventName = "event name";
44
44
  const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
45
45
  const body = networkHandler.bodyForEventSchemaCall(constants_1.mockedReturns.SESSION_ID, eventName, eventProperties, null, null);
46
46
  expect(body).toEqual(Object.assign(Object.assign({}, baseBody), { type: "event", eventName,
47
47
  eventProperties, avoFunction: false, eventId: null, eventHash: null }));
48
48
  });
49
- test("bodyForEventSchemaCall returns base body + event schema used for event sending from avo functions", () => {
49
+ test("bodyForEventSchemaCall returns base body + event schema used for event sending from Codegen", () => {
50
50
  const eventName = "event name";
51
51
  const eventId = "event id";
52
52
  const eventHash = "event hash";
@@ -0,0 +1,125 @@
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
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const AvoInspector_1 = require("../AvoInspector");
13
+ const AvoNetworkCallsHandler_1 = require("../AvoNetworkCallsHandler");
14
+ const constants_1 = require("./constants");
15
+ describe("AvoNetworkCallsHandler", () => {
16
+ let inspectorCallSpy;
17
+ let inspector;
18
+ const { apiKey, env, version, appName } = constants_1.defaultOptions;
19
+ beforeAll(() => {
20
+ inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
21
+ inspector.enableLogging(false);
22
+ inspectorCallSpy = jest
23
+ .spyOn(AvoNetworkCallsHandler_1.AvoNetworkCallsHandler.prototype, "callInspectorWithBatchBody");
24
+ inspectorCallSpy.mockImplementation(() => {
25
+ inspector.avoNetworkCallsHandler["samplingRate"] = 0.1;
26
+ return Promise.resolve();
27
+ });
28
+ });
29
+ afterEach(() => {
30
+ jest.clearAllMocks();
31
+ // @ts-ignore
32
+ inspector.avoDeduplicator._clearEvents();
33
+ });
34
+ 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
+ yield inspector.trackSchemaFromEvent(eventName, properties);
44
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
45
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toBeCalledWith([
46
+ expect.objectContaining({
47
+ type: "sessionStarted",
48
+ apiKey: apiKey,
49
+ appName: appName,
50
+ appVersion: version,
51
+ libVersion: expect.anything(),
52
+ env: env,
53
+ libPlatform: "node",
54
+ messageId: expect.anything(),
55
+ trackingId: "",
56
+ createdAt: expect.any(String),
57
+ sessionId: expect.anything(),
58
+ samplingRate: 1,
59
+ }),
60
+ ]);
61
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
62
+ expect.objectContaining({
63
+ type: "event",
64
+ apiKey: apiKey,
65
+ appName: appName,
66
+ appVersion: version,
67
+ libVersion: expect.anything(),
68
+ env: env,
69
+ libPlatform: "node",
70
+ messageId: expect.anything(),
71
+ trackingId: "",
72
+ createdAt: expect.anything(),
73
+ sessionId: expect.anything(),
74
+ samplingRate: 0.1,
75
+ }),
76
+ ]);
77
+ }));
78
+ 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
+ const eventId = "testId";
87
+ const eventHash = "testHash";
88
+ const schema = inspector.extractSchema(properties);
89
+ // @ts-ignore
90
+ yield inspector._avoFunctionTrackSchemaFromEvent(eventName, properties, eventId, eventHash);
91
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
92
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toBeCalledWith([
93
+ expect.objectContaining({
94
+ type: "sessionStarted",
95
+ apiKey: apiKey,
96
+ appName: appName,
97
+ appVersion: version,
98
+ libVersion: expect.anything(),
99
+ env: env,
100
+ libPlatform: "node",
101
+ messageId: expect.anything(),
102
+ trackingId: "",
103
+ createdAt: expect.anything(),
104
+ sessionId: expect.anything(),
105
+ samplingRate: 0.1,
106
+ })
107
+ ]);
108
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
109
+ expect.objectContaining({
110
+ type: "event",
111
+ apiKey: apiKey,
112
+ appName: appName,
113
+ appVersion: version,
114
+ libVersion: expect.anything(),
115
+ env: env,
116
+ libPlatform: "node",
117
+ messageId: expect.anything(),
118
+ trackingId: "",
119
+ createdAt: expect.anything(),
120
+ sessionId: expect.anything(),
121
+ samplingRate: 0.1,
122
+ }),
123
+ ]);
124
+ }));
125
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-avo-inspector",
3
- "version": "1.0.1",
3
+ "version": "1.0.3-beta.0",
4
4
  "description": "Avo Inspector for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "repository": "https://github.com/avohq/node-avo-inspector",
@@ -1,16 +1,16 @@
1
1
  import { AvoInspectorEnv, AvoInspectorEnvValueType } from "./AvoInspectorEnv";
2
2
  import { AvoSchemaParser } from "./AvoSchemaParser";
3
- import { AvoBatcher } from "./AvoBatcher";
4
3
  import { AvoNetworkCallsHandler } from "./AvoNetworkCallsHandler";
5
4
  import { AvoDeduplicator } from "./AvoDeduplicator";
6
5
 
7
6
  import { isValueEmpty } from "./utils";
7
+ import { AvoGuid } from "./AvoGuid";
8
8
 
9
9
  const libVersion = require("../package.json").version;
10
10
 
11
11
  export class AvoInspector {
12
12
  environment: AvoInspectorEnvValueType;
13
- avoBatcher: AvoBatcher;
13
+ avoNetworkCallsHandler: AvoNetworkCallsHandler;
14
14
  avoDeduplicator: AvoDeduplicator;
15
15
  apiKey: string;
16
16
  version: string;
@@ -66,14 +66,13 @@ export class AvoInspector {
66
66
  AvoInspector._shouldLog = false;
67
67
  }
68
68
 
69
- let avoNetworkCallsHandler = new AvoNetworkCallsHandler(
69
+ this.avoNetworkCallsHandler = new AvoNetworkCallsHandler(
70
70
  this.apiKey,
71
71
  this.environment.toString(),
72
72
  options.appName || "",
73
73
  this.version,
74
74
  libVersion
75
75
  );
76
- this.avoBatcher = new AvoBatcher(avoNetworkCallsHandler);
77
76
  this.avoDeduplicator = new AvoDeduplicator();
78
77
  }
79
78
 
@@ -98,9 +97,9 @@ export class AvoInspector {
98
97
  if (AvoInspector.shouldLog) {
99
98
  console.log(
100
99
  "Avo Inspector: supplied event " +
101
- eventName +
102
- " with params " +
103
- JSON.stringify(eventProperties)
100
+ eventName +
101
+ " with params " +
102
+ JSON.stringify(eventProperties)
104
103
  );
105
104
  }
106
105
  let eventSchema = this.extractSchema(eventProperties, false);
@@ -152,9 +151,9 @@ export class AvoInspector {
152
151
  if (AvoInspector.shouldLog) {
153
152
  console.log(
154
153
  "Avo Inspector: supplied event " +
155
- eventName +
156
- " with params " +
157
- JSON.stringify(eventProperties)
154
+ eventName +
155
+ " with params " +
156
+ JSON.stringify(eventProperties)
158
157
  );
159
158
  }
160
159
  let eventSchema = this.extractSchema(eventProperties, false);
@@ -194,12 +193,34 @@ export class AvoInspector {
194
193
  eventHash: string | null
195
194
  ): Promise<void> {
196
195
  try {
197
- return this.avoBatcher.handleTrackSchema(
198
- eventName,
199
- eventSchema,
200
- eventId,
201
- eventHash
202
- );
196
+ const seesionId = AvoGuid.newGuid();
197
+ return this.avoNetworkCallsHandler
198
+ .callInspectorWithBatchBody([
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
+ });
203
224
  } catch (e) {
204
225
  console.error(
205
226
  "Avo Inspector: something went wrong. Please report to support@avo.app.",
@@ -227,9 +248,9 @@ export class AvoInspector {
227
248
  if (this.avoDeduplicator.hasSeenEventParams(eventProperties, true)) {
228
249
  if (shouldLogIfEnabled && AvoInspector.shouldLog) {
229
250
  console.warn(
230
- "Avo Inspector: WARNING! You are trying to extract schema shape that was just reported by your Avo functions. " +
231
- "This is an indicator of duplicate inspector reporting. " +
232
- "Please reach out to support@avo.app for advice if you are not sure how to handle this."
251
+ "Avo Inspector: WARNING! You are trying to extract schema shape that was just reported by your Codegen. " +
252
+ "This is an indicator of duplicate inspector reporting. " +
253
+ "Please reach out to support@avo.app for advice if you are not sure how to handle this."
233
254
  );
234
255
  }
235
256
  }
@@ -237,7 +258,7 @@ export class AvoInspector {
237
258
  if (AvoInspector.shouldLog) {
238
259
  console.log(
239
260
  "Avo Inspector: extracting schema from " +
240
- JSON.stringify(eventProperties)
261
+ JSON.stringify(eventProperties)
241
262
  );
242
263
  }
243
264
 
@@ -1,5 +1,6 @@
1
1
  import { AvoGuid } from "./AvoGuid";
2
2
  import { AvoInspector } from "./AvoInspector";
3
+ import { request } from "https";
3
4
 
4
5
  export interface BaseBody {
5
6
  apiKey: string;
@@ -102,7 +103,7 @@ export class AvoNetworkCallsHandler {
102
103
  "Content-Length": Buffer.byteLength(data),
103
104
  },
104
105
  };
105
- var req = require("https").request(options, (res: any) => {
106
+ var req = request(options, (res: any) => {
106
107
  const chunks: any = [];
107
108
  res.on("data", (data: any) => chunks.push(data));
108
109
  res.on("end", () => {
@@ -1,9 +1,28 @@
1
1
  import { AvoInspector } from "../AvoInspector";
2
2
  import { AvoInspectorEnv } from "../AvoInspectorEnv";
3
3
 
4
- import { error } from "./constants";
4
+ import { error, defaultOptions } from "./constants";
5
+ import { AvoNetworkCallsHandler } from "../AvoNetworkCallsHandler";
6
+
7
+ jest.mock("../AvoNetworkCallsHandler");
5
8
 
6
9
  describe("Initialization", () => {
10
+
11
+ test("Network Handler is initialized on Inspector init", () => {
12
+ const inspectorVersion = process.env.npm_package_version || "";
13
+ const { apiKey, env, version, appName } = defaultOptions;
14
+
15
+ let inspector = new AvoInspector(defaultOptions);
16
+ inspector.enableLogging(true);
17
+
18
+ expect(AvoNetworkCallsHandler).toHaveBeenCalledTimes(1);
19
+ expect(AvoNetworkCallsHandler).toHaveBeenCalledWith(apiKey,
20
+ env,
21
+ "my-test-app",
22
+ version,
23
+ inspectorVersion);
24
+ });
25
+
7
26
  test("Api Key is set", () => {
8
27
  // Given
9
28
  const apiKey = "api-key-xxx";
@@ -1,6 +1,5 @@
1
1
  import { AvoDeduplicator } from "../AvoDeduplicator";
2
2
  import { deepEquals } from "../utils";
3
- import { AvoSchemaParser } from "../AvoSchemaParser";
4
3
  import { AvoInspector } from "../AvoInspector";
5
4
  import { defaultOptions } from "./constants";
6
5
 
@@ -91,8 +90,7 @@ describe("Deduplicator", () => {
91
90
  );
92
91
 
93
92
  expect(manuallyTrackedSchema.length).toBe(4);
94
- expect(avoTrackedSchema.length).toBe(0);
95
- expect(manuallyTrackedSchemaAgain.length).toBe(4);
93
+ expect(manuallyTrackedSchema.length + avoTrackedSchema.length + manuallyTrackedSchemaAgain.length).toBe(8);
96
94
  });
97
95
 
98
96
  test(`Inspector deduplicates only one event when track in avo, manually and then in avo again`, async () => {
@@ -64,7 +64,7 @@ describe("NetworkCallsHandler", () => {
64
64
  });
65
65
  });
66
66
 
67
- test("bodyForEventSchemaCall returns base body + event schema used for event sending from non avo functions", () => {
67
+ test("bodyForEventSchemaCall returns base body + event schema used for event sending from non Codegen", () => {
68
68
  const eventName = "event name";
69
69
  const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
70
70
 
@@ -87,7 +87,7 @@ describe("NetworkCallsHandler", () => {
87
87
  });
88
88
  });
89
89
 
90
- test("bodyForEventSchemaCall returns base body + event schema used for event sending from avo functions", () => {
90
+ test("bodyForEventSchemaCall returns base body + event schema used for event sending from Codegen", () => {
91
91
  const eventName = "event name";
92
92
  const eventId = "event id";
93
93
  const eventHash = "event hash";
@@ -0,0 +1,137 @@
1
+ import { AvoInspector } from "../AvoInspector";
2
+ import { AvoNetworkCallsHandler } from "../AvoNetworkCallsHandler";
3
+
4
+ import { defaultOptions } from "./constants";
5
+
6
+ describe("AvoNetworkCallsHandler", () => {
7
+ let inspectorCallSpy: jest.SpyInstance<any, unknown[]>;
8
+
9
+ let inspector: AvoInspector;
10
+
11
+ const { apiKey, env, version, appName } = defaultOptions;
12
+
13
+ beforeAll(() => {
14
+ inspector = new AvoInspector(defaultOptions);
15
+ inspector.enableLogging(false);
16
+
17
+ inspectorCallSpy = jest
18
+ .spyOn(AvoNetworkCallsHandler.prototype as any, "callInspectorWithBatchBody");
19
+
20
+ inspectorCallSpy.mockImplementation(() => {
21
+ inspector.avoNetworkCallsHandler["samplingRate"] = 0.1;
22
+ return Promise.resolve();
23
+ });
24
+ });
25
+
26
+ afterEach(() => {
27
+ jest.clearAllMocks();
28
+ // @ts-ignore
29
+ inspector.avoDeduplicator._clearEvents();
30
+ });
31
+
32
+ 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
+ await inspector.trackSchemaFromEvent(eventName, properties);
44
+
45
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
46
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toBeCalledWith([
47
+ expect.objectContaining({
48
+ type: "sessionStarted",
49
+ apiKey: apiKey,
50
+ appName: appName,
51
+ appVersion: version,
52
+ libVersion: expect.anything(),
53
+ env: env,
54
+ libPlatform: "node",
55
+ messageId: expect.anything(),
56
+ trackingId: "",
57
+ createdAt: expect.any(String),
58
+ sessionId: expect.anything(),
59
+ samplingRate: 1,
60
+ }),
61
+ ]
62
+ );
63
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
64
+ expect.objectContaining({
65
+ type: "event",
66
+ apiKey: apiKey,
67
+ appName: appName,
68
+ appVersion: version,
69
+ libVersion: expect.anything(),
70
+ env: env,
71
+ libPlatform: "node",
72
+ messageId: expect.anything(),
73
+ trackingId: "",
74
+ createdAt: expect.anything(),
75
+ sessionId: expect.anything(),
76
+ samplingRate: 0.1,
77
+ }),
78
+ ]);
79
+ });
80
+
81
+ 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
+ const eventId = "testId";
90
+ const eventHash = "testHash";
91
+
92
+ const schema = inspector.extractSchema(properties);
93
+
94
+ // @ts-ignore
95
+ await inspector._avoFunctionTrackSchemaFromEvent(
96
+ eventName,
97
+ properties,
98
+ eventId,
99
+ eventHash
100
+ );
101
+
102
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
103
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toBeCalledWith([
104
+ expect.objectContaining({
105
+ type: "sessionStarted",
106
+ apiKey: apiKey,
107
+ appName: appName,
108
+ appVersion: version,
109
+ libVersion: expect.anything(),
110
+ env: env,
111
+ libPlatform: "node",
112
+ messageId: expect.anything(),
113
+ trackingId: "",
114
+ createdAt: expect.anything(),
115
+ sessionId: expect.anything(),
116
+ samplingRate: 0.1,
117
+ })
118
+ ]
119
+ );
120
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
121
+ expect.objectContaining({
122
+ type: "event",
123
+ apiKey: apiKey,
124
+ appName: appName,
125
+ appVersion: version,
126
+ libVersion: expect.anything(),
127
+ env: env,
128
+ libPlatform: "node",
129
+ messageId: expect.anything(),
130
+ trackingId: "",
131
+ createdAt: expect.anything(),
132
+ sessionId: expect.anything(),
133
+ samplingRate: 0.1,
134
+ }),
135
+ ]);
136
+ });
137
+ });
@@ -1,17 +0,0 @@
1
- import { AvoNetworkCallsHandler } from "./AvoNetworkCallsHandler";
2
- export interface AvoBatcherType {
3
- handleTrackSchema(eventName: string, schema: Array<{
4
- propertyName: string;
5
- propertyType: string;
6
- children?: any;
7
- }>, eventId: string | null, eventHash: string | null): Promise<void>;
8
- }
9
- export declare class AvoBatcher implements AvoBatcherType {
10
- private networkCallsHandler;
11
- constructor(networkCallsHandler: AvoNetworkCallsHandler);
12
- handleTrackSchema(eventName: string, schema: Array<{
13
- propertyName: string;
14
- propertyType: string;
15
- children?: any;
16
- }>, eventId: string | null, eventHash: string | null): Promise<void>;
17
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AvoBatcher = void 0;
4
- const AvoInspector_1 = require("./AvoInspector");
5
- const AvoGuid_1 = require("./AvoGuid");
6
- class AvoBatcher {
7
- constructor(networkCallsHandler) {
8
- this.networkCallsHandler = networkCallsHandler;
9
- }
10
- handleTrackSchema(eventName, schema, eventId, eventHash) {
11
- const seeionId = AvoGuid_1.AvoGuid.newGuid();
12
- return this.networkCallsHandler
13
- .callInspectorWithBatchBody([
14
- this.networkCallsHandler.bodyForSessionStartedCall(seeionId),
15
- ])
16
- .then(() => {
17
- if (AvoInspector_1.AvoInspector.shouldLog) {
18
- console.log("Avo Inspector: schema sent successfully.");
19
- }
20
- this.networkCallsHandler.callInspectorWithBatchBody([
21
- this.networkCallsHandler.bodyForEventSchemaCall(seeionId, eventName, schema, eventId, eventHash),
22
- ]);
23
- })
24
- .catch((err) => {
25
- if (AvoInspector_1.AvoInspector.shouldLog) {
26
- console.log("Avo Inspector: schema sending failed: " + err + ".");
27
- }
28
- });
29
- }
30
- }
31
- exports.AvoBatcher = AvoBatcher;
@@ -1,66 +0,0 @@
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
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const AvoInspector_1 = require("../AvoInspector");
13
- const AvoNetworkCallsHandler_1 = require("../AvoNetworkCallsHandler");
14
- const constants_1 = require("./constants");
15
- describe("Batcher", () => {
16
- let inspectorCallSpy;
17
- const { apiKey, env, version, shouldLog, appName } = constants_1.defaultOptions;
18
- beforeAll(() => {
19
- inspectorCallSpy = jest.spyOn(AvoNetworkCallsHandler_1.AvoNetworkCallsHandler.prototype, "callInspectorWithBatchBody");
20
- });
21
- afterEach(() => {
22
- jest.clearAllMocks();
23
- });
24
- test("handleTrackSchema sends session to AvoNetworkCallsHandler and then sends event respecting sampling rate", () => __awaiter(void 0, void 0, void 0, function* () {
25
- const inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
26
- inspector.enableLogging(false);
27
- inspectorCallSpy.mockImplementation(() => {
28
- inspector.avoBatcher["networkCallsHandler"]["samplingRate"] = 0.1;
29
- return Promise.resolve();
30
- });
31
- yield inspector.avoBatcher.handleTrackSchema("event name", [], null, null);
32
- expect(inspectorCallSpy).toHaveBeenCalledTimes(2);
33
- expect(inspectorCallSpy).toHaveBeenCalledWith([
34
- expect.objectContaining({
35
- type: "sessionStarted",
36
- apiKey: apiKey,
37
- appName: appName,
38
- appVersion: version,
39
- libVersion: expect.anything(),
40
- env: env,
41
- libPlatform: "node",
42
- messageId: expect.anything(),
43
- trackingId: "",
44
- createdAt: expect.anything(),
45
- sessionId: expect.anything(),
46
- samplingRate: 1,
47
- }),
48
- ]);
49
- expect(inspectorCallSpy).toHaveBeenCalledWith([
50
- expect.objectContaining({
51
- type: "event",
52
- apiKey: apiKey,
53
- appName: appName,
54
- appVersion: version,
55
- libVersion: expect.anything(),
56
- env: env,
57
- libPlatform: "node",
58
- messageId: expect.anything(),
59
- trackingId: "",
60
- createdAt: expect.anything(),
61
- sessionId: expect.anything(),
62
- samplingRate: 0.1,
63
- }),
64
- ]);
65
- }));
66
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,60 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const AvoBatcher_1 = require("../AvoBatcher");
4
- const AvoInspector_1 = require("../AvoInspector");
5
- const AvoNetworkCallsHandler_1 = require("../AvoNetworkCallsHandler");
6
- const constants_1 = require("./constants");
7
- const inspectorVersion = process.env.npm_package_version || "";
8
- jest.mock("../AvoBatcher");
9
- describe("Batcher", () => {
10
- let inspector;
11
- const { apiKey, env, version } = constants_1.defaultOptions;
12
- let networkHandler = new AvoNetworkCallsHandler_1.AvoNetworkCallsHandler(apiKey, env, "my-test-app", version, inspectorVersion);
13
- beforeAll(() => {
14
- inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
15
- inspector.enableLogging(false);
16
- jest
17
- .spyOn(AvoBatcher_1.AvoBatcher.prototype, "handleTrackSchema")
18
- .mockImplementation(() => {
19
- return Promise.resolve();
20
- });
21
- });
22
- afterEach(() => {
23
- jest.clearAllMocks();
24
- // @ts-ignore
25
- inspector.avoDeduplicator._clearEvents();
26
- });
27
- test("Batcher is initialized on Inspector init", () => {
28
- expect(AvoBatcher_1.AvoBatcher).toHaveBeenCalledTimes(1);
29
- expect(AvoBatcher_1.AvoBatcher).toHaveBeenCalledWith(networkHandler);
30
- });
31
- test("handleTrackSchema is called on trackSchemaFromEvent", () => {
32
- const eventName = "event name";
33
- const properties = {
34
- prop0: "",
35
- prop2: false,
36
- prop3: 0,
37
- prop4: 0.0,
38
- };
39
- const schema = inspector.extractSchema(properties);
40
- inspector.trackSchemaFromEvent(eventName, properties);
41
- expect(inspector.avoBatcher.handleTrackSchema).toHaveBeenCalledTimes(1);
42
- expect(inspector.avoBatcher.handleTrackSchema).toBeCalledWith(eventName, schema, null, null);
43
- });
44
- test("handleTrackSchema is called on _avoFunctionTrackSchemaFromEvent", () => {
45
- const eventName = "event name";
46
- const properties = {
47
- prop0: "",
48
- prop2: false,
49
- prop3: 0,
50
- prop4: 0.0,
51
- };
52
- const eventId = "testId";
53
- const eventHash = "testHash";
54
- const schema = inspector.extractSchema(properties);
55
- // @ts-ignore
56
- inspector._avoFunctionTrackSchemaFromEvent(eventName, properties, eventId, eventHash);
57
- expect(inspector.avoBatcher.handleTrackSchema).toHaveBeenCalledTimes(1);
58
- expect(inspector.avoBatcher.handleTrackSchema).toBeCalledWith(eventName, schema, eventId, eventHash);
59
- });
60
- });
package/src/AvoBatcher.ts DELETED
@@ -1,60 +0,0 @@
1
- import { AvoNetworkCallsHandler } from "./AvoNetworkCallsHandler";
2
- import { AvoInspector } from "./AvoInspector";
3
- import { AvoGuid } from "./AvoGuid";
4
-
5
- export interface AvoBatcherType {
6
- handleTrackSchema(
7
- eventName: string,
8
- schema: Array<{
9
- propertyName: string;
10
- propertyType: string;
11
- children?: any;
12
- }>,
13
- eventId: string | null,
14
- eventHash: string | null
15
- ): Promise<void>;
16
- }
17
-
18
- export class AvoBatcher implements AvoBatcherType {
19
- private networkCallsHandler: AvoNetworkCallsHandler;
20
-
21
- constructor(networkCallsHandler: AvoNetworkCallsHandler) {
22
- this.networkCallsHandler = networkCallsHandler;
23
- }
24
-
25
- handleTrackSchema(
26
- eventName: string,
27
- schema: Array<{
28
- propertyName: string;
29
- propertyType: string;
30
- children?: any;
31
- }>,
32
- eventId: string | null,
33
- eventHash: string | null
34
- ): Promise<void> {
35
- const seeionId = AvoGuid.newGuid();
36
- return this.networkCallsHandler
37
- .callInspectorWithBatchBody([
38
- this.networkCallsHandler.bodyForSessionStartedCall(seeionId),
39
- ])
40
- .then(() => {
41
- if (AvoInspector.shouldLog) {
42
- console.log("Avo Inspector: schema sent successfully.");
43
- }
44
- this.networkCallsHandler.callInspectorWithBatchBody([
45
- this.networkCallsHandler.bodyForEventSchemaCall(
46
- seeionId,
47
- eventName,
48
- schema,
49
- eventId,
50
- eventHash
51
- ),
52
- ]);
53
- })
54
- .catch((err) => {
55
- if (AvoInspector.shouldLog) {
56
- console.log("Avo Inspector: schema sending failed: " + err + ".");
57
- }
58
- });
59
- }
60
- }
@@ -1,67 +0,0 @@
1
- import { AvoInspector } from "../AvoInspector";
2
- import { AvoNetworkCallsHandler } from "../AvoNetworkCallsHandler";
3
-
4
- import { defaultOptions } from "./constants";
5
-
6
- describe("Batcher", () => {
7
- let inspectorCallSpy: jest.SpyInstance<any, unknown[]>;
8
-
9
- const { apiKey, env, version, shouldLog, appName } = defaultOptions;
10
-
11
- beforeAll(() => {
12
- inspectorCallSpy = jest.spyOn(
13
- AvoNetworkCallsHandler.prototype as any,
14
- "callInspectorWithBatchBody"
15
- );
16
- });
17
-
18
- afterEach(() => {
19
- jest.clearAllMocks();
20
- });
21
-
22
- test("handleTrackSchema sends session to AvoNetworkCallsHandler and then sends event respecting sampling rate", async () => {
23
- const inspector = new AvoInspector(defaultOptions);
24
- inspector.enableLogging(false);
25
-
26
- inspectorCallSpy.mockImplementation(() => {
27
- inspector.avoBatcher["networkCallsHandler"]["samplingRate"] = 0.1;
28
- return Promise.resolve();
29
- });
30
-
31
- await inspector.avoBatcher.handleTrackSchema("event name", [], null, null);
32
-
33
- expect(inspectorCallSpy).toHaveBeenCalledTimes(2);
34
- expect(inspectorCallSpy).toHaveBeenCalledWith([
35
- expect.objectContaining({
36
- type: "sessionStarted",
37
- apiKey: apiKey,
38
- appName: appName,
39
- appVersion: version,
40
- libVersion: expect.anything(),
41
- env: env,
42
- libPlatform: "node",
43
- messageId: expect.anything(),
44
- trackingId: "",
45
- createdAt: expect.anything(),
46
- sessionId: expect.anything(),
47
- samplingRate: 1,
48
- }),
49
- ]);
50
- expect(inspectorCallSpy).toHaveBeenCalledWith([
51
- expect.objectContaining({
52
- type: "event",
53
- apiKey: apiKey,
54
- appName: appName,
55
- appVersion: version,
56
- libVersion: expect.anything(),
57
- env: env,
58
- libPlatform: "node",
59
- messageId: expect.anything(),
60
- trackingId: "",
61
- createdAt: expect.anything(),
62
- sessionId: expect.anything(),
63
- samplingRate: 0.1,
64
- }),
65
- ]);
66
- });
67
- });
@@ -1,96 +0,0 @@
1
- import { AvoBatcher } from "../AvoBatcher";
2
- import { AvoInspector } from "../AvoInspector";
3
- import { AvoNetworkCallsHandler } from "../AvoNetworkCallsHandler";
4
-
5
- import { defaultOptions } from "./constants";
6
-
7
- const inspectorVersion = process.env.npm_package_version || "";
8
-
9
- jest.mock("../AvoBatcher");
10
-
11
- describe("Batcher", () => {
12
- let inspector: AvoInspector;
13
-
14
- const { apiKey, env, version } = defaultOptions;
15
- let networkHandler = new AvoNetworkCallsHandler(
16
- apiKey,
17
- env,
18
- "my-test-app",
19
- version,
20
- inspectorVersion
21
- );
22
-
23
- beforeAll(() => {
24
- inspector = new AvoInspector(defaultOptions);
25
- inspector.enableLogging(false);
26
-
27
- jest
28
- .spyOn(AvoBatcher.prototype as any, "handleTrackSchema")
29
- .mockImplementation(() => {
30
- return Promise.resolve();
31
- });
32
- });
33
-
34
- afterEach(() => {
35
- jest.clearAllMocks();
36
- // @ts-ignore
37
- inspector.avoDeduplicator._clearEvents();
38
- });
39
-
40
- test("Batcher is initialized on Inspector init", () => {
41
- expect(AvoBatcher).toHaveBeenCalledTimes(1);
42
- expect(AvoBatcher).toHaveBeenCalledWith(networkHandler);
43
- });
44
-
45
- test("handleTrackSchema is called on trackSchemaFromEvent", () => {
46
- const eventName = "event name";
47
- const properties = {
48
- prop0: "",
49
- prop2: false,
50
- prop3: 0,
51
- prop4: 0.0,
52
- };
53
-
54
- const schema = inspector.extractSchema(properties);
55
-
56
- inspector.trackSchemaFromEvent(eventName, properties);
57
-
58
- expect(inspector.avoBatcher.handleTrackSchema).toHaveBeenCalledTimes(1);
59
- expect(inspector.avoBatcher.handleTrackSchema).toBeCalledWith(
60
- eventName,
61
- schema,
62
- null,
63
- null
64
- );
65
- });
66
-
67
- test("handleTrackSchema is called on _avoFunctionTrackSchemaFromEvent", () => {
68
- const eventName = "event name";
69
- const properties = {
70
- prop0: "",
71
- prop2: false,
72
- prop3: 0,
73
- prop4: 0.0,
74
- };
75
- const eventId = "testId";
76
- const eventHash = "testHash";
77
-
78
- const schema = inspector.extractSchema(properties);
79
-
80
- // @ts-ignore
81
- inspector._avoFunctionTrackSchemaFromEvent(
82
- eventName,
83
- properties,
84
- eventId,
85
- eventHash
86
- );
87
-
88
- expect(inspector.avoBatcher.handleTrackSchema).toHaveBeenCalledTimes(1);
89
- expect(inspector.avoBatcher.handleTrackSchema).toBeCalledWith(
90
- eventName,
91
- schema,
92
- eventId,
93
- eventHash
94
- );
95
- });
96
- });