@xenia-martech/sdk-js 1.0.0-dev.26 → 1.0.0-dev.27

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 (36) hide show
  1. package/README.md +21 -1
  2. package/dist/{actions-DFLoSXO_.d.cts → actions-CkiSdvSa.d.cts} +65 -5
  3. package/dist/{actions-DFLoSXO_.d.ts → actions-CkiSdvSa.d.ts} +65 -5
  4. package/dist/{chunk-D37RD3UC.js → chunk-7D2HJMW6.js} +31 -3
  5. package/dist/chunk-7D2HJMW6.js.map +1 -0
  6. package/dist/{chunk-3KYKKIIG.cjs → chunk-UOW3GK4I.cjs} +32 -2
  7. package/dist/chunk-UOW3GK4I.cjs.map +1 -0
  8. package/dist/client/index.cjs +73 -8
  9. package/dist/client/index.cjs.map +1 -1
  10. package/dist/client/index.d.cts +2 -2
  11. package/dist/client/index.d.ts +2 -2
  12. package/dist/client/index.js +73 -8
  13. package/dist/client/index.js.map +1 -1
  14. package/dist/client/react/index.cjs +73 -8
  15. package/dist/client/react/index.cjs.map +1 -1
  16. package/dist/client/react/index.d.cts +2 -2
  17. package/dist/client/react/index.d.ts +2 -2
  18. package/dist/client/react/index.js +73 -8
  19. package/dist/client/react/index.js.map +1 -1
  20. package/dist/{cookie-names-LgNDAreS.d.cts → cookie-names-C3LKDhsg.d.cts} +34 -0
  21. package/dist/{cookie-names-LgNDAreS.d.ts → cookie-names-C3LKDhsg.d.ts} +34 -0
  22. package/dist/index.cjs +23 -19
  23. package/dist/index.d.cts +23 -3
  24. package/dist/index.d.ts +23 -3
  25. package/dist/index.js +1 -1
  26. package/dist/server/index.cjs +53 -22
  27. package/dist/server/index.cjs.map +1 -1
  28. package/dist/server/index.d.cts +31 -11
  29. package/dist/server/index.d.ts +31 -11
  30. package/dist/server/index.js +43 -12
  31. package/dist/server/index.js.map +1 -1
  32. package/dist/xenia-sdk.iife.js +3 -3
  33. package/dist/xenia-sdk.iife.js.map +1 -1
  34. package/package.json +1 -1
  35. package/dist/chunk-3KYKKIIG.cjs.map +0 -1
  36. package/dist/chunk-D37RD3UC.js.map +0 -1
@@ -86,6 +86,23 @@ interface XeniaConfig {
86
86
  * cookie read via `getVisitorId()`.
87
87
  */
88
88
  visitorId?: string;
89
+ /**
90
+ * The ambient request context for a **server** client — the SDK-provided page/campaign fields
91
+ * (`path`, `query`, `url`, `referrer`, `utmSource`/`utmMedium`/`utmCampaign`/`utmContent`/`utmId`)
92
+ * that get attached to EVERY `resolve()`/`resolveBatch()`/`webhook()` made with this client, so
93
+ * journey rules can gate on `query.<name>` or `path` without any per-call wiring.
94
+ *
95
+ * The browser client needs nothing here — it reads these off `window.location` itself. A server
96
+ * runtime has no ambient request, so bind it once when constructing the per-request client:
97
+ *
98
+ * ```ts
99
+ * createXeniaServer({ …, context: contextFromUrl(currentUrl, referrer) })
100
+ * ```
101
+ *
102
+ * Lowest precedence: a per-call `context`/`identity` overrides it, and the SDK-owned `visitorId`
103
+ * still wins over everything.
104
+ */
105
+ context?: EventContext;
89
106
  /**
90
107
  * Persist the SDK visitor id to a first-party `xenia_vid` cookie so your
91
108
  * **same-origin** server can read it (`getVisitorId(cookieHeader)`) and bind
@@ -304,6 +321,23 @@ interface ResolveOptions {
304
321
  locale?: string;
305
322
  /** When true, ask the server to splice and return `content`. */
306
323
  merge?: boolean;
324
+ /**
325
+ * Caller-supplied identity fields (`deviceId`/`visitorId`/`customerId`/
326
+ * `email`), merged into `data` the same way `ServerWebhookOptions.identity`
327
+ * is merged for `webhook()`. Only relevant server-side — the browser client
328
+ * collects these ambiently instead and ignores this field. The SDK-owned
329
+ * `visitorId` (from `config.visitorId`) still wins over a value supplied
330
+ * here, same precedence as `webhook()`.
331
+ */
332
+ identity?: EventIdentity;
333
+ /**
334
+ * Caller-supplied page context (`path`/`query`/`url`/`referrer`/`utm*`),
335
+ * merged into `data` the same way `ServerWebhookOptions.context` is merged
336
+ * for `webhook()`. There's no ambient `window`/`document` server-side, so a
337
+ * server caller who wants these SDK-provided fields on a resolve() call
338
+ * must pass them explicitly (e.g. from the current request's headers).
339
+ */
340
+ context?: EventContext;
307
341
  }
308
342
  /** Body posted to `POST /api/events/v1/identify` — binds visitor to a known user. */
309
343
  interface IdentifyRequest {
@@ -86,6 +86,23 @@ interface XeniaConfig {
86
86
  * cookie read via `getVisitorId()`.
87
87
  */
88
88
  visitorId?: string;
89
+ /**
90
+ * The ambient request context for a **server** client — the SDK-provided page/campaign fields
91
+ * (`path`, `query`, `url`, `referrer`, `utmSource`/`utmMedium`/`utmCampaign`/`utmContent`/`utmId`)
92
+ * that get attached to EVERY `resolve()`/`resolveBatch()`/`webhook()` made with this client, so
93
+ * journey rules can gate on `query.<name>` or `path` without any per-call wiring.
94
+ *
95
+ * The browser client needs nothing here — it reads these off `window.location` itself. A server
96
+ * runtime has no ambient request, so bind it once when constructing the per-request client:
97
+ *
98
+ * ```ts
99
+ * createXeniaServer({ …, context: contextFromUrl(currentUrl, referrer) })
100
+ * ```
101
+ *
102
+ * Lowest precedence: a per-call `context`/`identity` overrides it, and the SDK-owned `visitorId`
103
+ * still wins over everything.
104
+ */
105
+ context?: EventContext;
89
106
  /**
90
107
  * Persist the SDK visitor id to a first-party `xenia_vid` cookie so your
91
108
  * **same-origin** server can read it (`getVisitorId(cookieHeader)`) and bind
@@ -304,6 +321,23 @@ interface ResolveOptions {
304
321
  locale?: string;
305
322
  /** When true, ask the server to splice and return `content`. */
306
323
  merge?: boolean;
324
+ /**
325
+ * Caller-supplied identity fields (`deviceId`/`visitorId`/`customerId`/
326
+ * `email`), merged into `data` the same way `ServerWebhookOptions.identity`
327
+ * is merged for `webhook()`. Only relevant server-side — the browser client
328
+ * collects these ambiently instead and ignores this field. The SDK-owned
329
+ * `visitorId` (from `config.visitorId`) still wins over a value supplied
330
+ * here, same precedence as `webhook()`.
331
+ */
332
+ identity?: EventIdentity;
333
+ /**
334
+ * Caller-supplied page context (`path`/`query`/`url`/`referrer`/`utm*`),
335
+ * merged into `data` the same way `ServerWebhookOptions.context` is merged
336
+ * for `webhook()`. There's no ambient `window`/`document` server-side, so a
337
+ * server caller who wants these SDK-provided fields on a resolve() call
338
+ * must pass them explicitly (e.g. from the current request's headers).
339
+ */
340
+ context?: EventContext;
307
341
  }
308
342
  /** Body posted to `POST /api/events/v1/identify` — binds visitor to a known user. */
309
343
  interface IdentifyRequest {
package/dist/index.cjs CHANGED
@@ -1,80 +1,84 @@
1
1
  'use strict';
2
2
 
3
- var chunk3KYKKIIG_cjs = require('./chunk-3KYKKIIG.cjs');
3
+ var chunkUOW3GK4I_cjs = require('./chunk-UOW3GK4I.cjs');
4
4
 
5
5
 
6
6
 
7
7
  Object.defineProperty(exports, "CONSENT_COOKIE", {
8
8
  enumerable: true,
9
- get: function () { return chunk3KYKKIIG_cjs.CONSENT_COOKIE; }
9
+ get: function () { return chunkUOW3GK4I_cjs.CONSENT_COOKIE; }
10
10
  });
11
11
  Object.defineProperty(exports, "DEFAULT_PREFIX", {
12
12
  enumerable: true,
13
- get: function () { return chunk3KYKKIIG_cjs.DEFAULT_PREFIX; }
13
+ get: function () { return chunkUOW3GK4I_cjs.DEFAULT_PREFIX; }
14
14
  });
15
15
  Object.defineProperty(exports, "HttpClient", {
16
16
  enumerable: true,
17
- get: function () { return chunk3KYKKIIG_cjs.HttpClient; }
17
+ get: function () { return chunkUOW3GK4I_cjs.HttpClient; }
18
18
  });
19
19
  Object.defineProperty(exports, "HttpError", {
20
20
  enumerable: true,
21
- get: function () { return chunk3KYKKIIG_cjs.HttpError; }
21
+ get: function () { return chunkUOW3GK4I_cjs.HttpError; }
22
22
  });
23
23
  Object.defineProperty(exports, "LocalStorageResponseStore", {
24
24
  enumerable: true,
25
- get: function () { return chunk3KYKKIIG_cjs.LocalStorageResponseStore; }
25
+ get: function () { return chunkUOW3GK4I_cjs.LocalStorageResponseStore; }
26
26
  });
27
27
  Object.defineProperty(exports, "MAX_EVENT_ID_LENGTH", {
28
28
  enumerable: true,
29
- get: function () { return chunk3KYKKIIG_cjs.MAX_EVENT_ID_LENGTH; }
29
+ get: function () { return chunkUOW3GK4I_cjs.MAX_EVENT_ID_LENGTH; }
30
30
  });
31
31
  Object.defineProperty(exports, "MemoryResponseStore", {
32
32
  enumerable: true,
33
- get: function () { return chunk3KYKKIIG_cjs.MemoryResponseStore; }
33
+ get: function () { return chunkUOW3GK4I_cjs.MemoryResponseStore; }
34
34
  });
35
35
  Object.defineProperty(exports, "VISITOR_COOKIE", {
36
36
  enumerable: true,
37
- get: function () { return chunk3KYKKIIG_cjs.VISITOR_COOKIE; }
37
+ get: function () { return chunkUOW3GK4I_cjs.VISITOR_COOKIE; }
38
38
  });
39
39
  Object.defineProperty(exports, "XeniaApi", {
40
40
  enumerable: true,
41
- get: function () { return chunk3KYKKIIG_cjs.XeniaApi; }
41
+ get: function () { return chunkUOW3GK4I_cjs.XeniaApi; }
42
42
  });
43
43
  Object.defineProperty(exports, "buildUrl", {
44
44
  enumerable: true,
45
- get: function () { return chunk3KYKKIIG_cjs.buildUrl; }
45
+ get: function () { return chunkUOW3GK4I_cjs.buildUrl; }
46
46
  });
47
47
  Object.defineProperty(exports, "consentCookieName", {
48
48
  enumerable: true,
49
- get: function () { return chunk3KYKKIIG_cjs.consentCookieName; }
49
+ get: function () { return chunkUOW3GK4I_cjs.consentCookieName; }
50
+ });
51
+ Object.defineProperty(exports, "contextFromUrl", {
52
+ enumerable: true,
53
+ get: function () { return chunkUOW3GK4I_cjs.contextFromUrl; }
50
54
  });
51
55
  Object.defineProperty(exports, "extractContentSwaps", {
52
56
  enumerable: true,
53
- get: function () { return chunk3KYKKIIG_cjs.extractContentSwaps; }
57
+ get: function () { return chunkUOW3GK4I_cjs.extractContentSwaps; }
54
58
  });
55
59
  Object.defineProperty(exports, "generateUlid", {
56
60
  enumerable: true,
57
- get: function () { return chunk3KYKKIIG_cjs.generateUlid; }
61
+ get: function () { return chunkUOW3GK4I_cjs.generateUlid; }
58
62
  });
59
63
  Object.defineProperty(exports, "isBrowser", {
60
64
  enumerable: true,
61
- get: function () { return chunk3KYKKIIG_cjs.isBrowser; }
65
+ get: function () { return chunkUOW3GK4I_cjs.isBrowser; }
62
66
  });
63
67
  Object.defineProperty(exports, "parseActions", {
64
68
  enumerable: true,
65
- get: function () { return chunk3KYKKIIG_cjs.parseActions; }
69
+ get: function () { return chunkUOW3GK4I_cjs.parseActions; }
66
70
  });
67
71
  Object.defineProperty(exports, "spliceSlots", {
68
72
  enumerable: true,
69
- get: function () { return chunk3KYKKIIG_cjs.spliceSlots; }
73
+ get: function () { return chunkUOW3GK4I_cjs.spliceSlots; }
70
74
  });
71
75
  Object.defineProperty(exports, "toEventId", {
72
76
  enumerable: true,
73
- get: function () { return chunk3KYKKIIG_cjs.toEventId; }
77
+ get: function () { return chunkUOW3GK4I_cjs.toEventId; }
74
78
  });
75
79
  Object.defineProperty(exports, "visitorCookieName", {
76
80
  enumerable: true,
77
- get: function () { return chunk3KYKKIIG_cjs.visitorCookieName; }
81
+ get: function () { return chunkUOW3GK4I_cjs.visitorCookieName; }
78
82
  });
79
83
  //# sourceMappingURL=index.cjs.map
80
84
  //# sourceMappingURL=index.cjs.map
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RetryConfig, H as HttpMethod, J as JsonValue, W as WebhookResponse } from './cookie-names-LgNDAreS.cjs';
2
- export { A as AnalyticsContext, a as AnalyticsEventName, b as AnalyticsIdentity, c as AnalyticsIngestResponse, C as CONSENT_COOKIE, d as ConsentState, D as DEFAULT_PREFIX, E as EventContext, e as EventIdentity, I as IdentifyRequest, f as IdentifyResponse, g as InteractionType, h as JsonObject, M as MessageCta, i as MessagePlacement, P as PersonalizationSwap, j as ResolveBatchItem, k as ResolveBatchResult, l as ResolveOptions, m as ResolveResult, T as TrackOptions, V as VISITOR_COOKIE, X as XeniaAction, n as XeniaApi, o as XeniaConfig, p as XeniaEvent, q as consentCookieName, r as parseActions, v as visitorCookieName } from './cookie-names-LgNDAreS.cjs';
1
+ import { R as RetryConfig, H as HttpMethod, J as JsonValue, E as EventContext, W as WebhookResponse } from './cookie-names-C3LKDhsg.cjs';
2
+ export { A as AnalyticsContext, a as AnalyticsEventName, b as AnalyticsIdentity, c as AnalyticsIngestResponse, C as CONSENT_COOKIE, d as ConsentState, D as DEFAULT_PREFIX, e as EventIdentity, I as IdentifyRequest, f as IdentifyResponse, g as InteractionType, h as JsonObject, M as MessageCta, i as MessagePlacement, P as PersonalizationSwap, j as ResolveBatchItem, k as ResolveBatchResult, l as ResolveOptions, m as ResolveResult, T as TrackOptions, V as VISITOR_COOKIE, X as XeniaAction, n as XeniaApi, o as XeniaConfig, p as XeniaEvent, q as consentCookieName, r as parseActions, v as visitorCookieName } from './cookie-names-C3LKDhsg.cjs';
3
3
 
4
4
  /** Returns true when running in a browser environment (SSR-safe). */
5
5
  declare function isBrowser(): boolean;
@@ -121,6 +121,26 @@ declare function spliceSlots(slots: Record<string, JsonValue>, swaps: ContentSwa
121
121
  /** Extract `xeniaContentSwaps` from a webhook-response body, if present and well-formed. */
122
122
  declare function extractContentSwaps(body: JsonValue): ContentSwap[] | undefined;
123
123
 
124
+ /**
125
+ * Derive the SDK-provided page/campaign fields from a URL — the server-side counterpart to the
126
+ * browser client's `Identity.collectContext()`, which reads the same fields off `window.location`.
127
+ *
128
+ * Exists because a server runtime has no ambient `window`: a Node client is constructed per request,
129
+ * so the one thing it needs handed over is the URL being served. Everything a journey rule can
130
+ * reference — `path`, `query.<name>`, `url`, `referrer`, `utmSource`/`utmMedium`/`utmCampaign`/
131
+ * `utmContent`/`utmId` — is derived here rather than by each app, so a server-rendered page gets the
132
+ * same field set a browser page does without hand-rolling UTM parsing at every call site.
133
+ *
134
+ * Field names and semantics deliberately mirror `collectContext()` exactly: `query` is the flattened
135
+ * search params, UTMs come from the standard `utm_*` params, and absent values are omitted rather
136
+ * than set to `""` (a blank string is a value a condition could accidentally match).
137
+ *
138
+ * @param url Absolute URL of the request being served (e.g. `https://shop.example/product/x?ref=a`).
139
+ * A relative or unparseable value yields an empty context rather than throwing.
140
+ * @param referrer Optional referring URL, typically the request's `Referer` header.
141
+ */
142
+ declare function contextFromUrl(url: string | null | undefined, referrer?: string | null): EventContext;
143
+
124
144
  /**
125
145
  * Persists webhook responses the SDK receives, keyed by an arbitrary string
126
146
  * (typically the eventId, triggerId, or journeyId). Lets the app read back the
@@ -155,4 +175,4 @@ declare class LocalStorageResponseStore implements ResponseStore {
155
175
  clear(): void;
156
176
  }
157
177
 
158
- export { type ContentSwap, HttpClient, type HttpClientOptions, HttpError, HttpMethod, type HttpResult, JsonValue, LocalStorageResponseStore, MAX_EVENT_ID_LENGTH, MemoryResponseStore, type RequestOptions, type ResponseStore, RetryConfig, WebhookResponse, buildUrl, extractContentSwaps, generateUlid, isBrowser, spliceSlots, toEventId };
178
+ export { type ContentSwap, EventContext, HttpClient, type HttpClientOptions, HttpError, HttpMethod, type HttpResult, JsonValue, LocalStorageResponseStore, MAX_EVENT_ID_LENGTH, MemoryResponseStore, type RequestOptions, type ResponseStore, RetryConfig, WebhookResponse, buildUrl, contextFromUrl, extractContentSwaps, generateUlid, isBrowser, spliceSlots, toEventId };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RetryConfig, H as HttpMethod, J as JsonValue, W as WebhookResponse } from './cookie-names-LgNDAreS.js';
2
- export { A as AnalyticsContext, a as AnalyticsEventName, b as AnalyticsIdentity, c as AnalyticsIngestResponse, C as CONSENT_COOKIE, d as ConsentState, D as DEFAULT_PREFIX, E as EventContext, e as EventIdentity, I as IdentifyRequest, f as IdentifyResponse, g as InteractionType, h as JsonObject, M as MessageCta, i as MessagePlacement, P as PersonalizationSwap, j as ResolveBatchItem, k as ResolveBatchResult, l as ResolveOptions, m as ResolveResult, T as TrackOptions, V as VISITOR_COOKIE, X as XeniaAction, n as XeniaApi, o as XeniaConfig, p as XeniaEvent, q as consentCookieName, r as parseActions, v as visitorCookieName } from './cookie-names-LgNDAreS.js';
1
+ import { R as RetryConfig, H as HttpMethod, J as JsonValue, E as EventContext, W as WebhookResponse } from './cookie-names-C3LKDhsg.js';
2
+ export { A as AnalyticsContext, a as AnalyticsEventName, b as AnalyticsIdentity, c as AnalyticsIngestResponse, C as CONSENT_COOKIE, d as ConsentState, D as DEFAULT_PREFIX, e as EventIdentity, I as IdentifyRequest, f as IdentifyResponse, g as InteractionType, h as JsonObject, M as MessageCta, i as MessagePlacement, P as PersonalizationSwap, j as ResolveBatchItem, k as ResolveBatchResult, l as ResolveOptions, m as ResolveResult, T as TrackOptions, V as VISITOR_COOKIE, X as XeniaAction, n as XeniaApi, o as XeniaConfig, p as XeniaEvent, q as consentCookieName, r as parseActions, v as visitorCookieName } from './cookie-names-C3LKDhsg.js';
3
3
 
4
4
  /** Returns true when running in a browser environment (SSR-safe). */
5
5
  declare function isBrowser(): boolean;
@@ -121,6 +121,26 @@ declare function spliceSlots(slots: Record<string, JsonValue>, swaps: ContentSwa
121
121
  /** Extract `xeniaContentSwaps` from a webhook-response body, if present and well-formed. */
122
122
  declare function extractContentSwaps(body: JsonValue): ContentSwap[] | undefined;
123
123
 
124
+ /**
125
+ * Derive the SDK-provided page/campaign fields from a URL — the server-side counterpart to the
126
+ * browser client's `Identity.collectContext()`, which reads the same fields off `window.location`.
127
+ *
128
+ * Exists because a server runtime has no ambient `window`: a Node client is constructed per request,
129
+ * so the one thing it needs handed over is the URL being served. Everything a journey rule can
130
+ * reference — `path`, `query.<name>`, `url`, `referrer`, `utmSource`/`utmMedium`/`utmCampaign`/
131
+ * `utmContent`/`utmId` — is derived here rather than by each app, so a server-rendered page gets the
132
+ * same field set a browser page does without hand-rolling UTM parsing at every call site.
133
+ *
134
+ * Field names and semantics deliberately mirror `collectContext()` exactly: `query` is the flattened
135
+ * search params, UTMs come from the standard `utm_*` params, and absent values are omitted rather
136
+ * than set to `""` (a blank string is a value a condition could accidentally match).
137
+ *
138
+ * @param url Absolute URL of the request being served (e.g. `https://shop.example/product/x?ref=a`).
139
+ * A relative or unparseable value yields an empty context rather than throwing.
140
+ * @param referrer Optional referring URL, typically the request's `Referer` header.
141
+ */
142
+ declare function contextFromUrl(url: string | null | undefined, referrer?: string | null): EventContext;
143
+
124
144
  /**
125
145
  * Persists webhook responses the SDK receives, keyed by an arbitrary string
126
146
  * (typically the eventId, triggerId, or journeyId). Lets the app read back the
@@ -155,4 +175,4 @@ declare class LocalStorageResponseStore implements ResponseStore {
155
175
  clear(): void;
156
176
  }
157
177
 
158
- export { type ContentSwap, HttpClient, type HttpClientOptions, HttpError, HttpMethod, type HttpResult, JsonValue, LocalStorageResponseStore, MAX_EVENT_ID_LENGTH, MemoryResponseStore, type RequestOptions, type ResponseStore, RetryConfig, WebhookResponse, buildUrl, extractContentSwaps, generateUlid, isBrowser, spliceSlots, toEventId };
178
+ export { type ContentSwap, EventContext, HttpClient, type HttpClientOptions, HttpError, HttpMethod, type HttpResult, JsonValue, LocalStorageResponseStore, MAX_EVENT_ID_LENGTH, MemoryResponseStore, type RequestOptions, type ResponseStore, RetryConfig, WebhookResponse, buildUrl, contextFromUrl, extractContentSwaps, generateUlid, isBrowser, spliceSlots, toEventId };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { CONSENT_COOKIE, DEFAULT_PREFIX, HttpClient, HttpError, LocalStorageResponseStore, MAX_EVENT_ID_LENGTH, MemoryResponseStore, VISITOR_COOKIE, XeniaApi, buildUrl, consentCookieName, extractContentSwaps, generateUlid, isBrowser, parseActions, spliceSlots, toEventId, visitorCookieName } from './chunk-D37RD3UC.js';
1
+ export { CONSENT_COOKIE, DEFAULT_PREFIX, HttpClient, HttpError, LocalStorageResponseStore, MAX_EVENT_ID_LENGTH, MemoryResponseStore, VISITOR_COOKIE, XeniaApi, buildUrl, consentCookieName, contextFromUrl, extractContentSwaps, generateUlid, isBrowser, parseActions, spliceSlots, toEventId, visitorCookieName } from './chunk-7D2HJMW6.js';
2
2
  //# sourceMappingURL=index.js.map
3
3
  //# sourceMappingURL=index.js.map
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunk3KYKKIIG_cjs = require('../chunk-3KYKKIIG.cjs');
3
+ var chunkUOW3GK4I_cjs = require('../chunk-UOW3GK4I.cjs');
4
4
 
5
5
  // src/core/event-payload.ts
6
6
  var RESERVED = /* @__PURE__ */ new Set([
@@ -21,7 +21,7 @@ var RESERVED = /* @__PURE__ */ new Set([
21
21
  "path",
22
22
  "query"
23
23
  ]);
24
- function compact(obj) {
24
+ function compact2(obj) {
25
25
  const out = {};
26
26
  for (const [k, v] of Object.entries(obj)) {
27
27
  if (v !== void 0) out[k] = v;
@@ -45,8 +45,8 @@ function buildEventPayload(input) {
45
45
  // what they reliably know (real page url, visitor/device id), so a stray prop
46
46
  // can't override the true page context — see merge order below.
47
47
  ...reserved,
48
- ...compact(input.context ?? {}),
49
- ...compact(input.identity ?? {}),
48
+ ...compact2(input.context ?? {}),
49
+ ...compact2(input.identity ?? {}),
50
50
  eventId: input.eventId,
51
51
  ...input.eventType ? { eventType: input.eventType } : {},
52
52
  ...input.journeyId ? { journeyId: input.journeyId } : {}
@@ -59,9 +59,12 @@ var XeniaServer = class {
59
59
  api;
60
60
  /** SDK-owned visitor anchor for this (per-request) client — see {@link XeniaConfig.visitorId}. */
61
61
  visitorId;
62
+ /** Ambient request context attached to every call — see {@link XeniaConfig.context}. */
63
+ context;
62
64
  constructor(config) {
63
- this.api = new chunk3KYKKIIG_cjs.XeniaApi(config);
65
+ this.api = new chunkUOW3GK4I_cjs.XeniaApi(config);
64
66
  this.visitorId = config.visitorId;
67
+ this.context = config.context;
65
68
  }
66
69
  /**
67
70
  * Send a unified event server-side (`POST /api/events/v1`) and return the
@@ -72,7 +75,7 @@ var XeniaServer = class {
72
75
  async track(eventName, opts = {}) {
73
76
  const event = {
74
77
  eventName,
75
- eventId: opts.eventId ? chunk3KYKKIIG_cjs.toEventId(opts.eventId) : chunk3KYKKIIG_cjs.generateUlid(),
78
+ eventId: opts.eventId ? chunkUOW3GK4I_cjs.toEventId(opts.eventId) : chunkUOW3GK4I_cjs.generateUlid(),
76
79
  eventVersion: opts.eventVersion ?? 1,
77
80
  occurredAtMs: opts.occurredAtMs ?? Date.now(),
78
81
  source: opts.source ?? "xenia-server-sdk",
@@ -103,7 +106,9 @@ var XeniaServer = class {
103
106
  // cookie per request) is attached automatically. Falls back to a
104
107
  // per-call visitorId only when none is bound.
105
108
  identity: { ...opts.identity, visitorId: this.visitorId ?? opts.identity?.visitorId },
106
- context: opts.context,
109
+ // Ambient request context (config.context, bound once per request) under a per-call
110
+ // override — same precedence resolve() uses, so both surfaces carry the same fields.
111
+ context: { ...this.context, ...opts.context },
107
112
  props: data
108
113
  });
109
114
  return this.api.webhook(triggerId, payload, opts.method ?? "POST");
@@ -117,6 +122,13 @@ var XeniaServer = class {
117
122
  * (`POST /api/personalization/resolve`). Requires a configured SDK token
118
123
  * (`token`/`getToken`) — resolve is auth-required. With `merge: true` the
119
124
  * returned `content` is server-spliced.
125
+ *
126
+ * Pass `opts.identity`/`opts.context` to attach the same SDK-provided
127
+ * fields (`deviceId`, `customerId`, `email`, `path`, `url`, `referrer`,
128
+ * `utm*`, …) the browser client collects ambiently — there's no ambient
129
+ * `window`/`document` server-side, so a server caller must supply whatever
130
+ * it knows (e.g. from the current request) explicitly. See
131
+ * {@link ResolveOptions}.
120
132
  */
121
133
  async resolve(contentKey, opts) {
122
134
  return this.api.resolve(contentKey, this.withVisitor(opts));
@@ -126,14 +138,33 @@ var XeniaServer = class {
126
138
  return this.api.resolveBatch(items, this.withVisitor(opts));
127
139
  }
128
140
  /**
129
- * Stamp the SDK-owned visitor id (`config.visitorId`) into the resolve `data`
130
- * under the reserved `_xenia_vid` key so the backend can bucket holdout
131
- * experiments and record exposure per visitor. Set last so a caller can't
132
- * override it; a no-op when no `visitorId` was bound on this client.
141
+ * Merge the ambient `config.context` and `opts.identity`/`opts.context` into
142
+ * the resolve `data`, then stamp the SDK-owned visitor id
143
+ * (`config.visitorId`) under both the reserved `_xenia_vid` key (read by the
144
+ * backend for holdout/exposure bucketing) and the flat `visitorId` field —
145
+ * mirrors `webhook()`'s merge order/precedence exactly: caller `data` <
146
+ * ambient `config.context` < caller `context`/`identity` < SDK-owned
147
+ * `visitorId`/`_xenia_vid`, set LAST so neither can be overridden per call.
148
+ *
149
+ * `config.context` is what makes the SDK-provided fields automatic on the
150
+ * server: bound once per request, every `resolve()` carries `path`/`query`/
151
+ * `url`/`referrer`/UTMs with no per-call wiring, matching what the browser
152
+ * client collects from `window` on its own. A no-op when nothing is bound and
153
+ * nothing was passed — preserving today's behavior for callers using neither.
133
154
  */
134
155
  withVisitor(opts) {
135
- if (!this.visitorId) return opts;
136
- return { ...opts, data: { ...opts?.data ?? {}, _xenia_vid: this.visitorId } };
156
+ const identity = chunkUOW3GK4I_cjs.compact({
157
+ ...opts?.identity,
158
+ visitorId: this.visitorId ?? opts?.identity?.visitorId
159
+ });
160
+ const merged = {
161
+ ...chunkUOW3GK4I_cjs.compact(this.context ?? {}),
162
+ ...chunkUOW3GK4I_cjs.compact(opts?.context ?? {}),
163
+ ...identity
164
+ };
165
+ if (Object.keys(merged).length === 0 && !this.visitorId) return opts;
166
+ if (this.visitorId) merged._xenia_vid = this.visitorId;
167
+ return { ...opts, data: { ...opts?.data ?? {}, ...merged } };
137
168
  }
138
169
  };
139
170
  function createXeniaServer(config) {
@@ -142,7 +173,7 @@ function createXeniaServer(config) {
142
173
 
143
174
  // src/server/token.ts
144
175
  async function refreshSdkToken(opts) {
145
- const http = new chunk3KYKKIIG_cjs.HttpClient({ baseUrl: opts.baseUrl, timeout: opts.timeout });
176
+ const http = new chunkUOW3GK4I_cjs.HttpClient({ baseUrl: opts.baseUrl, timeout: opts.timeout });
146
177
  const { data } = await http.request({
147
178
  method: "POST",
148
179
  path: "/api/personalization/token/refresh",
@@ -187,10 +218,10 @@ function parseCookies(header) {
187
218
  return out;
188
219
  }
189
220
  function getVisitorId(cookieHeader, prefix) {
190
- return parseCookies(cookieHeader)[chunk3KYKKIIG_cjs.visitorCookieName(prefix)] ?? null;
221
+ return parseCookies(cookieHeader)[chunkUOW3GK4I_cjs.visitorCookieName(prefix)] ?? null;
191
222
  }
192
223
  function getConsent(cookieHeader, prefix) {
193
- const raw = parseCookies(cookieHeader)[chunk3KYKKIIG_cjs.consentCookieName(prefix)];
224
+ const raw = parseCookies(cookieHeader)[chunkUOW3GK4I_cjs.consentCookieName(prefix)];
194
225
  if (!raw) return { analytics: false, marketing: false, personalization: false };
195
226
  try {
196
227
  const parsed = JSON.parse(raw);
@@ -207,27 +238,27 @@ function getConsent(cookieHeader, prefix) {
207
238
 
208
239
  Object.defineProperty(exports, "CONSENT_COOKIE", {
209
240
  enumerable: true,
210
- get: function () { return chunk3KYKKIIG_cjs.CONSENT_COOKIE; }
241
+ get: function () { return chunkUOW3GK4I_cjs.CONSENT_COOKIE; }
211
242
  });
212
243
  Object.defineProperty(exports, "VISITOR_COOKIE", {
213
244
  enumerable: true,
214
- get: function () { return chunk3KYKKIIG_cjs.VISITOR_COOKIE; }
245
+ get: function () { return chunkUOW3GK4I_cjs.VISITOR_COOKIE; }
215
246
  });
216
247
  Object.defineProperty(exports, "XeniaApi", {
217
248
  enumerable: true,
218
- get: function () { return chunk3KYKKIIG_cjs.XeniaApi; }
249
+ get: function () { return chunkUOW3GK4I_cjs.XeniaApi; }
219
250
  });
220
251
  Object.defineProperty(exports, "consentCookieName", {
221
252
  enumerable: true,
222
- get: function () { return chunk3KYKKIIG_cjs.consentCookieName; }
253
+ get: function () { return chunkUOW3GK4I_cjs.consentCookieName; }
223
254
  });
224
255
  Object.defineProperty(exports, "parseActions", {
225
256
  enumerable: true,
226
- get: function () { return chunk3KYKKIIG_cjs.parseActions; }
257
+ get: function () { return chunkUOW3GK4I_cjs.parseActions; }
227
258
  });
228
259
  Object.defineProperty(exports, "visitorCookieName", {
229
260
  enumerable: true,
230
- get: function () { return chunk3KYKKIIG_cjs.visitorCookieName; }
261
+ get: function () { return chunkUOW3GK4I_cjs.visitorCookieName; }
231
262
  });
232
263
  exports.XeniaServer = XeniaServer;
233
264
  exports.createTokenRoute = createTokenRoute;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/core/event-payload.ts","../../src/server/xenia-server.ts","../../src/server/token.ts","../../src/server/cookies.ts"],"names":["XeniaApi","toEventId","generateUlid","HttpClient","visitorCookieName","consentCookieName"],"mappings":";;;;;AAGA,IAAM,QAAA,uBAAe,GAAA,CAAI;AAAA,EACvB,SAAA;AAAA,EACA,WAAA;AAAA,EACA,WAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,YAAA;AAAA,EACA,OAAA;AAAA,EACA,WAAA;AAAA,EACA,WAAA;AAAA,EACA,aAAA;AAAA,EACA,YAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,UAAA;AAAA,EACA,MAAA;AAAA,EACA;AACF,CAAC,CAAA;AAaD,SAAS,QAAQ,GAAA,EAAuE;AACtF,EAAA,MAAM,MAAiC,EAAC;AACxC,EAAA,KAAA,MAAW,CAAC,CAAA,EAAG,CAAC,KAAK,MAAA,CAAO,OAAA,CAAQ,GAAG,CAAA,EAAG;AACxC,IAAA,IAAI,CAAA,KAAM,MAAA,EAAW,GAAA,CAAI,CAAC,CAAA,GAAI,CAAA;AAAA,EAChC;AACA,EAAA,OAAO,GAAA;AACT;AAWO,SAAS,kBAAkB,KAAA,EAAsC;AACtE,EAAA,MAAM,cAAyC,EAAC;AAChD,EAAA,MAAM,WAAsC,EAAC;AAC7C,EAAA,IAAI,MAAM,KAAA,EAAO;AACf,IAAA,KAAA,MAAW,CAAC,GAAG,CAAC,CAAA,IAAK,OAAO,OAAA,CAAQ,KAAA,CAAM,KAAK,CAAA,EAAG;AAChD,MAAA,IAAI,MAAM,MAAA,EAAW;AAKrB,MAAA,IAAI,SAAS,GAAA,CAAI,CAAC,CAAA,EAAG,QAAA,CAAS,CAAC,CAAA,GAAI,CAAA;AAAA,WAC9B,WAAA,CAAY,CAA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,GAAA;AACT;AASO,SAAS,YAAA,CACd,cACA,MAAA,EACe;AACf,EAAA,OAAO,aAAa,YAAY,CAAA,CAAEC,mCAAA,CAAkB,MAAM,CAAC,CAAA,IAAK,IAAA;AAClE;AAQO,SAAS,UAAA,CACd,cACA,MAAA,EACc;AACd,EAAA,MAAM,MAAM,YAAA,CAAa,YAAY,CAAA,CAAEC,mCAAA,CAAkB,MAAM,CAAC,CAAA;AAChE,EAAA,IAAI,CAAC,KAAK,OAAO,EAAE,WAAW,KAAA,EAAO,SAAA,EAAW,KAAA,EAAO,eAAA,EAAiB,KAAA,EAAM;AAC9E,EAAA,IAAI;AACF,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,KAAA,CAAM,GAAG,CAAA;AAC7B,IAAA,OAAO,MAAA;AAAA,EACT,CAAA,CAAA,MAAQ;AACN,IAAA,MAAM,MAAA,GAAS,IAAI,GAAA,CAAI,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA,CAAE,GAAA,CAAI,CAAC,CAAA,KAAM,CAAA,CAAE,IAAA,EAAM,CAAC,CAAA;AAC1D,IAAA,OAAO;AAAA,MACL,SAAA,EAAW,MAAA,CAAO,GAAA,CAAI,WAAW,CAAA;AAAA,MACjC,SAAA,EAAW,MAAA,CAAO,GAAA,CAAI,WAAW,CAAA;AAAA,MACjC,eAAA,EAAiB,MAAA,CAAO,GAAA,CAAI,iBAAiB;AAAA,KAC/C;AAAA,EACF;AACF","file":"index.cjs","sourcesContent":["import type { EventContext, EventIdentity, EventPayload, JsonValue } from \"./types\";\n\n/** Reserved keys that map to first-class fields (not passthrough data). */\nconst RESERVED = new Set([\n \"eventId\",\n \"eventType\",\n \"journeyId\",\n \"deviceId\",\n \"visitorId\",\n \"customerId\",\n \"email\",\n \"utmSource\",\n \"utmMedium\",\n \"utmCampaign\",\n \"utmContent\",\n \"utmId\",\n \"url\",\n \"referrer\",\n \"path\",\n \"query\",\n]);\n\nexport interface BuildEventInput {\n eventId: string;\n eventType?: string;\n journeyId?: string;\n identity?: EventIdentity;\n context?: EventContext;\n /** Arbitrary extra properties merged as passthrough data. */\n props?: Record<string, JsonValue | undefined>;\n}\n\n/** Drop `undefined` values so the JSON body stays clean. */\nfunction compact(obj: Record<string, JsonValue | undefined>): Record<string, JsonValue> {\n const out: Record<string, JsonValue> = {};\n for (const [k, v] of Object.entries(obj)) {\n if (v !== undefined) out[k] = v;\n }\n return out;\n}\n\n/**\n * Build the request body for the webhook `/flow` call (`XeniaApi.webhook`).\n *\n * Merge order (later wins): passthrough props < context < identity < core.\n * Field names match the backend `EventTriggerRequest` DTO exactly.\n *\n * @internal Used only by the webhook request/response path — the unified event\n * surface (`track()` → `POST /api/events/v1`) builds {@link XeniaEvent} instead.\n */\nexport function buildEventPayload(input: BuildEventInput): EventPayload {\n const passthrough: Record<string, JsonValue> = {};\n const reserved: Record<string, JsonValue> = {};\n if (input.props) {\n for (const [k, v] of Object.entries(input.props)) {\n if (v === undefined) continue;\n // A prop whose key matches a reserved field (e.g. `email`, `customerId`,\n // `utmSource`) is promoted to that first-class field; everything else is\n // custom passthrough data. Previously reserved-named props were dropped\n // outright — so `track(\"sign_up\", { email })` never sent `email` at all.\n if (RESERVED.has(k)) reserved[k] = v;\n else passthrough[k] = v;\n }\n }\n\n const payload = {\n ...passthrough,\n // Reserved-named props fill first-class fields (e.g. `email`, `customerId`)\n // the SDK didn't auto-collect. Auto-collected context/identity still win for\n // what they reliably know (real page url, visitor/device id), so a stray prop\n // can't override the true page context — see merge order below.\n ...reserved,\n ...compact((input.context ?? {}) as Record<string, JsonValue | undefined>),\n ...compact((input.identity ?? {}) as Record<string, JsonValue | undefined>),\n eventId: input.eventId,\n ...(input.eventType ? { eventType: input.eventType } : {}),\n ...(input.journeyId ? { journeyId: input.journeyId } : {}),\n };\n\n return payload as EventPayload;\n}\n","import { XeniaApi } from \"../core/api\";\nimport { buildEventPayload } from \"../core/event-payload\";\nimport { generateUlid, toEventId } from \"../core/id\";\nimport type {\n AnalyticsContext,\n AnalyticsEventName,\n CallerIdentity,\n AnalyticsIngestResponse,\n ConsentState,\n EventContext,\n EventIdentity,\n IdentifyRequest,\n IdentifyResponse,\n JsonValue,\n ResolveBatchItem,\n ResolveBatchResult,\n ResolveOptions,\n ResolveResult,\n WebhookResponse,\n XeniaConfig,\n XeniaEvent,\n} from \"../core/types\";\n\n/**\n * Extra per-call identity/context for server-side webhook calls. There's no\n * ambient `window` on the server to auto-collect `path`/`query`/`url`/UTM\n * params the way the browser client does — pass whatever's known at the call\n * site (e.g. the current request's pathname from Next.js `headers()`/route\n * params) via `context`, and `deviceId`/`visitorId`/etc. via `identity`.\n */\nexport interface ServerWebhookOptions {\n method?: \"GET\" | \"POST\";\n journeyId?: string;\n identity?: EventIdentity;\n context?: EventContext;\n}\n\n/**\n * Options for a server-side unified event (`POST /api/events/v1`). There's no\n * ambient request context on the server — pass whatever's known at the call\n * site (e.g. pathname/user-agent from the current request, session id from a\n * cookie) via `identity`/`context`.\n */\nexport interface ServerTrackOptions {\n /** When true, also match/advance a journey (never sampled/shed). Default false. */\n trigger?: boolean;\n /** Stable dedup key (e.g. `\"ord-1001\"`) for idempotent conversions. */\n eventId?: string;\n eventVersion?: number;\n occurredAtMs?: number;\n /** Origin tag. Default `\"xenia-server-sdk\"`. */\n source?: string;\n /** Identity fields for this event (except the SDK-owned `xeniaVisitorId`,\n * which comes from `config.visitorId` and can't be set per call). */\n identity?: CallerIdentity;\n context?: AnalyticsContext;\n properties?: Record<string, JsonValue | undefined>;\n consent?: ConsentState;\n}\n\n/**\n * Server-native Xenia client. Same method names as the browser client, but\n * every call is stateless: identity and context are passed explicitly rather\n * than read from the DOM/localStorage. Uses global `fetch` (Node ≥18).\n */\nexport class XeniaServer {\n readonly api: XeniaApi;\n /** SDK-owned visitor anchor for this (per-request) client — see {@link XeniaConfig.visitorId}. */\n private readonly visitorId?: string;\n\n constructor(config: XeniaConfig) {\n this.api = new XeniaApi(config);\n this.visitorId = config.visitorId;\n }\n\n /**\n * Send a unified event server-side (`POST /api/events/v1`) and return the\n * ingest response (including per-result `triggered`). `opts.trigger: true`\n * also matches/advances a journey; pass `opts.eventId` (e.g. `\"ord-1001\"`)\n * as a stable dedup key for idempotent conversions.\n */\n async track(\n eventName: AnalyticsEventName,\n opts: ServerTrackOptions = {},\n ): Promise<AnalyticsIngestResponse> {\n const event: XeniaEvent = {\n eventName,\n eventId: opts.eventId ? toEventId(opts.eventId) : generateUlid(),\n eventVersion: opts.eventVersion ?? 1,\n occurredAtMs: opts.occurredAtMs ?? Date.now(),\n source: opts.source ?? \"xenia-server-sdk\",\n identity: {\n ...opts.identity,\n // SDK-owned, set LAST so it can't be overridden per call. Comes from\n // `config.visitorId` (bind it per request from the `xenia_vid` cookie).\n xeniaVisitorId: this.visitorId,\n },\n context: opts.context,\n properties: opts.properties,\n consent: opts.consent,\n trigger: opts.trigger === true ? true : undefined,\n };\n return this.api.sendEvents([event]);\n }\n\n /**\n * Send a webhook request and await the synchronous response. Merges\n * `opts.identity`/`opts.context` into the payload the same way the browser\n * client's auto-attached fields do (`path`, `query`, `url`, `referrer`,\n * `deviceId`, `visitorId`, etc.) — see `ServerWebhookOptions`.\n */\n async webhook(\n triggerId: string,\n data?: Record<string, JsonValue | undefined>,\n opts: ServerWebhookOptions = {},\n ): Promise<WebhookResponse> {\n const payload = buildEventPayload({\n eventId: triggerId,\n journeyId: opts.journeyId,\n // SDK-owned visitor anchor wins: config.visitorId (bind the `xenia_vid`\n // cookie per request) is attached automatically. Falls back to a\n // per-call visitorId only when none is bound.\n identity: { ...opts.identity, visitorId: this.visitorId ?? opts.identity?.visitorId },\n context: opts.context,\n props: data,\n });\n return this.api.webhook(triggerId, payload as JsonValue, opts.method ?? \"POST\");\n }\n\n /** Bind visitor→user server-side — `POST /api/events/v1/identify`. */\n async identify(body: IdentifyRequest): Promise<IdentifyResponse> {\n return this.api.identify(body);\n }\n\n /**\n * Resolve in-entry content personalization for one content key\n * (`POST /api/personalization/resolve`). Requires a configured SDK token\n * (`token`/`getToken`) — resolve is auth-required. With `merge: true` the\n * returned `content` is server-spliced.\n */\n async resolve(contentKey: string, opts?: ResolveOptions): Promise<ResolveResult> {\n return this.api.resolve(contentKey, this.withVisitor(opts));\n }\n\n /** Resolve several content keys in one round-trip (`POST /api/personalization/resolve-batch`). */\n async resolveBatch(\n items: ResolveBatchItem[],\n opts?: ResolveOptions,\n ): Promise<ResolveBatchResult[]> {\n return this.api.resolveBatch(items, this.withVisitor(opts));\n }\n\n /**\n * Stamp the SDK-owned visitor id (`config.visitorId`) into the resolve `data`\n * under the reserved `_xenia_vid` key so the backend can bucket holdout\n * experiments and record exposure per visitor. Set last so a caller can't\n * override it; a no-op when no `visitorId` was bound on this client.\n */\n private withVisitor(opts?: ResolveOptions): ResolveOptions | undefined {\n if (!this.visitorId) return opts;\n return { ...opts, data: { ...(opts?.data ?? {}), _xenia_vid: this.visitorId } };\n }\n}\n\n/** Create a server-native client. */\nexport function createXeniaServer(config: XeniaConfig): XeniaServer {\n return new XeniaServer(config);\n}\n","import { HttpClient } from \"../core/http-client\";\nimport type { JsonObject } from \"../core/types\";\n\nexport interface RefreshTokenOptions {\n /**\n * Xenia base URL. Defaults to the hosted engine,\n * `https://engine.xeniamartech.com/xenia` — omit unless self-hosting.\n */\n baseUrl?: string;\n /**\n * The personalization SDK token created once in the dashboard (API Tokens →\n * Create token). Stays server-side; the browser never sees it — only the\n * short-lived token this exchanges it for does.\n */\n sdkToken: string;\n /** Lifetime of the returned token, in minutes. Default 15, capped server-side at 60. */\n ttlMinutes?: number;\n timeout?: number;\n}\n\nexport interface RefreshedToken {\n token: string;\n expiresAt?: string;\n}\n\n/**\n * Exchange the long-lived personalization SDK token for a short-lived one with\n * the same tenant/env scope, via `POST /api/personalization/token/refresh`.\n * Self-service — no dashboard credential needed, just the SDK token you\n * already have. Call this from your backend and hand the result to the\n * browser (e.g. from a token route the client SDK calls via `getToken`).\n */\nexport async function refreshSdkToken(opts: RefreshTokenOptions): Promise<RefreshedToken> {\n const http = new HttpClient({ baseUrl: opts.baseUrl, timeout: opts.timeout });\n const { data } = await http.request({\n method: \"POST\",\n path: \"/api/personalization/token/refresh\",\n headers: { Authorization: `Bearer ${opts.sdkToken}` },\n body: { ttlMinutes: opts.ttlMinutes ?? null },\n });\n const obj = (data ?? {}) as JsonObject;\n return {\n token: String(obj.token ?? \"\"),\n expiresAt: typeof obj.expiresAt === \"string\" ? obj.expiresAt : undefined,\n };\n}\n\n/**\n * Build a framework-agnostic request handler that hands the browser SDK a\n * personalization token — mount it at the endpoint `getToken` calls. Needs\n * only the SDK token you already created in the dashboard; mints a fresh\n * short-lived token per request so the long-lived one never reaches the\n * browser.\n *\n * @example\n * ```ts\n * export const GET = createTokenRoute({\n * sdkToken: process.env.XENIA_SDK_TOKEN!,\n * // baseUrl: process.env.XENIA_URL, // only to override the default hosted engine\n * });\n * ```\n */\nexport function createTokenRoute(config: RefreshTokenOptions): (request: Request) => Promise<Response> {\n return async (): Promise<Response> => {\n try {\n const refreshed = await refreshSdkToken(config);\n return new Response(JSON.stringify(refreshed), {\n status: 200,\n headers: { \"Content-Type\": \"application/json\", \"Cache-Control\": \"no-store\" },\n });\n } catch (err) {\n const message = err instanceof Error ? err.message : \"token refresh failed\";\n return new Response(JSON.stringify({ error: message }), {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n });\n }\n };\n}\n","import {\n CONSENT_COOKIE,\n VISITOR_COOKIE,\n consentCookieName,\n visitorCookieName,\n} from \"../core/cookie-names\";\nimport type { ConsentState } from \"../core/types\";\n\n/**\n * Cookie names the SDK reads server-side (set by the browser SDK / your app).\n *\n * The constants are the DEFAULT-prefix names; the functions build the name for any prefix. If you\n * pass `config.prefix` to the browser client, pass the same prefix here — the browser namespaces\n * the cookie by it, so the default name won't be there to find.\n */\nexport { CONSENT_COOKIE, VISITOR_COOKIE, consentCookieName, visitorCookieName };\n\n/**\n * Parse a raw `Cookie` header into a map. Framework-agnostic so it works with\n * `Request.headers.get(\"cookie\")`, Node `req.headers.cookie`, etc.\n */\nexport function parseCookies(header: string | null | undefined): Record<string, string> {\n const out: Record<string, string> = {};\n if (!header) return out;\n for (const part of header.split(\";\")) {\n const idx = part.indexOf(\"=\");\n if (idx === -1) continue;\n const key = part.slice(0, idx).trim();\n const value = part.slice(idx + 1).trim();\n if (key) out[key] = decodeURIComponent(value);\n }\n return out;\n}\n\n/**\n * Read the visitor id the browser SDK wrote, so a server-side event can be bound to the same\n * visitor (`createXeniaServer({ visitorId })`) and attribute to a browser-entered journey.\n *\n * @param prefix Must match the browser client's `config.prefix`. The cookie is namespaced by it, so\n * a mismatch finds nothing and quietly leaves the server with no visitor.\n */\nexport function getVisitorId(\n cookieHeader: string | null | undefined,\n prefix?: string,\n): string | null {\n return parseCookies(cookieHeader)[visitorCookieName(prefix)] ?? null;\n}\n\n/**\n * Read consent from a cookie header. Accepts a JSON blob or a comma-list of\n * granted scopes (e.g. `analytics,personalization`). Absent → all denied.\n *\n * @param prefix Must match the browser client's `config.prefix` — see {@link getVisitorId}.\n */\nexport function getConsent(\n cookieHeader: string | null | undefined,\n prefix?: string,\n): ConsentState {\n const raw = parseCookies(cookieHeader)[consentCookieName(prefix)];\n if (!raw) return { analytics: false, marketing: false, personalization: false };\n try {\n const parsed = JSON.parse(raw) as ConsentState;\n return parsed;\n } catch {\n const scopes = new Set(raw.split(\",\").map((s) => s.trim()));\n return {\n analytics: scopes.has(\"analytics\"),\n marketing: scopes.has(\"marketing\"),\n personalization: scopes.has(\"personalization\"),\n };\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/core/event-payload.ts","../../src/server/xenia-server.ts","../../src/server/token.ts","../../src/server/cookies.ts"],"names":["compact","XeniaApi","toEventId","generateUlid","HttpClient","visitorCookieName","consentCookieName"],"mappings":";;;;;AAGA,IAAM,QAAA,uBAAe,GAAA,CAAI;AAAA,EACvB,SAAA;AAAA,EACA,WAAA;AAAA,EACA,WAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,E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;AACF","file":"index.cjs","sourcesContent":["import type { EventContext, EventIdentity, EventPayload, JsonValue } from \"./types\";\n\n/** Reserved keys that map to first-class fields (not passthrough data). */\nconst RESERVED = new Set([\n \"eventId\",\n \"eventType\",\n \"journeyId\",\n \"deviceId\",\n \"visitorId\",\n \"customerId\",\n \"email\",\n \"utmSource\",\n \"utmMedium\",\n \"utmCampaign\",\n \"utmContent\",\n \"utmId\",\n \"url\",\n \"referrer\",\n \"path\",\n \"query\",\n]);\n\nexport interface BuildEventInput {\n eventId: string;\n eventType?: string;\n journeyId?: string;\n identity?: EventIdentity;\n context?: EventContext;\n /** Arbitrary extra properties merged as passthrough data. */\n props?: Record<string, JsonValue | undefined>;\n}\n\n/** Drop `undefined` values so the JSON body stays clean. */\nfunction compact(obj: Record<string, JsonValue | undefined>): Record<string, JsonValue> {\n const out: Record<string, JsonValue> = {};\n for (const [k, v] of Object.entries(obj)) {\n if (v !== undefined) out[k] = v;\n }\n return out;\n}\n\n/**\n * Build the request body for the webhook `/flow` call (`XeniaApi.webhook`).\n *\n * Merge order (later wins): passthrough props < context < identity < core.\n * Field names match the backend `EventTriggerRequest` DTO exactly.\n *\n * @internal Used only by the webhook request/response path — the unified event\n * surface (`track()` → `POST /api/events/v1`) builds {@link XeniaEvent} instead.\n */\nexport function buildEventPayload(input: BuildEventInput): EventPayload {\n const passthrough: Record<string, JsonValue> = {};\n const reserved: Record<string, JsonValue> = {};\n if (input.props) {\n for (const [k, v] of Object.entries(input.props)) {\n if (v === undefined) continue;\n // A prop whose key matches a reserved field (e.g. `email`, `customerId`,\n // `utmSource`) is promoted to that first-class field; everything else is\n // custom passthrough data. Previously reserved-named props were dropped\n // outright — so `track(\"sign_up\", { email })` never sent `email` at all.\n if (RESERVED.has(k)) reserved[k] = v;\n else passthrough[k] = v;\n }\n }\n\n const payload = {\n ...passthrough,\n // Reserved-named props fill first-class fields (e.g. `email`, `customerId`)\n // the SDK didn't auto-collect. Auto-collected context/identity still win for\n // what they reliably know (real page url, visitor/device id), so a stray prop\n // can't override the true page context — see merge order below.\n ...reserved,\n ...compact((input.context ?? {}) as Record<string, JsonValue | undefined>),\n ...compact((input.identity ?? {}) as Record<string, JsonValue | undefined>),\n eventId: input.eventId,\n ...(input.eventType ? { eventType: input.eventType } : {}),\n ...(input.journeyId ? { journeyId: input.journeyId } : {}),\n };\n\n return payload as EventPayload;\n}\n","import { compact, XeniaApi } from \"../core/api\";\nimport { buildEventPayload } from \"../core/event-payload\";\nimport { generateUlid, toEventId } from \"../core/id\";\nimport type {\n AnalyticsContext,\n AnalyticsEventName,\n CallerIdentity,\n AnalyticsIngestResponse,\n ConsentState,\n EventContext,\n EventIdentity,\n IdentifyRequest,\n IdentifyResponse,\n JsonValue,\n ResolveBatchItem,\n ResolveBatchResult,\n ResolveOptions,\n ResolveResult,\n WebhookResponse,\n XeniaConfig,\n XeniaEvent,\n} from \"../core/types\";\n\n/**\n * Extra per-call identity/context for server-side webhook calls — an override on top of whatever the\n * client already carries.\n *\n * There's no ambient `window` on the server to auto-collect `path`/`query`/`url`/UTM params the way\n * the browser client does, so bind them ONCE per request via {@link XeniaConfig.context} (see\n * `contextFromUrl`) and every call attaches them automatically. These per-call fields are for the\n * remainder: values known only at this call site, or a deliberate override of the ambient ones.\n */\nexport interface ServerWebhookOptions {\n method?: \"GET\" | \"POST\";\n journeyId?: string;\n identity?: EventIdentity;\n context?: EventContext;\n}\n\n/**\n * Options for a server-side unified event (`POST /api/events/v1`). There's no\n * ambient request context on the server — pass whatever's known at the call\n * site (e.g. pathname/user-agent from the current request, session id from a\n * cookie) via `identity`/`context`.\n */\nexport interface ServerTrackOptions {\n /** When true, also match/advance a journey (never sampled/shed). Default false. */\n trigger?: boolean;\n /** Stable dedup key (e.g. `\"ord-1001\"`) for idempotent conversions. */\n eventId?: string;\n eventVersion?: number;\n occurredAtMs?: number;\n /** Origin tag. Default `\"xenia-server-sdk\"`. */\n source?: string;\n /** Identity fields for this event (except the SDK-owned `xeniaVisitorId`,\n * which comes from `config.visitorId` and can't be set per call). */\n identity?: CallerIdentity;\n context?: AnalyticsContext;\n properties?: Record<string, JsonValue | undefined>;\n consent?: ConsentState;\n}\n\n/**\n * Server-native Xenia client. Same method names as the browser client, but\n * every call is stateless: identity and context are passed explicitly rather\n * than read from the DOM/localStorage. Uses global `fetch` (Node ≥18).\n */\nexport class XeniaServer {\n readonly api: XeniaApi;\n /** SDK-owned visitor anchor for this (per-request) client — see {@link XeniaConfig.visitorId}. */\n private readonly visitorId?: string;\n /** Ambient request context attached to every call — see {@link XeniaConfig.context}. */\n private readonly context?: EventContext;\n\n constructor(config: XeniaConfig) {\n this.api = new XeniaApi(config);\n this.visitorId = config.visitorId;\n this.context = config.context;\n }\n\n /**\n * Send a unified event server-side (`POST /api/events/v1`) and return the\n * ingest response (including per-result `triggered`). `opts.trigger: true`\n * also matches/advances a journey; pass `opts.eventId` (e.g. `\"ord-1001\"`)\n * as a stable dedup key for idempotent conversions.\n */\n async track(\n eventName: AnalyticsEventName,\n opts: ServerTrackOptions = {},\n ): Promise<AnalyticsIngestResponse> {\n const event: XeniaEvent = {\n eventName,\n eventId: opts.eventId ? toEventId(opts.eventId) : generateUlid(),\n eventVersion: opts.eventVersion ?? 1,\n occurredAtMs: opts.occurredAtMs ?? Date.now(),\n source: opts.source ?? \"xenia-server-sdk\",\n identity: {\n ...opts.identity,\n // SDK-owned, set LAST so it can't be overridden per call. Comes from\n // `config.visitorId` (bind it per request from the `xenia_vid` cookie).\n xeniaVisitorId: this.visitorId,\n },\n context: opts.context,\n properties: opts.properties,\n consent: opts.consent,\n trigger: opts.trigger === true ? true : undefined,\n };\n return this.api.sendEvents([event]);\n }\n\n /**\n * Send a webhook request and await the synchronous response. Merges\n * `opts.identity`/`opts.context` into the payload the same way the browser\n * client's auto-attached fields do (`path`, `query`, `url`, `referrer`,\n * `deviceId`, `visitorId`, etc.) — see `ServerWebhookOptions`.\n */\n async webhook(\n triggerId: string,\n data?: Record<string, JsonValue | undefined>,\n opts: ServerWebhookOptions = {},\n ): Promise<WebhookResponse> {\n const payload = buildEventPayload({\n eventId: triggerId,\n journeyId: opts.journeyId,\n // SDK-owned visitor anchor wins: config.visitorId (bind the `xenia_vid`\n // cookie per request) is attached automatically. Falls back to a\n // per-call visitorId only when none is bound.\n identity: { ...opts.identity, visitorId: this.visitorId ?? opts.identity?.visitorId },\n // Ambient request context (config.context, bound once per request) under a per-call\n // override — same precedence resolve() uses, so both surfaces carry the same fields.\n context: { ...this.context, ...opts.context },\n props: data,\n });\n return this.api.webhook(triggerId, payload as JsonValue, opts.method ?? \"POST\");\n }\n\n /** Bind visitor→user server-side — `POST /api/events/v1/identify`. */\n async identify(body: IdentifyRequest): Promise<IdentifyResponse> {\n return this.api.identify(body);\n }\n\n /**\n * Resolve in-entry content personalization for one content key\n * (`POST /api/personalization/resolve`). Requires a configured SDK token\n * (`token`/`getToken`) — resolve is auth-required. With `merge: true` the\n * returned `content` is server-spliced.\n *\n * Pass `opts.identity`/`opts.context` to attach the same SDK-provided\n * fields (`deviceId`, `customerId`, `email`, `path`, `url`, `referrer`,\n * `utm*`, …) the browser client collects ambiently — there's no ambient\n * `window`/`document` server-side, so a server caller must supply whatever\n * it knows (e.g. from the current request) explicitly. See\n * {@link ResolveOptions}.\n */\n async resolve(contentKey: string, opts?: ResolveOptions): Promise<ResolveResult> {\n return this.api.resolve(contentKey, this.withVisitor(opts));\n }\n\n /** Resolve several content keys in one round-trip (`POST /api/personalization/resolve-batch`). */\n async resolveBatch(\n items: ResolveBatchItem[],\n opts?: ResolveOptions,\n ): Promise<ResolveBatchResult[]> {\n return this.api.resolveBatch(items, this.withVisitor(opts));\n }\n\n /**\n * Merge the ambient `config.context` and `opts.identity`/`opts.context` into\n * the resolve `data`, then stamp the SDK-owned visitor id\n * (`config.visitorId`) under both the reserved `_xenia_vid` key (read by the\n * backend for holdout/exposure bucketing) and the flat `visitorId` field —\n * mirrors `webhook()`'s merge order/precedence exactly: caller `data` <\n * ambient `config.context` < caller `context`/`identity` < SDK-owned\n * `visitorId`/`_xenia_vid`, set LAST so neither can be overridden per call.\n *\n * `config.context` is what makes the SDK-provided fields automatic on the\n * server: bound once per request, every `resolve()` carries `path`/`query`/\n * `url`/`referrer`/UTMs with no per-call wiring, matching what the browser\n * client collects from `window` on its own. A no-op when nothing is bound and\n * nothing was passed — preserving today's behavior for callers using neither.\n */\n private withVisitor(opts?: ResolveOptions): ResolveOptions | undefined {\n const identity = compact({\n ...opts?.identity,\n visitorId: this.visitorId ?? opts?.identity?.visitorId,\n });\n const merged = {\n ...compact((this.context ?? {}) as Record<string, JsonValue | undefined>),\n ...compact((opts?.context ?? {}) as Record<string, JsonValue | undefined>),\n ...identity,\n };\n if (Object.keys(merged).length === 0 && !this.visitorId) return opts;\n if (this.visitorId) merged._xenia_vid = this.visitorId;\n return { ...opts, data: { ...(opts?.data ?? {}), ...merged } };\n }\n}\n\n/** Create a server-native client. */\nexport function createXeniaServer(config: XeniaConfig): XeniaServer {\n return new XeniaServer(config);\n}\n","import { HttpClient } from \"../core/http-client\";\nimport type { JsonObject } from \"../core/types\";\n\nexport interface RefreshTokenOptions {\n /**\n * Xenia base URL. Defaults to the hosted engine,\n * `https://engine.xeniamartech.com/xenia` — omit unless self-hosting.\n */\n baseUrl?: string;\n /**\n * The personalization SDK token created once in the dashboard (API Tokens →\n * Create token). Stays server-side; the browser never sees it — only the\n * short-lived token this exchanges it for does.\n */\n sdkToken: string;\n /** Lifetime of the returned token, in minutes. Default 15, capped server-side at 60. */\n ttlMinutes?: number;\n timeout?: number;\n}\n\nexport interface RefreshedToken {\n token: string;\n expiresAt?: string;\n}\n\n/**\n * Exchange the long-lived personalization SDK token for a short-lived one with\n * the same tenant/env scope, via `POST /api/personalization/token/refresh`.\n * Self-service — no dashboard credential needed, just the SDK token you\n * already have. Call this from your backend and hand the result to the\n * browser (e.g. from a token route the client SDK calls via `getToken`).\n */\nexport async function refreshSdkToken(opts: RefreshTokenOptions): Promise<RefreshedToken> {\n const http = new HttpClient({ baseUrl: opts.baseUrl, timeout: opts.timeout });\n const { data } = await http.request({\n method: \"POST\",\n path: \"/api/personalization/token/refresh\",\n headers: { Authorization: `Bearer ${opts.sdkToken}` },\n body: { ttlMinutes: opts.ttlMinutes ?? null },\n });\n const obj = (data ?? {}) as JsonObject;\n return {\n token: String(obj.token ?? \"\"),\n expiresAt: typeof obj.expiresAt === \"string\" ? obj.expiresAt : undefined,\n };\n}\n\n/**\n * Build a framework-agnostic request handler that hands the browser SDK a\n * personalization token — mount it at the endpoint `getToken` calls. Needs\n * only the SDK token you already created in the dashboard; mints a fresh\n * short-lived token per request so the long-lived one never reaches the\n * browser.\n *\n * @example\n * ```ts\n * export const GET = createTokenRoute({\n * sdkToken: process.env.XENIA_SDK_TOKEN!,\n * // baseUrl: process.env.XENIA_URL, // only to override the default hosted engine\n * });\n * ```\n */\nexport function createTokenRoute(config: RefreshTokenOptions): (request: Request) => Promise<Response> {\n return async (): Promise<Response> => {\n try {\n const refreshed = await refreshSdkToken(config);\n return new Response(JSON.stringify(refreshed), {\n status: 200,\n headers: { \"Content-Type\": \"application/json\", \"Cache-Control\": \"no-store\" },\n });\n } catch (err) {\n const message = err instanceof Error ? err.message : \"token refresh failed\";\n return new Response(JSON.stringify({ error: message }), {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n });\n }\n };\n}\n","import {\n CONSENT_COOKIE,\n VISITOR_COOKIE,\n consentCookieName,\n visitorCookieName,\n} from \"../core/cookie-names\";\nimport type { ConsentState } from \"../core/types\";\n\n/**\n * Cookie names the SDK reads server-side (set by the browser SDK / your app).\n *\n * The constants are the DEFAULT-prefix names; the functions build the name for any prefix. If you\n * pass `config.prefix` to the browser client, pass the same prefix here — the browser namespaces\n * the cookie by it, so the default name won't be there to find.\n */\nexport { CONSENT_COOKIE, VISITOR_COOKIE, consentCookieName, visitorCookieName };\n\n/**\n * Parse a raw `Cookie` header into a map. Framework-agnostic so it works with\n * `Request.headers.get(\"cookie\")`, Node `req.headers.cookie`, etc.\n */\nexport function parseCookies(header: string | null | undefined): Record<string, string> {\n const out: Record<string, string> = {};\n if (!header) return out;\n for (const part of header.split(\";\")) {\n const idx = part.indexOf(\"=\");\n if (idx === -1) continue;\n const key = part.slice(0, idx).trim();\n const value = part.slice(idx + 1).trim();\n if (key) out[key] = decodeURIComponent(value);\n }\n return out;\n}\n\n/**\n * Read the visitor id the browser SDK wrote, so a server-side event can be bound to the same\n * visitor (`createXeniaServer({ visitorId })`) and attribute to a browser-entered journey.\n *\n * @param prefix Must match the browser client's `config.prefix`. The cookie is namespaced by it, so\n * a mismatch finds nothing and quietly leaves the server with no visitor.\n */\nexport function getVisitorId(\n cookieHeader: string | null | undefined,\n prefix?: string,\n): string | null {\n return parseCookies(cookieHeader)[visitorCookieName(prefix)] ?? null;\n}\n\n/**\n * Read consent from a cookie header. Accepts a JSON blob or a comma-list of\n * granted scopes (e.g. `analytics,personalization`). Absent → all denied.\n *\n * @param prefix Must match the browser client's `config.prefix` — see {@link getVisitorId}.\n */\nexport function getConsent(\n cookieHeader: string | null | undefined,\n prefix?: string,\n): ConsentState {\n const raw = parseCookies(cookieHeader)[consentCookieName(prefix)];\n if (!raw) return { analytics: false, marketing: false, personalization: false };\n try {\n const parsed = JSON.parse(raw) as ConsentState;\n return parsed;\n } catch {\n const scopes = new Set(raw.split(\",\").map((s) => s.trim()));\n return {\n analytics: scopes.has(\"analytics\"),\n marketing: scopes.has(\"marketing\"),\n personalization: scopes.has(\"personalization\"),\n };\n }\n}\n"]}