@symbo.ls/sdk 2.34.35 → 3.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.
Files changed (187) hide show
  1. package/README.md +13 -315
  2. package/dist/cjs/config/environment.js +50 -124
  3. package/dist/cjs/index.js +22 -169
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +277 -751
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +679 -0
  8. package/dist/cjs/services/SocketIOService.js +309 -0
  9. package/dist/cjs/services/SocketService.js +161 -0
  10. package/dist/cjs/services/SymstoryService.js +485 -0
  11. package/dist/cjs/services/index.js +16 -64
  12. package/dist/cjs/utils/basedQuerys.js +61 -0
  13. package/dist/cjs/utils/permission.js +4 -4
  14. package/dist/cjs/utils/services.js +80 -301
  15. package/dist/cjs/utils/symstoryClient.js +228 -0
  16. package/dist/cjs/utils/validation.js +3 -0
  17. package/dist/esm/config/environment.js +50 -124
  18. package/dist/esm/index.js +11282 -49789
  19. package/dist/esm/services/AIService.js +185 -0
  20. package/dist/esm/services/AuthService.js +358 -1506
  21. package/dist/esm/services/BaseService.js +6 -757
  22. package/dist/esm/services/BasedService.js +4651 -0
  23. package/dist/esm/services/SocketIOService.js +467 -0
  24. package/dist/esm/services/SocketService.js +191 -0
  25. package/dist/esm/services/SymstoryService.js +6849 -0
  26. package/dist/esm/services/index.js +11188 -49352
  27. package/dist/esm/utils/basedQuerys.js +43 -0
  28. package/dist/esm/utils/permission.js +4 -4
  29. package/dist/esm/utils/services.js +80 -301
  30. package/dist/esm/utils/symstoryClient.js +334 -0
  31. package/dist/esm/utils/validation.js +26 -93
  32. package/dist/node/config/environment.js +50 -124
  33. package/dist/node/index.js +26 -202
  34. package/dist/node/services/AIService.js +136 -0
  35. package/dist/node/services/AuthService.js +278 -751
  36. package/dist/node/services/BaseService.js +6 -148
  37. package/dist/node/services/BasedService.js +650 -0
  38. package/dist/node/services/SocketIOService.js +280 -0
  39. package/dist/node/services/SocketService.js +142 -0
  40. package/dist/node/services/SymstoryService.js +456 -0
  41. package/dist/node/services/index.js +16 -64
  42. package/dist/node/utils/basedQuerys.js +42 -0
  43. package/dist/node/utils/permission.js +4 -4
  44. package/dist/node/utils/services.js +80 -301
  45. package/dist/node/utils/symstoryClient.js +199 -0
  46. package/dist/node/utils/validation.js +3 -0
  47. package/package.json +21 -43
  48. package/src/config/environment.js +50 -126
  49. package/src/index.js +24 -208
  50. package/src/services/AIService.js +150 -0
  51. package/src/services/AuthService.js +298 -893
  52. package/src/services/BaseService.js +6 -166
  53. package/src/services/BasedService.js +722 -0
  54. package/src/services/SocketIOService.js +356 -0
  55. package/src/services/SocketService.js +168 -0
  56. package/src/services/SymstoryService.js +563 -0
  57. package/src/services/index.js +13 -80
  58. package/src/utils/basedQuerys.js +41 -0
  59. package/src/utils/permission.js +4 -4
  60. package/src/utils/services.js +83 -325
  61. package/src/utils/symstoryClient.js +218 -0
  62. package/src/utils/validation.js +3 -0
  63. package/dist/cjs/services/AdminService.js +0 -351
  64. package/dist/cjs/services/BranchService.js +0 -484
  65. package/dist/cjs/services/CollabService.js +0 -743
  66. package/dist/cjs/services/DnsService.js +0 -340
  67. package/dist/cjs/services/FeatureFlagService.js +0 -175
  68. package/dist/cjs/services/FileService.js +0 -201
  69. package/dist/cjs/services/IntegrationService.js +0 -538
  70. package/dist/cjs/services/MetricsService.js +0 -62
  71. package/dist/cjs/services/PaymentService.js +0 -271
  72. package/dist/cjs/services/PlanService.js +0 -426
  73. package/dist/cjs/services/ProjectService.js +0 -1207
  74. package/dist/cjs/services/PullRequestService.js +0 -503
  75. package/dist/cjs/services/ScreenshotService.js +0 -304
  76. package/dist/cjs/services/SubscriptionService.js +0 -396
  77. package/dist/cjs/services/TrackingService.js +0 -661
  78. package/dist/cjs/services/WaitlistService.js +0 -148
  79. package/dist/cjs/state/RootStateManager.js +0 -65
  80. package/dist/cjs/state/rootEventBus.js +0 -74
  81. package/dist/cjs/utils/CollabClient.js +0 -223
  82. package/dist/cjs/utils/TokenManager.js +0 -422
  83. package/dist/cjs/utils/changePreprocessor.js +0 -199
  84. package/dist/cjs/utils/jsonDiff.js +0 -145
  85. package/dist/cjs/utils/ordering.js +0 -309
  86. package/dist/esm/services/AdminService.js +0 -1132
  87. package/dist/esm/services/BranchService.js +0 -1265
  88. package/dist/esm/services/CollabService.js +0 -26838
  89. package/dist/esm/services/DnsService.js +0 -1121
  90. package/dist/esm/services/FeatureFlagService.js +0 -956
  91. package/dist/esm/services/FileService.js +0 -982
  92. package/dist/esm/services/IntegrationService.js +0 -1319
  93. package/dist/esm/services/MetricsService.js +0 -843
  94. package/dist/esm/services/PaymentService.js +0 -1052
  95. package/dist/esm/services/PlanService.js +0 -1207
  96. package/dist/esm/services/ProjectService.js +0 -2526
  97. package/dist/esm/services/PullRequestService.js +0 -1284
  98. package/dist/esm/services/ScreenshotService.js +0 -1085
  99. package/dist/esm/services/SubscriptionService.js +0 -1177
  100. package/dist/esm/services/TrackingService.js +0 -18343
  101. package/dist/esm/services/WaitlistService.js +0 -929
  102. package/dist/esm/state/RootStateManager.js +0 -90
  103. package/dist/esm/state/rootEventBus.js +0 -56
  104. package/dist/esm/utils/CollabClient.js +0 -18901
  105. package/dist/esm/utils/TokenManager.js +0 -408
  106. package/dist/esm/utils/changePreprocessor.js +0 -542
  107. package/dist/esm/utils/jsonDiff.js +0 -7011
  108. package/dist/esm/utils/ordering.js +0 -291
  109. package/dist/node/services/AdminService.js +0 -332
  110. package/dist/node/services/BranchService.js +0 -465
  111. package/dist/node/services/CollabService.js +0 -724
  112. package/dist/node/services/DnsService.js +0 -321
  113. package/dist/node/services/FeatureFlagService.js +0 -156
  114. package/dist/node/services/FileService.js +0 -182
  115. package/dist/node/services/IntegrationService.js +0 -519
  116. package/dist/node/services/MetricsService.js +0 -43
  117. package/dist/node/services/PaymentService.js +0 -252
  118. package/dist/node/services/PlanService.js +0 -407
  119. package/dist/node/services/ProjectService.js +0 -1188
  120. package/dist/node/services/PullRequestService.js +0 -484
  121. package/dist/node/services/ScreenshotService.js +0 -285
  122. package/dist/node/services/SubscriptionService.js +0 -377
  123. package/dist/node/services/TrackingService.js +0 -632
  124. package/dist/node/services/WaitlistService.js +0 -129
  125. package/dist/node/state/RootStateManager.js +0 -36
  126. package/dist/node/state/rootEventBus.js +0 -55
  127. package/dist/node/utils/CollabClient.js +0 -194
  128. package/dist/node/utils/TokenManager.js +0 -403
  129. package/dist/node/utils/changePreprocessor.js +0 -180
  130. package/dist/node/utils/jsonDiff.js +0 -116
  131. package/dist/node/utils/ordering.js +0 -290
  132. package/src/services/AdminService.js +0 -374
  133. package/src/services/BranchService.js +0 -536
  134. package/src/services/CollabService.js +0 -900
  135. package/src/services/DnsService.js +0 -366
  136. package/src/services/FeatureFlagService.js +0 -174
  137. package/src/services/FileService.js +0 -213
  138. package/src/services/IntegrationService.js +0 -548
  139. package/src/services/MetricsService.js +0 -40
  140. package/src/services/PaymentService.js +0 -287
  141. package/src/services/PlanService.js +0 -468
  142. package/src/services/ProjectService.js +0 -1366
  143. package/src/services/PullRequestService.js +0 -537
  144. package/src/services/ScreenshotService.js +0 -258
  145. package/src/services/SubscriptionService.js +0 -425
  146. package/src/services/TrackingService.js +0 -853
  147. package/src/services/WaitlistService.js +0 -130
  148. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  149. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  150. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  151. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  152. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  153. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  154. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  155. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  156. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  157. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  158. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  159. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  160. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  161. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  162. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  163. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  164. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  165. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  166. package/src/services/tests/FileService/validateFile.test.js +0 -63
  167. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  168. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  169. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  170. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  171. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  172. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  173. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  174. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  175. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  176. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  177. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  178. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  179. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  180. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  181. package/src/state/RootStateManager.js +0 -76
  182. package/src/state/rootEventBus.js +0 -67
  183. package/src/utils/CollabClient.js +0 -248
  184. package/src/utils/TokenManager.js +0 -479
  185. package/src/utils/changePreprocessor.js +0 -239
  186. package/src/utils/jsonDiff.js +0 -144
  187. package/src/utils/ordering.js +0 -271
@@ -1,632 +0,0 @@
1
- import { BaseService } from "./BaseService.js";
2
- import environment from "../config/environment.js";
3
- const DEFAULT_MAX_QUEUE_SIZE = 100;
4
- const isBrowserEnvironment = () => typeof window !== "undefined";
5
- const DEFAULT_TRACKING_OPTIONS = {
6
- // Enabled by default unless explicitly disabled via SDK options or runtime config
7
- enabled: true,
8
- sessionTracking: true,
9
- enableTracing: true,
10
- maxQueueSize: DEFAULT_MAX_QUEUE_SIZE,
11
- transports: null,
12
- instrumentations: null,
13
- instrumentationsFactory: null,
14
- webInstrumentationOptions: null,
15
- globalAttributes: {},
16
- transport: null,
17
- user: null
18
- };
19
- const sanitizeAttributes = (value) => {
20
- if (!value || typeof value !== "object") {
21
- return {};
22
- }
23
- try {
24
- return JSON.parse(JSON.stringify(value));
25
- } catch (error) {
26
- console.warn("[TrackingService] Failed to sanitize attributes:", error);
27
- return { ...value };
28
- }
29
- };
30
- class TrackingService extends BaseService {
31
- constructor({ context, options } = {}) {
32
- super({ context, options });
33
- this._faroClient = null;
34
- this._queue = [];
35
- this._initialized = false;
36
- this._enabled = DEFAULT_TRACKING_OPTIONS.enabled;
37
- this._runtimeConfig = {};
38
- this._setupPromise = null;
39
- this._trackingOptions = {
40
- ...DEFAULT_TRACKING_OPTIONS,
41
- ...(options == null ? void 0 : options.tracking) || {}
42
- };
43
- this._globalAttributes = sanitizeAttributes(
44
- this._trackingOptions.globalAttributes || {}
45
- );
46
- this._trackingOptions.globalAttributes = { ...this._globalAttributes };
47
- }
48
- async init({ context = {}, options = {} } = {}) {
49
- var _a;
50
- this.updateContext(context);
51
- this._trackingOptions = {
52
- ...DEFAULT_TRACKING_OPTIONS,
53
- ...this._trackingOptions,
54
- ...((_a = this._options) == null ? void 0 : _a.tracking) || {},
55
- ...options.tracking || {}
56
- };
57
- this._globalAttributes = sanitizeAttributes(
58
- this._trackingOptions.globalAttributes || {}
59
- );
60
- this._enabled = this._trackingOptions.enabled !== false;
61
- if (!this._enabled) {
62
- this._setReady();
63
- return this;
64
- }
65
- if (!isBrowserEnvironment()) {
66
- this._enabled = false;
67
- this._setReady();
68
- return this;
69
- }
70
- this._runtimeConfig = this._buildRuntimeConfig();
71
- this._trackingOptions.transports = this._runtimeConfig.transports;
72
- const hasCustomTransports = Array.isArray(this._runtimeConfig.transports) && this._runtimeConfig.transports.length > 0;
73
- if (!this._runtimeConfig.url && !hasCustomTransports) {
74
- console.warn("[TrackingService] Grafana Faro URL missing. Tracking will stay disabled.");
75
- this._enabled = false;
76
- this._setReady();
77
- return this;
78
- }
79
- if (this._initialized) {
80
- this._setReady();
81
- return this;
82
- }
83
- if (!this._setupPromise) {
84
- this._setupPromise = this._loadFaroClient(this._runtimeConfig);
85
- }
86
- await this._setupPromise;
87
- return this;
88
- }
89
- updateContext(context) {
90
- super.updateContext(context);
91
- const trackingContext = context == null ? void 0 : context.tracking;
92
- if (trackingContext) {
93
- if (Object.hasOwn(trackingContext, "user")) {
94
- this.setUser(trackingContext.user, { queue: false });
95
- }
96
- if (Object.hasOwn(trackingContext, "globalAttributes")) {
97
- this.setGlobalAttributes(trackingContext.globalAttributes);
98
- }
99
- }
100
- }
101
- configureTracking(trackingOptions = {}) {
102
- if (!trackingOptions || typeof trackingOptions !== "object") {
103
- return this;
104
- }
105
- this._trackingOptions = {
106
- ...this._trackingOptions,
107
- ...trackingOptions
108
- };
109
- if (Object.hasOwn(trackingOptions, "globalAttributes")) {
110
- this.setGlobalAttributes(trackingOptions.globalAttributes);
111
- }
112
- if (Object.hasOwn(trackingOptions, "user")) {
113
- this.setUser(trackingOptions.user);
114
- this._trackingOptions.user = trackingOptions.user;
115
- }
116
- return this;
117
- }
118
- configure(trackingOptions = {}) {
119
- return this.configureTracking(trackingOptions);
120
- }
121
- trackEvent(name, attributes, options = {}) {
122
- if (!name) {
123
- throw new Error("Event name is required for trackEvent");
124
- }
125
- const mergedAttributes = this._mergeAttributes(attributes);
126
- const eventOptions = options && typeof options === "object" ? options : {};
127
- const { domain, queue, ...restOptions } = eventOptions;
128
- const eventDomain = Object.hasOwn(eventOptions, "domain") ? domain : null;
129
- const invokeTracking = (client) => {
130
- const api = client == null ? void 0 : client.api;
131
- if (!(api == null ? void 0 : api.pushEvent)) {
132
- console.warn("[TrackingService] Faro pushEvent API not available");
133
- return;
134
- }
135
- api.pushEvent(name, mergedAttributes, eventDomain, restOptions);
136
- };
137
- if (Object.hasOwn(eventOptions, "queue")) {
138
- this._withClient(invokeTracking, { queue: Boolean(queue) });
139
- } else {
140
- this._withClient(invokeTracking);
141
- }
142
- }
143
- trackError(error, options = {}) {
144
- if (!error) {
145
- return;
146
- }
147
- const err = error instanceof Error ? error : new Error(
148
- typeof error === "string" ? error : "Unknown error captured by TrackingService"
149
- );
150
- const isContextOnly = options && typeof options === "object" && options !== null && !("context" in options || "type" in options || "stackFrames" in options || "skipDedupe" in options || "spanContext" in options || "timestampOverwriteMs" in options || "originalError" in options);
151
- const normalizedOptions = isContextOnly ? { context: options } : options || {};
152
- const mergedContext = this._mergeAttributes(normalizedOptions.context);
153
- const apiOptions = {
154
- ...normalizedOptions,
155
- ...Object.keys(mergedContext).length > 0 ? { context: mergedContext } : {}
156
- };
157
- const queueConfigured = Object.hasOwn(apiOptions, "queue");
158
- const { queue, ...errorOptions } = apiOptions;
159
- const invokeTracking = (client) => {
160
- const api = client == null ? void 0 : client.api;
161
- if (!(api == null ? void 0 : api.pushError)) {
162
- console.warn("[TrackingService] Faro pushError API not available");
163
- return;
164
- }
165
- api.pushError(err, errorOptions);
166
- };
167
- if (queueConfigured) {
168
- this._withClient(invokeTracking, { queue: Boolean(queue) });
169
- } else {
170
- this._withClient(invokeTracking);
171
- }
172
- }
173
- captureException(error, options = {}) {
174
- this.trackError(error, options);
175
- }
176
- logMessage(message, level = "info", context = null) {
177
- if (!message) {
178
- return;
179
- }
180
- const payload = Array.isArray(message) ? message : [message];
181
- const logContext = this._mergeAttributes(context);
182
- const severity = level || "info";
183
- const options = {
184
- level: severity,
185
- ...Object.keys(logContext).length > 0 ? { context: logContext } : {}
186
- };
187
- this._withClient((client) => {
188
- const api = client == null ? void 0 : client.api;
189
- if (!(api == null ? void 0 : api.pushLog)) {
190
- console.warn("[TrackingService] Faro pushLog API not available");
191
- return;
192
- }
193
- api.pushLog(payload, options);
194
- });
195
- }
196
- logDebug(message, context) {
197
- this.logMessage(message, "debug", context);
198
- }
199
- logInfo(message, context) {
200
- this.logMessage(message, "info", context);
201
- }
202
- logWarning(message, context) {
203
- this.logMessage(message, "warn", context);
204
- }
205
- logWarn(message, context) {
206
- this.logWarning(message, context);
207
- }
208
- logErrorMessage(message, context) {
209
- this.logMessage(message, "error", context);
210
- }
211
- logError(message, context) {
212
- this.logErrorMessage(message, context);
213
- }
214
- addBreadcrumb(message, attributes) {
215
- if (!message) {
216
- return;
217
- }
218
- const breadcrumbAttributes = {
219
- category: "custom",
220
- ...sanitizeAttributes(attributes || {}),
221
- message
222
- };
223
- this.trackEvent("breadcrumb", breadcrumbAttributes);
224
- }
225
- trackMeasurement(type, values, options = {}) {
226
- if (!type) {
227
- throw new Error("Measurement type is required for trackMeasurement");
228
- }
229
- if (values == null) {
230
- throw new Error("Measurement values are required for trackMeasurement");
231
- }
232
- const measurementValues = typeof values === "object" && !Array.isArray(values) ? sanitizeAttributes(values) : { value: Number(values) };
233
- if (typeof measurementValues.value === "number" && Number.isNaN(measurementValues.value)) {
234
- throw new Error("Measurement value must be a valid number");
235
- }
236
- const measurementOptions = options && typeof options === "object" ? options : {};
237
- const {
238
- attributes: measurementAttributesOption,
239
- context: measurementContextOption,
240
- queue,
241
- ...transportOptions
242
- } = measurementOptions;
243
- const hasGlobalAttributes = Object.keys(this._globalAttributes).length > 0;
244
- const attributePayload = measurementAttributesOption ? this._mergeAttributes(measurementAttributesOption) : hasGlobalAttributes ? { ...this._globalAttributes } : {};
245
- const payload = {
246
- type,
247
- values: measurementValues,
248
- ...attributePayload && Object.keys(attributePayload).length > 0 ? { attributes: attributePayload } : {}
249
- };
250
- const context = measurementContextOption ? this._mergeAttributes(measurementContextOption) : hasGlobalAttributes ? { ...this._globalAttributes } : {};
251
- const apiOptions = {
252
- ...transportOptions,
253
- ...Object.keys(context).length > 0 ? { context } : {}
254
- };
255
- const invokeTracking = (client) => {
256
- const api = client == null ? void 0 : client.api;
257
- if (!(api == null ? void 0 : api.pushMeasurement)) {
258
- console.warn("[TrackingService] Faro pushMeasurement API not available");
259
- return;
260
- }
261
- api.pushMeasurement(payload, apiOptions);
262
- };
263
- if (Object.hasOwn(measurementOptions, "queue")) {
264
- this._withClient(invokeTracking, { queue: Boolean(queue) });
265
- } else {
266
- this._withClient(invokeTracking);
267
- }
268
- }
269
- trackView(name, attributes) {
270
- if (!name) {
271
- throw new Error("View name is required for trackView");
272
- }
273
- const viewAttributes = sanitizeAttributes(attributes || {});
274
- const merged = this._mergeAttributes(viewAttributes);
275
- this._withClient((client) => {
276
- const api = client == null ? void 0 : client.api;
277
- if (!(api == null ? void 0 : api.setView)) {
278
- console.warn("[TrackingService] Faro setView API not available");
279
- return;
280
- }
281
- api.setView({
282
- name,
283
- ...merged
284
- });
285
- });
286
- }
287
- setUser(user, options = {}) {
288
- if (user == null) {
289
- this.clearUser();
290
- return;
291
- }
292
- if (typeof user !== "object") {
293
- throw new Error("User must be an object");
294
- }
295
- const userData = sanitizeAttributes(user);
296
- const queueConfigured = options && typeof options === "object" && Object.hasOwn(options, "queue");
297
- const invokeTracking = (client) => {
298
- const api = client == null ? void 0 : client.api;
299
- if (!(api == null ? void 0 : api.setUser)) {
300
- console.warn("[TrackingService] Faro setUser API not available");
301
- return;
302
- }
303
- api.setUser(userData);
304
- };
305
- if (queueConfigured) {
306
- this._withClient(invokeTracking, { queue: Boolean(options.queue) });
307
- } else {
308
- this._withClient(invokeTracking);
309
- }
310
- }
311
- clearUser() {
312
- this._withClient((client) => {
313
- const api = client == null ? void 0 : client.api;
314
- if (api == null ? void 0 : api.setUser) {
315
- api.setUser(null);
316
- } else {
317
- console.warn("[TrackingService] Faro setUser API not available");
318
- }
319
- });
320
- }
321
- setSession(session, options = {}) {
322
- if (session == null) {
323
- this.clearSession();
324
- return;
325
- }
326
- if (typeof session !== "object") {
327
- throw new Error("Session must be an object");
328
- }
329
- const sessionData = sanitizeAttributes(session);
330
- const queueConfigured = Object.hasOwn(options, "queue");
331
- const { queue, ...sessionOptions } = options;
332
- const invokeTracking = (client) => {
333
- const api = client == null ? void 0 : client.api;
334
- if (!(api == null ? void 0 : api.setSession)) {
335
- console.warn("[TrackingService] Faro setSession API not available");
336
- return;
337
- }
338
- api.setSession(sessionData, sessionOptions);
339
- };
340
- if (queueConfigured) {
341
- this._withClient(invokeTracking, { queue: Boolean(queue) });
342
- } else {
343
- this._withClient(invokeTracking);
344
- }
345
- }
346
- clearSession() {
347
- this._withClient((client) => {
348
- const api = client == null ? void 0 : client.api;
349
- if (api == null ? void 0 : api.setSession) {
350
- api.setSession(null);
351
- } else {
352
- console.warn("[TrackingService] Faro setSession API not available");
353
- }
354
- });
355
- }
356
- setGlobalAttributes(attributes) {
357
- if (attributes == null) {
358
- this._globalAttributes = {};
359
- this._trackingOptions.globalAttributes = {};
360
- return;
361
- }
362
- if (typeof attributes !== "object") {
363
- throw new Error("Global attributes must be an object");
364
- }
365
- const sanitized = sanitizeAttributes(attributes);
366
- this._globalAttributes = sanitized;
367
- this._trackingOptions.globalAttributes = sanitized;
368
- }
369
- setGlobalAttribute(key, value) {
370
- if (!key) {
371
- throw new Error("Global attribute key is required");
372
- }
373
- const sanitized = sanitizeAttributes({ [key]: value });
374
- this._globalAttributes = {
375
- ...this._globalAttributes,
376
- ...sanitized
377
- };
378
- this._trackingOptions.globalAttributes = { ...this._globalAttributes };
379
- }
380
- removeGlobalAttribute(key) {
381
- if (!key) {
382
- throw new Error("Global attribute key is required to remove it");
383
- }
384
- if (Object.hasOwn(this._globalAttributes, key)) {
385
- const rest = { ...this._globalAttributes };
386
- delete rest[key];
387
- this._globalAttributes = rest;
388
- this._trackingOptions.globalAttributes = rest;
389
- }
390
- }
391
- _mergeAttributes(attributes) {
392
- const hasInput = attributes && typeof attributes === "object";
393
- const sanitized = hasInput ? sanitizeAttributes(attributes) : {};
394
- if (!this._globalAttributes || Object.keys(this._globalAttributes).length === 0) {
395
- return sanitized;
396
- }
397
- return {
398
- ...this._globalAttributes,
399
- ...sanitized
400
- };
401
- }
402
- flushQueue() {
403
- if (!this._queue.length) {
404
- return;
405
- }
406
- const queue = [...this._queue];
407
- this._queue.length = 0;
408
- if (!this._faroClient) {
409
- return;
410
- }
411
- queue.forEach((callback) => {
412
- try {
413
- callback(this._faroClient);
414
- } catch (error) {
415
- console.error("[TrackingService] Failed to flush queued tracking call", error);
416
- }
417
- });
418
- }
419
- getClient() {
420
- return this._faroClient;
421
- }
422
- isEnabled() {
423
- return this._enabled && Boolean(this._faroClient);
424
- }
425
- isInitialized() {
426
- return this._initialized;
427
- }
428
- destroy() {
429
- var _a;
430
- this._queue.length = 0;
431
- if ((_a = this._faroClient) == null ? void 0 : _a.destroy) {
432
- try {
433
- this._faroClient.destroy();
434
- } catch (error) {
435
- console.warn("[TrackingService] Failed to destroy Faro client cleanly", error);
436
- }
437
- }
438
- if (isBrowserEnvironment() && window.symbols && window.symbols.faro === this._faroClient) {
439
- delete window.symbols.faro;
440
- }
441
- this._faroClient = null;
442
- this._initialized = false;
443
- this._setupPromise = null;
444
- this._setReady(false);
445
- this._enabled = this._trackingOptions.enabled !== false;
446
- }
447
- _buildRuntimeConfig() {
448
- var _a, _b;
449
- const contextConfig = ((_a = this._context) == null ? void 0 : _a.tracking) || {};
450
- const merged = {
451
- ...DEFAULT_TRACKING_OPTIONS,
452
- ...this._trackingOptions,
453
- ...contextConfig
454
- };
455
- const transportList = [];
456
- if (Array.isArray(merged.transports)) {
457
- transportList.push(...merged.transports.filter(Boolean));
458
- } else if (merged.transport) {
459
- transportList.push(merged.transport);
460
- }
461
- const configuredUrl = merged.url || contextConfig.url || this._trackingOptions.url;
462
- let url = configuredUrl;
463
- if (!url && transportList.length === 0) {
464
- url = environment.grafanaUrl;
465
- }
466
- const appName = merged.appName || contextConfig.appName || this._trackingOptions.appName || environment.grafanaAppName || "Symbols Platform";
467
- const appVersion = merged.appVersion || contextConfig.appVersion || this._trackingOptions.appVersion || ((_b = this._context) == null ? void 0 : _b.appVersion);
468
- const environmentName = merged.environment || contextConfig.environment || this._trackingOptions.environment || this._resolveEnvironmentName();
469
- const contextGlobalAttributes = sanitizeAttributes(contextConfig.globalAttributes || {});
470
- const globalAttributes = sanitizeAttributes({
471
- ...this._globalAttributes,
472
- ...contextGlobalAttributes
473
- });
474
- const runtimeConfig = {
475
- url,
476
- appName,
477
- appVersion,
478
- environment: environmentName,
479
- globalAttributes,
480
- sessionTracking: merged.sessionTracking !== false,
481
- enableTracing: merged.enableTracing !== false,
482
- user: merged.user,
483
- maxQueueSize: typeof merged.maxQueueSize === "number" && merged.maxQueueSize > 0 ? merged.maxQueueSize : DEFAULT_MAX_QUEUE_SIZE
484
- };
485
- if (typeof merged.isolate === "boolean") {
486
- runtimeConfig.isolate = merged.isolate;
487
- }
488
- if (Array.isArray(merged.instrumentations)) {
489
- runtimeConfig.instrumentations = merged.instrumentations;
490
- }
491
- if (typeof merged.instrumentationsFactory === "function") {
492
- runtimeConfig.instrumentationsFactory = merged.instrumentationsFactory;
493
- }
494
- if (merged.webInstrumentationOptions != null) {
495
- runtimeConfig.webInstrumentationOptions = merged.webInstrumentationOptions;
496
- }
497
- if (transportList.length > 0) {
498
- runtimeConfig.transports = transportList;
499
- }
500
- return runtimeConfig;
501
- }
502
- _resolveEnvironmentName() {
503
- if (environment.isProduction) {
504
- return "production";
505
- }
506
- if (environment.isStaging) {
507
- return "staging";
508
- }
509
- if (environment.isTesting) {
510
- return "testing";
511
- }
512
- if (environment.isDevelopment) {
513
- return "development";
514
- }
515
- return process.env.SYMBOLS_APP_ENV || process.env.NODE_ENV || "development";
516
- }
517
- async _loadFaroClient(runtimeConfig) {
518
- try {
519
- const tracingImport = runtimeConfig.enableTracing === false ? Promise.resolve(null) : import("@grafana/faro-web-tracing").catch((error) => {
520
- console.warn("[TrackingService] Tracing instrumentation failed to load:", error);
521
- return null;
522
- });
523
- const [{ initializeFaro, getWebInstrumentations }, tracingModule] = await Promise.all([
524
- import("@grafana/faro-web-sdk"),
525
- tracingImport
526
- ]);
527
- const TracingInstrumentation = tracingModule == null ? void 0 : tracingModule.TracingInstrumentation;
528
- const instrumentations = await this._resolveInstrumentations({
529
- runtimeConfig,
530
- getWebInstrumentations,
531
- TracingInstrumentation
532
- });
533
- const initializeOptions = {
534
- app: {
535
- name: runtimeConfig.appName,
536
- version: runtimeConfig.appVersion,
537
- environment: runtimeConfig.environment
538
- },
539
- instrumentations,
540
- globalAttributes: sanitizeAttributes(runtimeConfig.globalAttributes)
541
- };
542
- if (runtimeConfig.url) {
543
- initializeOptions.url = runtimeConfig.url;
544
- }
545
- if (Array.isArray(runtimeConfig.transports) && runtimeConfig.transports.length > 0) {
546
- initializeOptions.transports = runtimeConfig.transports;
547
- }
548
- if (runtimeConfig.sessionTracking === false) {
549
- initializeOptions.sessionTracking = false;
550
- }
551
- if (runtimeConfig.isolate === true) {
552
- initializeOptions.isolate = true;
553
- }
554
- this._faroClient = initializeFaro(initializeOptions);
555
- if (runtimeConfig.user) {
556
- this.setUser(runtimeConfig.user, { queue: false });
557
- }
558
- if (isBrowserEnvironment()) {
559
- window.symbols ||= {};
560
- window.symbols.faro = this._faroClient;
561
- }
562
- this._initialized = true;
563
- this._setReady();
564
- this.flushQueue();
565
- } catch (error) {
566
- this._enabled = false;
567
- this._setError(error);
568
- this._ready = true;
569
- console.error("[TrackingService] Failed to initialize Grafana Faro client:", error);
570
- } finally {
571
- this._setupPromise = null;
572
- }
573
- }
574
- async _resolveInstrumentations({
575
- runtimeConfig,
576
- getWebInstrumentations,
577
- TracingInstrumentation
578
- }) {
579
- if (Array.isArray(runtimeConfig.instrumentations)) {
580
- return runtimeConfig.instrumentations;
581
- }
582
- if (typeof runtimeConfig.instrumentationsFactory === "function") {
583
- try {
584
- const instrumentations = await runtimeConfig.instrumentationsFactory({
585
- runtimeConfig,
586
- getWebInstrumentations,
587
- TracingInstrumentation
588
- });
589
- if (Array.isArray(instrumentations)) {
590
- return instrumentations;
591
- }
592
- } catch (error) {
593
- console.warn("[TrackingService] Custom instrumentation factory failed:", error);
594
- }
595
- }
596
- const instrumentationOptions = runtimeConfig.webInstrumentationOptions == null ? {} : runtimeConfig.webInstrumentationOptions;
597
- const defaultInstrumentations = getWebInstrumentations(instrumentationOptions);
598
- if (runtimeConfig.enableTracing !== false && TracingInstrumentation) {
599
- try {
600
- defaultInstrumentations.push(new TracingInstrumentation());
601
- } catch (error) {
602
- console.warn("[TrackingService] Failed to instantiate tracing instrumentation:", error);
603
- }
604
- }
605
- return defaultInstrumentations;
606
- }
607
- _withClient(callback, options = {}) {
608
- if (!this._enabled) {
609
- return null;
610
- }
611
- if (this._faroClient) {
612
- try {
613
- return callback(this._faroClient);
614
- } catch (error) {
615
- console.error("[TrackingService] Tracking callback failed:", error);
616
- return null;
617
- }
618
- }
619
- if (options.queue === false) {
620
- return null;
621
- }
622
- const queueLimit = this._runtimeConfig.maxQueueSize ?? this._trackingOptions.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE;
623
- if (this._queue.length >= queueLimit) {
624
- this._queue.shift();
625
- }
626
- this._queue.push(callback);
627
- return null;
628
- }
629
- }
630
- export {
631
- TrackingService
632
- };