@posthog/core 1.0.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/error-tracking/chunk-ids.d.ts +5 -0
- package/dist/error-tracking/chunk-ids.d.ts.map +1 -0
- package/dist/error-tracking/chunk-ids.js +68 -0
- package/dist/error-tracking/chunk-ids.mjs +34 -0
- package/dist/error-tracking/coercers/dom-exception-coercer.d.ts +10 -0
- package/dist/error-tracking/coercers/dom-exception-coercer.d.ts.map +1 -0
- package/dist/error-tracking/coercers/dom-exception-coercer.js +65 -0
- package/dist/error-tracking/coercers/dom-exception-coercer.mjs +31 -0
- package/dist/error-tracking/coercers/error-coercer.d.ts +9 -0
- package/dist/error-tracking/coercers/error-coercer.d.ts.map +1 -0
- package/dist/error-tracking/coercers/error-coercer.js +61 -0
- package/dist/error-tracking/coercers/error-coercer.mjs +27 -0
- package/dist/error-tracking/coercers/error-event-coercer.d.ts +7 -0
- package/dist/error-tracking/coercers/error-event-coercer.d.ts.map +1 -0
- package/dist/error-tracking/coercers/error-event-coercer.js +52 -0
- package/dist/error-tracking/coercers/error-event-coercer.mjs +18 -0
- package/dist/error-tracking/coercers/event-coercer.d.ts +6 -0
- package/dist/error-tracking/coercers/event-coercer.d.ts.map +1 -0
- package/dist/error-tracking/coercers/event-coercer.js +51 -0
- package/dist/error-tracking/coercers/event-coercer.mjs +17 -0
- package/dist/error-tracking/coercers/index.d.ts +9 -0
- package/dist/error-tracking/coercers/index.d.ts.map +1 -0
- package/dist/error-tracking/coercers/index.js +123 -0
- package/dist/error-tracking/coercers/index.mjs +8 -0
- package/dist/error-tracking/coercers/object-coercer.d.ts +14 -0
- package/dist/error-tracking/coercers/object-coercer.d.ts.map +1 -0
- package/dist/error-tracking/coercers/object-coercer.js +85 -0
- package/dist/error-tracking/coercers/object-coercer.mjs +51 -0
- package/dist/error-tracking/coercers/primitive-coercer.d.ts +7 -0
- package/dist/error-tracking/coercers/primitive-coercer.d.ts.map +1 -0
- package/dist/error-tracking/coercers/primitive-coercer.js +49 -0
- package/dist/error-tracking/coercers/primitive-coercer.mjs +15 -0
- package/dist/error-tracking/coercers/promise-rejection-event.d.ts +7 -0
- package/dist/error-tracking/coercers/promise-rejection-event.d.ts.map +1 -0
- package/dist/error-tracking/coercers/promise-rejection-event.js +59 -0
- package/dist/error-tracking/coercers/promise-rejection-event.mjs +25 -0
- package/dist/error-tracking/coercers/string-coercer.d.ts +7 -0
- package/dist/error-tracking/coercers/string-coercer.d.ts.map +1 -0
- package/dist/error-tracking/coercers/string-coercer.js +63 -0
- package/dist/error-tracking/coercers/string-coercer.mjs +29 -0
- package/dist/error-tracking/coercers/utils.d.ts +8 -0
- package/dist/error-tracking/coercers/utils.d.ts.map +1 -0
- package/dist/error-tracking/coercers/utils.js +55 -0
- package/dist/error-tracking/coercers/utils.mjs +18 -0
- package/dist/error-tracking/error-properties-builder.d.ts +18 -0
- package/dist/error-tracking/error-properties-builder.d.ts.map +1 -0
- package/dist/error-tracking/error-properties-builder.js +152 -0
- package/dist/error-tracking/error-properties-builder.mjs +118 -0
- package/dist/error-tracking/index.d.ts +6 -0
- package/dist/error-tracking/index.d.ts.map +1 -0
- package/dist/error-tracking/index.js +87 -0
- package/dist/error-tracking/index.mjs +4 -0
- package/dist/error-tracking/parsers/base.d.ts +9 -0
- package/dist/error-tracking/parsers/base.d.ts.map +1 -0
- package/dist/error-tracking/parsers/base.js +71 -0
- package/dist/error-tracking/parsers/base.mjs +19 -0
- package/dist/error-tracking/parsers/chrome.d.ts +3 -0
- package/dist/error-tracking/parsers/chrome.d.ts.map +1 -0
- package/dist/error-tracking/parsers/chrome.js +61 -0
- package/dist/error-tracking/parsers/chrome.mjs +27 -0
- package/dist/error-tracking/parsers/gecko.d.ts +3 -0
- package/dist/error-tracking/parsers/gecko.d.ts.map +1 -0
- package/dist/error-tracking/parsers/gecko.js +58 -0
- package/dist/error-tracking/parsers/gecko.mjs +24 -0
- package/dist/error-tracking/parsers/index.d.ts +9 -0
- package/dist/error-tracking/parsers/index.d.ts.map +1 -0
- package/dist/error-tracking/parsers/index.js +99 -0
- package/dist/error-tracking/parsers/index.mjs +44 -0
- package/dist/error-tracking/parsers/node.d.ts +3 -0
- package/dist/error-tracking/parsers/node.d.ts.map +1 -0
- package/dist/error-tracking/parsers/node.js +99 -0
- package/dist/error-tracking/parsers/node.mjs +65 -0
- package/dist/error-tracking/parsers/opera.d.ts +4 -0
- package/dist/error-tracking/parsers/opera.d.ts.map +1 -0
- package/dist/error-tracking/parsers/opera.js +49 -0
- package/dist/error-tracking/parsers/opera.mjs +12 -0
- package/dist/error-tracking/parsers/react-native.d.ts +1 -0
- package/dist/error-tracking/parsers/react-native.d.ts.map +1 -0
- package/dist/error-tracking/parsers/react-native.js +5 -0
- package/dist/error-tracking/parsers/react-native.mjs +0 -0
- package/dist/error-tracking/parsers/safari.d.ts +22 -0
- package/dist/error-tracking/parsers/safari.d.ts.map +1 -0
- package/dist/error-tracking/parsers/safari.js +47 -0
- package/dist/error-tracking/parsers/safari.mjs +13 -0
- package/dist/error-tracking/parsers/winjs.d.ts +3 -0
- package/dist/error-tracking/parsers/winjs.d.ts.map +1 -0
- package/dist/error-tracking/parsers/winjs.js +41 -0
- package/dist/error-tracking/parsers/winjs.mjs +7 -0
- package/dist/error-tracking/types.d.ts +87 -0
- package/dist/error-tracking/types.d.ts.map +1 -0
- package/dist/error-tracking/types.js +43 -0
- package/dist/error-tracking/types.mjs +9 -0
- package/dist/error-tracking/utils.d.ts +13 -0
- package/dist/error-tracking/utils.d.ts.map +1 -0
- package/dist/error-tracking/utils.js +57 -0
- package/dist/error-tracking/utils.mjs +23 -0
- package/dist/eventemitter.js +4 -4
- package/dist/eventemitter.mjs +4 -4
- package/dist/featureFlagUtils.js +20 -45
- package/dist/featureFlagUtils.mjs +20 -45
- package/dist/gzip.js +1 -2
- package/dist/gzip.mjs +1 -2
- package/dist/index.d.ts +5 -255
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +61 -1223
- package/dist/index.mjs +6 -1190
- package/dist/posthog-core-stateless.d.ts +204 -0
- package/dist/posthog-core-stateless.d.ts.map +1 -0
- package/dist/posthog-core-stateless.js +675 -0
- package/dist/posthog-core-stateless.mjs +632 -0
- package/dist/posthog-core.d.ts +171 -0
- package/dist/posthog-core.d.ts.map +1 -0
- package/dist/posthog-core.js +554 -0
- package/dist/posthog-core.mjs +520 -0
- package/dist/testing/PostHogCoreTestClient.d.ts +2 -1
- package/dist/testing/PostHogCoreTestClient.d.ts.map +1 -1
- package/dist/testing/PostHogCoreTestClient.js +9 -11
- package/dist/testing/PostHogCoreTestClient.mjs +8 -10
- package/dist/testing/test-utils.d.ts +2 -0
- package/dist/testing/test-utils.d.ts.map +1 -1
- package/dist/testing/test-utils.js +13 -1
- package/dist/testing/test-utils.mjs +11 -2
- package/dist/utils/bucketed-rate-limiter.d.ts.map +1 -1
- package/dist/utils/bucketed-rate-limiter.js +8 -12
- package/dist/utils/bucketed-rate-limiter.mjs +8 -12
- package/dist/utils/index.js +3 -3
- package/dist/utils/index.mjs +3 -3
- package/dist/utils/number-utils.d.ts.map +1 -1
- package/dist/utils/type-utils.d.ts +8 -1
- package/dist/utils/type-utils.d.ts.map +1 -1
- package/dist/utils/type-utils.js +61 -6
- package/dist/utils/type-utils.mjs +36 -2
- package/dist/vendor/uuidv7.js +12 -16
- package/dist/vendor/uuidv7.mjs +12 -16
- package/package.json +10 -4
- package/src/__tests__/featureFlagUtils.spec.ts +427 -0
- package/src/__tests__/gzip.spec.ts +69 -0
- package/src/__tests__/posthog.ai.spec.ts +110 -0
- package/src/__tests__/posthog.capture.spec.ts +91 -0
- package/src/__tests__/posthog.core.spec.ts +135 -0
- package/src/__tests__/posthog.debug.spec.ts +36 -0
- package/src/__tests__/posthog.enqueue.spec.ts +93 -0
- package/src/__tests__/posthog.featureflags.spec.ts +1106 -0
- package/src/__tests__/posthog.featureflags.v1.spec.ts +922 -0
- package/src/__tests__/posthog.flush.spec.ts +237 -0
- package/src/__tests__/posthog.gdpr.spec.ts +50 -0
- package/src/__tests__/posthog.groups.spec.ts +96 -0
- package/src/__tests__/posthog.identify.spec.ts +194 -0
- package/src/__tests__/posthog.init.spec.ts +110 -0
- package/src/__tests__/posthog.listeners.spec.ts +51 -0
- package/src/__tests__/posthog.register.spec.ts +47 -0
- package/src/__tests__/posthog.reset.spec.ts +76 -0
- package/src/__tests__/posthog.sessions.spec.ts +63 -0
- package/src/__tests__/posthog.setProperties.spec.ts +102 -0
- package/src/__tests__/posthog.shutdown.spec.ts +88 -0
- package/src/__tests__/utils.spec.ts +36 -0
- package/src/error-tracking/chunk-ids.ts +58 -0
- package/src/error-tracking/coercers/dom-exception-coercer.ts +38 -0
- package/src/error-tracking/coercers/error-coercer.ts +36 -0
- package/src/error-tracking/coercers/error-event-coercer.ts +24 -0
- package/src/error-tracking/coercers/event-coercer.ts +19 -0
- package/src/error-tracking/coercers/index.ts +8 -0
- package/src/error-tracking/coercers/object-coercer.ts +76 -0
- package/src/error-tracking/coercers/primitive-coercer.ts +19 -0
- package/src/error-tracking/coercers/promise-rejection-event.spec.ts +77 -0
- package/src/error-tracking/coercers/promise-rejection-event.ts +53 -0
- package/src/error-tracking/coercers/string-coercer.spec.ts +26 -0
- package/src/error-tracking/coercers/string-coercer.ts +31 -0
- package/src/error-tracking/coercers/utils.ts +33 -0
- package/src/error-tracking/error-properties-builder.coerce.spec.ts +202 -0
- package/src/error-tracking/error-properties-builder.parse.spec.ts +30 -0
- package/src/error-tracking/error-properties-builder.ts +169 -0
- package/src/error-tracking/index.ts +5 -0
- package/src/error-tracking/parsers/base.ts +29 -0
- package/src/error-tracking/parsers/chrome.ts +53 -0
- package/src/error-tracking/parsers/gecko.ts +38 -0
- package/src/error-tracking/parsers/index.ts +104 -0
- package/src/error-tracking/parsers/node.ts +111 -0
- package/src/error-tracking/parsers/opera.ts +18 -0
- package/src/error-tracking/parsers/react-native.ts +0 -0
- package/src/error-tracking/parsers/safari.ts +33 -0
- package/src/error-tracking/parsers/winjs.ts +12 -0
- package/src/error-tracking/types.ts +107 -0
- package/src/error-tracking/utils.ts +39 -0
- package/src/eventemitter.ts +27 -0
- package/src/featureFlagUtils.ts +192 -0
- package/src/gzip.ts +29 -0
- package/src/index.ts +8 -0
- package/src/posthog-core-stateless.ts +1226 -0
- package/src/posthog-core.ts +958 -0
- package/src/testing/PostHogCoreTestClient.ts +91 -0
- package/src/testing/index.ts +2 -0
- package/src/testing/test-utils.ts +47 -0
- package/src/types.ts +544 -0
- package/src/utils/bucketed-rate-limiter.spec.ts +33 -0
- package/src/utils/bucketed-rate-limiter.ts +85 -0
- package/src/utils/index.ts +98 -0
- package/src/utils/number-utils.spec.ts +89 -0
- package/src/utils/number-utils.ts +30 -0
- package/src/utils/promise-queue.spec.ts +55 -0
- package/src/utils/promise-queue.ts +30 -0
- package/src/utils/string-utils.ts +23 -0
- package/src/utils/type-utils.ts +134 -0
- package/src/vendor/uuidv7.ts +479 -0
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { createFlagsResponseFromFlagsAndPayloads, getFeatureFlagValue, getFlagValuesFromFlags, getPayloadsFromFlags, normalizeFlagsResponse, updateFlagValue } from "./featureFlagUtils.mjs";
|
|
2
|
+
import { Compression, PostHogPersistedProperty } from "./types.mjs";
|
|
3
|
+
import { PostHogCoreStateless, QuotaLimitedFeature, maybeAdd } from "./posthog-core-stateless.mjs";
|
|
4
|
+
import { uuidv7 } from "./vendor/uuidv7.mjs";
|
|
5
|
+
import { isPlainError } from "./utils/index.mjs";
|
|
6
|
+
class PostHogCore extends PostHogCoreStateless {
|
|
7
|
+
constructor(apiKey, options){
|
|
8
|
+
const disableGeoipOption = options?.disableGeoip ?? false;
|
|
9
|
+
const featureFlagsRequestTimeoutMs = options?.featureFlagsRequestTimeoutMs ?? 10000;
|
|
10
|
+
super(apiKey, {
|
|
11
|
+
...options,
|
|
12
|
+
disableGeoip: disableGeoipOption,
|
|
13
|
+
featureFlagsRequestTimeoutMs
|
|
14
|
+
}), this.flagCallReported = {}, this._sessionMaxLengthSeconds = 86400, this.sessionProps = {};
|
|
15
|
+
this.sendFeatureFlagEvent = options?.sendFeatureFlagEvent ?? true;
|
|
16
|
+
this._sessionExpirationTimeSeconds = options?.sessionExpirationTimeSeconds ?? 1800;
|
|
17
|
+
}
|
|
18
|
+
setupBootstrap(options) {
|
|
19
|
+
const bootstrap = options?.bootstrap;
|
|
20
|
+
if (!bootstrap) return;
|
|
21
|
+
if (bootstrap.distinctId) if (bootstrap.isIdentifiedId) {
|
|
22
|
+
const distinctId = this.getPersistedProperty(PostHogPersistedProperty.DistinctId);
|
|
23
|
+
if (!distinctId) this.setPersistedProperty(PostHogPersistedProperty.DistinctId, bootstrap.distinctId);
|
|
24
|
+
} else {
|
|
25
|
+
const anonymousId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
|
|
26
|
+
if (!anonymousId) this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, bootstrap.distinctId);
|
|
27
|
+
}
|
|
28
|
+
const bootstrapFeatureFlags = bootstrap.featureFlags;
|
|
29
|
+
const bootstrapFeatureFlagPayloads = bootstrap.featureFlagPayloads ?? {};
|
|
30
|
+
if (bootstrapFeatureFlags && Object.keys(bootstrapFeatureFlags).length) {
|
|
31
|
+
const normalizedBootstrapFeatureFlagDetails = createFlagsResponseFromFlagsAndPayloads(bootstrapFeatureFlags, bootstrapFeatureFlagPayloads);
|
|
32
|
+
if (Object.keys(normalizedBootstrapFeatureFlagDetails.flags).length > 0) {
|
|
33
|
+
this.setBootstrappedFeatureFlagDetails(normalizedBootstrapFeatureFlagDetails);
|
|
34
|
+
const currentFeatureFlagDetails = this.getKnownFeatureFlagDetails() || {
|
|
35
|
+
flags: {},
|
|
36
|
+
requestId: void 0
|
|
37
|
+
};
|
|
38
|
+
const newFeatureFlagDetails = {
|
|
39
|
+
flags: {
|
|
40
|
+
...normalizedBootstrapFeatureFlagDetails.flags,
|
|
41
|
+
...currentFeatureFlagDetails.flags
|
|
42
|
+
},
|
|
43
|
+
requestId: normalizedBootstrapFeatureFlagDetails.requestId
|
|
44
|
+
};
|
|
45
|
+
this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
clearProps() {
|
|
50
|
+
this.props = void 0;
|
|
51
|
+
this.sessionProps = {};
|
|
52
|
+
this.flagCallReported = {};
|
|
53
|
+
}
|
|
54
|
+
on(event, cb) {
|
|
55
|
+
return this._events.on(event, cb);
|
|
56
|
+
}
|
|
57
|
+
reset(propertiesToKeep) {
|
|
58
|
+
this.wrap(()=>{
|
|
59
|
+
const allPropertiesToKeep = [
|
|
60
|
+
PostHogPersistedProperty.Queue,
|
|
61
|
+
...propertiesToKeep || []
|
|
62
|
+
];
|
|
63
|
+
this.clearProps();
|
|
64
|
+
for (const key of Object.keys(PostHogPersistedProperty))if (!allPropertiesToKeep.includes(PostHogPersistedProperty[key])) this.setPersistedProperty(PostHogPersistedProperty[key], null);
|
|
65
|
+
this.reloadFeatureFlags();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
getCommonEventProperties() {
|
|
69
|
+
const featureFlags = this.getFeatureFlags();
|
|
70
|
+
const featureVariantProperties = {};
|
|
71
|
+
if (featureFlags) for (const [feature, variant] of Object.entries(featureFlags))featureVariantProperties[`$feature/${feature}`] = variant;
|
|
72
|
+
return {
|
|
73
|
+
...maybeAdd('$active_feature_flags', featureFlags ? Object.keys(featureFlags) : void 0),
|
|
74
|
+
...featureVariantProperties,
|
|
75
|
+
...super.getCommonEventProperties()
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
enrichProperties(properties) {
|
|
79
|
+
return {
|
|
80
|
+
...this.props,
|
|
81
|
+
...this.sessionProps,
|
|
82
|
+
...properties || {},
|
|
83
|
+
...this.getCommonEventProperties(),
|
|
84
|
+
$session_id: this.getSessionId()
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
getSessionId() {
|
|
88
|
+
if (!this._isInitialized) return '';
|
|
89
|
+
let sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
|
|
90
|
+
const sessionLastTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp) || 0;
|
|
91
|
+
const sessionStartTimestamp = this.getPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp) || 0;
|
|
92
|
+
const now = Date.now();
|
|
93
|
+
const sessionLastDif = now - sessionLastTimestamp;
|
|
94
|
+
const sessionStartDif = now - sessionStartTimestamp;
|
|
95
|
+
if (!sessionId || sessionLastDif > 1000 * this._sessionExpirationTimeSeconds || sessionStartDif > 1000 * this._sessionMaxLengthSeconds) {
|
|
96
|
+
sessionId = uuidv7();
|
|
97
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionId, sessionId);
|
|
98
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp, now);
|
|
99
|
+
}
|
|
100
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, now);
|
|
101
|
+
return sessionId;
|
|
102
|
+
}
|
|
103
|
+
resetSessionId() {
|
|
104
|
+
this.wrap(()=>{
|
|
105
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionId, null);
|
|
106
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, null);
|
|
107
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp, null);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
getAnonymousId() {
|
|
111
|
+
if (!this._isInitialized) return '';
|
|
112
|
+
let anonId = this.getPersistedProperty(PostHogPersistedProperty.AnonymousId);
|
|
113
|
+
if (!anonId) {
|
|
114
|
+
anonId = uuidv7();
|
|
115
|
+
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, anonId);
|
|
116
|
+
}
|
|
117
|
+
return anonId;
|
|
118
|
+
}
|
|
119
|
+
getDistinctId() {
|
|
120
|
+
if (!this._isInitialized) return '';
|
|
121
|
+
return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
|
|
122
|
+
}
|
|
123
|
+
registerForSession(properties) {
|
|
124
|
+
this.sessionProps = {
|
|
125
|
+
...this.sessionProps,
|
|
126
|
+
...properties
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
unregisterForSession(property) {
|
|
130
|
+
delete this.sessionProps[property];
|
|
131
|
+
}
|
|
132
|
+
identify(distinctId, properties, options) {
|
|
133
|
+
this.wrap(()=>{
|
|
134
|
+
const previousDistinctId = this.getDistinctId();
|
|
135
|
+
distinctId = distinctId || previousDistinctId;
|
|
136
|
+
if (properties?.$groups) this.groups(properties.$groups);
|
|
137
|
+
const userPropsOnce = properties?.$set_once;
|
|
138
|
+
delete properties?.$set_once;
|
|
139
|
+
const userProps = properties?.$set || properties;
|
|
140
|
+
const allProperties = this.enrichProperties({
|
|
141
|
+
$anon_distinct_id: this.getAnonymousId(),
|
|
142
|
+
...maybeAdd('$set', userProps),
|
|
143
|
+
...maybeAdd('$set_once', userPropsOnce)
|
|
144
|
+
});
|
|
145
|
+
if (distinctId !== previousDistinctId) {
|
|
146
|
+
this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
|
|
147
|
+
this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
|
|
148
|
+
this.reloadFeatureFlags();
|
|
149
|
+
}
|
|
150
|
+
super.identifyStateless(distinctId, allProperties, options);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
capture(event, properties, options) {
|
|
154
|
+
this.wrap(()=>{
|
|
155
|
+
const distinctId = this.getDistinctId();
|
|
156
|
+
if (properties?.$groups) this.groups(properties.$groups);
|
|
157
|
+
const allProperties = this.enrichProperties(properties);
|
|
158
|
+
super.captureStateless(distinctId, event, allProperties, options);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
alias(alias) {
|
|
162
|
+
this.wrap(()=>{
|
|
163
|
+
const distinctId = this.getDistinctId();
|
|
164
|
+
const allProperties = this.enrichProperties({});
|
|
165
|
+
super.aliasStateless(alias, distinctId, allProperties);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
autocapture(eventType, elements, properties = {}, options) {
|
|
169
|
+
this.wrap(()=>{
|
|
170
|
+
const distinctId = this.getDistinctId();
|
|
171
|
+
const payload = {
|
|
172
|
+
distinct_id: distinctId,
|
|
173
|
+
event: '$autocapture',
|
|
174
|
+
properties: {
|
|
175
|
+
...this.enrichProperties(properties),
|
|
176
|
+
$event_type: eventType,
|
|
177
|
+
$elements: elements
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
this.enqueue('autocapture', payload, options);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
groups(groups) {
|
|
184
|
+
this.wrap(()=>{
|
|
185
|
+
const existingGroups = this.props.$groups || {};
|
|
186
|
+
this.register({
|
|
187
|
+
$groups: {
|
|
188
|
+
...existingGroups,
|
|
189
|
+
...groups
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
if (Object.keys(groups).find((type)=>existingGroups[type] !== groups[type])) this.reloadFeatureFlags();
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
group(groupType, groupKey, groupProperties, options) {
|
|
196
|
+
this.wrap(()=>{
|
|
197
|
+
this.groups({
|
|
198
|
+
[groupType]: groupKey
|
|
199
|
+
});
|
|
200
|
+
if (groupProperties) this.groupIdentify(groupType, groupKey, groupProperties, options);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
groupIdentify(groupType, groupKey, groupProperties, options) {
|
|
204
|
+
this.wrap(()=>{
|
|
205
|
+
const distinctId = this.getDistinctId();
|
|
206
|
+
const eventProperties = this.enrichProperties({});
|
|
207
|
+
super.groupIdentifyStateless(groupType, groupKey, groupProperties, options, distinctId, eventProperties);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
setPersonPropertiesForFlags(properties) {
|
|
211
|
+
this.wrap(()=>{
|
|
212
|
+
const existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
|
|
213
|
+
this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {
|
|
214
|
+
...existingProperties,
|
|
215
|
+
...properties
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
resetPersonPropertiesForFlags() {
|
|
220
|
+
this.wrap(()=>{
|
|
221
|
+
this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, null);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
setGroupPropertiesForFlags(properties) {
|
|
225
|
+
this.wrap(()=>{
|
|
226
|
+
const existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
|
|
227
|
+
if (0 !== Object.keys(existingProperties).length) Object.keys(existingProperties).forEach((groupType)=>{
|
|
228
|
+
existingProperties[groupType] = {
|
|
229
|
+
...existingProperties[groupType],
|
|
230
|
+
...properties[groupType]
|
|
231
|
+
};
|
|
232
|
+
delete properties[groupType];
|
|
233
|
+
});
|
|
234
|
+
this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {
|
|
235
|
+
...existingProperties,
|
|
236
|
+
...properties
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
resetGroupPropertiesForFlags() {
|
|
241
|
+
this.wrap(()=>{
|
|
242
|
+
this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, null);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
async remoteConfigAsync() {
|
|
246
|
+
await this._initPromise;
|
|
247
|
+
if (this._remoteConfigResponsePromise) return this._remoteConfigResponsePromise;
|
|
248
|
+
return this._remoteConfigAsync();
|
|
249
|
+
}
|
|
250
|
+
async flagsAsync(sendAnonDistinctId = true) {
|
|
251
|
+
await this._initPromise;
|
|
252
|
+
if (this._flagsResponsePromise) return this._flagsResponsePromise;
|
|
253
|
+
return this._flagsAsync(sendAnonDistinctId);
|
|
254
|
+
}
|
|
255
|
+
cacheSessionReplay(source, response) {
|
|
256
|
+
const sessionReplay = response?.sessionRecording;
|
|
257
|
+
if (sessionReplay) {
|
|
258
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, sessionReplay);
|
|
259
|
+
this.logMsgIfDebug(()=>console.log('PostHog Debug', `Session replay config from ${source}: `, JSON.stringify(sessionReplay)));
|
|
260
|
+
} else if ('boolean' == typeof sessionReplay && false === sessionReplay) {
|
|
261
|
+
this.logMsgIfDebug(()=>console.info('PostHog Debug', `Session replay config from ${source} disabled.`));
|
|
262
|
+
this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, null);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async _remoteConfigAsync() {
|
|
266
|
+
this._remoteConfigResponsePromise = this._initPromise.then(()=>{
|
|
267
|
+
let remoteConfig = this.getPersistedProperty(PostHogPersistedProperty.RemoteConfig);
|
|
268
|
+
this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Cached remote config: ', JSON.stringify(remoteConfig)));
|
|
269
|
+
return super.getRemoteConfig().then((response)=>{
|
|
270
|
+
if (response) {
|
|
271
|
+
const remoteConfigWithoutSurveys = {
|
|
272
|
+
...response
|
|
273
|
+
};
|
|
274
|
+
delete remoteConfigWithoutSurveys.surveys;
|
|
275
|
+
this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Fetched remote config: ', JSON.stringify(remoteConfigWithoutSurveys)));
|
|
276
|
+
if (false === this.disableSurveys) {
|
|
277
|
+
const surveys = response.surveys;
|
|
278
|
+
let hasSurveys = true;
|
|
279
|
+
if (Array.isArray(surveys)) this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Surveys fetched from remote config: ', JSON.stringify(surveys)));
|
|
280
|
+
else {
|
|
281
|
+
this.logMsgIfDebug(()=>console.log('PostHog Debug', 'There are no surveys.'));
|
|
282
|
+
hasSurveys = false;
|
|
283
|
+
}
|
|
284
|
+
if (hasSurveys) this.setPersistedProperty(PostHogPersistedProperty.Surveys, surveys);
|
|
285
|
+
else this.setPersistedProperty(PostHogPersistedProperty.Surveys, null);
|
|
286
|
+
} else this.setPersistedProperty(PostHogPersistedProperty.Surveys, null);
|
|
287
|
+
this.setPersistedProperty(PostHogPersistedProperty.RemoteConfig, remoteConfigWithoutSurveys);
|
|
288
|
+
this.cacheSessionReplay('remote config', response);
|
|
289
|
+
if (false === response.hasFeatureFlags) {
|
|
290
|
+
this.setKnownFeatureFlagDetails({
|
|
291
|
+
flags: {}
|
|
292
|
+
});
|
|
293
|
+
this.logMsgIfDebug(()=>console.warn('Remote config has no feature flags, will not load feature flags.'));
|
|
294
|
+
} else if (false !== this.preloadFeatureFlags) this.reloadFeatureFlags();
|
|
295
|
+
if (!response.supportedCompression?.includes(Compression.GZipJS)) this.disableCompression = true;
|
|
296
|
+
remoteConfig = response;
|
|
297
|
+
}
|
|
298
|
+
return remoteConfig;
|
|
299
|
+
});
|
|
300
|
+
}).finally(()=>{
|
|
301
|
+
this._remoteConfigResponsePromise = void 0;
|
|
302
|
+
});
|
|
303
|
+
return this._remoteConfigResponsePromise;
|
|
304
|
+
}
|
|
305
|
+
async _flagsAsync(sendAnonDistinctId = true) {
|
|
306
|
+
this._flagsResponsePromise = this._initPromise.then(async ()=>{
|
|
307
|
+
const distinctId = this.getDistinctId();
|
|
308
|
+
const groups = this.props.$groups || {};
|
|
309
|
+
const personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
|
|
310
|
+
const groupProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
|
|
311
|
+
const extraProperties = {
|
|
312
|
+
$anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : void 0
|
|
313
|
+
};
|
|
314
|
+
const res = await super.getFlags(distinctId, groups, personProperties, groupProperties, extraProperties);
|
|
315
|
+
if (res?.quotaLimited?.includes(QuotaLimitedFeature.FeatureFlags)) {
|
|
316
|
+
this.setKnownFeatureFlagDetails(null);
|
|
317
|
+
console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all flags. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
|
|
318
|
+
return res;
|
|
319
|
+
}
|
|
320
|
+
if (res?.featureFlags) {
|
|
321
|
+
if (this.sendFeatureFlagEvent) this.flagCallReported = {};
|
|
322
|
+
let newFeatureFlagDetails = res;
|
|
323
|
+
if (res.errorsWhileComputingFlags) {
|
|
324
|
+
const currentFlagDetails = this.getKnownFeatureFlagDetails();
|
|
325
|
+
this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Cached feature flags: ', JSON.stringify(currentFlagDetails)));
|
|
326
|
+
newFeatureFlagDetails = {
|
|
327
|
+
...res,
|
|
328
|
+
flags: {
|
|
329
|
+
...currentFlagDetails?.flags,
|
|
330
|
+
...res.flags
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
|
|
335
|
+
this.setPersistedProperty(PostHogPersistedProperty.FlagsEndpointWasHit, true);
|
|
336
|
+
this.cacheSessionReplay('flags', res);
|
|
337
|
+
}
|
|
338
|
+
return res;
|
|
339
|
+
}).finally(()=>{
|
|
340
|
+
this._flagsResponsePromise = void 0;
|
|
341
|
+
});
|
|
342
|
+
return this._flagsResponsePromise;
|
|
343
|
+
}
|
|
344
|
+
setKnownFeatureFlagDetails(flagsResponse) {
|
|
345
|
+
this.wrap(()=>{
|
|
346
|
+
this.setPersistedProperty(PostHogPersistedProperty.FeatureFlagDetails, flagsResponse);
|
|
347
|
+
this._events.emit('featureflags', getFlagValuesFromFlags(flagsResponse?.flags ?? {}));
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
getKnownFeatureFlagDetails() {
|
|
351
|
+
const storedDetails = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagDetails);
|
|
352
|
+
if (!storedDetails) {
|
|
353
|
+
const featureFlags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
354
|
+
const featureFlagPayloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
|
|
355
|
+
if (void 0 === featureFlags && void 0 === featureFlagPayloads) return;
|
|
356
|
+
return createFlagsResponseFromFlagsAndPayloads(featureFlags ?? {}, featureFlagPayloads ?? {});
|
|
357
|
+
}
|
|
358
|
+
return normalizeFlagsResponse(storedDetails);
|
|
359
|
+
}
|
|
360
|
+
getKnownFeatureFlags() {
|
|
361
|
+
const featureFlagDetails = this.getKnownFeatureFlagDetails();
|
|
362
|
+
if (!featureFlagDetails) return;
|
|
363
|
+
return getFlagValuesFromFlags(featureFlagDetails.flags);
|
|
364
|
+
}
|
|
365
|
+
getKnownFeatureFlagPayloads() {
|
|
366
|
+
const featureFlagDetails = this.getKnownFeatureFlagDetails();
|
|
367
|
+
if (!featureFlagDetails) return;
|
|
368
|
+
return getPayloadsFromFlags(featureFlagDetails.flags);
|
|
369
|
+
}
|
|
370
|
+
getBootstrappedFeatureFlagDetails() {
|
|
371
|
+
const details = this.getPersistedProperty(PostHogPersistedProperty.BootstrapFeatureFlagDetails);
|
|
372
|
+
if (!details) return;
|
|
373
|
+
return details;
|
|
374
|
+
}
|
|
375
|
+
setBootstrappedFeatureFlagDetails(details) {
|
|
376
|
+
this.setPersistedProperty(PostHogPersistedProperty.BootstrapFeatureFlagDetails, details);
|
|
377
|
+
}
|
|
378
|
+
getBootstrappedFeatureFlags() {
|
|
379
|
+
const details = this.getBootstrappedFeatureFlagDetails();
|
|
380
|
+
if (!details) return;
|
|
381
|
+
return getFlagValuesFromFlags(details.flags);
|
|
382
|
+
}
|
|
383
|
+
getBootstrappedFeatureFlagPayloads() {
|
|
384
|
+
const details = this.getBootstrappedFeatureFlagDetails();
|
|
385
|
+
if (!details) return;
|
|
386
|
+
return getPayloadsFromFlags(details.flags);
|
|
387
|
+
}
|
|
388
|
+
getFeatureFlag(key) {
|
|
389
|
+
const details = this.getFeatureFlagDetails();
|
|
390
|
+
if (!details) return;
|
|
391
|
+
const featureFlag = details.flags[key];
|
|
392
|
+
let response = getFeatureFlagValue(featureFlag);
|
|
393
|
+
if (void 0 === response) response = false;
|
|
394
|
+
if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
|
|
395
|
+
const bootstrappedResponse = this.getBootstrappedFeatureFlags()?.[key];
|
|
396
|
+
const bootstrappedPayload = this.getBootstrappedFeatureFlagPayloads()?.[key];
|
|
397
|
+
this.flagCallReported[key] = true;
|
|
398
|
+
this.capture('$feature_flag_called', {
|
|
399
|
+
$feature_flag: key,
|
|
400
|
+
$feature_flag_response: response,
|
|
401
|
+
...maybeAdd('$feature_flag_id', featureFlag?.metadata?.id),
|
|
402
|
+
...maybeAdd('$feature_flag_version', featureFlag?.metadata?.version),
|
|
403
|
+
...maybeAdd('$feature_flag_reason', featureFlag?.reason?.description ?? featureFlag?.reason?.code),
|
|
404
|
+
...maybeAdd('$feature_flag_bootstrapped_response', bootstrappedResponse),
|
|
405
|
+
...maybeAdd('$feature_flag_bootstrapped_payload', bootstrappedPayload),
|
|
406
|
+
$used_bootstrap_value: !this.getPersistedProperty(PostHogPersistedProperty.FlagsEndpointWasHit),
|
|
407
|
+
...maybeAdd('$feature_flag_request_id', details.requestId)
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
return response;
|
|
411
|
+
}
|
|
412
|
+
getFeatureFlagPayload(key) {
|
|
413
|
+
const payloads = this.getFeatureFlagPayloads();
|
|
414
|
+
if (!payloads) return;
|
|
415
|
+
const response = payloads[key];
|
|
416
|
+
if (void 0 === response) return null;
|
|
417
|
+
return response;
|
|
418
|
+
}
|
|
419
|
+
getFeatureFlagPayloads() {
|
|
420
|
+
return this.getFeatureFlagDetails()?.featureFlagPayloads;
|
|
421
|
+
}
|
|
422
|
+
getFeatureFlags() {
|
|
423
|
+
return this.getFeatureFlagDetails()?.featureFlags;
|
|
424
|
+
}
|
|
425
|
+
getFeatureFlagDetails() {
|
|
426
|
+
let details = this.getKnownFeatureFlagDetails();
|
|
427
|
+
const overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
|
|
428
|
+
if (!overriddenFlags) return details;
|
|
429
|
+
details = details ?? {
|
|
430
|
+
featureFlags: {},
|
|
431
|
+
featureFlagPayloads: {},
|
|
432
|
+
flags: {}
|
|
433
|
+
};
|
|
434
|
+
const flags = details.flags ?? {};
|
|
435
|
+
for(const key in overriddenFlags)if (overriddenFlags[key]) flags[key] = updateFlagValue(flags[key], overriddenFlags[key]);
|
|
436
|
+
else delete flags[key];
|
|
437
|
+
const result = {
|
|
438
|
+
...details,
|
|
439
|
+
flags
|
|
440
|
+
};
|
|
441
|
+
return normalizeFlagsResponse(result);
|
|
442
|
+
}
|
|
443
|
+
getFeatureFlagsAndPayloads() {
|
|
444
|
+
const flags = this.getFeatureFlags();
|
|
445
|
+
const payloads = this.getFeatureFlagPayloads();
|
|
446
|
+
return {
|
|
447
|
+
flags,
|
|
448
|
+
payloads
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
isFeatureEnabled(key) {
|
|
452
|
+
const response = this.getFeatureFlag(key);
|
|
453
|
+
if (void 0 === response) return;
|
|
454
|
+
return !!response;
|
|
455
|
+
}
|
|
456
|
+
reloadFeatureFlags(options) {
|
|
457
|
+
this.flagsAsync(true).then((res)=>{
|
|
458
|
+
options?.cb?.(void 0, res?.featureFlags);
|
|
459
|
+
}).catch((e)=>{
|
|
460
|
+
options?.cb?.(e, void 0);
|
|
461
|
+
if (!options?.cb) this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Error reloading feature flags', e));
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
async reloadRemoteConfigAsync() {
|
|
465
|
+
return await this.remoteConfigAsync();
|
|
466
|
+
}
|
|
467
|
+
async reloadFeatureFlagsAsync(sendAnonDistinctId) {
|
|
468
|
+
return (await this.flagsAsync(sendAnonDistinctId ?? true))?.featureFlags;
|
|
469
|
+
}
|
|
470
|
+
onFeatureFlags(cb) {
|
|
471
|
+
return this.on('featureflags', async ()=>{
|
|
472
|
+
const flags = this.getFeatureFlags();
|
|
473
|
+
if (flags) cb(flags);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
onFeatureFlag(key, cb) {
|
|
477
|
+
return this.on('featureflags', async ()=>{
|
|
478
|
+
const flagResponse = this.getFeatureFlag(key);
|
|
479
|
+
if (void 0 !== flagResponse) cb(flagResponse);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
async overrideFeatureFlag(flags) {
|
|
483
|
+
this.wrap(()=>{
|
|
484
|
+
if (null === flags) return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, null);
|
|
485
|
+
return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
captureException(error, additionalProperties) {
|
|
489
|
+
const properties = {
|
|
490
|
+
$exception_level: 'error',
|
|
491
|
+
$exception_list: [
|
|
492
|
+
{
|
|
493
|
+
type: isPlainError(error) ? error.name : 'Error',
|
|
494
|
+
value: isPlainError(error) ? error.message : error,
|
|
495
|
+
mechanism: {
|
|
496
|
+
handled: true,
|
|
497
|
+
synthetic: false
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
],
|
|
501
|
+
...additionalProperties
|
|
502
|
+
};
|
|
503
|
+
properties.$exception_personURL = new URL(`/project/${this.apiKey}/person/${this.getDistinctId()}`, this.host).toString();
|
|
504
|
+
this.capture('$exception', properties);
|
|
505
|
+
}
|
|
506
|
+
captureTraceFeedback(traceId, userFeedback) {
|
|
507
|
+
this.capture('$ai_feedback', {
|
|
508
|
+
$ai_feedback_text: userFeedback,
|
|
509
|
+
$ai_trace_id: String(traceId)
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
captureTraceMetric(traceId, metricName, metricValue) {
|
|
513
|
+
this.capture('$ai_metric', {
|
|
514
|
+
$ai_metric_name: metricName,
|
|
515
|
+
$ai_metric_value: String(metricValue),
|
|
516
|
+
$ai_trace_id: String(traceId)
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
export { PostHogCore };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PostHogCore } from '../posthog-core';
|
|
2
|
+
import type { JsonType, PostHogCoreOptions, PostHogFetchOptions, PostHogFetchResponse, PostHogFlagsResponse } from '../types';
|
|
2
3
|
export interface PostHogCoreTestClientMocks {
|
|
3
4
|
fetch: jest.Mock<Promise<PostHogFetchResponse>, [string, PostHogFetchOptions]>;
|
|
4
5
|
storage: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PostHogCoreTestClient.d.ts","sourceRoot":"","sources":["../../src/testing/PostHogCoreTestClient.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"PostHogCoreTestClient.d.ts","sourceRoot":"","sources":["../../src/testing/PostHogCoreTestClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EACV,QAAQ,EACR,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACrB,MAAM,SAAS,CAAA;AAIhB,MAAM,WAAW,0BAA0B;IACzC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC,EAAE,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAA;IAC9E,OAAO,EAAE;QACP,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,SAAS,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;QAC7C,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC,CAAC,CAAA;KAC/C,CAAA;CACF;AAED,qBAAa,qBAAsB,SAAQ,WAAW;IAIlD,OAAO,CAAC,KAAK;IAHR,iBAAiB,CAAC,EAAE,MAAM,CAAA;gBAGvB,KAAK,EAAE,0BAA0B,EACzC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,kBAAkB;IAQvB,QAAQ,CACb,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAM,EAC5C,gBAAgB,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,EAC7C,eAAe,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM,EAC5D,YAAY,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,GACrC,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC;IAI5C,oBAAoB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC;IAGvC,oBAAoB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI;IAG3D,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAG/E,YAAY,IAAI,MAAM;IAGtB,iBAAiB,IAAI,MAAM;IAG3B,kBAAkB,IAAI,MAAM;CAG7B;AAED,eAAO,MAAM,gBAAgB,GAC3B,QAAQ,MAAM,EACd,UAAU,kBAAkB,EAC5B,aAAa,CAAC,KAAK,EAAE,0BAA0B,KAAK,IAAI,EACxD,eAAc;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAA;CAAO,KACtD,CAAC,qBAAqB,EAAE,0BAA0B,CAsBpD,CAAA"}
|
|
@@ -27,11 +27,14 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
27
27
|
PostHogCoreTestClient: ()=>PostHogCoreTestClient,
|
|
28
28
|
createTestClient: ()=>createTestClient
|
|
29
29
|
});
|
|
30
|
-
const
|
|
30
|
+
const external_posthog_core_js_namespaceObject = require("../posthog-core.js");
|
|
31
31
|
const version = '2.0.0-alpha';
|
|
32
|
-
class PostHogCoreTestClient extends
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
class PostHogCoreTestClient extends external_posthog_core_js_namespaceObject.PostHogCore {
|
|
33
|
+
constructor(mocks, apiKey, options){
|
|
34
|
+
super(apiKey, options), this.mocks = mocks;
|
|
35
|
+
this.setupBootstrap(options);
|
|
36
|
+
}
|
|
37
|
+
getFlags(distinctId, groups = {}, personProperties = {}, groupProperties = {}, extraPayload = {}) {
|
|
35
38
|
return super.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
|
|
36
39
|
}
|
|
37
40
|
getPersistedProperty(key) {
|
|
@@ -52,13 +55,8 @@ class PostHogCoreTestClient extends external_index_js_namespaceObject.PostHogCor
|
|
|
52
55
|
getCustomUserAgent() {
|
|
53
56
|
return 'posthog-core-tests';
|
|
54
57
|
}
|
|
55
|
-
constructor(mocks, apiKey, options){
|
|
56
|
-
super(apiKey, options), this.mocks = mocks;
|
|
57
|
-
this.setupBootstrap(options);
|
|
58
|
-
}
|
|
59
58
|
}
|
|
60
|
-
const createTestClient =
|
|
61
|
-
let storageCache = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : {};
|
|
59
|
+
const createTestClient = (apiKey, options, setupMocks, storageCache = {})=>{
|
|
62
60
|
const mocks = {
|
|
63
61
|
fetch: jest.fn(),
|
|
64
62
|
storage: {
|
|
@@ -75,7 +73,7 @@ const createTestClient = function(apiKey, options, setupMocks) {
|
|
|
75
73
|
status: 'ok'
|
|
76
74
|
})
|
|
77
75
|
}));
|
|
78
|
-
|
|
76
|
+
setupMocks?.(mocks);
|
|
79
77
|
return [
|
|
80
78
|
new PostHogCoreTestClient(mocks, apiKey, {
|
|
81
79
|
disableCompression: true,
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { PostHogCore } from "../
|
|
1
|
+
import { PostHogCore } from "../posthog-core.mjs";
|
|
2
2
|
const version = '2.0.0-alpha';
|
|
3
3
|
class PostHogCoreTestClient extends PostHogCore {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
constructor(mocks, apiKey, options){
|
|
5
|
+
super(apiKey, options), this.mocks = mocks;
|
|
6
|
+
this.setupBootstrap(options);
|
|
7
|
+
}
|
|
8
|
+
getFlags(distinctId, groups = {}, personProperties = {}, groupProperties = {}, extraPayload = {}) {
|
|
6
9
|
return super.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
|
|
7
10
|
}
|
|
8
11
|
getPersistedProperty(key) {
|
|
@@ -23,13 +26,8 @@ class PostHogCoreTestClient extends PostHogCore {
|
|
|
23
26
|
getCustomUserAgent() {
|
|
24
27
|
return 'posthog-core-tests';
|
|
25
28
|
}
|
|
26
|
-
constructor(mocks, apiKey, options){
|
|
27
|
-
super(apiKey, options), this.mocks = mocks;
|
|
28
|
-
this.setupBootstrap(options);
|
|
29
|
-
}
|
|
30
29
|
}
|
|
31
|
-
const createTestClient =
|
|
32
|
-
let storageCache = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : {};
|
|
30
|
+
const createTestClient = (apiKey, options, setupMocks, storageCache = {})=>{
|
|
33
31
|
const mocks = {
|
|
34
32
|
fetch: jest.fn(),
|
|
35
33
|
storage: {
|
|
@@ -46,7 +44,7 @@ const createTestClient = function(apiKey, options, setupMocks) {
|
|
|
46
44
|
status: 'ok'
|
|
47
45
|
})
|
|
48
46
|
}));
|
|
49
|
-
|
|
47
|
+
setupMocks?.(mocks);
|
|
50
48
|
return [
|
|
51
49
|
new PostHogCoreTestClient(mocks, apiKey, {
|
|
52
50
|
disableCompression: true,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { Logger } from '../types';
|
|
1
2
|
export declare const wait: (t: number) => Promise<void>;
|
|
2
3
|
export declare const waitForPromises: () => Promise<void>;
|
|
3
4
|
export declare const parseBody: (mockCall: any) => any;
|
|
4
5
|
export declare const createImperativePromise: <T>() => [Promise<T>, (value: T) => void];
|
|
5
6
|
export declare const delay: (ms: number) => Promise<void>;
|
|
7
|
+
export declare const createMockLogger: () => Logger;
|
|
6
8
|
//# sourceMappingURL=test-utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-utils.d.ts","sourceRoot":"","sources":["../../src/testing/test-utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,IAAI,GAAU,GAAG,MAAM,KAAG,OAAO,CAAC,IAAI,CAElD,CAAA;AAED,eAAO,MAAM,eAAe,QAAa,OAAO,CAAC,IAAI,CAQpD,CAAA;AAED,eAAO,MAAM,SAAS,GAAI,UAAU,GAAG,KAAG,GAIzC,CAAA;AAED,eAAO,MAAM,uBAAuB,GAAI,CAAC,OAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAM5E,CAAA;AAED,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,IAAI,CAI9C,CAAA"}
|
|
1
|
+
{"version":3,"file":"test-utils.d.ts","sourceRoot":"","sources":["../../src/testing/test-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAEhC,eAAO,MAAM,IAAI,GAAU,GAAG,MAAM,KAAG,OAAO,CAAC,IAAI,CAElD,CAAA;AAED,eAAO,MAAM,eAAe,QAAa,OAAO,CAAC,IAAI,CAQpD,CAAA;AAED,eAAO,MAAM,SAAS,GAAI,UAAU,GAAG,KAAG,GAIzC,CAAA;AAED,eAAO,MAAM,uBAAuB,GAAI,CAAC,OAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAM5E,CAAA;AAED,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,IAAI,CAI9C,CAAA;AAED,eAAO,MAAM,gBAAgB,QAAO,MAUnC,CAAA"}
|
|
@@ -25,6 +25,7 @@ var __webpack_exports__ = {};
|
|
|
25
25
|
__webpack_require__.r(__webpack_exports__);
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
27
|
createImperativePromise: ()=>createImperativePromise,
|
|
28
|
+
createMockLogger: ()=>createMockLogger,
|
|
28
29
|
delay: ()=>delay,
|
|
29
30
|
parseBody: ()=>parseBody,
|
|
30
31
|
wait: ()=>wait,
|
|
@@ -52,19 +53,30 @@ const createImperativePromise = ()=>{
|
|
|
52
53
|
});
|
|
53
54
|
return [
|
|
54
55
|
promise,
|
|
55
|
-
(val)=>
|
|
56
|
+
(val)=>resolve?.(val)
|
|
56
57
|
];
|
|
57
58
|
};
|
|
58
59
|
const delay = (ms)=>new Promise((resolve)=>{
|
|
59
60
|
setTimeout(resolve, ms);
|
|
60
61
|
});
|
|
62
|
+
const createMockLogger = ()=>({
|
|
63
|
+
_log: jest.fn(),
|
|
64
|
+
info: jest.fn(),
|
|
65
|
+
warn: jest.fn(),
|
|
66
|
+
error: jest.fn(),
|
|
67
|
+
critical: jest.fn(),
|
|
68
|
+
uninitializedWarning: jest.fn(),
|
|
69
|
+
createLogger: createMockLogger
|
|
70
|
+
});
|
|
61
71
|
exports.createImperativePromise = __webpack_exports__.createImperativePromise;
|
|
72
|
+
exports.createMockLogger = __webpack_exports__.createMockLogger;
|
|
62
73
|
exports.delay = __webpack_exports__.delay;
|
|
63
74
|
exports.parseBody = __webpack_exports__.parseBody;
|
|
64
75
|
exports.wait = __webpack_exports__.wait;
|
|
65
76
|
exports.waitForPromises = __webpack_exports__.waitForPromises;
|
|
66
77
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
67
78
|
"createImperativePromise",
|
|
79
|
+
"createMockLogger",
|
|
68
80
|
"delay",
|
|
69
81
|
"parseBody",
|
|
70
82
|
"wait",
|