@croct/sdk 0.9.1 → 0.11.0-alpha
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/activeRecord.js +32 -36
- package/cache/fallbackCache.js +15 -32
- package/cache/inMemoryCache.js +9 -10
- package/cache/index.js +1 -1
- package/cache/localStorageCache.js +24 -25
- package/channel/beaconSocketChannel.d.ts +1 -1
- package/channel/beaconSocketChannel.js +49 -79
- package/channel/channel.d.ts +1 -1
- package/channel/encodedChannel.js +8 -10
- package/channel/guaranteedChannel.d.ts +4 -4
- package/channel/guaranteedChannel.js +41 -43
- package/channel/index.js +1 -1
- package/channel/queuedChannel.js +35 -64
- package/channel/retryChannel.d.ts +1 -1
- package/channel/retryChannel.js +44 -77
- package/channel/sandboxChannel.js +17 -18
- package/channel/socketChannel.d.ts +4 -4
- package/channel/socketChannel.js +77 -79
- package/cid/cachedAssigner.js +15 -27
- package/cid/fixedAssigner.js +5 -6
- package/cid/index.js +1 -1
- package/cid/remoteAssigner.js +23 -36
- package/constants.d.ts +3 -2
- package/constants.js +6 -5
- package/container.d.ts +13 -6
- package/container.js +152 -168
- package/contentFetcher.d.ts +59 -0
- package/contentFetcher.js +129 -0
- package/context.d.ts +3 -3
- package/context.js +36 -38
- package/error.js +2 -2
- package/evaluator.d.ts +33 -24
- package/evaluator.js +126 -117
- package/eventManager.d.ts +1 -1
- package/eventManager.js +14 -15
- package/facade/contentFetcherFacade.d.ts +27 -0
- package/facade/contentFetcherFacade.js +40 -0
- package/facade/evaluatorFacade.d.ts +13 -3
- package/facade/evaluatorFacade.js +57 -72
- package/facade/sdkFacade.d.ts +10 -3
- package/facade/sdkFacade.js +129 -141
- package/facade/sessionFacade.js +6 -7
- package/facade/sessionPatch.js +9 -13
- package/facade/trackerFacade.js +32 -38
- package/facade/userFacade.js +10 -11
- package/facade/userPatch.js +9 -13
- package/index.js +2 -2
- package/logging/consoleLogger.js +18 -35
- package/logging/index.js +1 -1
- package/logging/namespacedLogger.js +14 -15
- package/logging/nullLogger.js +10 -13
- package/namespacedStorage.js +30 -47
- package/package.json +13 -16
- package/patch.d.ts +1 -1
- package/queue/capacityRestrictedQueue.js +17 -18
- package/queue/inMemoryQueue.js +22 -28
- package/queue/index.js +1 -1
- package/queue/monitoredQueue.d.ts +2 -2
- package/queue/monitoredQueue.js +39 -40
- package/queue/persistentQueue.js +33 -38
- package/retry/arbitraryPolicy.js +8 -10
- package/retry/backoffPolicy.d.ts +1 -1
- package/retry/backoffPolicy.js +11 -13
- package/retry/index.js +1 -1
- package/retry/maxAttemptsPolicy.js +7 -8
- package/retry/neverPolicy.js +6 -9
- package/schema/attributeSchema.js +1 -1
- package/schema/contentFetcherSchemas.d.ts +2 -0
- package/schema/contentFetcherSchemas.js +22 -0
- package/schema/contentSchemas.js +1 -1
- package/schema/contextSchemas.js +1 -1
- package/schema/ecommerceSchemas.js +1 -1
- package/schema/evaluatorSchemas.d.ts +2 -0
- package/schema/{evaluationSchemas.js → evaluatorSchemas.js} +3 -3
- package/schema/eventSchemas.js +8 -9
- package/schema/index.d.ts +2 -1
- package/schema/index.js +3 -2
- package/schema/loggerSchema.js +1 -1
- package/schema/operationSchemas.js +8 -8
- package/schema/sdkFacadeSchemas.js +9 -6
- package/schema/sdkSchemas.js +8 -5
- package/schema/tokenSchema.js +5 -4
- package/schema/userSchema.js +2 -2
- package/sdk.d.ts +9 -3
- package/sdk.js +81 -127
- package/sdkEvents.d.ts +3 -3
- package/sourceLocation.d.ts +3 -3
- package/sourceLocation.js +13 -14
- package/tab.d.ts +5 -5
- package/tab.js +50 -80
- package/token/cachedTokenStore.js +9 -10
- package/token/inMemoryTokenStore.js +7 -8
- package/token/index.js +1 -1
- package/token/replicatedTokenStore.js +7 -8
- package/token/token.d.ts +9 -5
- package/token/token.js +63 -57
- package/tracker.d.ts +4 -4
- package/tracker.js +145 -122
- package/trackingEvents.d.ts +36 -36
- package/trackingEvents.js +12 -6
- package/transformer.js +1 -1
- package/utilityTypes.d.ts +2 -2
- package/uuid.js +9 -7
- package/validation/arrayType.d.ts +2 -2
- package/validation/arrayType.js +29 -27
- package/validation/booleanType.js +11 -15
- package/validation/functionType.js +11 -15
- package/validation/index.js +1 -1
- package/validation/jsonType.d.ts +2 -2
- package/validation/jsonType.js +61 -80
- package/validation/mixedSchema.js +4 -7
- package/validation/nullType.js +11 -15
- package/validation/numberType.d.ts +1 -1
- package/validation/numberType.js +23 -22
- package/validation/objectType.d.ts +1 -1
- package/validation/objectType.js +61 -73
- package/validation/schema.js +6 -10
- package/validation/stringType.d.ts +1 -1
- package/validation/stringType.js +36 -47
- package/validation/unionType.js +27 -77
- package/validation/violation.js +1 -2
- package/schema/evaluationSchemas.d.ts +0 -2
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import { JsonObject, JsonValue } from '@croct/json';
|
|
2
2
|
import { Evaluator, EvaluationContext } from '../evaluator';
|
|
3
3
|
import { Tab } from '../tab';
|
|
4
|
-
|
|
4
|
+
import { TokenProvider } from '../token';
|
|
5
|
+
import { CidAssigner } from '../cid';
|
|
6
|
+
export type EvaluationOptions = {
|
|
5
7
|
timeout?: number;
|
|
6
8
|
attributes?: JsonObject;
|
|
7
9
|
};
|
|
8
10
|
export interface ContextFactory {
|
|
9
11
|
createContext(attributes?: JsonObject): EvaluationContext;
|
|
10
12
|
}
|
|
13
|
+
export type Configuration = {
|
|
14
|
+
evaluator: Evaluator;
|
|
15
|
+
contextFactory: ContextFactory;
|
|
16
|
+
userTokenProvider: TokenProvider;
|
|
17
|
+
cidAssigner: CidAssigner;
|
|
18
|
+
};
|
|
11
19
|
export declare class EvaluatorFacade {
|
|
12
20
|
private readonly evaluator;
|
|
13
21
|
private readonly contextFactory;
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
private readonly tokenProvider;
|
|
23
|
+
private readonly cidAssigner;
|
|
24
|
+
constructor(configuration: Configuration);
|
|
25
|
+
evaluate(query: string, options?: EvaluationOptions): Promise<JsonValue>;
|
|
16
26
|
}
|
|
17
27
|
export declare class MinimalContextFactory implements ContextFactory {
|
|
18
28
|
createContext(attributes?: JsonObject): EvaluationContext;
|
|
@@ -1,72 +1,69 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TabContextFactory = exports.MinimalContextFactory = exports.EvaluatorFacade = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var error_1 = require("../error");
|
|
4
|
+
const schema_1 = require("../schema");
|
|
5
|
+
const error_1 = require("../error");
|
|
7
6
|
function validate(options) {
|
|
8
|
-
if (typeof options !== 'object' || options === null) {
|
|
9
|
-
throw new Error('The options must be an object.');
|
|
10
|
-
}
|
|
11
7
|
try {
|
|
12
|
-
schema_1.
|
|
8
|
+
schema_1.evaluationOptionsSchema.validate(options);
|
|
13
9
|
}
|
|
14
10
|
catch (violation) {
|
|
15
|
-
throw new Error(
|
|
11
|
+
throw new Error(`Invalid options: ${(0, error_1.formatCause)(violation)}`);
|
|
16
12
|
}
|
|
17
13
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
this.evaluator = evaluator;
|
|
21
|
-
this.contextFactory = contextFactory;
|
|
14
|
+
class EvaluatorFacade {
|
|
15
|
+
constructor(configuration) {
|
|
16
|
+
this.evaluator = configuration.evaluator;
|
|
17
|
+
this.contextFactory = configuration.contextFactory;
|
|
18
|
+
this.tokenProvider = configuration.userTokenProvider;
|
|
19
|
+
this.cidAssigner = configuration.cidAssigner;
|
|
22
20
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (typeof
|
|
26
|
-
throw new Error('The
|
|
21
|
+
async evaluate(query, options = {}) {
|
|
22
|
+
var _a;
|
|
23
|
+
if (typeof query !== 'string' || query.length === 0) {
|
|
24
|
+
throw new Error('The query must be a non-empty string.');
|
|
27
25
|
}
|
|
28
26
|
validate(options);
|
|
29
|
-
return this.evaluator.evaluate(
|
|
27
|
+
return this.evaluator.evaluate(query, {
|
|
28
|
+
clientId: await this.cidAssigner.assignCid(),
|
|
29
|
+
userToken: (_a = this.tokenProvider.getToken()) !== null && _a !== void 0 ? _a : undefined,
|
|
30
30
|
timeout: options.timeout,
|
|
31
31
|
context: this.contextFactory.createContext(options.attributes),
|
|
32
32
|
});
|
|
33
|
-
};
|
|
34
|
-
return EvaluatorFacade;
|
|
35
|
-
}());
|
|
36
|
-
exports.EvaluatorFacade = EvaluatorFacade;
|
|
37
|
-
var MinimalContextFactory = /** @class */ (function () {
|
|
38
|
-
function MinimalContextFactory() {
|
|
39
33
|
}
|
|
40
|
-
|
|
34
|
+
}
|
|
35
|
+
exports.EvaluatorFacade = EvaluatorFacade;
|
|
36
|
+
class MinimalContextFactory {
|
|
37
|
+
createContext(attributes) {
|
|
41
38
|
if (attributes === undefined) {
|
|
42
39
|
return {};
|
|
43
40
|
}
|
|
44
41
|
return { attributes: attributes };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
}());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
48
44
|
exports.MinimalContextFactory = MinimalContextFactory;
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
class TabContextFactory {
|
|
46
|
+
constructor(tab) {
|
|
51
47
|
this.tab = tab;
|
|
52
48
|
}
|
|
53
|
-
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
createContext(attributes) {
|
|
50
|
+
var _a;
|
|
51
|
+
const url = new URL(this.tab.url);
|
|
52
|
+
const context = {};
|
|
53
|
+
const page = {
|
|
57
54
|
title: this.tab.title,
|
|
58
55
|
url: url.toString(),
|
|
59
56
|
};
|
|
60
|
-
|
|
57
|
+
const { referrer } = this.tab;
|
|
61
58
|
if (referrer.length > 0) {
|
|
62
59
|
page.referrer = referrer;
|
|
63
60
|
}
|
|
64
61
|
context.page = page;
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
context.
|
|
62
|
+
const timeZone = (_a = Intl.DateTimeFormat().resolvedOptions().timeZone) !== null && _a !== void 0 ? _a : null;
|
|
63
|
+
if (timeZone !== null) {
|
|
64
|
+
context.timeZone = timeZone;
|
|
68
65
|
}
|
|
69
|
-
|
|
66
|
+
const campaign = TabContextFactory.createCampaign(url);
|
|
70
67
|
if (Object.keys(campaign).length > 0) {
|
|
71
68
|
context.campaign = campaign;
|
|
72
69
|
}
|
|
@@ -74,41 +71,29 @@ var TabContextFactory = /** @class */ (function () {
|
|
|
74
71
|
context.attributes = attributes;
|
|
75
72
|
}
|
|
76
73
|
return context;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
case 'utm_content':
|
|
98
|
-
campaign.content = value;
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
104
|
-
finally {
|
|
105
|
-
try {
|
|
106
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
74
|
+
}
|
|
75
|
+
static createCampaign(url) {
|
|
76
|
+
const campaign = {};
|
|
77
|
+
for (const [parameter, value] of url.searchParams.entries()) {
|
|
78
|
+
switch (parameter.toLowerCase()) {
|
|
79
|
+
case 'utm_campaign':
|
|
80
|
+
campaign.name = value;
|
|
81
|
+
break;
|
|
82
|
+
case 'utm_source':
|
|
83
|
+
campaign.source = value;
|
|
84
|
+
break;
|
|
85
|
+
case 'utm_term':
|
|
86
|
+
campaign.term = value;
|
|
87
|
+
break;
|
|
88
|
+
case 'utm_medium':
|
|
89
|
+
campaign.medium = value;
|
|
90
|
+
break;
|
|
91
|
+
case 'utm_content':
|
|
92
|
+
campaign.content = value;
|
|
93
|
+
break;
|
|
107
94
|
}
|
|
108
|
-
finally { if (e_1) throw e_1.error; }
|
|
109
95
|
}
|
|
110
96
|
return campaign;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
}());
|
|
97
|
+
}
|
|
98
|
+
}
|
|
114
99
|
exports.TabContextFactory = TabContextFactory;
|
package/facade/sdkFacade.d.ts
CHANGED
|
@@ -2,14 +2,15 @@ import { EvaluatorFacade } from './evaluatorFacade';
|
|
|
2
2
|
import { TrackerFacade } from './trackerFacade';
|
|
3
3
|
import { Context, TokenScope } from '../context';
|
|
4
4
|
import { UserFacade } from './userFacade';
|
|
5
|
-
import { Token } from '../token';
|
|
5
|
+
import { Token, TokenStore } from '../token';
|
|
6
6
|
import { SessionFacade } from './sessionFacade';
|
|
7
7
|
import { Logger } from '../logging';
|
|
8
8
|
import { SdkEventMap } from '../sdkEvents';
|
|
9
9
|
import { EventManager } from '../eventManager';
|
|
10
10
|
import { CidAssigner } from '../cid';
|
|
11
11
|
import { UrlSanitizer } from '../tab';
|
|
12
|
-
|
|
12
|
+
import { ContentFetcherFacade } from './contentFetcherFacade';
|
|
13
|
+
export type Configuration = {
|
|
13
14
|
appId: string;
|
|
14
15
|
tokenScope?: TokenScope;
|
|
15
16
|
debug?: boolean;
|
|
@@ -17,6 +18,7 @@ export declare type Configuration = {
|
|
|
17
18
|
track?: boolean;
|
|
18
19
|
token?: string | null;
|
|
19
20
|
userId?: string;
|
|
21
|
+
clientId?: string;
|
|
20
22
|
eventMetadata?: {
|
|
21
23
|
[key: string]: string;
|
|
22
24
|
};
|
|
@@ -24,7 +26,8 @@ export declare type Configuration = {
|
|
|
24
26
|
urlSanitizer?: UrlSanitizer;
|
|
25
27
|
trackerEndpointUrl?: string;
|
|
26
28
|
evaluationEndpointUrl?: string;
|
|
27
|
-
|
|
29
|
+
contentEndpointUrl?: string;
|
|
30
|
+
cidAssignerEndpointUrl?: string;
|
|
28
31
|
};
|
|
29
32
|
export declare class SdkFacade {
|
|
30
33
|
private readonly sdk;
|
|
@@ -32,14 +35,18 @@ export declare class SdkFacade {
|
|
|
32
35
|
private userFacade?;
|
|
33
36
|
private sessionFacade?;
|
|
34
37
|
private evaluatorFacade?;
|
|
38
|
+
private contentFetcherFacade?;
|
|
35
39
|
private constructor();
|
|
36
40
|
static init(configuration: Configuration): SdkFacade;
|
|
37
41
|
get context(): Context;
|
|
38
42
|
get cidAssigner(): CidAssigner;
|
|
43
|
+
get previewTokenStore(): TokenStore;
|
|
44
|
+
get userTokenStore(): TokenStore;
|
|
39
45
|
get tracker(): TrackerFacade;
|
|
40
46
|
get user(): UserFacade;
|
|
41
47
|
get session(): SessionFacade;
|
|
42
48
|
get evaluator(): EvaluatorFacade;
|
|
49
|
+
get contentFetcher(): ContentFetcherFacade;
|
|
43
50
|
get eventManager(): EventManager<Record<string, Record<string, unknown>>, SdkEventMap>;
|
|
44
51
|
identify(userId: string): void;
|
|
45
52
|
anonymize(): void;
|
package/facade/sdkFacade.js
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SdkFacade = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
const evaluatorFacade_1 = require("./evaluatorFacade");
|
|
5
|
+
const trackerFacade_1 = require("./trackerFacade");
|
|
6
|
+
const userFacade_1 = require("./userFacade");
|
|
7
|
+
const token_1 = require("../token");
|
|
8
|
+
const error_1 = require("../error");
|
|
9
|
+
const schema_1 = require("../schema");
|
|
10
|
+
const sdk_1 = require("../sdk");
|
|
11
|
+
const sessionFacade_1 = require("./sessionFacade");
|
|
12
|
+
const contentFetcherFacade_1 = require("./contentFetcherFacade");
|
|
13
13
|
function validateConfiguration(configuration) {
|
|
14
|
-
if (typeof configuration !== 'object' || configuration === null) {
|
|
15
|
-
throw new Error('The configuration must be a key-value map.');
|
|
16
|
-
}
|
|
17
14
|
try {
|
|
18
15
|
schema_1.sdkFacadeConfigurationSchema.validate(configuration);
|
|
19
16
|
}
|
|
20
17
|
catch (violation) {
|
|
21
|
-
throw new Error(
|
|
18
|
+
throw new Error(`Invalid configuration: ${(0, error_1.formatCause)(violation)}`);
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
class SdkFacade {
|
|
22
|
+
constructor(sdk) {
|
|
26
23
|
this.sdk = sdk;
|
|
27
24
|
}
|
|
28
|
-
|
|
25
|
+
static init(configuration) {
|
|
29
26
|
var _a, _b, _c;
|
|
30
27
|
validateConfiguration(configuration);
|
|
31
|
-
|
|
28
|
+
const { track = true, userId, token, ...containerConfiguration } = configuration;
|
|
32
29
|
if (userId !== undefined && token !== undefined) {
|
|
33
30
|
throw new Error('Either the user ID or token can be specified, but not both.');
|
|
34
31
|
}
|
|
35
|
-
|
|
32
|
+
const sdk = new SdkFacade(sdk_1.Sdk.init({
|
|
33
|
+
...containerConfiguration,
|
|
34
|
+
tokenScope: (_a = containerConfiguration.tokenScope) !== null && _a !== void 0 ? _a : 'global',
|
|
35
|
+
debug: (_b = containerConfiguration.debug) !== null && _b !== void 0 ? _b : false,
|
|
36
|
+
test: (_c = containerConfiguration.test) !== null && _c !== void 0 ? _c : false,
|
|
37
|
+
}));
|
|
36
38
|
if (userId !== undefined) {
|
|
37
39
|
sdk.identify(userId);
|
|
38
40
|
}
|
|
@@ -48,100 +50,99 @@ var SdkFacade = /** @class */ (function () {
|
|
|
48
50
|
sdk.tracker.enable();
|
|
49
51
|
}
|
|
50
52
|
return sdk;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
SdkFacade.prototype.identify = function (userId) {
|
|
53
|
+
}
|
|
54
|
+
get context() {
|
|
55
|
+
return this.sdk.context;
|
|
56
|
+
}
|
|
57
|
+
get cidAssigner() {
|
|
58
|
+
return this.sdk.cidAssigner;
|
|
59
|
+
}
|
|
60
|
+
get previewTokenStore() {
|
|
61
|
+
return this.sdk.previewTokenStore;
|
|
62
|
+
}
|
|
63
|
+
get userTokenStore() {
|
|
64
|
+
return this.sdk.userTokenStore;
|
|
65
|
+
}
|
|
66
|
+
get tracker() {
|
|
67
|
+
if (this.trackerFacade === undefined) {
|
|
68
|
+
this.trackerFacade = new trackerFacade_1.TrackerFacade(this.sdk.tracker);
|
|
69
|
+
}
|
|
70
|
+
return this.trackerFacade;
|
|
71
|
+
}
|
|
72
|
+
get user() {
|
|
73
|
+
if (this.userFacade === undefined) {
|
|
74
|
+
this.userFacade = new userFacade_1.UserFacade(this.context, this.sdk.tracker);
|
|
75
|
+
}
|
|
76
|
+
return this.userFacade;
|
|
77
|
+
}
|
|
78
|
+
get session() {
|
|
79
|
+
if (this.sessionFacade === undefined) {
|
|
80
|
+
this.sessionFacade = new sessionFacade_1.SessionFacade(this.sdk.tracker);
|
|
81
|
+
}
|
|
82
|
+
return this.sessionFacade;
|
|
83
|
+
}
|
|
84
|
+
get evaluator() {
|
|
85
|
+
if (this.evaluatorFacade === undefined) {
|
|
86
|
+
this.evaluatorFacade = new evaluatorFacade_1.EvaluatorFacade({
|
|
87
|
+
evaluator: this.sdk.evaluator,
|
|
88
|
+
contextFactory: new evaluatorFacade_1.TabContextFactory(this.sdk
|
|
89
|
+
.context
|
|
90
|
+
.getTab()),
|
|
91
|
+
cidAssigner: this.sdk.cidAssigner,
|
|
92
|
+
userTokenProvider: this.sdk.userTokenStore,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return this.evaluatorFacade;
|
|
96
|
+
}
|
|
97
|
+
get contentFetcher() {
|
|
98
|
+
if (this.contentFetcherFacade === undefined) {
|
|
99
|
+
this.contentFetcherFacade = new contentFetcherFacade_1.ContentFetcherFacade({
|
|
100
|
+
contentFetcher: this.sdk.contentFetcher,
|
|
101
|
+
contextFactory: new evaluatorFacade_1.TabContextFactory(this.sdk
|
|
102
|
+
.context
|
|
103
|
+
.getTab()),
|
|
104
|
+
cidAssigner: this.sdk.cidAssigner,
|
|
105
|
+
previewTokenProvider: this.sdk.previewTokenStore,
|
|
106
|
+
userTokenProvider: this.sdk.userTokenStore,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return this.contentFetcherFacade;
|
|
110
|
+
}
|
|
111
|
+
get eventManager() {
|
|
112
|
+
const { eventManager } = this.sdk;
|
|
113
|
+
return {
|
|
114
|
+
addListener: eventManager.addListener.bind(eventManager),
|
|
115
|
+
removeListener: eventManager.removeListener.bind(eventManager),
|
|
116
|
+
dispatch: (eventName, event) => {
|
|
117
|
+
if (!/[a-z][a-z_]+\.[a-z][a-z_]+/i.test(eventName)) {
|
|
118
|
+
throw new Error('The event name must be in the form of "namespaced.eventName", where '
|
|
119
|
+
+ 'both the namespace and event name must start with a letter, followed by '
|
|
120
|
+
+ 'any series of letters and underscores.');
|
|
121
|
+
}
|
|
122
|
+
eventManager.dispatch(eventName, event);
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
identify(userId) {
|
|
126
127
|
this.setToken(token_1.Token.issue(this.sdk.appId, userId));
|
|
127
|
-
}
|
|
128
|
-
|
|
128
|
+
}
|
|
129
|
+
anonymize() {
|
|
129
130
|
if (!this.context.isAnonymous()) {
|
|
130
131
|
this.unsetToken();
|
|
131
132
|
}
|
|
132
|
-
}
|
|
133
|
-
|
|
133
|
+
}
|
|
134
|
+
getToken() {
|
|
134
135
|
return this.context.getToken();
|
|
135
|
-
}
|
|
136
|
-
|
|
136
|
+
}
|
|
137
|
+
setToken(token) {
|
|
137
138
|
var _a;
|
|
138
|
-
|
|
139
|
+
const currentToken = this.getToken();
|
|
139
140
|
if (currentToken !== null && currentToken.toString() === token.toString()) {
|
|
140
141
|
return;
|
|
141
142
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
const currentSubject = (_a = currentToken === null || currentToken === void 0 ? void 0 : currentToken.getSubject()) !== null && _a !== void 0 ? _a : null;
|
|
144
|
+
const subject = token.getSubject();
|
|
145
|
+
const logger = this.getLogger();
|
|
145
146
|
if (subject === currentSubject) {
|
|
146
147
|
this.context.setToken(token);
|
|
147
148
|
logger.debug('Token refreshed');
|
|
@@ -160,17 +161,17 @@ var SdkFacade = /** @class */ (function () {
|
|
|
160
161
|
type: 'userSignedIn',
|
|
161
162
|
userId: subject,
|
|
162
163
|
});
|
|
163
|
-
logger.info(
|
|
164
|
+
logger.info(`User signed in as ${subject}`);
|
|
164
165
|
}
|
|
165
166
|
logger.debug('New token saved, ');
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
|
|
167
|
+
}
|
|
168
|
+
unsetToken() {
|
|
169
|
+
const token = this.getToken();
|
|
169
170
|
if (token === null) {
|
|
170
171
|
return;
|
|
171
172
|
}
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
const logger = this.getLogger();
|
|
174
|
+
const subject = token.getSubject();
|
|
174
175
|
if (subject !== null) {
|
|
175
176
|
this.trackInternalEvent({
|
|
176
177
|
type: 'userSignedOut',
|
|
@@ -180,39 +181,26 @@ var SdkFacade = /** @class */ (function () {
|
|
|
180
181
|
}
|
|
181
182
|
this.context.setToken(null);
|
|
182
183
|
logger.debug('Token removed');
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
this.sdk
|
|
184
|
+
}
|
|
185
|
+
trackInternalEvent(event) {
|
|
186
|
+
this.sdk
|
|
187
|
+
.tracker
|
|
188
|
+
.track(event)
|
|
189
|
+
.catch(() => {
|
|
186
190
|
// suppress error as it is already logged by the tracker
|
|
187
191
|
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
var subnamespace = [];
|
|
200
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
201
|
-
subnamespace[_i - 1] = arguments[_i];
|
|
202
|
-
}
|
|
203
|
-
return (_a = this.sdk).getTabStorage.apply(_a, tslib_1.__spreadArray([namespace], tslib_1.__read(subnamespace), false));
|
|
204
|
-
};
|
|
205
|
-
SdkFacade.prototype.getBrowserStorage = function (namespace) {
|
|
206
|
-
var _a;
|
|
207
|
-
var subnamespace = [];
|
|
208
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
209
|
-
subnamespace[_i - 1] = arguments[_i];
|
|
210
|
-
}
|
|
211
|
-
return (_a = this.sdk).getBrowserStorage.apply(_a, tslib_1.__spreadArray([namespace], tslib_1.__read(subnamespace), false));
|
|
212
|
-
};
|
|
213
|
-
SdkFacade.prototype.close = function () {
|
|
192
|
+
}
|
|
193
|
+
getLogger(...namespace) {
|
|
194
|
+
return this.sdk.getLogger(...namespace);
|
|
195
|
+
}
|
|
196
|
+
getTabStorage(namespace, ...subnamespace) {
|
|
197
|
+
return this.sdk.getTabStorage(namespace, ...subnamespace);
|
|
198
|
+
}
|
|
199
|
+
getBrowserStorage(namespace, ...subnamespace) {
|
|
200
|
+
return this.sdk.getBrowserStorage(namespace, ...subnamespace);
|
|
201
|
+
}
|
|
202
|
+
close() {
|
|
214
203
|
return this.sdk.close();
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
}());
|
|
204
|
+
}
|
|
205
|
+
}
|
|
218
206
|
exports.SdkFacade = SdkFacade;
|
package/facade/sessionFacade.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SessionFacade = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const sessionPatch_1 = require("./sessionPatch");
|
|
5
|
+
class SessionFacade {
|
|
6
|
+
constructor(tracker) {
|
|
7
7
|
this.tracker = tracker;
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
edit() {
|
|
10
10
|
return new sessionPatch_1.SessionPatch(this.tracker);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
}());
|
|
11
|
+
}
|
|
12
|
+
}
|
|
14
13
|
exports.SessionFacade = SessionFacade;
|
package/facade/sessionPatch.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SessionPatch = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var _this = _super.call(this) || this;
|
|
10
|
-
_this.tracker = tracker;
|
|
11
|
-
return _this;
|
|
4
|
+
const activeRecord_1 = require("../activeRecord");
|
|
5
|
+
class SessionPatch extends activeRecord_1.ActiveRecord {
|
|
6
|
+
constructor(tracker) {
|
|
7
|
+
super();
|
|
8
|
+
this.tracker = tracker;
|
|
12
9
|
}
|
|
13
|
-
|
|
10
|
+
save() {
|
|
14
11
|
if (!this.isDirty()) {
|
|
15
12
|
// Empty patch
|
|
16
13
|
return Promise.resolve({
|
|
@@ -18,13 +15,12 @@ var SessionPatch = /** @class */ (function (_super) {
|
|
|
18
15
|
patch: { operations: [] },
|
|
19
16
|
});
|
|
20
17
|
}
|
|
21
|
-
|
|
18
|
+
const promise = this.tracker.track({
|
|
22
19
|
type: 'sessionAttributesChanged',
|
|
23
20
|
patch: this.buildPatch(),
|
|
24
21
|
});
|
|
25
22
|
this.reset();
|
|
26
23
|
return promise;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
}(activeRecord_1.ActiveRecord));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
30
26
|
exports.SessionPatch = SessionPatch;
|