@contentful/optimization-core 0.1.0-alpha11 → 0.1.0-alpha12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <p align="center">
2
2
  <a href="https://www.contentful.com/developers/docs/personalization/">
3
- <img alt="Contentful Logo" title="Contentful" src="https://raw.githubusercontent.com/contentful/optimization/v0.1.0-alpha11/contentful-icon.png" width="150">
3
+ <img alt="Contentful Logo" title="Contentful" src="https://raw.githubusercontent.com/contentful/optimization/v0.1.0-alpha12/contentful-icon.png" width="150">
4
4
  </a>
5
5
  </p>
6
6
 
@@ -11,7 +11,7 @@
11
11
  <div align="center">
12
12
 
13
13
  [Guides](https://contentful.github.io/optimization/documents/Guides.html) ·
14
- [Reference](https://contentful.github.io/optimization) · [Contributing](https://github.com/contentful/optimization/blob/v0.1.0-alpha11/CONTRIBUTING.md)
14
+ [Reference](https://contentful.github.io/optimization) · [Contributing](https://github.com/contentful/optimization/blob/v0.1.0-alpha12/CONTRIBUTING.md)
15
15
 
16
16
  </div>
17
17
 
@@ -84,7 +84,9 @@ Configure and initialize the Core SDK:
84
84
  ```ts
85
85
  const statefulOptimization = new CoreStateful({ clientId: 'abc123' })
86
86
  const statelessOptimization = new CoreStateless({ clientId: 'abc123' })
87
- const requestOptimization = statelessOptimization.forRequest()
87
+ const requestOptions = { locale: 'de-DE' }
88
+
89
+ await statelessOptimization.page({}, requestOptions)
88
90
  ```
89
91
 
90
92
  ## Working with Stateless Core
@@ -97,8 +99,8 @@ In stateless environments, Core will not maintain any internal state, which incl
97
99
  These concerns should be handled by consumers to fit their specific architectural and design
98
100
  specifications.
99
101
 
100
- Request-emitting methods in `CoreStateless` are bound per request via
101
- `optimization.forRequest(...)`.
102
+ Request-scoped Experience options in `CoreStateless` are passed as the final argument to each
103
+ stateless event method.
102
104
 
103
105
  ## Working with Stateful Core
104
106
 
@@ -164,8 +166,8 @@ Configuration method signatures:
164
166
 
165
167
  - `beaconHandler`: `(url: string | URL, data: BatchInsightsEventArray) => boolean`
166
168
 
167
- In stateless environments, bind `ip`, `locale`, `plainText`, and `preflight` per request with
168
- `optimization.forRequest(...)` instead of constructor config.
169
+ In stateless environments, pass `ip`, `locale`, `plainText`, and `preflight` as the final argument
170
+ to stateless event methods instead of constructor config.
169
171
 
170
172
  ### Queue Policy Options
171
173
 
@@ -375,14 +377,19 @@ Arguments:
375
377
  ### Event Methods
376
378
 
377
379
  In `CoreStateful`, call these methods on the root instance. In `CoreStateless`, call them on the
378
- request scope returned by `optimization.forRequest(...)`:
380
+ root instance and pass request-scoped Experience options as the final argument when needed:
379
381
 
380
382
  ```ts
381
- const requestOptimization = optimization.forRequest({
383
+ const requestOptions = {
382
384
  locale: 'de-DE',
383
- })
385
+ }
386
+
387
+ await optimization.page({ ...requestContext, profile }, requestOptions)
384
388
  ```
385
389
 
390
+ Request-scoped Experience options stay separate from the event payload. Event context data belongs
391
+ in `payload`, while `ip`, `locale`, `plainText`, and `preflight` belong in `requestOptions`.
392
+
386
393
  Only the following methods may return an `OptimizationData` object:
387
394
 
388
395
  - `identify`
@@ -411,6 +418,7 @@ Arguments:
411
418
 
412
419
  - `payload`\*: Identify event builder arguments object, including an optional `profile` property
413
420
  with a `PartialProfile` value that requires only an `id`
421
+ - `requestOptions`: Optional request-scoped Experience API options passed as the final argument
414
422
 
415
423
  #### `page`
416
424
 
@@ -420,6 +428,7 @@ Arguments:
420
428
 
421
429
  - `payload`\*: Page view event builder arguments object, including an optional `profile` property
422
430
  with a `PartialProfile` value that requires only an `id`
431
+ - `requestOptions`: Optional request-scoped Experience API options passed as the final argument
423
432
 
424
433
  #### `screen`
425
434
 
@@ -429,6 +438,7 @@ Arguments:
429
438
 
430
439
  - `payload`\*: Screen view event builder arguments object, including an optional `profile` property
431
440
  with a `PartialProfile` value that requires only an `id`
441
+ - `requestOptions`: Optional request-scoped Experience API options passed as the final argument
432
442
 
433
443
  #### `track`
434
444
 
@@ -438,6 +448,7 @@ Arguments:
438
448
 
439
449
  - `payload`\*: Track event builder arguments object, including an optional `profile` property with a
440
450
  `PartialProfile` value that requires only an `id`
451
+ - `requestOptions`: Optional request-scoped Experience API options passed as the final argument
441
452
 
442
453
  #### `trackView`
443
454
 
@@ -450,6 +461,8 @@ Arguments:
450
461
  - `payload`\*: Entry view event builder arguments object. When `payload.sticky` is `true`, `profile`
451
462
  is optional and the returned Experience profile is reused for Insights delivery. Otherwise,
452
463
  `profile` is required and must contain at least an `id`
464
+ - `requestOptions`: Optional request-scoped Experience API options passed as the final argument.
465
+ Only used when `payload.sticky` is `true`
453
466
 
454
467
  #### `trackClick`
455
468
 
@@ -463,6 +476,8 @@ Arguments:
463
476
 
464
477
  - `payload`\*: Entry click event builder arguments object, including a required `profile` property
465
478
  with a `PartialProfile` value that requires only an `id`
479
+ - `requestOptions`: Optional request-scoped Experience API options accepted for signature
480
+ consistency; currently unused
466
481
 
467
482
  #### `trackHover`
468
483
 
@@ -476,6 +491,8 @@ Arguments:
476
491
 
477
492
  - `payload`\*: Entry hover event builder arguments object, including a required `profile` property
478
493
  with a `PartialProfile` value that requires only an `id`
494
+ - `requestOptions`: Optional request-scoped Experience API options accepted for signature
495
+ consistency; currently unused
479
496
 
480
497
  #### `trackFlagView`
481
498
 
@@ -490,6 +507,8 @@ Arguments:
490
507
 
491
508
  - `payload`\*: Flag view event builder arguments object, including a required `profile` property
492
509
  with a `PartialProfile` value that requires only an `id`
510
+ - `requestOptions`: Optional request-scoped Experience API options accepted for signature
511
+ consistency; currently unused
493
512
 
494
513
  ## Stateful-only Core Methods
495
514
 
package/dist/260.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const OPTIMIZATION_CORE_SDK_VERSION = "0.1.0-alpha11";
1
+ const OPTIMIZATION_CORE_SDK_VERSION = "0.1.0-alpha12";
2
2
  const OPTIMIZATION_CORE_SDK_NAME = "@contentful/optimization-core";
3
3
  const ANONYMOUS_ID_COOKIE = 'ctfl-opt-aid';
4
4
  const ANONYMOUS_ID_KEY = '__ctfl_opt_anonymous_id__';
@@ -36,7 +36,7 @@ __webpack_require__.d(__webpack_exports__, {
36
36
  PROFILE_CACHE_KEY: ()=>PROFILE_CACHE_KEY,
37
37
  SELECTED_OPTIMIZATIONS_CACHE_KEY: ()=>SELECTED_OPTIMIZATIONS_CACHE_KEY
38
38
  });
39
- const OPTIMIZATION_CORE_SDK_VERSION = "0.1.0-alpha11";
39
+ const OPTIMIZATION_CORE_SDK_VERSION = "0.1.0-alpha12";
40
40
  const OPTIMIZATION_CORE_SDK_NAME = "@contentful/optimization-core";
41
41
  const ANONYMOUS_ID_COOKIE = 'ctfl-opt-aid';
42
42
  const ANONYMOUS_ID_KEY = '__ctfl_opt_anonymous_id__';
package/dist/index.cjs CHANGED
@@ -22,7 +22,7 @@ var __webpack_modules__ = {
22
22
  PROFILE_CACHE_KEY: ()=>PROFILE_CACHE_KEY,
23
23
  SELECTED_OPTIMIZATIONS_CACHE_KEY: ()=>SELECTED_OPTIMIZATIONS_CACHE_KEY
24
24
  });
25
- const OPTIMIZATION_CORE_SDK_VERSION = "0.1.0-alpha11";
25
+ const OPTIMIZATION_CORE_SDK_VERSION = "0.1.0-alpha12";
26
26
  const OPTIMIZATION_CORE_SDK_NAME = "@contentful/optimization-core";
27
27
  const ANONYMOUS_ID_COOKIE = 'ctfl-opt-aid';
28
28
  const ANONYMOUS_ID_KEY = '__ctfl_opt_anonymous_id__';
@@ -124,7 +124,6 @@ var __webpack_exports__ = {};
124
124
  UniversalEventBuilderArgs: ()=>UniversalEventBuilderArgs,
125
125
  EntryInteractionBuilderArgsBase: ()=>EntryInteractionBuilderArgsBase,
126
126
  effect: ()=>signals_core_namespaceObject.effect,
127
- CoreStatelessRequestScope: ()=>CoreStatelessRequestScope,
128
127
  PREVIEW_PANEL_SIGNALS_SYMBOL: ()=>symbols.PREVIEW_PANEL_SIGNALS_SYMBOL,
129
128
  guardedBy: ()=>guardedBy,
130
129
  CHANGES_CACHE_KEY: ()=>constants.CHANGES_CACHE_KEY,
@@ -1362,76 +1361,89 @@ var __webpack_exports__ = {};
1362
1361
  }
1363
1362
  const src_CoreStateful = CoreStateful;
1364
1363
  var api_schemas = __webpack_require__("./src/api-schemas.ts");
1365
- const TRACK_CLICK_PROFILE_ERROR = 'CoreStateless.forRequest().trackClick() requires `payload.profile.id` for Insights delivery.';
1366
- const TRACK_HOVER_PROFILE_ERROR = 'CoreStateless.forRequest().trackHover() requires `payload.profile.id` for Insights delivery.';
1367
- const TRACK_FLAG_VIEW_PROFILE_ERROR = 'CoreStateless.forRequest().trackFlagView() requires `payload.profile.id` for Insights delivery.';
1368
- const NON_STICKY_TRACK_VIEW_PROFILE_ERROR = 'CoreStateless.forRequest().trackView() requires `payload.profile.id` when `payload.sticky` is not `true`.';
1369
- const STICKY_TRACK_VIEW_PROFILE_ERROR = 'CoreStateless.forRequest().trackView() could not derive a profile from the sticky Experience response. Pass `payload.profile.id` explicitly if you need a fallback.';
1364
+ const TRACK_CLICK_PROFILE_ERROR = 'CoreStateless.trackClick() requires `payload.profile.id` for Insights delivery.';
1365
+ const TRACK_HOVER_PROFILE_ERROR = 'CoreStateless.trackHover() requires `payload.profile.id` for Insights delivery.';
1366
+ const TRACK_FLAG_VIEW_PROFILE_ERROR = 'CoreStateless.trackFlagView() requires `payload.profile.id` for Insights delivery.';
1367
+ const NON_STICKY_TRACK_VIEW_PROFILE_ERROR = 'CoreStateless.trackView() requires `payload.profile.id` when `payload.sticky` is not `true`.';
1368
+ const STICKY_TRACK_VIEW_PROFILE_ERROR = 'CoreStateless.trackView() could not derive a profile from the sticky Experience response. Pass `payload.profile.id` explicitly if you need a fallback.';
1369
+ const CoreStateless_hasDefinedValues = (record)=>Object.values(record).some((value)=>void 0 !== value);
1370
1370
  const requireInsightsProfile = (profile, errorMessage)=>{
1371
1371
  if (void 0 !== profile) return profile;
1372
1372
  throw new Error(errorMessage);
1373
1373
  };
1374
- class CoreStatelessRequestScope {
1375
- core;
1376
- options;
1377
- constructor(core, options = {}){
1378
- this.core = core;
1379
- this.options = Object.freeze({
1380
- ...options
1374
+ const createStatelessExperienceApiConfig = (api)=>{
1375
+ if (void 0 === api) return;
1376
+ const experienceConfig = {
1377
+ baseUrl: api.experienceBaseUrl,
1378
+ enabledFeatures: api.enabledFeatures
1379
+ };
1380
+ return CoreStateless_hasDefinedValues(experienceConfig) ? experienceConfig : void 0;
1381
+ };
1382
+ const createStatelessInsightsApiConfig = (api)=>{
1383
+ if (api?.insightsBaseUrl === void 0) return;
1384
+ return {
1385
+ baseUrl: api.insightsBaseUrl
1386
+ };
1387
+ };
1388
+ class CoreStateless extends src_CoreBase {
1389
+ constructor(config){
1390
+ super(config, {
1391
+ experience: createStatelessExperienceApiConfig(config.api),
1392
+ insights: createStatelessInsightsApiConfig(config.api)
1381
1393
  });
1382
1394
  }
1383
- async identify(payload) {
1395
+ async identify(payload, requestOptions) {
1384
1396
  const { profile, ...builderArgs } = payload;
1385
- return await this.sendExperienceEvent(this.core.eventBuilder.buildIdentify(builderArgs), profile);
1397
+ return await this.sendExperienceEvent(this.eventBuilder.buildIdentify(builderArgs), profile, requestOptions);
1386
1398
  }
1387
- async page(payload = {}) {
1399
+ async page(payload = {}, requestOptions) {
1388
1400
  const { profile, ...builderArgs } = payload;
1389
- return await this.sendExperienceEvent(this.core.eventBuilder.buildPageView(builderArgs), profile);
1401
+ return await this.sendExperienceEvent(this.eventBuilder.buildPageView(builderArgs), profile, requestOptions);
1390
1402
  }
1391
- async screen(payload) {
1403
+ async screen(payload, requestOptions) {
1392
1404
  const { profile, ...builderArgs } = payload;
1393
- return await this.sendExperienceEvent(this.core.eventBuilder.buildScreenView(builderArgs), profile);
1405
+ return await this.sendExperienceEvent(this.eventBuilder.buildScreenView(builderArgs), profile, requestOptions);
1394
1406
  }
1395
- async track(payload) {
1407
+ async track(payload, requestOptions) {
1396
1408
  const { profile, ...builderArgs } = payload;
1397
- return await this.sendExperienceEvent(this.core.eventBuilder.buildTrack(builderArgs), profile);
1409
+ return await this.sendExperienceEvent(this.eventBuilder.buildTrack(builderArgs), profile, requestOptions);
1398
1410
  }
1399
- async trackView(payload) {
1411
+ async trackView(payload, requestOptions) {
1400
1412
  const { profile, ...builderArgs } = payload;
1401
1413
  let result;
1402
1414
  let insightsProfile = profile;
1403
1415
  if (payload.sticky) {
1404
- result = await this.sendExperienceEvent(this.core.eventBuilder.buildView(builderArgs), profile);
1416
+ result = await this.sendExperienceEvent(this.eventBuilder.buildView(builderArgs), profile, requestOptions);
1405
1417
  const { profile: responseProfile } = result;
1406
1418
  insightsProfile = responseProfile;
1407
1419
  }
1408
- await this.sendInsightsEvent(this.core.eventBuilder.buildView(builderArgs), requireInsightsProfile(insightsProfile, payload.sticky ? STICKY_TRACK_VIEW_PROFILE_ERROR : NON_STICKY_TRACK_VIEW_PROFILE_ERROR));
1420
+ await this.sendInsightsEvent(this.eventBuilder.buildView(builderArgs), requireInsightsProfile(insightsProfile, payload.sticky ? STICKY_TRACK_VIEW_PROFILE_ERROR : NON_STICKY_TRACK_VIEW_PROFILE_ERROR));
1409
1421
  return result;
1410
1422
  }
1411
- async trackClick(payload) {
1423
+ async trackClick(payload, _requestOptions) {
1412
1424
  const { profile, ...builderArgs } = payload;
1413
- await this.sendInsightsEvent(this.core.eventBuilder.buildClick(builderArgs), requireInsightsProfile(profile, TRACK_CLICK_PROFILE_ERROR));
1425
+ await this.sendInsightsEvent(this.eventBuilder.buildClick(builderArgs), requireInsightsProfile(profile, TRACK_CLICK_PROFILE_ERROR));
1414
1426
  }
1415
- async trackHover(payload) {
1427
+ async trackHover(payload, _requestOptions) {
1416
1428
  const { profile, ...builderArgs } = payload;
1417
- await this.sendInsightsEvent(this.core.eventBuilder.buildHover(builderArgs), requireInsightsProfile(profile, TRACK_HOVER_PROFILE_ERROR));
1429
+ await this.sendInsightsEvent(this.eventBuilder.buildHover(builderArgs), requireInsightsProfile(profile, TRACK_HOVER_PROFILE_ERROR));
1418
1430
  }
1419
- async trackFlagView(payload) {
1431
+ async trackFlagView(payload, _requestOptions) {
1420
1432
  const { profile, ...builderArgs } = payload;
1421
- await this.sendInsightsEvent(this.core.eventBuilder.buildFlagView(builderArgs), requireInsightsProfile(profile, TRACK_FLAG_VIEW_PROFILE_ERROR));
1433
+ await this.sendInsightsEvent(this.eventBuilder.buildFlagView(builderArgs), requireInsightsProfile(profile, TRACK_FLAG_VIEW_PROFILE_ERROR));
1422
1434
  }
1423
- async sendExperienceEvent(event, profile) {
1424
- const intercepted = await this.core.interceptors.event.run(event);
1435
+ async sendExperienceEvent(event, profile, requestOptions) {
1436
+ const intercepted = await this.interceptors.event.run(event);
1425
1437
  const validEvent = (0, api_schemas_.parseWithFriendlyError)(api_schemas_.ExperienceEvent, intercepted);
1426
- return await this.core.api.experience.upsertProfile({
1438
+ return await this.api.experience.upsertProfile({
1427
1439
  profileId: profile?.id,
1428
1440
  events: [
1429
1441
  validEvent
1430
1442
  ]
1431
- }, this.options);
1443
+ }, requestOptions);
1432
1444
  }
1433
1445
  async sendInsightsEvent(event, profile) {
1434
- const intercepted = await this.core.interceptors.event.run(event);
1446
+ const intercepted = await this.interceptors.event.run(event);
1435
1447
  const validEvent = (0, api_schemas_.parseWithFriendlyError)(api_schemas_.InsightsEvent, intercepted);
1436
1448
  const batchEvent = (0, api_schemas_.parseWithFriendlyError)(api_schemas_.BatchInsightsEventArray, [
1437
1449
  {
@@ -1441,33 +1453,7 @@ var __webpack_exports__ = {};
1441
1453
  ]
1442
1454
  }
1443
1455
  ]);
1444
- await this.core.api.insights.sendBatchEvents(batchEvent);
1445
- }
1446
- }
1447
- const CoreStateless_hasDefinedValues = (record)=>Object.values(record).some((value)=>void 0 !== value);
1448
- const createStatelessExperienceApiConfig = (api)=>{
1449
- if (void 0 === api) return;
1450
- const experienceConfig = {
1451
- baseUrl: api.experienceBaseUrl,
1452
- enabledFeatures: api.enabledFeatures
1453
- };
1454
- return CoreStateless_hasDefinedValues(experienceConfig) ? experienceConfig : void 0;
1455
- };
1456
- const createStatelessInsightsApiConfig = (api)=>{
1457
- if (api?.insightsBaseUrl === void 0) return;
1458
- return {
1459
- baseUrl: api.insightsBaseUrl
1460
- };
1461
- };
1462
- class CoreStateless extends src_CoreBase {
1463
- constructor(config){
1464
- super(config, {
1465
- experience: createStatelessExperienceApiConfig(config.api),
1466
- insights: createStatelessInsightsApiConfig(config.api)
1467
- });
1468
- }
1469
- forRequest(options = {}) {
1470
- return new CoreStatelessRequestScope(this, options);
1456
+ await this.api.insights.sendBatchEvents(batchEvent);
1471
1457
  }
1472
1458
  }
1473
1459
  const src_CoreStateless = CoreStateless;
@@ -1524,7 +1510,6 @@ exports.CONSENT_KEY = __webpack_exports__.CONSENT_KEY;
1524
1510
  exports.ClickBuilderArgs = __webpack_exports__.ClickBuilderArgs;
1525
1511
  exports.CoreStateful = __webpack_exports__.CoreStateful;
1526
1512
  exports.CoreStateless = __webpack_exports__.CoreStateless;
1527
- exports.CoreStatelessRequestScope = __webpack_exports__.CoreStatelessRequestScope;
1528
1513
  exports.DEBUG_FLAG_KEY = __webpack_exports__.DEBUG_FLAG_KEY;
1529
1514
  exports.DEFAULT_PAGE_PROPERTIES = __webpack_exports__.DEFAULT_PAGE_PROPERTIES;
1530
1515
  exports.EntryInteractionBuilderArgsBase = __webpack_exports__.EntryInteractionBuilderArgsBase;
@@ -1564,7 +1549,6 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
1564
1549
  "ClickBuilderArgs",
1565
1550
  "CoreStateful",
1566
1551
  "CoreStateless",
1567
- "CoreStatelessRequestScope",
1568
1552
  "DEBUG_FLAG_KEY",
1569
1553
  "DEFAULT_PAGE_PROPERTIES",
1570
1554
  "EntryInteractionBuilderArgsBase",