@eventvisor/sdk 0.0.2
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/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/attributesManager.d.ts +36 -0
- package/dist/bucketer.d.ts +30 -0
- package/dist/compareVersions.d.ts +4 -0
- package/dist/conditions.d.ts +20 -0
- package/dist/datafileReader.d.ts +29 -0
- package/dist/effectsManager.d.ts +33 -0
- package/dist/emitter.d.ts +11 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.gz +0 -0
- package/dist/index.mjs.map +1 -0
- package/dist/instance.d.ts +67 -0
- package/dist/logger.d.ts +26 -0
- package/dist/modulesManager.d.ts +67 -0
- package/dist/murmurhash.d.ts +1 -0
- package/dist/persister.d.ts +40 -0
- package/dist/sourceResolver.d.ts +31 -0
- package/dist/transformer.d.ts +21 -0
- package/dist/validator.d.ts +28 -0
- package/jest.config.js +6 -0
- package/lib/attributesManager.d.ts +36 -0
- package/lib/bucketer.d.ts +30 -0
- package/lib/compareVersions.d.ts +4 -0
- package/lib/conditions.d.ts +20 -0
- package/lib/datafileReader.d.ts +29 -0
- package/lib/effectsManager.d.ts +33 -0
- package/lib/emitter.d.ts +11 -0
- package/lib/index.d.ts +12 -0
- package/lib/instance.d.ts +67 -0
- package/lib/logger.d.ts +26 -0
- package/lib/modulesManager.d.ts +67 -0
- package/lib/murmurhash.d.ts +1 -0
- package/lib/persister.d.ts +40 -0
- package/lib/sourceResolver.d.ts +31 -0
- package/lib/transformer.d.ts +21 -0
- package/lib/validator.d.ts +28 -0
- package/package.json +45 -0
- package/src/attributesManager.ts +181 -0
- package/src/bucketer.spec.ts +156 -0
- package/src/bucketer.ts +152 -0
- package/src/compareVersions.ts +93 -0
- package/src/conditions.ts +224 -0
- package/src/datafileReader.ts +133 -0
- package/src/effectsManager.ts +214 -0
- package/src/emitter.ts +64 -0
- package/src/index.spec.ts +5 -0
- package/src/index.ts +14 -0
- package/src/instance.spec.ts +184 -0
- package/src/instance.ts +608 -0
- package/src/logger.ts +90 -0
- package/src/modulesManager.ts +276 -0
- package/src/murmurhash.ts +71 -0
- package/src/persister.ts +162 -0
- package/src/sourceResolver.spec.ts +253 -0
- package/src/sourceResolver.ts +213 -0
- package/src/transformer.ts +316 -0
- package/src/transformer_static.spec.ts +377 -0
- package/src/transformer_types.spec.ts +820 -0
- package/src/validator.spec.ts +579 -0
- package/src/validator.ts +366 -0
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.esm.json +8 -0
- package/webpack.config.js +80 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { DatafileContent } from "@eventvisor/types";
|
|
2
|
+
|
|
3
|
+
import { createInstance } from "./instance";
|
|
4
|
+
import { Module } from "./modulesManager";
|
|
5
|
+
import { emptyDatafile } from "./datafileReader";
|
|
6
|
+
|
|
7
|
+
function waitFor(durationInMs: number) {
|
|
8
|
+
return new Promise((resolve) => setTimeout(resolve, durationInMs));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe("sdk: instance", function () {
|
|
12
|
+
it("should be a function", function () {
|
|
13
|
+
expect(createInstance).toBeDefined();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should be initialized", async function () {
|
|
17
|
+
const datafile: DatafileContent = {
|
|
18
|
+
...emptyDatafile,
|
|
19
|
+
attributes: {
|
|
20
|
+
userId: {
|
|
21
|
+
type: "string",
|
|
22
|
+
},
|
|
23
|
+
deviceId: {
|
|
24
|
+
type: "string",
|
|
25
|
+
},
|
|
26
|
+
browser: {
|
|
27
|
+
type: "object",
|
|
28
|
+
properties: {
|
|
29
|
+
name: { type: "string" },
|
|
30
|
+
version: { type: "string" },
|
|
31
|
+
},
|
|
32
|
+
required: ["name", "version"],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
events: {
|
|
36
|
+
pageView: {
|
|
37
|
+
type: "object",
|
|
38
|
+
properties: {
|
|
39
|
+
url: { type: "string" },
|
|
40
|
+
},
|
|
41
|
+
required: ["url"],
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
effects: {
|
|
45
|
+
someEffectName: {
|
|
46
|
+
on: {
|
|
47
|
+
event_tracked: ["pageView"],
|
|
48
|
+
attribute_set: ["userId"],
|
|
49
|
+
},
|
|
50
|
+
state: {
|
|
51
|
+
handled: false,
|
|
52
|
+
},
|
|
53
|
+
conditions: [
|
|
54
|
+
{
|
|
55
|
+
state: "handled",
|
|
56
|
+
operator: "equals",
|
|
57
|
+
value: false,
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
steps: [
|
|
61
|
+
{
|
|
62
|
+
handler: "test",
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
transforms: [
|
|
66
|
+
{
|
|
67
|
+
type: "set",
|
|
68
|
+
target: "handled",
|
|
69
|
+
value: true,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
destinations: {
|
|
77
|
+
test: {
|
|
78
|
+
transport: "test",
|
|
79
|
+
transforms: [
|
|
80
|
+
{
|
|
81
|
+
type: "set",
|
|
82
|
+
value: {},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
type: "set",
|
|
86
|
+
source: "attributes",
|
|
87
|
+
target: "attributes",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
type: "set",
|
|
91
|
+
source: "payload",
|
|
92
|
+
target: "payload",
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
type: "set",
|
|
96
|
+
source: "eventName",
|
|
97
|
+
target: "eventName",
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const capturedEvents: Record<string, any>[] = [];
|
|
105
|
+
const capturedHandles: Record<string, any>[] = [];
|
|
106
|
+
|
|
107
|
+
const testModule: Module = {
|
|
108
|
+
name: "test",
|
|
109
|
+
|
|
110
|
+
transport: async ({ destinationName, eventName, payload }) => {
|
|
111
|
+
capturedEvents.push({ destinationName, eventName, payload });
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
handle: async ({ effectName, step }) => {
|
|
115
|
+
capturedHandles.push({ effectName, step });
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const eventvisor = createInstance({
|
|
120
|
+
datafile,
|
|
121
|
+
modules: [testModule],
|
|
122
|
+
logLevel: "warn",
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
await eventvisor.onReady();
|
|
126
|
+
|
|
127
|
+
expect(eventvisor.isReady()).toBe(true);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Attributes
|
|
131
|
+
*/
|
|
132
|
+
expect(eventvisor.getAttributeValue("userId")).toBeNull();
|
|
133
|
+
expect(eventvisor.isAttributeSet("userId")).toBe(false);
|
|
134
|
+
|
|
135
|
+
eventvisor.setAttribute("userId", "user-123");
|
|
136
|
+
eventvisor.setAttribute("deviceId", "device-234");
|
|
137
|
+
|
|
138
|
+
await waitFor(0);
|
|
139
|
+
|
|
140
|
+
expect(eventvisor.getAttributeValue("userId")).toBe("user-123");
|
|
141
|
+
expect(eventvisor.getAttributeValue("deviceId")).toBe("device-234");
|
|
142
|
+
expect(eventvisor.isAttributeSet("userId")).toBe(true);
|
|
143
|
+
expect(eventvisor.isAttributeSet("deviceId")).toBe(true);
|
|
144
|
+
|
|
145
|
+
eventvisor.removeAttribute("deviceId");
|
|
146
|
+
|
|
147
|
+
await waitFor(0);
|
|
148
|
+
|
|
149
|
+
expect(eventvisor.getAttributeValue("deviceId")).toBeNull();
|
|
150
|
+
|
|
151
|
+
eventvisor.setAttribute("browser", { name: "Chrome", version: "100" });
|
|
152
|
+
eventvisor.setAttribute("deviceId", "device-234");
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Events
|
|
156
|
+
*/
|
|
157
|
+
eventvisor.track("pageView", { url: "https://www.example.com" });
|
|
158
|
+
|
|
159
|
+
await waitFor(0);
|
|
160
|
+
|
|
161
|
+
expect(capturedEvents[0].payload).toEqual({
|
|
162
|
+
eventName: "pageView",
|
|
163
|
+
payload: { url: "https://www.example.com" },
|
|
164
|
+
attributes: {
|
|
165
|
+
userId: "user-123",
|
|
166
|
+
deviceId: "device-234",
|
|
167
|
+
browser: { name: "Chrome", version: "100" },
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Effects
|
|
173
|
+
*/
|
|
174
|
+
expect(capturedHandles.length).toEqual(1);
|
|
175
|
+
expect(capturedHandles[0].effectName).toBe("someEffectName");
|
|
176
|
+
|
|
177
|
+
eventvisor.track("pageView", { url: "https://www.example.com" });
|
|
178
|
+
|
|
179
|
+
await waitFor(0);
|
|
180
|
+
|
|
181
|
+
// should not increase
|
|
182
|
+
expect(capturedHandles.length).toEqual(1);
|
|
183
|
+
});
|
|
184
|
+
});
|