posthog-node 2.1.0 → 2.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/lib/index.d.ts CHANGED
@@ -6,6 +6,11 @@ declare type PosthogCoreOptions = {
6
6
  enable?: boolean;
7
7
  sendFeatureFlagEvent?: boolean;
8
8
  preloadFeatureFlags?: boolean;
9
+ bootstrap?: {
10
+ distinctId?: string;
11
+ isIdentifiedId?: boolean;
12
+ featureFlags?: Record<string, boolean | string>;
13
+ };
9
14
  fetchRetryCount?: number;
10
15
  fetchRetryDelay?: number;
11
16
  sessionExpirationTimeSeconds?: number;
package/lib/index.esm.js CHANGED
@@ -155,7 +155,7 @@ function __spreadArray(to, from, pack) {
155
155
  return to.concat(ar || Array.prototype.slice.call(from));
156
156
  }
157
157
 
158
- var version = "2.1.0";
158
+ var version = "2.2.0";
159
159
 
160
160
  var PostHogPersistedProperty;
161
161
  (function (PostHogPersistedProperty) {
@@ -752,6 +752,26 @@ var PostHogCore = /** @class */ (function () {
752
752
  }
753
753
  return __assign({ $lib: this.getLibraryId(), $lib_version: this.getLibraryVersion(), $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties);
754
754
  };
755
+ PostHogCore.prototype.setupBootstrap = function (options) {
756
+ var _a, _b, _c, _d;
757
+ if ((_a = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _a === void 0 ? void 0 : _a.distinctId) {
758
+ if ((_b = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _b === void 0 ? void 0 : _b.isIdentifiedId) {
759
+ this.setPersistedProperty(PostHogPersistedProperty.DistinctId, options.bootstrap.distinctId);
760
+ }
761
+ else {
762
+ this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, options.bootstrap.distinctId);
763
+ }
764
+ }
765
+ if ((_c = options === null || options === void 0 ? void 0 : options.bootstrap) === null || _c === void 0 ? void 0 : _c.featureFlags) {
766
+ var activeFlags = Object.keys(((_d = options.bootstrap) === null || _d === void 0 ? void 0 : _d.featureFlags) || {})
767
+ .filter(function (flag) { var _a, _b; return !!((_b = (_a = options.bootstrap) === null || _a === void 0 ? void 0 : _a.featureFlags) === null || _b === void 0 ? void 0 : _b[flag]); })
768
+ .reduce(function (res, key) {
769
+ var _a, _b;
770
+ return ((res[key] = ((_b = (_a = options.bootstrap) === null || _a === void 0 ? void 0 : _a.featureFlags) === null || _b === void 0 ? void 0 : _b[key]) || false), res);
771
+ }, {});
772
+ this.setKnownFeatureFlags(activeFlags);
773
+ }
774
+ };
755
775
  Object.defineProperty(PostHogCore.prototype, "props", {
756
776
  // NOTE: Props are lazy loaded from localstorage hence the complex getter setter logic
757
777
  get: function () {
@@ -995,8 +1015,7 @@ var PostHogCore = /** @class */ (function () {
995
1015
  .then(function (r) { return r.json(); })
996
1016
  .then(function (res) {
997
1017
  if (res.featureFlags) {
998
- _this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, res.featureFlags);
999
- _this._events.emit('featureflags', res.featureFlags);
1018
+ _this.setKnownFeatureFlags(res.featureFlags);
1000
1019
  }
1001
1020
  return res;
1002
1021
  })
@@ -1007,6 +1026,10 @@ var PostHogCore = /** @class */ (function () {
1007
1026
  });
1008
1027
  });
1009
1028
  };
1029
+ PostHogCore.prototype.setKnownFeatureFlags = function (featureFlags) {
1030
+ this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, featureFlags);
1031
+ this._events.emit('featureflags', featureFlags);
1032
+ };
1010
1033
  PostHogCore.prototype.getFeatureFlag = function (key) {
1011
1034
  var featureFlags = this.getFeatureFlags();
1012
1035
  if (!featureFlags) {
@@ -1497,16 +1520,44 @@ function () {
1497
1520
  };
1498
1521
 
1499
1522
  FeatureFlagsPoller.prototype.matchFeatureFlagProperties = function (flag, distinctId, properties) {
1500
- var _this = this;
1523
+ var _a;
1501
1524
 
1502
1525
  var flagFilters = flag.filters || {};
1503
1526
  var flagConditions = flagFilters.groups || [];
1504
1527
  var isInconclusive = false;
1505
- var result = undefined;
1506
- flagConditions.forEach(function (condition) {
1528
+ var result = undefined; // # Stable sort conditions with variant overrides to the top. This ensures that if overrides are present, they are
1529
+ // # evaluated first, and the variant override is applied to the first matching condition.
1530
+
1531
+ var sortedFlagConditions = __spreadArray([], flagConditions, true).sort(function (conditionA, conditionB) {
1532
+ var AHasVariantOverride = !!conditionA.variant;
1533
+ var BHasVariantOverride = !!conditionB.variant;
1534
+
1535
+ if (AHasVariantOverride && BHasVariantOverride) {
1536
+ return 0;
1537
+ } else if (AHasVariantOverride) {
1538
+ return -1;
1539
+ } else if (BHasVariantOverride) {
1540
+ return 1;
1541
+ } else {
1542
+ return 0;
1543
+ }
1544
+ });
1545
+
1546
+ var _loop_1 = function (condition) {
1507
1547
  try {
1508
- if (_this.isConditionMatch(flag, distinctId, condition, properties)) {
1509
- result = _this.getMatchingVariant(flag, distinctId) || true;
1548
+ if (this_1.isConditionMatch(flag, distinctId, condition, properties)) {
1549
+ var variantOverride_1 = condition.variant;
1550
+ var flagVariants = ((_a = flagFilters.multivariate) === null || _a === void 0 ? void 0 : _a.variants) || [];
1551
+
1552
+ if (variantOverride_1 && flagVariants.some(function (variant) {
1553
+ return variant.key === variantOverride_1;
1554
+ })) {
1555
+ result = variantOverride_1;
1556
+ } else {
1557
+ result = this_1.getMatchingVariant(flag, distinctId) || true;
1558
+ }
1559
+
1560
+ return "break";
1510
1561
  }
1511
1562
  } catch (e) {
1512
1563
  if (e instanceof InconclusiveMatchError) {
@@ -1515,7 +1566,17 @@ function () {
1515
1566
  throw e;
1516
1567
  }
1517
1568
  }
1518
- });
1569
+ };
1570
+
1571
+ var this_1 = this;
1572
+
1573
+ for (var _i = 0, sortedFlagConditions_1 = sortedFlagConditions; _i < sortedFlagConditions_1.length; _i++) {
1574
+ var condition = sortedFlagConditions_1[_i];
1575
+
1576
+ var state_1 = _loop_1(condition);
1577
+
1578
+ if (state_1 === "break") break;
1579
+ }
1519
1580
 
1520
1581
  if (result !== undefined) {
1521
1582
  return result;
@@ -1812,6 +1873,17 @@ function matchProperty(property, propertyValues) {
1812
1873
  case 'lte':
1813
1874
  return typeof overrideValue == typeof value && overrideValue <= value;
1814
1875
 
1876
+ case 'is_date_after':
1877
+ case 'is_date_before':
1878
+ var parsedDate = convertToDateTime(value);
1879
+ var overrideDate = convertToDateTime(overrideValue);
1880
+
1881
+ if (operator === 'is_date_before') {
1882
+ return overrideDate < parsedDate;
1883
+ }
1884
+
1885
+ return overrideDate > parsedDate;
1886
+
1815
1887
  default:
1816
1888
  console.error("Unknown operator: ".concat(operator));
1817
1889
  return false;
@@ -1827,6 +1899,22 @@ function isValidRegex(regex) {
1827
1899
  }
1828
1900
  }
1829
1901
 
1902
+ function convertToDateTime(value) {
1903
+ if (value instanceof Date) {
1904
+ return value;
1905
+ } else if (typeof value === 'string' || typeof value === 'number') {
1906
+ var date = new Date(value);
1907
+
1908
+ if (!isNaN(date.valueOf())) {
1909
+ return date;
1910
+ }
1911
+
1912
+ throw new InconclusiveMatchError("".concat(value, " is in an invalid date format"));
1913
+ } else {
1914
+ throw new InconclusiveMatchError("The date provided ".concat(value, " must be a string, number, or date object"));
1915
+ }
1916
+ }
1917
+
1830
1918
  var THIRTY_SECONDS = 30 * 1000;
1831
1919
  var MAX_CACHE_SIZE = 50 * 1000;
1832
1920