@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,853 +0,0 @@
1
- import { BaseService } from './BaseService.js'
2
- import environment from '../config/environment.js'
3
-
4
- const DEFAULT_MAX_QUEUE_SIZE = 100
5
-
6
- const isBrowserEnvironment = () => typeof window !== 'undefined'
7
-
8
- const DEFAULT_TRACKING_OPTIONS = {
9
- // Enabled by default unless explicitly disabled via SDK options or runtime config
10
- enabled: true,
11
- sessionTracking: true,
12
- enableTracing: true,
13
- maxQueueSize: DEFAULT_MAX_QUEUE_SIZE,
14
- transports: null,
15
- instrumentations: null,
16
- instrumentationsFactory: null,
17
- webInstrumentationOptions: null,
18
- globalAttributes: {},
19
- transport: null,
20
- user: null
21
- }
22
-
23
- const sanitizeAttributes = (value) => {
24
- if (!value || typeof value !== 'object') {
25
- return {}
26
- }
27
-
28
- try {
29
- return JSON.parse(JSON.stringify(value))
30
- } catch (error) {
31
- console.warn('[TrackingService] Failed to sanitize attributes:', error)
32
- return { ...value }
33
- }
34
- }
35
-
36
- export class TrackingService extends BaseService {
37
- constructor ({ context, options } = {}) {
38
- super({ context, options })
39
-
40
- this._faroClient = null
41
- this._queue = []
42
- this._initialized = false
43
- this._enabled = DEFAULT_TRACKING_OPTIONS.enabled
44
- this._runtimeConfig = {}
45
- this._setupPromise = null
46
-
47
- this._trackingOptions = {
48
- ...DEFAULT_TRACKING_OPTIONS,
49
- ...(options?.tracking || {})
50
- }
51
-
52
- this._globalAttributes = sanitizeAttributes(
53
- this._trackingOptions.globalAttributes || {}
54
- )
55
- this._trackingOptions.globalAttributes = { ...this._globalAttributes }
56
- }
57
-
58
- async init ({ context = {}, options = {} } = {}) {
59
- this.updateContext(context)
60
-
61
- // Merge tracking options from constructor, SDK options and runtime overrides
62
- this._trackingOptions = {
63
- ...DEFAULT_TRACKING_OPTIONS,
64
- ...this._trackingOptions,
65
- ...(this._options?.tracking || {}),
66
- ...(options.tracking || {})
67
- }
68
-
69
- this._globalAttributes = sanitizeAttributes(
70
- this._trackingOptions.globalAttributes || {}
71
- )
72
-
73
- this._enabled = this._trackingOptions.enabled !== false
74
-
75
- if (!this._enabled) {
76
- this._setReady()
77
- return this
78
- }
79
-
80
- if (!isBrowserEnvironment()) {
81
- this._enabled = false
82
- this._setReady()
83
- return this
84
- }
85
-
86
- this._runtimeConfig = this._buildRuntimeConfig()
87
- this._trackingOptions.transports = this._runtimeConfig.transports
88
-
89
- const hasCustomTransports =
90
- Array.isArray(this._runtimeConfig.transports) &&
91
- this._runtimeConfig.transports.length > 0
92
-
93
- if (!this._runtimeConfig.url && !hasCustomTransports) {
94
- console.warn('[TrackingService] Grafana Faro URL missing. Tracking will stay disabled.')
95
- this._enabled = false
96
- this._setReady()
97
- return this
98
- }
99
-
100
- if (this._initialized) {
101
- this._setReady()
102
- return this
103
- }
104
-
105
- if (!this._setupPromise) {
106
- this._setupPromise = this._loadFaroClient(this._runtimeConfig)
107
- }
108
-
109
- await this._setupPromise
110
-
111
- return this
112
- }
113
-
114
- updateContext (context) {
115
- super.updateContext(context)
116
-
117
- const trackingContext = context?.tracking
118
-
119
- if (trackingContext) {
120
- if (Object.hasOwn(trackingContext, 'user')) {
121
- this.setUser(trackingContext.user, { queue: false })
122
- }
123
-
124
- if (Object.hasOwn(trackingContext, 'globalAttributes')) {
125
- this.setGlobalAttributes(trackingContext.globalAttributes)
126
- }
127
- }
128
- }
129
-
130
- configureTracking (trackingOptions = {}) {
131
- if (!trackingOptions || typeof trackingOptions !== 'object') {
132
- return this
133
- }
134
-
135
- this._trackingOptions = {
136
- ...this._trackingOptions,
137
- ...trackingOptions
138
- }
139
-
140
- if (Object.hasOwn(trackingOptions, 'globalAttributes')) {
141
- this.setGlobalAttributes(trackingOptions.globalAttributes)
142
- }
143
-
144
- if (Object.hasOwn(trackingOptions, 'user')) {
145
- this.setUser(trackingOptions.user)
146
- this._trackingOptions.user = trackingOptions.user
147
- }
148
-
149
- return this
150
- }
151
-
152
- configure (trackingOptions = {}) {
153
- return this.configureTracking(trackingOptions)
154
- }
155
-
156
- trackEvent (name, attributes, options = {}) {
157
- if (!name) {
158
- throw new Error('Event name is required for trackEvent')
159
- }
160
-
161
- const mergedAttributes = this._mergeAttributes(attributes)
162
- const eventOptions = options && typeof options === 'object' ? options : {}
163
- const { domain, queue, ...restOptions } = eventOptions
164
- const eventDomain = Object.hasOwn(eventOptions, 'domain')
165
- ? domain
166
- : null
167
-
168
- const invokeTracking = client => {
169
- const api = client?.api
170
- if (!api?.pushEvent) {
171
- console.warn('[TrackingService] Faro pushEvent API not available')
172
- return
173
- }
174
-
175
- api.pushEvent(name, mergedAttributes, eventDomain, restOptions)
176
- }
177
-
178
- if (Object.hasOwn(eventOptions, 'queue')) {
179
- this._withClient(invokeTracking, { queue: Boolean(queue) })
180
- } else {
181
- this._withClient(invokeTracking)
182
- }
183
- }
184
-
185
- trackError (error, options = {}) {
186
- if (!error) {
187
- return
188
- }
189
-
190
- const err =
191
- error instanceof Error
192
- ? error
193
- : new Error(
194
- typeof error === 'string' ? error : 'Unknown error captured by TrackingService'
195
- )
196
-
197
- const isContextOnly =
198
- options &&
199
- typeof options === 'object' &&
200
- options !== null &&
201
- !(
202
- 'context' in options ||
203
- 'type' in options ||
204
- 'stackFrames' in options ||
205
- 'skipDedupe' in options ||
206
- 'spanContext' in options ||
207
- 'timestampOverwriteMs' in options ||
208
- 'originalError' in options
209
- )
210
-
211
- const normalizedOptions = isContextOnly
212
- ? { context: options }
213
- : options || {}
214
-
215
- const mergedContext = this._mergeAttributes(normalizedOptions.context)
216
- const apiOptions = {
217
- ...normalizedOptions,
218
- ...(Object.keys(mergedContext).length > 0 ? { context: mergedContext } : {})
219
- }
220
-
221
- const queueConfigured = Object.hasOwn(apiOptions, 'queue')
222
- const { queue, ...errorOptions } = apiOptions
223
-
224
- const invokeTracking = client => {
225
- const api = client?.api
226
- if (!api?.pushError) {
227
- console.warn('[TrackingService] Faro pushError API not available')
228
- return
229
- }
230
-
231
- api.pushError(err, errorOptions)
232
- }
233
-
234
- if (queueConfigured) {
235
- this._withClient(invokeTracking, { queue: Boolean(queue) })
236
- } else {
237
- this._withClient(invokeTracking)
238
- }
239
- }
240
-
241
- captureException (error, options = {}) {
242
- this.trackError(error, options)
243
- }
244
-
245
- logMessage (message, level = 'info', context = null) {
246
- if (!message) {
247
- return
248
- }
249
-
250
- const payload = Array.isArray(message) ? message : [message]
251
- const logContext = this._mergeAttributes(context)
252
- const severity = level || 'info'
253
-
254
- const options = {
255
- level: severity,
256
- ...(Object.keys(logContext).length > 0 ? { context: logContext } : {})
257
- }
258
-
259
- this._withClient(client => {
260
- const api = client?.api
261
- if (!api?.pushLog) {
262
- console.warn('[TrackingService] Faro pushLog API not available')
263
- return
264
- }
265
-
266
- api.pushLog(payload, options)
267
- })
268
- }
269
-
270
- logDebug (message, context) {
271
- this.logMessage(message, 'debug', context)
272
- }
273
-
274
- logInfo (message, context) {
275
- this.logMessage(message, 'info', context)
276
- }
277
-
278
- logWarning (message, context) {
279
- this.logMessage(message, 'warn', context)
280
- }
281
-
282
- logWarn (message, context) {
283
- this.logWarning(message, context)
284
- }
285
-
286
- logErrorMessage (message, context) {
287
- this.logMessage(message, 'error', context)
288
- }
289
-
290
- logError (message, context) {
291
- this.logErrorMessage(message, context)
292
- }
293
-
294
- addBreadcrumb (message, attributes) {
295
- if (!message) {
296
- return
297
- }
298
-
299
- const breadcrumbAttributes = {
300
- category: 'custom',
301
- ...sanitizeAttributes(attributes || {}),
302
- message
303
- }
304
-
305
- this.trackEvent('breadcrumb', breadcrumbAttributes)
306
- }
307
-
308
- trackMeasurement (type, values, options = {}) {
309
- if (!type) {
310
- throw new Error('Measurement type is required for trackMeasurement')
311
- }
312
-
313
- if (values == null) {
314
- throw new Error('Measurement values are required for trackMeasurement')
315
- }
316
-
317
- const measurementValues =
318
- typeof values === 'object' && !Array.isArray(values)
319
- ? sanitizeAttributes(values)
320
- : { value: Number(values) }
321
-
322
- if (typeof measurementValues.value === 'number' && Number.isNaN(measurementValues.value)) {
323
- throw new Error('Measurement value must be a valid number')
324
- }
325
-
326
- const measurementOptions = options && typeof options === 'object' ? options : {}
327
- const {
328
- attributes: measurementAttributesOption,
329
- context: measurementContextOption,
330
- queue,
331
- ...transportOptions
332
- } = measurementOptions
333
-
334
- const hasGlobalAttributes = Object.keys(this._globalAttributes).length > 0
335
-
336
- const attributePayload = measurementAttributesOption
337
- ? this._mergeAttributes(measurementAttributesOption)
338
- : hasGlobalAttributes
339
- ? { ...this._globalAttributes }
340
- : {}
341
-
342
- const payload = {
343
- type,
344
- values: measurementValues,
345
- ...(attributePayload && Object.keys(attributePayload).length > 0
346
- ? { attributes: attributePayload }
347
- : {})
348
- }
349
-
350
- const context = measurementContextOption
351
- ? this._mergeAttributes(measurementContextOption)
352
- : hasGlobalAttributes
353
- ? { ...this._globalAttributes }
354
- : {}
355
- const apiOptions = {
356
- ...transportOptions,
357
- ...(Object.keys(context).length > 0 ? { context } : {})
358
- }
359
-
360
- const invokeTracking = client => {
361
- const api = client?.api
362
- if (!api?.pushMeasurement) {
363
- console.warn('[TrackingService] Faro pushMeasurement API not available')
364
- return
365
- }
366
-
367
- api.pushMeasurement(payload, apiOptions)
368
- }
369
-
370
- if (Object.hasOwn(measurementOptions, 'queue')) {
371
- this._withClient(invokeTracking, { queue: Boolean(queue) })
372
- } else {
373
- this._withClient(invokeTracking)
374
- }
375
- }
376
-
377
- trackView (name, attributes) {
378
- if (!name) {
379
- throw new Error('View name is required for trackView')
380
- }
381
-
382
- const viewAttributes = sanitizeAttributes(attributes || {})
383
- const merged = this._mergeAttributes(viewAttributes)
384
-
385
- this._withClient(client => {
386
- const api = client?.api
387
- if (!api?.setView) {
388
- console.warn('[TrackingService] Faro setView API not available')
389
- return
390
- }
391
-
392
- api.setView({
393
- name,
394
- ...merged
395
- })
396
- })
397
- }
398
-
399
- setUser (user, options = {}) {
400
- if (user == null) {
401
- this.clearUser()
402
- return
403
- }
404
-
405
- if (typeof user !== 'object') {
406
- throw new Error('User must be an object')
407
- }
408
-
409
- const userData = sanitizeAttributes(user)
410
-
411
- const queueConfigured =
412
- options && typeof options === 'object' && Object.hasOwn(options, 'queue')
413
-
414
- const invokeTracking = client => {
415
- const api = client?.api
416
- if (!api?.setUser) {
417
- console.warn('[TrackingService] Faro setUser API not available')
418
- return
419
- }
420
-
421
- api.setUser(userData)
422
- }
423
-
424
- if (queueConfigured) {
425
- this._withClient(invokeTracking, { queue: Boolean(options.queue) })
426
- } else {
427
- this._withClient(invokeTracking)
428
- }
429
- }
430
-
431
- clearUser () {
432
- this._withClient(client => {
433
- const api = client?.api
434
- if (api?.setUser) {
435
- api.setUser(null)
436
- } else {
437
- console.warn('[TrackingService] Faro setUser API not available')
438
- }
439
- })
440
- }
441
-
442
- setSession (session, options = {}) {
443
- if (session == null) {
444
- this.clearSession()
445
- return
446
- }
447
-
448
- if (typeof session !== 'object') {
449
- throw new Error('Session must be an object')
450
- }
451
-
452
- const sessionData = sanitizeAttributes(session)
453
- const queueConfigured = Object.hasOwn(options, 'queue')
454
- const { queue, ...sessionOptions } = options
455
-
456
- const invokeTracking = client => {
457
- const api = client?.api
458
- if (!api?.setSession) {
459
- console.warn('[TrackingService] Faro setSession API not available')
460
- return
461
- }
462
-
463
- api.setSession(sessionData, sessionOptions)
464
- }
465
-
466
- if (queueConfigured) {
467
- this._withClient(invokeTracking, { queue: Boolean(queue) })
468
- } else {
469
- this._withClient(invokeTracking)
470
- }
471
- }
472
-
473
- clearSession () {
474
- this._withClient(client => {
475
- const api = client?.api
476
- if (api?.setSession) {
477
- api.setSession(null)
478
- } else {
479
- console.warn('[TrackingService] Faro setSession API not available')
480
- }
481
- })
482
- }
483
-
484
- setGlobalAttributes (attributes) {
485
- if (attributes == null) {
486
- this._globalAttributes = {}
487
- this._trackingOptions.globalAttributes = {}
488
- return
489
- }
490
-
491
- if (typeof attributes !== 'object') {
492
- throw new Error('Global attributes must be an object')
493
- }
494
-
495
- const sanitized = sanitizeAttributes(attributes)
496
- this._globalAttributes = sanitized
497
- this._trackingOptions.globalAttributes = sanitized
498
- }
499
-
500
- setGlobalAttribute (key, value) {
501
- if (!key) {
502
- throw new Error('Global attribute key is required')
503
- }
504
-
505
- const sanitized = sanitizeAttributes({ [key]: value })
506
- this._globalAttributes = {
507
- ...this._globalAttributes,
508
- ...sanitized
509
- }
510
- this._trackingOptions.globalAttributes = { ...this._globalAttributes }
511
- }
512
-
513
- removeGlobalAttribute (key) {
514
- if (!key) {
515
- throw new Error('Global attribute key is required to remove it')
516
- }
517
-
518
- if (Object.hasOwn(this._globalAttributes, key)) {
519
- const rest = { ...this._globalAttributes }
520
- delete rest[key]
521
- this._globalAttributes = rest
522
- this._trackingOptions.globalAttributes = rest
523
- }
524
- }
525
-
526
- _mergeAttributes (attributes) {
527
- const hasInput = attributes && typeof attributes === 'object'
528
- const sanitized = hasInput ? sanitizeAttributes(attributes) : {}
529
-
530
- if (!this._globalAttributes || Object.keys(this._globalAttributes).length === 0) {
531
- return sanitized
532
- }
533
-
534
- return {
535
- ...this._globalAttributes,
536
- ...sanitized
537
- }
538
- }
539
-
540
- flushQueue () {
541
- if (!this._queue.length) {
542
- return
543
- }
544
-
545
- const queue = [...this._queue]
546
- this._queue.length = 0
547
-
548
- if (!this._faroClient) {
549
- return
550
- }
551
-
552
- queue.forEach(callback => {
553
- try {
554
- callback(this._faroClient)
555
- } catch (error) {
556
- console.error('[TrackingService] Failed to flush queued tracking call', error)
557
- }
558
- })
559
- }
560
-
561
- getClient () {
562
- return this._faroClient
563
- }
564
-
565
- isEnabled () {
566
- return this._enabled && Boolean(this._faroClient)
567
- }
568
-
569
- isInitialized () {
570
- return this._initialized
571
- }
572
-
573
- destroy () {
574
- this._queue.length = 0
575
-
576
- if (this._faroClient?.destroy) {
577
- try {
578
- this._faroClient.destroy()
579
- } catch (error) {
580
- console.warn('[TrackingService] Failed to destroy Faro client cleanly', error)
581
- }
582
- }
583
-
584
- if (isBrowserEnvironment() && window.symbols && window.symbols.faro === this._faroClient) {
585
- delete window.symbols.faro
586
- }
587
-
588
- this._faroClient = null
589
- this._initialized = false
590
- this._setupPromise = null
591
- this._setReady(false)
592
- this._enabled = this._trackingOptions.enabled !== false
593
- }
594
-
595
- _buildRuntimeConfig () {
596
- const contextConfig = this._context?.tracking || {}
597
- const merged = {
598
- ...DEFAULT_TRACKING_OPTIONS,
599
- ...this._trackingOptions,
600
- ...contextConfig
601
- }
602
-
603
- const transportList = []
604
-
605
- if (Array.isArray(merged.transports)) {
606
- transportList.push(...merged.transports.filter(Boolean))
607
- } else if (merged.transport) {
608
- transportList.push(merged.transport)
609
- }
610
-
611
- const configuredUrl =
612
- merged.url ||
613
- contextConfig.url ||
614
- this._trackingOptions.url
615
-
616
- let url = configuredUrl
617
-
618
- if (!url && transportList.length === 0) {
619
- url = environment.grafanaUrl
620
- }
621
-
622
- const appName =
623
- merged.appName ||
624
- contextConfig.appName ||
625
- this._trackingOptions.appName ||
626
- environment.grafanaAppName ||
627
- 'Symbols Platform'
628
-
629
- const appVersion =
630
- merged.appVersion ||
631
- contextConfig.appVersion ||
632
- this._trackingOptions.appVersion ||
633
- this._context?.appVersion
634
-
635
- const environmentName =
636
- merged.environment ||
637
- contextConfig.environment ||
638
- this._trackingOptions.environment ||
639
- this._resolveEnvironmentName()
640
-
641
- const contextGlobalAttributes = sanitizeAttributes(contextConfig.globalAttributes || {})
642
- const globalAttributes = sanitizeAttributes({
643
- ...this._globalAttributes,
644
- ...contextGlobalAttributes
645
- })
646
-
647
- const runtimeConfig = {
648
- url,
649
- appName,
650
- appVersion,
651
- environment: environmentName,
652
- globalAttributes,
653
- sessionTracking: merged.sessionTracking !== false,
654
- enableTracing: merged.enableTracing !== false,
655
- user: merged.user,
656
- maxQueueSize:
657
- typeof merged.maxQueueSize === 'number' && merged.maxQueueSize > 0
658
- ? merged.maxQueueSize
659
- : DEFAULT_MAX_QUEUE_SIZE
660
- }
661
-
662
- if (typeof merged.isolate === 'boolean') {
663
- runtimeConfig.isolate = merged.isolate
664
- }
665
-
666
- if (Array.isArray(merged.instrumentations)) {
667
- runtimeConfig.instrumentations = merged.instrumentations
668
- }
669
-
670
- if (typeof merged.instrumentationsFactory === 'function') {
671
- runtimeConfig.instrumentationsFactory = merged.instrumentationsFactory
672
- }
673
-
674
- if (merged.webInstrumentationOptions != null) {
675
- runtimeConfig.webInstrumentationOptions = merged.webInstrumentationOptions
676
- }
677
-
678
- if (transportList.length > 0) {
679
- runtimeConfig.transports = transportList
680
- }
681
-
682
- return runtimeConfig
683
- }
684
-
685
- _resolveEnvironmentName () {
686
- if (environment.isProduction) {
687
- return 'production'
688
- }
689
-
690
- if (environment.isStaging) {
691
- return 'staging'
692
- }
693
-
694
- if (environment.isTesting) {
695
- return 'testing'
696
- }
697
-
698
- if (environment.isDevelopment) {
699
- return 'development'
700
- }
701
-
702
- return process.env.SYMBOLS_APP_ENV || process.env.NODE_ENV || 'development'
703
- }
704
-
705
- async _loadFaroClient (runtimeConfig) {
706
- try {
707
- const tracingImport = runtimeConfig.enableTracing === false
708
- ? Promise.resolve(null)
709
- : import('@grafana/faro-web-tracing').catch(error => {
710
- console.warn('[TrackingService] Tracing instrumentation failed to load:', error)
711
- return null
712
- })
713
-
714
- const [{ initializeFaro, getWebInstrumentations }, tracingModule] = await Promise.all([
715
- import('@grafana/faro-web-sdk'),
716
- tracingImport
717
- ])
718
-
719
- const TracingInstrumentation = tracingModule?.TracingInstrumentation
720
-
721
- const instrumentations = await this._resolveInstrumentations({
722
- runtimeConfig,
723
- getWebInstrumentations,
724
- TracingInstrumentation
725
- })
726
-
727
- const initializeOptions = {
728
- app: {
729
- name: runtimeConfig.appName,
730
- version: runtimeConfig.appVersion,
731
- environment: runtimeConfig.environment
732
- },
733
- instrumentations,
734
- globalAttributes: sanitizeAttributes(runtimeConfig.globalAttributes)
735
- }
736
-
737
- if (runtimeConfig.url) {
738
- initializeOptions.url = runtimeConfig.url
739
- }
740
-
741
- if (Array.isArray(runtimeConfig.transports) && runtimeConfig.transports.length > 0) {
742
- initializeOptions.transports = runtimeConfig.transports
743
- }
744
-
745
- if (runtimeConfig.sessionTracking === false) {
746
- initializeOptions.sessionTracking = false
747
- }
748
-
749
- if (runtimeConfig.isolate === true) {
750
- initializeOptions.isolate = true
751
- }
752
-
753
- this._faroClient = initializeFaro(initializeOptions)
754
-
755
- if (runtimeConfig.user) {
756
- this.setUser(runtimeConfig.user, { queue: false })
757
- }
758
-
759
- if (isBrowserEnvironment()) {
760
- window.symbols ||= {}
761
- window.symbols.faro = this._faroClient
762
- }
763
-
764
- this._initialized = true
765
- this._setReady()
766
- this.flushQueue()
767
- } catch (error) {
768
- this._enabled = false
769
- this._setError(error)
770
- this._ready = true
771
- console.error('[TrackingService] Failed to initialize Grafana Faro client:', error)
772
- } finally {
773
- this._setupPromise = null
774
- }
775
- }
776
-
777
- async _resolveInstrumentations ({
778
- runtimeConfig,
779
- getWebInstrumentations,
780
- TracingInstrumentation
781
- }) {
782
- if (Array.isArray(runtimeConfig.instrumentations)) {
783
- return runtimeConfig.instrumentations
784
- }
785
-
786
- if (typeof runtimeConfig.instrumentationsFactory === 'function') {
787
- try {
788
- const instrumentations = await runtimeConfig.instrumentationsFactory({
789
- runtimeConfig,
790
- getWebInstrumentations,
791
- TracingInstrumentation
792
- })
793
-
794
- if (Array.isArray(instrumentations)) {
795
- return instrumentations
796
- }
797
- } catch (error) {
798
- console.warn('[TrackingService] Custom instrumentation factory failed:', error)
799
- }
800
- }
801
-
802
- const instrumentationOptions =
803
- runtimeConfig.webInstrumentationOptions == null
804
- ? {}
805
- : runtimeConfig.webInstrumentationOptions
806
-
807
- const defaultInstrumentations = getWebInstrumentations(instrumentationOptions)
808
-
809
- if (runtimeConfig.enableTracing !== false && TracingInstrumentation) {
810
- try {
811
- defaultInstrumentations.push(new TracingInstrumentation())
812
- } catch (error) {
813
- console.warn('[TrackingService] Failed to instantiate tracing instrumentation:', error)
814
- }
815
- }
816
-
817
- return defaultInstrumentations
818
- }
819
-
820
- _withClient (callback, options = {}) {
821
- if (!this._enabled) {
822
- return null
823
- }
824
-
825
- if (this._faroClient) {
826
- try {
827
- return callback(this._faroClient)
828
- } catch (error) {
829
- console.error('[TrackingService] Tracking callback failed:', error)
830
- return null
831
- }
832
- }
833
-
834
- if (options.queue === false) {
835
- return null
836
- }
837
-
838
- const queueLimit =
839
- this._runtimeConfig.maxQueueSize ??
840
- this._trackingOptions.maxQueueSize ??
841
- DEFAULT_MAX_QUEUE_SIZE
842
-
843
- if (this._queue.length >= queueLimit) {
844
- this._queue.shift()
845
- }
846
-
847
- this._queue.push(callback)
848
-
849
- return null
850
- }
851
- }
852
-
853
-