@compilacion/colleciones-clientos 1.0.12 → 2.0.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.
@@ -2,58 +2,7 @@
2
2
  'use strict';
3
3
 
4
4
  // helper 'private' functions
5
- /**
6
- * Converts a snake_case string to camelCase.
7
- * Removes non-ASCII characters.
8
- * @param {string} str
9
- * @returns {string}
10
- */
11
- const underscoreToCamelCase = function (str) {
12
- str = str.replace(/[^\x00-\x7F_]/g, '');
13
- return str
14
- .split('_')
15
- .map((word, index) => {
16
- if (index === 0) {
17
- return word;
18
- }
19
- return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
20
- })
21
- .join('');
22
- };
23
5
 
24
- /**
25
- * Formats adjectives by converting them to camelCase strings.
26
- * Accepts a single string or an array of strings.
27
- * @param {string|string[]|undefined} adjectiveParameter
28
- * @returns {string[]|undefined}
29
- */
30
- const formatAdjectives = function (adjectiveParameter) {
31
- const errorMsg = new Error('Adjective must be a string, an array of strings, or undefined');
32
- if (typeof adjectiveParameter === 'undefined') {
33
- return undefined;
34
- }
35
- if (typeof adjectiveParameter === 'string') {
36
- return [underscoreToCamelCase(adjectiveParameter)];
37
- }
38
- if (Array.isArray(adjectiveParameter) && adjectiveParameter.every(item => typeof item === 'string')) {
39
- return adjectiveParameter.map(adjective => underscoreToCamelCase(adjective));
40
- }
41
- throw errorMsg;
42
- };
43
-
44
- /**
45
- * Converts an array of adjective strings to a dot-prefixed, dot-separated string.
46
- * Removes duplicates and sorts alphabetically.
47
- * @param {string[]} adjective
48
- * @returns {string|undefined}
49
- */
50
- const stringifyAdjectives = function (adjective) {
51
- if (!Array.isArray(adjective) || adjective.length === 0) {
52
- return undefined;
53
- }
54
- const uniqueSorted = [...new Set(adjective)].sort();
55
- return '.' + uniqueSorted.join('.');
56
- };
57
6
 
58
7
  /**
59
8
  * Encodes a string to Base64.
@@ -95,171 +44,264 @@
95
44
 
96
45
  /**
97
46
  * Base class representing a structured event with timestamp, identifiers, and data fields.
47
+ * This class is used to model events in a semantic and structured format for tracking purposes.
98
48
  */
99
- class CollecionesBaseEvent {
49
+ class CollecionesEvent {
100
50
 
101
51
  /**
102
52
  * Constructs a new event with default metadata and timestamps.
103
53
  */
104
54
  constructor() {
105
- this.eventName = '';
106
- this.data = {};
107
- this.data.meta = {
108
- eventFormat: 'CollecionesBaseEvent',
55
+ // initialize event properties
56
+ this.entity = '';
57
+ this.adjevtives = [];
58
+ this.identifiers = {};
59
+ this.action = '';
60
+ this.actor = {
61
+ name:'',
62
+ identifiers: []
63
+ };
64
+ this.context = {};
65
+ this.references = {};
66
+ // set default values
67
+ this.meta = {
68
+ eventFormat: 'CollecionesEvent',
109
69
  eventFormatVersion: '1'
110
70
  };
111
- this.data.timestamps = {};
112
- this.data.timestamps.clientDatetimeUtc = new Date().toISOString();
71
+ this.meta.tracker = '';
72
+ this.meta.app = '';
73
+ this.meta.timestamps = {};
74
+ this.meta.timestamps.clientDatetimeUtc = new Date().toISOString();
113
75
  if (typeof window !== 'undefined' && typeof navigator !== 'undefined' && navigator.language) {
114
- this.data.timestamps.clientDatetimeLocal = new Date(Date.now() - new Date().getTimezoneOffset() * 60000).toISOString();
115
- this.data.timestamps.timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
116
- this.data.timestamps.timeZoneOffset = new Date().getTimezoneOffset();
76
+ this.meta.timestamps.clientDatetimeLocal = new Date(Date.now() - new Date().getTimezoneOffset() * 60000).toISOString();
77
+ this.meta.timestamps.timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
78
+ this.meta.timestamps.timeZoneOffset = new Date().getTimezoneOffset();
117
79
  } else {
118
- this.data.timestamps.clientDatetimeLocal = new Date().toISOString();
119
- this.data.timestamps.timeZone = 'UTC';
80
+ this.meta.timestamps.clientDatetimeLocal = new Date().toISOString();
81
+ this.meta.timestamps.timeZone = 'UTC';
120
82
  }
121
83
  }
122
84
 
123
85
  /**
124
- * Sets the event name.
125
- * @param {string} name
86
+ * Returns the declared event format.
87
+ * @returns {string} The format name (e.g. 'CollecionesEvent').
126
88
  */
127
- setEventName(name) {
128
- this.eventName = name;
89
+ getEventFormat() {
90
+ let v = this.meta?.eventFormat;
91
+ return (typeof v !== 'undefined') ? v : '1';
129
92
  }
130
93
 
131
94
  /**
132
- * Returns the event name.
133
- * @returns {string}
95
+ * Returns the version of the event format.
96
+ * @returns {string} The format version string.
134
97
  */
135
- getEventName() {
136
- return this.eventName;
98
+ getEventFormatVersion() {
99
+ let v = this?.meta?.eventFormatVersion;
100
+ return (typeof v !== 'undefined') ? v : 'CollecionesEvent';
137
101
  }
138
102
 
139
103
  /**
140
- * Gets the format of the event.
141
- * @returns {string}
104
+ * Overrides or supplements the event's timestamp fields with custom values.
105
+ * @param {object} dateTimeObject - Key-value pairs to merge into the existing timestamp object.
142
106
  */
143
- getEventFormat() {
144
- let v = this.data?.meta?.eventFormat;
145
- return (typeof v !== 'undefined') ? v : '1';
107
+ overrideDatetime(dateTimeObject = {}) {
108
+ for (const [key, value] of Object.entries(dateTimeObject)) {
109
+ this.meta.timestamps[key] = value;
110
+ }
146
111
  }
147
112
 
148
113
  /**
149
- * Gets the version of the event format.
150
- * @returns {string}
114
+ * Sets the name of the tracker responsible for generating this event.
115
+ * @param {string} name - Tracker name.
151
116
  */
152
- getEventFormatVersion() {
153
- let v = this.data?.meta?.eventFormatVersion;
154
- return (typeof v !== 'undefined') ? v : 'CollecionesBaseEvent';
117
+ setTracker(name) {
118
+ this.meta.tracker = name;
155
119
  }
156
120
 
157
121
  /**
158
- * Replaces the entire data object.
159
- * @param {object} data
122
+ * Sets the name of the application that generated the event.
123
+ * @param {string} name - Application name.
160
124
  */
161
- setData(data) {
162
- this.data = data;
125
+ setAppName(name) {
126
+ this.meta.appName = name;
163
127
  }
164
128
 
165
129
  /**
166
- * Adds a field to the event's custom fields section.
167
- * @param {string} name
168
- * @param {*} value
130
+ * Sets the expected schema name for this event.
131
+ * @param {string} schema - The name of the schema.
169
132
  */
170
- addAttribute(name, value) {
171
- return this.addField(name, value);
133
+ setSchema(schema) {
134
+ if (typeof schema !== 'string') {
135
+ throw new Error('Schema must be a string');
136
+ }
137
+ this.meta.schema = schema;
172
138
  }
173
139
 
174
140
  /**
175
- * Adds a key-value pair to the custom fields.
176
- * @param {string} name
177
- * @param {*} value
141
+ * Sets the entity (subject) of the event.
142
+ * @param {string} entity - The entity name.
143
+ */
144
+ setEntity = function(entity) {
145
+ this.entity = entity;
146
+ }
147
+
148
+ /**
149
+ * Defines the main action of the event (e.g., 'clicked').
150
+ * @param {string} action - The action string.
178
151
  */
179
- addField(name, value) {
180
- if (typeof this.data.fields !== 'object' || this.data.fields === null) {
181
- this.data.fields = {};
152
+ setAction = function(action) {
153
+ this.action = action;
154
+ }
155
+
156
+ /**
157
+ * Adds an adjective that describes the entity in more detail.
158
+ * @param {string} adjective - An adjective string.
159
+ */
160
+ addAdjective = function(adjective) {
161
+ if (typeof adjective !== 'string') {
162
+ throw new Error('Adjective must be a string');
182
163
  }
183
- this.data.fields[name] = value;
164
+ this.adjevtives.push(adjective);
184
165
  }
185
166
 
186
167
  /**
187
- * Sets the name of the tracker used to generate the event.
188
- * @param {string} name
168
+ * Adds or updates an identifier for the primary entity.
169
+ * @param {string} name - The identifier key.
170
+ * @param {*} identifier - The identifier value.
189
171
  */
190
- setTracker(name) {
191
- this.data.tracker = name;
172
+ setIdentifier = function(name, identifier) {
173
+ if (typeof name !== 'string') {
174
+ throw new Error('Identifier name must be a string');
175
+ }
176
+ this.identifiers[name] = identifier;
192
177
  }
193
178
 
194
179
  /**
195
- * Sets the name of the application that created the event.
196
- * @param {string} name
180
+ * Defines the name of the actor (who or what performed the action).
181
+ * @param {string} name - Actor name (e.g., 'user').
197
182
  */
198
- setAppName(name) {
199
- this.data.appName = name;
183
+ setActor = function(name) {
184
+ if (typeof name !== 'string') {
185
+ throw new Error('Actor name must be a string');
186
+ }
187
+ this.actor.name = name;
200
188
  }
201
189
 
202
190
  /**
203
- * Adds multiple identifiers from an object.
204
- * @param {object} param
191
+ * Adds or updates an identifier for the actor.
192
+ * @param {string} name - Identifier name.
193
+ * @param {*} identifier - Identifier value.
205
194
  */
206
- convertParamIdentifiers(param) {
207
- if (typeof param === 'object' && param !== null && !Array.isArray(param)) {
208
- for (const [key, value] of Object.entries(param)) {
209
- this.addIdentifier(key, value);
210
- }
195
+ setActorIdentifier = function(name, identifier) {
196
+ if (typeof name !== 'string') {
197
+ throw new Error('Actor Identifier name must be a string');
211
198
  }
199
+ this.actor.identifiers[name] = identifier;
212
200
  }
213
201
 
214
202
  /**
215
- * Adds a single identifier to the event.
216
- * @param {string} name
217
- * @param {*} value
203
+ * Adds contextual information to the event.
204
+ * @param {string} context - The key of the context field.
205
+ * @param {*} value - The value of the context field.
218
206
  */
219
- addIdentifier(name, value) {
220
- if (typeof this.data.identifiers !== 'object' || this.data.identifiers === null) {
221
- this.data.identifiers = {};
207
+ setContext = function(context, value) {
208
+ if (typeof context !== 'string') {
209
+ throw new Error('Context must be a string');
222
210
  }
223
- this.data.identifiers[name] = value;
211
+ this.context[context] = value;
224
212
  }
225
213
 
226
214
  /**
227
- * Constructs the postable event object with metadata and encoded payload.
228
- * @returns {object}
215
+ * Declares an entity referenced by this event.
216
+ * @param {string} entity - The referenced entity name.
229
217
  */
230
- getPostObject() {
231
- this.data.timestamps.sendDatetimeUtc = new Date().toISOString();
232
- this.data.timestamps.sendDatetimeLocal = new Date(Date.now() - new Date().getTimezoneOffset() * 60000).toISOString();
233
- return {
234
- eventname: this.getEventName(),
235
- eventFormat: this.getEventFormat(),
236
- eventFormatVersion: this.getEventFormatVersion(),
237
- payload: toBase64(this.getPayload())
238
- };
218
+ setRefence = function(entity) {
219
+ if (typeof entity !== 'string') {
220
+ throw new Error('Referenced entity must be a string');
221
+ }
222
+ if(this.references[entity] === undefined) {
223
+ this.references[entity] = {identifiers: {}};
224
+ }
239
225
  }
240
226
 
241
227
  /**
242
- * Overrides or extends the timestamp fields of the event.
243
- * @param {object} dateTimeObject
228
+ * Adds or updates an identifier for a referenced entity.
229
+ * @param {string} entity - The referenced entity name.
230
+ * @param {string} name - The identifier key.
231
+ * @param {*} identifier - The identifier value.
244
232
  */
245
- overrideDatetime(dateTimeObject = {}) {
246
- for (const [key, value] of Object.entries(dateTimeObject)) {
247
- this.data.timestamps[key] = value;
233
+ setRefenceIdentifier = function(entity, name, identifier) {
234
+ if (typeof entity !== 'string') {
235
+ throw new Error('Referenced entity name must be a string');
236
+ }
237
+ if (typeof name !== 'string') {
238
+ throw new Error('Actor Identifier name must be a string');
248
239
  }
240
+ this.setRefence(entity);
241
+ this.references[entity].identifiers[name] = identifier;
249
242
  }
250
243
 
251
244
  /**
252
- * Serializes the event data to a JSON string.
253
- * @returns {string}
245
+ * Serializes the event to a plain object suitable for JSON.stringify().
246
+ * @returns {object}
254
247
  */
255
- getPayload() {
256
- return JSON.stringify(this.data);
248
+ toJSON() {
249
+ return {
250
+ class: 'CollecionesEvent',
251
+ entity: this.entity,
252
+ adjectives: this.adjevtives,
253
+ identifiers: this.identifiers,
254
+ action: this.action,
255
+ actor: this.actor,
256
+ context: this.context,
257
+ references: this.references,
258
+ meta: this.meta
259
+ };
257
260
  }
258
261
 
262
+ /**
263
+ * Recreates an instance of CollecionesEvent from a plain object.
264
+ * @param {object} obj
265
+ * @returns {CollecionesEvent}
266
+ */
267
+ static fromJSON(obj) {
268
+ if (!obj || obj.class !== 'CollecionesEvent') {
269
+ throw new Error('Invalid or missing event class type');
270
+ }
271
+ const instance = new CollecionesEvent();
272
+ instance.entity = obj.entity;
273
+ instance.adjevtives = obj.adjectives || [];
274
+ instance.identifiers = obj.identifiers || {};
275
+ instance.action = obj.action;
276
+ instance.actor = obj.actor || { name: '', identifiers: {} };
277
+ instance.context = obj.context || {};
278
+ instance.references = obj.references || {};
279
+ instance.meta = obj.meta || {};
280
+ return instance;
281
+ }
282
+
259
283
  }
260
284
 
261
285
  /**
262
- * Emitter class responsible for batching and sending events to a configured endpoint.
286
+ * CollecionesEmitter
287
+ * -------------------
288
+ * This class collects and sends event objects to a remote endpoint (e.g., an event collector API).
289
+ * It is used in both client- and server-side tracking scenarios, typically in combination with
290
+ * CollecionesEvent or subclasses like CollecionesBaseEvent.
291
+ *
292
+ * Behavior:
293
+ * - Events are buffered via `.track()` or `.trackAsync()`.
294
+ * - If the number of buffered events reaches `flushSize`, the buffer is automatically flushed.
295
+ * - If `flushInterval` is provided, the buffer is flushed at a fixed interval.
296
+ * - Flushing serializes each event using `.toJSON()`, encodes it with base64, and posts
297
+ * the resulting array to the configured endpoint.
298
+ *
299
+ * Example usage:
300
+ * const emitter = new CollecionesEmitter('/track', 5, 10000);
301
+ * emitter.track(new CollecionesEvent());
302
+ *
303
+ * Note:
304
+ * This class is stateful. To stop periodic flushing, call `.stopTimer()` when the emitter is no longer in use.
263
305
  */
264
306
  class CollecionesEmitter {
265
307
 
@@ -279,6 +321,7 @@
279
321
 
280
322
  /**
281
323
  * Starts the flush timer if a valid interval is set.
324
+ * @returns {void}
282
325
  */
283
326
  startTimer() {
284
327
  this.stopTimer();
@@ -289,6 +332,7 @@
289
332
 
290
333
  /**
291
334
  * Starts the flush timer only if not already running.
335
+ * @returns {void}
292
336
  */
293
337
  startTimerIfStopped() {
294
338
  if (!this.timer) {
@@ -298,6 +342,7 @@
298
342
 
299
343
  /**
300
344
  * Stops the active flush timer.
345
+ * @returns {void}
301
346
  */
302
347
  stopTimer() {
303
348
  if (this.timer) {
@@ -308,23 +353,28 @@
308
353
 
309
354
  /**
310
355
  * Adds an event to the buffer and flushes if threshold is reached.
311
- * @param {CollecionesBaseEvent} event
356
+ * Validates that the event is an instance of CollecionesEvent to ensure correct event type.
357
+ * @param {CollecionesEvent} event - Event instance to be tracked.
358
+ * @throws {Error} Throws if event is not a CollecionesEvent instance.
359
+ * @returns {void}
312
360
  */
313
361
  track(event) {
314
- if (!(event instanceof CollecionesBaseEvent)) {
315
- throw new Error('Event must be an instance of CollecionesBaseEvent');
362
+ if (!(event instanceof CollecionesEvent)) {
363
+ throw new Error('Event must be an instance of CollecionesEvent');
316
364
  }
317
365
  this.trackAsync(event);
318
366
  }
319
367
 
320
368
  /**
321
369
  * Asynchronously adds an event and flushes if the buffer size is exceeded.
322
- * @param {CollecionesBaseEvent} event
323
- * @returns {Promise<void>}
370
+ * Validates that the event is an instance of CollecionesEvent to ensure correct event type.
371
+ * @param {CollecionesEvent} event - Event instance to be tracked asynchronously.
372
+ * @throws {Error} Throws if event is not a CollecionesEvent instance.
373
+ * @returns {Promise<void>} Resolves when event is added and flush triggered if needed.
324
374
  */
325
375
  async trackAsync(event) {
326
- if (!(event instanceof CollecionesBaseEvent)) {
327
- throw new Error('Event must be an instance of CollecionesBaseEvent');
376
+ if (!(event instanceof CollecionesEvent)) {
377
+ throw new Error('Event must be an instance of CollecionesEvent');
328
378
  }
329
379
  this.startTimerIfStopped();
330
380
  this.buffer.push(event);
@@ -332,8 +382,11 @@
332
382
  }
333
383
 
334
384
  /**
335
- * Sends the buffered events to the server and clears the buffer.
336
- * @returns {Promise<boolean|undefined>}
385
+ * Sends all buffered events in a single POST request to the server.
386
+ * Each event is serialized via `.toJSON()` and encoded as a base64 string.
387
+ * Response status is checked for HTTP success; errors are logged but not thrown.
388
+ *
389
+ * @returns {Promise<boolean|undefined>} Resolves true if flush was triggered, or undefined if buffer was empty.
337
390
  */
338
391
  async flush() {
339
392
  if (this.buffer.length === 0) return;
@@ -341,7 +394,7 @@
341
394
  this.buffer = [];
342
395
  let postPayload = [];
343
396
  eventsToSend.forEach((e) => {
344
- postPayload.push( e.getPostObject() );
397
+ postPayload.push( toBase64( JSON.stringify(e.toJSON())) );
345
398
  });
346
399
  this.stopTimer();
347
400
  try {
@@ -381,22 +434,13 @@
381
434
  this.identifiers = {};
382
435
  }
383
436
 
384
- /**
385
- * Adds a global identifier to be included with every event.
386
- * @param {string} name - Identifier key.
387
- * @param {*} value - Identifier value.
388
- */
389
- addIdentifier(name, value) {
390
- this.identifiers[name] = value;
391
- }
392
-
393
437
  /**
394
438
  * Sends an event to all emitters after enriching it with identifiers and metadata.
395
- * @param {CollecionesBaseEvent} collecionesEvent - The event to be sent.
396
- * @throws {Error} If the input is not an instance of CollecionesBaseEvent.
439
+ * @param {CollecionesEvent} collecionesEvent - The event to be sent.
440
+ * @throws {Error} If the input is not an instance of CollecionesEvent.
397
441
  */
398
442
  track(collecionesEvent) {
399
- if (!(collecionesEvent instanceof CollecionesBaseEvent)) {
443
+ if (!(collecionesEvent instanceof CollecionesEvent)) {
400
444
  throw new Error('Event must be of type CollecionesEvent');
401
445
  }
402
446
  for (const [key, value] of Object.entries(this.identifiers)) {
@@ -427,217 +471,256 @@
427
471
 
428
472
  /**
429
473
  * Tracks an event, enriching it with browser context information.
430
- * @param {CollecionesBaseEvent} collecionesEvent - The event object to track.
431
- * @throws {Error} If the event is not an instance of CollecionesBaseEvent.
474
+ * @param {CollecionesEvent} collecionesEvent - The event object to track.
475
+ * @throws {Error} If the event is not an instance of CollecionesEvent.
432
476
  */
433
477
  track(collecionesEvent) {
434
- if (!(collecionesEvent instanceof CollecionesBaseEvent)) {
478
+ if (!(collecionesEvent instanceof CollecionesEvent)) {
435
479
  throw new Error('Event must be of type CollecionesEvent');
436
480
  }
437
- collecionesEvent.addField('browserContext', getBrowserContext());
481
+ collecionesEvent.setContext('browserContext', getBrowserContext());
438
482
  super.track(collecionesEvent);
439
483
  }
440
484
  }
441
485
 
442
486
  /**
443
- * Represents a semantic event with a name, data payload, and identifiers.
444
- * Extends CollecionesBaseEvent.
487
+ * Colleciones DSL
488
+ * ---------------
489
+ * This module provides a fluent, human-readable DSL (domain-specific language) for constructing
490
+ * structured tracking events based on CollecionesBaseEvent. Each step in the chain represents
491
+ * a semantically meaningful piece of the event: the entity, the action, the actor, references, etc.
492
+ *
493
+ * Entry Point:
494
+ * collecionesDsl.the('dark')
495
+ *
496
+ * Chaining:
497
+ * - .and('...') : add additional adjectives
498
+ * - ._('entity') : specify the entity being acted on
499
+ * - .identified.by('field') : declare primary identifier for the subject
500
+ * - .has.been('action') : declare what happened
501
+ * - .by('actor') : describe the actor performing the action
502
+ * - .identified.by('id') : add identifiers for the actor
503
+ * - .with('key').set.to('val') : add contextual data fields
504
+ * - .referring.to('entity') : describe referenced/related entities
505
+ * - .identified.by('refId') : add identifiers for the reference
506
+ * - .conform.to('SchemaName') : attach schema metadata
507
+ * - .then.track.with(emitter) : finalize and emit the event
508
+ *
509
+ * Each function in the chain passes forward a scoped version of the CollecionesBaseEvent instance,
510
+ * and enforces semantic constraints (e.g. `.as()` is required after `.by()`).
511
+ *
512
+ * Internal:
513
+ * - Uses setRefence / setRefenceIdentifier for references
514
+ * - Uses helpers.getEvent() for test access
515
+ * - Uses instanceof CollecionesEmitter for validation
516
+ *
517
+ * This file is designed for internal use by developers building with the Colleciones tracking model.
445
518
  */
446
- class CollecionesEvent extends CollecionesBaseEvent {
447
519
 
448
- /**
449
- * Constructs a new CollecionesEvent instance.
450
- * @param {string} name - The name of the event.
451
- * @param {object} data - The main data payload of the event.
452
- * @param {object} identifiers - A set of identifiers to associate with the event.
453
- */
454
- constructor(name, data, identifiers) {
455
- super();
456
- this.setEventName(name);
457
- Object.assign(this.data, data);
458
- this.convertParamIdentifiers(identifiers);
459
- }
460
520
 
461
- }
521
+ let init = (entity) => {
522
+ return ((entity)=>{
523
+ let eventInstance = new CollecionesEvent(); // Core event object
524
+ eventInstance.setEntity(entity);
462
525
 
463
- const byteToHex = [];
464
- for (let i = 0; i < 256; ++i) {
465
- byteToHex.push((i + 0x100).toString(16).slice(1));
466
- }
467
- function unsafeStringify(arr, offset = 0) {
468
- return (byteToHex[arr[offset + 0]] +
469
- byteToHex[arr[offset + 1]] +
470
- byteToHex[arr[offset + 2]] +
471
- byteToHex[arr[offset + 3]] +
472
- '-' +
473
- byteToHex[arr[offset + 4]] +
474
- byteToHex[arr[offset + 5]] +
475
- '-' +
476
- byteToHex[arr[offset + 6]] +
477
- byteToHex[arr[offset + 7]] +
478
- '-' +
479
- byteToHex[arr[offset + 8]] +
480
- byteToHex[arr[offset + 9]] +
481
- '-' +
482
- byteToHex[arr[offset + 10]] +
483
- byteToHex[arr[offset + 11]] +
484
- byteToHex[arr[offset + 12]] +
485
- byteToHex[arr[offset + 13]] +
486
- byteToHex[arr[offset + 14]] +
487
- byteToHex[arr[offset + 15]]).toLowerCase();
488
- }
526
+ let helpers = {
527
+ getEvent: () => {
528
+ return eventInstance;
529
+ },
530
+ };
489
531
 
490
- let getRandomValues;
491
- const rnds8 = new Uint8Array(16);
492
- function rng() {
493
- if (!getRandomValues) {
494
- if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
495
- throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
496
- }
497
- getRandomValues = crypto.getRandomValues.bind(crypto);
498
- }
499
- return getRandomValues(rnds8);
500
- }
532
+ // DSL function groups
533
+ let andEntity = () => {}; // .and(...) chaining after the()
534
+ let setEntityAfterAnd = () => {}; // ._('entity') after .and
535
+ let setEntityIdentifiers = () => {}; // .identified.by(...) for main subject
536
+ let setAction = () => {}; // .has.been(...) for action
537
+ let setActor = () => {}; // .by('actor')
538
+ let setActorIdentifier = () => {}; // .identified.by(...) inside .by(...)
539
+ let addContext = () => {}; // .with(...).set.to(...)
540
+ let addReference = () => {}; // .referring.to(...)
541
+ let getAddReference = () => {}; // .identified.by().as() inside .referring
542
+ let addReferenceIdentifier = () => {}; // .and(...) after reference
543
+ let conformingTo = () => {}; // .conform.to('SchemaName')
544
+ let track = () => {}; // .then.track.with(emitter)
545
+
546
+ // Adjective chaining: .and(...)._('entity')
547
+ setEntityAfterAnd = (entity) => {
548
+ eventInstance.addAdjective(eventInstance.entity);
549
+ eventInstance.setEntity(entity);
550
+ return {
551
+ as: () => {
552
+ return { helpers }
553
+ },
554
+ identified: {
555
+ by: setEntityIdentifiers
556
+ },
557
+ has: { been: setAction },
558
+ helpers
559
+ };
560
+ };
501
561
 
502
- const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
503
- var native = { randomUUID };
562
+ // Identifier setup: .identified.by().as()
563
+ setEntityIdentifiers = (name) => {
564
+ eventInstance.setIdentifier(name, null);
565
+ return {
566
+ as: (value) => {
567
+ eventInstance.setIdentifier(name, value);
568
+ return {
569
+ helpers,
570
+ and: {
571
+ by: setEntityIdentifiers
572
+ },
573
+ has: { been: setAction },
574
+ }
575
+ },
576
+ helpers
577
+ };
578
+ };
504
579
 
505
- function v4(options, buf, offset) {
506
- if (native.randomUUID && true && !options) {
507
- return native.randomUUID();
508
- }
509
- options = options || {};
510
- const rnds = options.random ?? options.rng?.() ?? rng();
511
- if (rnds.length < 16) {
512
- throw new Error('Random bytes length must be >= 16');
513
- }
514
- rnds[6] = (rnds[6] & 0x0f) | 0x40;
515
- rnds[8] = (rnds[8] & 0x3f) | 0x80;
516
- return unsafeStringify(rnds);
517
- }
580
+ // Additional adjectives: .and('...')._()
581
+ andEntity = (entity) => {
582
+ eventInstance.addAdjective(eventInstance.entity);
583
+ eventInstance.setEntity(entity);
584
+ return {
585
+ and: andEntity,
586
+ _ : setEntityAfterAnd,
587
+ helpers
588
+ }
589
+ };
518
590
 
519
- /**
520
- * Represents a structured semantic event with entity, action, and optional adjective.
521
- * Includes automatic naming and identifier mapping.
522
- * Extends CollecionesBaseEvent.
523
- */
524
- class CollecionesSemanticEvent extends CollecionesBaseEvent {
591
+ // Action: .has.been('...')
592
+ setAction = (action) => {
593
+ eventInstance.setAction(action);
594
+ return {
595
+ by: setActor,
596
+ referring: { to: addReference },
597
+ with: addContext,
598
+ conform: {to: conformingTo},
599
+ then: {track: {with: track}},
600
+ helpers
601
+ }
602
+ };
525
603
 
526
- /**
527
- * Constructs a new semantic event.
528
- * @param {string} entity - The entity involved in the event.
529
- * @param {string} action - The action performed on the entity.
530
- * @param {string|string[]|undefined} adjective - Optional adjective(s) modifying the event.
531
- * @param {object} identifiers - Key-value pairs to be added as identifiers.
532
- */
533
- constructor(entity, action, adjective, identifiers) {
534
- super();
535
- this.entity = underscoreToCamelCase(entity);
536
- this.action = underscoreToCamelCase(action);
537
- this.adjective = formatAdjectives(adjective);
538
- let adjectiveString = stringifyAdjectives(this.adjective);
539
- this.eventName = `${this.entity}${(adjectiveString !== undefined) ? adjectiveString : ''}__${this.action}`;
540
- this.convertParamIdentifiers(identifiers);
541
- this.data.entityIdentifiers = {};
542
- this.data.attributes = {};
543
- this.data.clientEventId = v4();
544
- this.data.meta = {
545
- eventFormat: 'CollecionesSemanticEvent',
546
- eventFormatVersion: '1'
604
+ // Actor: .by('user')
605
+ setActor = (name) => {
606
+ eventInstance.setActor(name);
607
+ return {
608
+ identified: {
609
+ by: setActorIdentifier
610
+ },
611
+ with: addContext,
612
+ helpers
613
+ }
547
614
  };
548
- }
549
615
 
550
- /**
551
- * This method is deprecated in semantic events and will throw if called.
552
- * @throws {Error}
553
- */
554
- setData() {
555
- throw new Error('setData deprecated in semantic events');
556
- }
616
+ // Actor identifier: .identified.by().as()
617
+ setActorIdentifier = (name) => {
618
+ return {
619
+ as: (value) => {
620
+ eventInstance.setActorIdentifier(name, value);
621
+ return {
622
+ helpers,
623
+ and: setActorIdentifier,
624
+ with: addContext,
625
+ referring: { to: addReference },
626
+ }
627
+ },
628
+ helpers
629
+ }
630
+ };
557
631
 
558
- /**
559
- * Adds a key-value pair as an identifier for the semantic entity.
560
- * @param {string} key - The identifier name.
561
- * @param {string|number} value - The identifier value.
562
- * @throws Will throw if key is not a string or value is not a string or number.
563
- */
564
- addEntityIdentifier(key, value) {
565
- if (typeof key !== 'string') {
566
- throw new Error('Entity identifier key must be a string');
567
- }
568
- if (typeof value !== 'string' && typeof value !== 'number') {
569
- throw new Error('Entity identifier value must be a string or number');
570
- }
571
- this.data.entityIdentifiers[key] = value;
572
- }
632
+ // Contextual field: .with('key').set.to('value')
633
+ addContext = (context) => {
634
+ return {
635
+ helpers,
636
+ set: {
637
+ to: (value) => {
638
+ eventInstance.setContext(context, value);
639
+ return {
640
+ helpers,
641
+ and: addContext,
642
+ referring: { to: addReference },
643
+ }
644
+ }
645
+ }
646
+ }
647
+ };
573
648
 
574
- }
649
+ // Reference: .referring.to('...')
650
+ addReference = (entity) => {
651
+ eventInstance.setRefence(entity);
652
+ return {
653
+ identified: {
654
+ by: getAddReference(entity)
655
+ },
656
+ conform: {to: conformingTo},
657
+ then: {track: {with: track}},
658
+ helpers
659
+ }
660
+ };
575
661
 
576
- /**
577
- * Represents a semantic event related to a collection of entities.
578
- * Extends CollecionesSemanticEvent and adds item-level detail and identifiers.
579
- */
580
- class CollecionesSemanticCollectionEvent extends CollecionesSemanticEvent {
662
+ // Reference identifier setup: .identified.by().as()
663
+ getAddReference = (entity) => {
664
+ return (name) => {
665
+ return {
666
+ as: (value) => {
667
+ eventInstance.setRefenceIdentifier(entity, name, value);
668
+ return {
669
+ helpers,
670
+ and: addReferenceIdentifier,
671
+ conform: {to: conformingTo},
672
+ then: {track: {with: track}},
673
+ }
674
+ },
675
+ helpers,
676
+ };
677
+ };
678
+ };
581
679
 
582
- /**
583
- * Constructs a new CollecionesSemanticCollectionEvent.
584
- * @param {string} itemEntity - The name of the item entity being acted on.
585
- * @param {string} action - The action performed.
586
- * @param {string|string[]|undefined} adjective - Optional adjective(s) describing the action.
587
- * @param {object} identifiers - Identifiers associated with the event.
588
- * @param {string} [collectionEntity] - Optional collection name, defaults to `${itemEntity}Collection`.
589
- */
590
- constructor(itemEntity, action, adjective, identifiers, collectionEntity) {
591
- if (typeof collectionEntity == 'undefined') {
592
- collectionEntity = `${itemEntity}Collection`;
593
- }
594
- super(collectionEntity, action, undefined, identifiers);
595
- this.data.meta = {
596
- eventFormat: 'CollecionesSemanticCollectionEvent',
597
- eventFormatVersion: '1'
680
+ // Schema declaration: .conform.to('...')
681
+ conformingTo = (name) => {
682
+ eventInstance.setSchema(name);
683
+ return {
684
+ then: {track: {with: track}},
685
+ helpers
686
+ }
598
687
  };
599
- this.adjective = formatAdjectives(adjective);
600
- let adjectiveString = stringifyAdjectives(this.adjective);
601
- itemEntity = underscoreToCamelCase(itemEntity);
602
- this.data.itemEntity = itemEntity;
603
- this.data.itemEventName = `${itemEntity}${(adjectiveString !== undefined) ? adjectiveString : ''}__${this.action}`;
604
- this.data.entityItemIdentifiers = {};
605
- }
606
688
 
607
- /**
608
- * Adds an identifier value (or values) for a specific entity item key.
609
- * @param {string} key - The identifier name.
610
- * @param {string|number|Array<string|number>} value - The identifier value(s).
611
- * @throws Will throw if key is not a string or value is of invalid type.
612
- */
613
- addEntityItemIdentifier(key, value) {
614
- if (typeof key !== 'string') {
615
- throw new Error('Entity identifier key must be a string');
616
- }
617
- if (typeof value !== 'string' && typeof value !== 'number' && !Array.isArray(value)) {
618
- throw new Error('Entity identifier value must be a string or number');
619
- }
620
- if (typeof this.data.entityItemIdentifiers[key] == 'undefined') {
621
- this.data.entityItemIdentifiers[key] = [];
622
- }
623
- if (Array.isArray(value)) {
624
- value.forEach((v) => {
625
- this.data.entityItemIdentifiers[key].push(v);
626
- });
627
- } else {
628
- this.data.entityItemIdentifiers[key].push(value);
689
+ // Final dispatch: .then.track.with(emitter)
690
+ track = (emitter) => {
691
+ if(emitter instanceof CollecionesEmitter){
692
+ emitter.track(eventInstance);
693
+ }
694
+ return {
695
+ and: track,
696
+ helpers,
697
+ }
698
+ };
699
+
700
+ return {
701
+ and: andEntity,
702
+ _: setEntityAfterAnd,
703
+ identified: {
704
+ by: setEntityIdentifiers
705
+ },
706
+ has: { been: setAction},
707
+ node: '_base',
708
+ helpers
629
709
  }
630
- }
631
710
 
632
- }
711
+ })(entity);
712
+ };
713
+
714
+ let collecionesDsl = {
715
+ the: init
716
+ };
633
717
 
634
718
  (function(global) {
635
719
  global.CollecionesEmitter = CollecionesEmitter;
636
720
  global.CollecionesTracker = CollecionesTracker;
637
721
  global.CollecionesWebTracker = CollecionesWebTracker;
638
722
  global.CollecionesEvent = CollecionesEvent;
639
- global.CollecionesSemanticEvent = CollecionesSemanticEvent;
640
- global.CollecionesSemanticCollectionEvent = CollecionesSemanticCollectionEvent;
723
+ global.collecionesDsl = collecionesDsl;
641
724
  })(typeof window !== 'undefined' ? window : globalThis);
642
725
 
643
726
  })();