@ketch-sdk/ketch-data-layer 1.1.1 → 1.1.3

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 (50) hide show
  1. package/README.md +201 -0
  2. package/dist/cookie/fetcher.js +26 -0
  3. package/dist/cookie/index.js +12 -0
  4. package/dist/cookie/util.js +44 -0
  5. package/dist/dataLayer/fetcher.js +30 -0
  6. package/dist/dataLayer/index.js +9 -0
  7. package/dist/index.js +220 -1
  8. package/dist/json/index.js +9 -0
  9. package/dist/json/structure.js +11 -0
  10. package/dist/jwt/index.js +9 -0
  11. package/dist/jwt/structure.js +11 -0
  12. package/dist/listener/index.d.ts +3 -1
  13. package/dist/listener/index.js +3 -0
  14. package/dist/localStorage/fetcher.js +5 -0
  15. package/dist/localStorage/index.js +9 -0
  16. package/dist/managed/fetcher.js +34 -0
  17. package/dist/managed/index.js +9 -0
  18. package/dist/mapper/index.d.ts +3 -0
  19. package/dist/mapper/index.js +3 -0
  20. package/dist/queryString/fetcher.js +21 -0
  21. package/dist/queryString/index.js +11 -0
  22. package/dist/queryString/structure.js +11 -0
  23. package/dist/semicolon/index.js +9 -0
  24. package/dist/semicolon/structure.js +12 -0
  25. package/dist/sessionStorage/fetcher.js +5 -0
  26. package/dist/sessionStorage/index.js +9 -0
  27. package/dist/storage/fetcher.js +33 -0
  28. package/dist/storage/index.d.ts +1 -2
  29. package/dist/storage/index.js +9 -0
  30. package/dist/string/index.js +9 -0
  31. package/dist/string/structure.js +9 -0
  32. package/dist/structure/index.js +3 -0
  33. package/dist/window/fetcher.js +66 -0
  34. package/dist/window/index.js +9 -0
  35. package/package.json +1 -1
  36. package/dist/cookie/index.test.d.ts +0 -1
  37. package/dist/dataLayer/index.test.d.ts +0 -1
  38. package/dist/index.test.d.ts +0 -1
  39. package/dist/json/index.test.d.ts +0 -1
  40. package/dist/jwt/index.test.d.ts +0 -1
  41. package/dist/licenses.txt +0 -992
  42. package/dist/localStorage/index.test.d.ts +0 -1
  43. package/dist/managed/index.test.d.ts +0 -1
  44. package/dist/queryString/index.test.d.ts +0 -1
  45. package/dist/semicolon/index.test.d.ts +0 -1
  46. package/dist/sessionStorage/index.test.d.ts +0 -1
  47. package/dist/storage/index.test.d.ts +0 -1
  48. package/dist/storage/notifier.d.ts +0 -3
  49. package/dist/string/index.test.d.ts +0 -1
  50. package/dist/window/index.test.d.ts +0 -6
package/README.md CHANGED
@@ -0,0 +1,201 @@
1
+ # ketch-data-layer
2
+
3
+ The `ketch-data-layer` library implements a [Watcher](#Watcher) and a set of [Fetcher](#Fetcher) and [Structure](#Structure)
4
+ implementations for fetching and structuring identities.
5
+
6
+ The following [Fetchers](#Fetcher) are implemented:
7
+ * cookie
8
+ * dataLayer
9
+ * localStorage
10
+ * managed
11
+ * queryString
12
+ * sessionStorage
13
+ * window
14
+
15
+ The following [Structures](#Structure) are implemented:
16
+ * json
17
+ * jwt
18
+ * queryString
19
+ * semicolon
20
+ * string
21
+
22
+ ## Identity
23
+
24
+ An Identity can be described using the following interface:
25
+
26
+ ```typescript
27
+ export interface Identity {
28
+ /**
29
+ * type is the location on the page from which to retrieve identity information
30
+ */
31
+ type: IdentityType;
32
+
33
+ /**
34
+ * variable is the name to look up the identity value in the specified location
35
+ */
36
+ variable: string;
37
+
38
+ /**
39
+ * format is the encoding of the value
40
+ */
41
+ format: IdentityFormat;
42
+
43
+ /**
44
+ * key is the identifier to find the identity within the value if the format is IDENTITY_FORMAT_STRING
45
+ * then key will be undefined
46
+ */
47
+ key?: string;
48
+
49
+ /**
50
+ * priority of the identity for consent conflict resolution
51
+ */
52
+ priority?: number;
53
+ }
54
+ ```
55
+
56
+ Priority is ignored by this library.
57
+
58
+ ## Watcher
59
+
60
+ The primary class is the `Watcher` which contains a series of registered identities that it watches for changes to.
61
+
62
+ ### Creating a Watcher
63
+
64
+ ```typescript
65
+ const w = new Watcher(window, {interval, timeout})
66
+ ```
67
+
68
+ Creates a new `Watcher` attached to the given `window`. The watcher will poll every `interval` until `timeout` seconds.
69
+
70
+ ### Add an identity
71
+
72
+ ```typescript
73
+ const name: string = ''
74
+ const identity: Identity | (() => Promise<string[]> = {}
75
+ w.add(name, identity)
76
+ ```
77
+
78
+ Registers an identity with the given `name` using the provided `identity` configuration. There are two
79
+ types of configuration allowed. The first type is an instance of an `Identity` object. If given this,
80
+ the watcher selects from it's know library based on the specification in the `Identity`. The second option,
81
+ which is a function returning a Promise of string values, can be used if the required identity cannot be
82
+ described using `Identity`.
83
+
84
+ ### Start watching
85
+
86
+ ```typescript
87
+ await w.start()
88
+ ```
89
+
90
+ Starts the [Watcher](#Watcher) watching based on the `interval` and `timeout` provided in the constructor.
91
+
92
+ ### Stop watching
93
+
94
+ ```typescript
95
+ w.stop()
96
+ ```
97
+
98
+ Stops the [Watcher](#Watcher).
99
+
100
+ ### Immediately notifying
101
+
102
+ ```typescript
103
+ await w.notify()
104
+ ```
105
+
106
+ Immediately fetches and notifies about identities by emitting an `identities` event.
107
+
108
+ ### Add a listener
109
+
110
+ ```typescript
111
+ const listener: (...args: any[]) => void = () => {}
112
+ w.addListener('identities', listener)
113
+ w.on('identities', listener)
114
+ ```
115
+
116
+ Add a listener function that gets called each time identities change. The argument to the listener
117
+ will be an `Identities` map.
118
+
119
+ ### Add a one-time listener
120
+
121
+ ```typescript
122
+ const listener: (...args: any[]) => void = () => {}
123
+ w.once('identities', listener)
124
+ ```
125
+
126
+ Adds a one-time listener function. The argument to the listener
127
+ will be an `Identities` map.
128
+
129
+ ### Remove a listener
130
+
131
+ ```typescript
132
+ const listener: (...args: any[]) => void = () => {}
133
+ w.removeListener('identities', listener)
134
+ w.off('identities', listener)
135
+ ```
136
+
137
+ Removes the given listener.
138
+
139
+ ### Remove all listeners
140
+
141
+ ```typescript
142
+ w.removeAllListeners('identities')
143
+ ```
144
+
145
+ Removes all listeners.
146
+
147
+ ## Fetcher
148
+
149
+ A `Fetcher` is a function with the following signature:
150
+
151
+ ```typescript
152
+ type Fetcher = async (w: Window, name: string) => Promise<any[]>
153
+ ```
154
+
155
+ The `w` parameter is the `Window` to attach to. The `name` is the name of the identity. The return
156
+ must be an array of discovered, structured identities. The structured identities will be destructured
157
+ using a [Structure](#Structure) function to provide the identity value.
158
+
159
+ ## Structure
160
+
161
+ A `Structure` is a function with the following signature:
162
+
163
+ ```typescript
164
+ type Mapper = {
165
+ [key: string]: string
166
+ }
167
+
168
+ type Structure = (value: any) => Mapper
169
+ ```
170
+
171
+ Given some structured value, the job of the `Structure` function is to return a key-value map representing the
172
+ values in that structure. If there is only a single identifiable value, then the key should be `value`.
173
+
174
+ ## Cookie
175
+
176
+ This library also exposes some utility functions that may be useful outside of the library's core purpose.
177
+
178
+ ### getCookie
179
+
180
+ ```typescript
181
+ import { getCookie } from '@ketch-sdk/ketch-data-layer/cookie'
182
+
183
+ const name: string = ''
184
+ const value = getCookie(window, name)
185
+ ```
186
+
187
+ Retrieves the full raw value of the cookie with the given name.
188
+
189
+ ### setCookie
190
+
191
+ ```typescript
192
+ import { setCookie } from '@ketch-sdk/ketch-data-layer/cookie'
193
+
194
+ const name: string = ''
195
+ const value: any = ''
196
+ const ttl: number = 2 // 2 days
197
+ setCookie(window, name, value, ttl)
198
+ ```
199
+
200
+ Sets the value of the cookie with the given name. The TTL of the cookie is set to `ttl` days. Note that
201
+ long-lasting cookies are subject to [Intelligent Tracking Prevention](https://webkit.org/tracking-prevention-policy/) limitations.
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const util_1 = require("./util");
13
+ function cookieFetcher(w, name) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ if (!w || name.length === 0) {
16
+ return [];
17
+ }
18
+ const pv = (0, util_1.getCookie)(w, name);
19
+ if (!pv || pv === '0') {
20
+ return [];
21
+ }
22
+ return [pv];
23
+ });
24
+ }
25
+ exports.default = cookieFetcher;
26
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmV0Y2hlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jb29raWUvZmV0Y2hlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7OztBQUFBLGlDQUFrQztBQUVsQyxTQUE4QixhQUFhLENBQUMsQ0FBUyxFQUFFLElBQVk7O1FBQ2pFLElBQUksQ0FBQyxDQUFDLElBQUksSUFBSSxDQUFDLE1BQU0sS0FBSyxDQUFDLEVBQUU7WUFDM0IsT0FBTyxFQUFFLENBQUE7U0FDVjtRQUVELE1BQU0sRUFBRSxHQUFHLElBQUEsZ0JBQVMsRUFBQyxDQUFDLEVBQUUsSUFBSSxDQUFDLENBQUE7UUFDN0IsSUFBSSxDQUFDLEVBQUUsSUFBSSxFQUFFLEtBQUssR0FBRyxFQUFFO1lBQ3JCLE9BQU8sRUFBRSxDQUFBO1NBQ1Y7UUFFRCxPQUFPLENBQUMsRUFBRSxDQUFDLENBQUE7SUFDYixDQUFDO0NBQUE7QUFYRCxnQ0FXQyJ9
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.setCookie = exports.getCookie = exports.fetcher = void 0;
7
+ const fetcher_1 = __importDefault(require("./fetcher"));
8
+ exports.fetcher = fetcher_1.default;
9
+ const util_1 = require("./util");
10
+ Object.defineProperty(exports, "getCookie", { enumerable: true, get: function () { return util_1.getCookie; } });
11
+ Object.defineProperty(exports, "setCookie", { enumerable: true, get: function () { return util_1.setCookie; } });
12
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29va2llL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLHdEQUErQjtBQUd0QixrQkFIRixpQkFBTyxDQUdFO0FBRmhCLGlDQUE2QztBQUUzQiwwRkFGVCxnQkFBUyxPQUVTO0FBQUUsMEZBRlQsZ0JBQVMsT0FFUyJ9
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setCookie = exports.getCookie = void 0;
4
+ /**
5
+ * Get a value from a cookie by the key.
6
+ *
7
+ * @param w The window object
8
+ * @param key The cookie key
9
+ */
10
+ function getCookie(w, key) {
11
+ return w.document.cookie.split('; ').reduce((r, v) => {
12
+ const parts = v.split('=');
13
+ return parts[0] === key ? decodeURIComponent(parts[1]) : r;
14
+ }, '');
15
+ }
16
+ exports.getCookie = getCookie;
17
+ /**
18
+ * Set the value against the given key
19
+ *
20
+ * @param w The window object
21
+ * @param key The cookie key
22
+ * @param value The value to set
23
+ * @param ttl The TTL of the cookie value in days
24
+ */
25
+ function setCookie(w, key, value, ttl) {
26
+ const days = ttl || 1;
27
+ const expires = new Date(Date.now() + days * 864e5).toUTCString();
28
+ const hostnameParts = w.document.location.hostname.split('.');
29
+ const details = `${key}=${encodeURIComponent(value)}; expires=${expires}; path=/; SameSite=None; Secure`;
30
+ // try to set cookie for last i parts of the domain
31
+ // if cookie not found (likely because domain in public suffix list), retry with an additional part on the domain
32
+ for (let i = 2; i <= hostnameParts.length; i++) {
33
+ // set cookie
34
+ w.document.cookie = `${details}; domain=${hostnameParts.slice(-1 * i).join('.')}`;
35
+ // return if set, otherwise retry with an additional part on the domain
36
+ if (getCookie(w, key)) {
37
+ return;
38
+ }
39
+ }
40
+ // set cookie without domain if hostnameParts.length < 2 or other error
41
+ w.document.cookie = details;
42
+ }
43
+ exports.setCookie = setCookie;
44
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jb29raWUvdXRpbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQTs7Ozs7R0FLRztBQUNILFNBQWdCLFNBQVMsQ0FBQyxDQUFTLEVBQUUsR0FBVztJQUM5QyxPQUFPLENBQUMsQ0FBQyxRQUFRLENBQUMsTUFBTSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQyxNQUFNLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxFQUFFLEVBQUU7UUFDbkQsTUFBTSxLQUFLLEdBQUcsQ0FBQyxDQUFDLEtBQUssQ0FBQyxHQUFHLENBQUMsQ0FBQTtRQUMxQixPQUFPLEtBQUssQ0FBQyxDQUFDLENBQUMsS0FBSyxHQUFHLENBQUMsQ0FBQyxDQUFDLGtCQUFrQixDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUE7SUFDNUQsQ0FBQyxFQUFFLEVBQUUsQ0FBQyxDQUFBO0FBQ1IsQ0FBQztBQUxELDhCQUtDO0FBRUQ7Ozs7Ozs7R0FPRztBQUNILFNBQWdCLFNBQVMsQ0FBQyxDQUFTLEVBQUUsR0FBVyxFQUFFLEtBQVUsRUFBRSxHQUFZO0lBQ3hFLE1BQU0sSUFBSSxHQUFHLEdBQUcsSUFBSSxDQUFDLENBQUE7SUFDckIsTUFBTSxPQUFPLEdBQUcsSUFBSSxJQUFJLENBQUMsSUFBSSxDQUFDLEdBQUcsRUFBRSxHQUFHLElBQUksR0FBRyxLQUFLLENBQUMsQ0FBQyxXQUFXLEVBQUUsQ0FBQTtJQUNqRSxNQUFNLGFBQWEsR0FBRyxDQUFDLENBQUMsUUFBUSxDQUFDLFFBQVEsQ0FBQyxRQUFRLENBQUMsS0FBSyxDQUFDLEdBQUcsQ0FBQyxDQUFBO0lBRTdELE1BQU0sT0FBTyxHQUFHLEdBQUcsR0FBRyxJQUFJLGtCQUFrQixDQUFDLEtBQUssQ0FBQyxhQUFhLE9BQU8saUNBQWlDLENBQUE7SUFFeEcsbURBQW1EO0lBQ25ELGlIQUFpSDtJQUNqSCxLQUFLLElBQUksQ0FBQyxHQUFHLENBQUMsRUFBRSxDQUFDLElBQUksYUFBYSxDQUFDLE1BQU0sRUFBRSxDQUFDLEVBQUUsRUFBRTtRQUM5QyxhQUFhO1FBQ2IsQ0FBQyxDQUFDLFFBQVEsQ0FBQyxNQUFNLEdBQUcsR0FBRyxPQUFPLFlBQVksYUFBYSxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLEVBQUUsQ0FBQTtRQUVqRix1RUFBdUU7UUFDdkUsSUFBSSxTQUFTLENBQUMsQ0FBQyxFQUFFLEdBQUcsQ0FBQyxFQUFFO1lBQ3JCLE9BQU07U0FDUDtLQUNGO0lBRUQsdUVBQXVFO0lBQ3ZFLENBQUMsQ0FBQyxRQUFRLENBQUMsTUFBTSxHQUFHLE9BQU8sQ0FBQTtBQUM3QixDQUFDO0FBckJELDhCQXFCQyJ9
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ function dataLayerFetcher(w, name) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ if (!w || !w.dataLayer || name.length === 0) {
15
+ return [];
16
+ }
17
+ let out = [];
18
+ for (const dl of w.dataLayer) {
19
+ if (Object.prototype.hasOwnProperty.call(dl, name)) {
20
+ const pv = dl[name];
21
+ if (pv && pv !== '0') {
22
+ out = out.concat(pv);
23
+ }
24
+ }
25
+ }
26
+ return out;
27
+ });
28
+ }
29
+ exports.default = dataLayerFetcher;
30
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmV0Y2hlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYXRhTGF5ZXIvZmV0Y2hlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7OztBQU1BLFNBQThCLGdCQUFnQixDQUFDLENBQVMsRUFBRSxJQUFZOztRQUNwRSxJQUFJLENBQUMsQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLFNBQVMsSUFBSSxJQUFJLENBQUMsTUFBTSxLQUFLLENBQUMsRUFBRTtZQUMzQyxPQUFPLEVBQUUsQ0FBQTtTQUNWO1FBRUQsSUFBSSxHQUFHLEdBQVUsRUFBRSxDQUFBO1FBRW5CLEtBQUssTUFBTSxFQUFFLElBQUksQ0FBQyxDQUFDLFNBQVMsRUFBRTtZQUM1QixJQUFJLE1BQU0sQ0FBQyxTQUFTLENBQUMsY0FBYyxDQUFDLElBQUksQ0FBQyxFQUFFLEVBQUUsSUFBSSxDQUFDLEVBQUU7Z0JBQ2xELE1BQU0sRUFBRSxHQUFHLEVBQUUsQ0FBQyxJQUFJLENBQUMsQ0FBQTtnQkFDbkIsSUFBSSxFQUFFLElBQUksRUFBRSxLQUFLLEdBQUcsRUFBRTtvQkFDcEIsR0FBRyxHQUFHLEdBQUcsQ0FBQyxNQUFNLENBQUMsRUFBRSxDQUFDLENBQUE7aUJBQ3JCO2FBQ0Y7U0FDRjtRQUVELE9BQU8sR0FBRyxDQUFBO0lBQ1osQ0FBQztDQUFBO0FBakJELG1DQWlCQyJ9
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetcher = void 0;
7
+ const fetcher_1 = __importDefault(require("./fetcher"));
8
+ exports.fetcher = fetcher_1.default;
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGF0YUxheWVyL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLHdEQUErQjtBQUV0QixrQkFGRixpQkFBTyxDQUVFIn0=