posthog-node 3.6.0 → 3.6.2
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/CHANGELOG.md +8 -0
- package/README.md +1 -1
- package/lib/index.cjs.js +405 -34
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.esm.js +405 -34
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/utils.d.ts +0 -1
- package/package.json +1 -1
- package/src/feature-flags.ts +5 -9
- package/src/posthog-node.ts +1 -1
- package/test/feature-flags.spec.ts +14 -19
- package/test/posthog-node.spec.ts +9 -9
|
@@ -6,7 +6,6 @@ export interface RetriableOptions {
|
|
|
6
6
|
retryCheck?: (err: any) => boolean;
|
|
7
7
|
}
|
|
8
8
|
export declare function retriable<T>(fn: () => Promise<T>, props?: RetriableOptions): Promise<T>;
|
|
9
|
-
export declare function generateUUID(globalThis?: any): string;
|
|
10
9
|
export declare function currentTimestamp(): number;
|
|
11
10
|
export declare function currentISOTime(): string;
|
|
12
11
|
export declare function safeSetTimeout(fn: () => void, timeout: number): any;
|
package/package.json
CHANGED
package/src/feature-flags.ts
CHANGED
|
@@ -532,13 +532,9 @@ function matchProperty(
|
|
|
532
532
|
}
|
|
533
533
|
}
|
|
534
534
|
case 'is_date_after':
|
|
535
|
-
case 'is_date_before':
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
let parsedDate = null
|
|
539
|
-
if (['is_relative_date_before', 'is_relative_date_after'].includes(operator)) {
|
|
540
|
-
parsedDate = relativeDateParseForFeatureFlagMatching(String(value))
|
|
541
|
-
} else {
|
|
535
|
+
case 'is_date_before': {
|
|
536
|
+
let parsedDate = relativeDateParseForFeatureFlagMatching(String(value))
|
|
537
|
+
if (parsedDate == null) {
|
|
542
538
|
parsedDate = convertToDateTime(value)
|
|
543
539
|
}
|
|
544
540
|
|
|
@@ -546,7 +542,7 @@ function matchProperty(
|
|
|
546
542
|
throw new InconclusiveMatchError(`Invalid date: ${value}`)
|
|
547
543
|
}
|
|
548
544
|
const overrideDate = convertToDateTime(overrideValue)
|
|
549
|
-
if (['is_date_before'
|
|
545
|
+
if (['is_date_before'].includes(operator)) {
|
|
550
546
|
return overrideDate < parsedDate
|
|
551
547
|
}
|
|
552
548
|
return overrideDate > parsedDate
|
|
@@ -691,7 +687,7 @@ function convertToDateTime(value: string | number | (string | number)[] | Date):
|
|
|
691
687
|
}
|
|
692
688
|
|
|
693
689
|
function relativeDateParseForFeatureFlagMatching(value: string): Date | null {
|
|
694
|
-
const regex =
|
|
690
|
+
const regex = /^-?(?<number>[0-9]+)(?<interval>[a-z])$/
|
|
695
691
|
const match = value.match(regex)
|
|
696
692
|
const parsedDt = new Date(new Date().toISOString())
|
|
697
693
|
|
package/src/posthog-node.ts
CHANGED
|
@@ -460,7 +460,7 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
460
460
|
personProperties?: Record<string, string>,
|
|
461
461
|
groupProperties?: Record<string, Record<string, string>>
|
|
462
462
|
): { allPersonProperties: Record<string, string>; allGroupProperties: Record<string, Record<string, string>> } {
|
|
463
|
-
const allPersonProperties = {
|
|
463
|
+
const allPersonProperties = { distinct_id: distinctId, ...(personProperties || {}) }
|
|
464
464
|
|
|
465
465
|
const allGroupProperties: Record<string, Record<string, string>> = {}
|
|
466
466
|
if (groups) {
|
|
@@ -354,7 +354,7 @@ describe('local evaluation', () => {
|
|
|
354
354
|
distinct_id: 'some-distinct-id_outside_rollout?',
|
|
355
355
|
groups: {},
|
|
356
356
|
person_properties: {
|
|
357
|
-
|
|
357
|
+
distinct_id: 'some-distinct-id_outside_rollout?',
|
|
358
358
|
region: 'USA',
|
|
359
359
|
email: 'a@b.com',
|
|
360
360
|
},
|
|
@@ -376,7 +376,7 @@ describe('local evaluation', () => {
|
|
|
376
376
|
token: 'TEST_API_KEY',
|
|
377
377
|
distinct_id: 'some-distinct-id',
|
|
378
378
|
groups: {},
|
|
379
|
-
person_properties: {
|
|
379
|
+
person_properties: { distinct_id: 'some-distinct-id', doesnt_matter: '1' },
|
|
380
380
|
group_properties: {},
|
|
381
381
|
geoip_disable: true,
|
|
382
382
|
}),
|
|
@@ -2054,7 +2054,7 @@ describe('match properties', () => {
|
|
|
2054
2054
|
it('with relative date operators', () => {
|
|
2055
2055
|
jest.setSystemTime(new Date('2022-05-01'))
|
|
2056
2056
|
|
|
2057
|
-
const property_a = { key: 'key', value: '6h', operator: '
|
|
2057
|
+
const property_a = { key: 'key', value: '-6h', operator: 'is_date_before' }
|
|
2058
2058
|
expect(matchProperty(property_a, { key: '2022-03-01' })).toBe(true)
|
|
2059
2059
|
expect(matchProperty(property_a, { key: '2022-04-30' })).toBe(true)
|
|
2060
2060
|
|
|
@@ -2073,58 +2073,53 @@ describe('match properties', () => {
|
|
|
2073
2073
|
// however js understands numbers as date offsets from utc epoch
|
|
2074
2074
|
expect(() => matchProperty(property_a, { key: 1 })).not.toThrow(InconclusiveMatchError)
|
|
2075
2075
|
|
|
2076
|
-
const property_b = { key: 'key', value: '1h', operator: '
|
|
2076
|
+
const property_b = { key: 'key', value: '1h', operator: 'is_date_after' }
|
|
2077
2077
|
expect(matchProperty(property_b, { key: '2022-05-02' })).toBe(true)
|
|
2078
2078
|
expect(matchProperty(property_b, { key: '2022-05-30' })).toBe(true)
|
|
2079
2079
|
expect(matchProperty(property_b, { key: new Date(2022, 4, 30) })).toBe(true)
|
|
2080
2080
|
expect(matchProperty(property_b, { key: new Date('2022-05-30') })).toBe(true)
|
|
2081
2081
|
expect(matchProperty(property_b, { key: '2022-04-30' })).toBe(false)
|
|
2082
2082
|
|
|
2083
|
-
// # Invalid flag property
|
|
2084
|
-
const property_c = { key: 'key', value: 1234, operator: 'is_relative_date_after' }
|
|
2085
|
-
expect(() => matchProperty(property_c, { key: '2022-05-30' })).toThrow(InconclusiveMatchError)
|
|
2086
|
-
expect(() => matchProperty(property_c, { key: 1 })).toThrow(InconclusiveMatchError)
|
|
2087
|
-
|
|
2088
2083
|
// # Try all possible relative dates
|
|
2089
|
-
const property_e = { key: 'key', value: '1h', operator: '
|
|
2084
|
+
const property_e = { key: 'key', value: '1h', operator: 'is_date_before' }
|
|
2090
2085
|
expect(matchProperty(property_e, { key: '2022-05-01 00:00:00' })).toBe(false)
|
|
2091
2086
|
expect(matchProperty(property_e, { key: '2022-04-30 22:00:00' })).toBe(true)
|
|
2092
2087
|
|
|
2093
|
-
const property_f = { key: 'key', value: '1d', operator: '
|
|
2088
|
+
const property_f = { key: 'key', value: '-1d', operator: 'is_date_before' }
|
|
2094
2089
|
expect(matchProperty(property_f, { key: '2022-04-29 23:59:00 GMT' })).toBe(true)
|
|
2095
2090
|
expect(matchProperty(property_f, { key: '2022-04-30 00:00:01 GMT' })).toBe(false)
|
|
2096
2091
|
|
|
2097
|
-
const property_g = { key: 'key', value: '1w', operator: '
|
|
2092
|
+
const property_g = { key: 'key', value: '1w', operator: 'is_date_before' }
|
|
2098
2093
|
expect(matchProperty(property_g, { key: '2022-04-23 00:00:00 GMT' })).toBe(true)
|
|
2099
2094
|
expect(matchProperty(property_g, { key: '2022-04-24 00:00:00 GMT' })).toBe(false)
|
|
2100
2095
|
expect(matchProperty(property_g, { key: '2022-04-24 00:00:01 GMT' })).toBe(false)
|
|
2101
2096
|
|
|
2102
|
-
const property_h = { key: 'key', value: '1m', operator: '
|
|
2097
|
+
const property_h = { key: 'key', value: '1m', operator: 'is_date_before' }
|
|
2103
2098
|
expect(matchProperty(property_h, { key: '2022-03-01 00:00:00 GMT' })).toBe(true)
|
|
2104
2099
|
expect(matchProperty(property_h, { key: '2022-04-05 00:00:00 GMT' })).toBe(false)
|
|
2105
2100
|
|
|
2106
|
-
const property_i = { key: 'key', value: '1y', operator: '
|
|
2101
|
+
const property_i = { key: 'key', value: '-1y', operator: 'is_date_before' }
|
|
2107
2102
|
expect(matchProperty(property_i, { key: '2021-04-28 00:00:00 GMT' })).toBe(true)
|
|
2108
2103
|
expect(matchProperty(property_i, { key: '2021-05-01 00:00:01 GMT' })).toBe(false)
|
|
2109
2104
|
|
|
2110
|
-
const property_j = { key: 'key', value: '122h', operator: '
|
|
2105
|
+
const property_j = { key: 'key', value: '122h', operator: 'is_date_after' }
|
|
2111
2106
|
expect(matchProperty(property_j, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
|
|
2112
2107
|
expect(matchProperty(property_j, { key: '2022-04-23 01:00:00 GMT' })).toBe(false)
|
|
2113
2108
|
|
|
2114
|
-
const property_k = { key: 'key', value: '2d', operator: '
|
|
2109
|
+
const property_k = { key: 'key', value: '2d', operator: 'is_date_after' }
|
|
2115
2110
|
expect(matchProperty(property_k, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
|
|
2116
2111
|
expect(matchProperty(property_k, { key: '2022-04-29 00:00:01 GMT' })).toBe(true)
|
|
2117
2112
|
expect(matchProperty(property_k, { key: '2022-04-29 00:00:00 GMT' })).toBe(false)
|
|
2118
2113
|
|
|
2119
|
-
const property_l = { key: 'key', value: '02w', operator: '
|
|
2114
|
+
const property_l = { key: 'key', value: '02w', operator: 'is_date_after' }
|
|
2120
2115
|
expect(matchProperty(property_l, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
|
|
2121
2116
|
expect(matchProperty(property_l, { key: '2022-04-16 00:00:00 GMT' })).toBe(false)
|
|
2122
2117
|
|
|
2123
|
-
const property_m = { key: 'key', value: '1m', operator: '
|
|
2118
|
+
const property_m = { key: 'key', value: '-1m', operator: 'is_date_after' }
|
|
2124
2119
|
expect(matchProperty(property_m, { key: '2022-04-01 00:00:01 GMT' })).toBe(true)
|
|
2125
2120
|
expect(matchProperty(property_m, { key: '2022-04-01 00:00:00 GMT' })).toBe(false)
|
|
2126
2121
|
|
|
2127
|
-
const property_n = { key: 'key', value: '1y', operator: '
|
|
2122
|
+
const property_n = { key: 'key', value: '1y', operator: 'is_date_after' }
|
|
2128
2123
|
expect(matchProperty(property_n, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
|
|
2129
2124
|
expect(matchProperty(property_n, { key: '2021-05-01 00:00:01 GMT' })).toBe(true)
|
|
2130
2125
|
expect(matchProperty(property_n, { key: '2021-05-01 00:00:00 GMT' })).toBe(false)
|
|
@@ -4,7 +4,7 @@ jest.mock('../src/fetch')
|
|
|
4
4
|
import fetch from '../src/fetch'
|
|
5
5
|
import { anyDecideCall, anyLocalEvalCall, apiImplementation } from './feature-flags.spec'
|
|
6
6
|
import { waitForPromises, wait } from '../../posthog-core/test/test-utils/test-utils'
|
|
7
|
-
import {
|
|
7
|
+
import { randomUUID } from 'crypto'
|
|
8
8
|
|
|
9
9
|
jest.mock('../package.json', () => ({ version: '1.2.3' }))
|
|
10
10
|
|
|
@@ -194,7 +194,7 @@ describe('PostHog Node.js', () => {
|
|
|
194
194
|
|
|
195
195
|
it('should allow overriding uuid', async () => {
|
|
196
196
|
expect(mockedFetch).toHaveBeenCalledTimes(0)
|
|
197
|
-
const uuid =
|
|
197
|
+
const uuid = randomUUID()
|
|
198
198
|
posthog.capture({ event: 'custom-time', distinctId: '123', uuid })
|
|
199
199
|
await waitForPromises()
|
|
200
200
|
jest.runOnlyPendingTimers()
|
|
@@ -1035,7 +1035,7 @@ describe('PostHog Node.js', () => {
|
|
|
1035
1035
|
distinct_id: 'some_id',
|
|
1036
1036
|
groups: { company: 'id:5', instance: 'app.posthog.com' },
|
|
1037
1037
|
person_properties: {
|
|
1038
|
-
|
|
1038
|
+
distinct_id: 'some_id',
|
|
1039
1039
|
x1: 'y1',
|
|
1040
1040
|
},
|
|
1041
1041
|
group_properties: {
|
|
@@ -1051,7 +1051,7 @@ describe('PostHog Node.js', () => {
|
|
|
1051
1051
|
|
|
1052
1052
|
await posthog.getFeatureFlag('random_key', 'some_id', {
|
|
1053
1053
|
groups: { company: 'id:5', instance: 'app.posthog.com' },
|
|
1054
|
-
personProperties: {
|
|
1054
|
+
personProperties: { distinct_id: 'override' },
|
|
1055
1055
|
groupProperties: { company: { $group_key: 'group_override' } },
|
|
1056
1056
|
})
|
|
1057
1057
|
jest.runOnlyPendingTimers()
|
|
@@ -1064,7 +1064,7 @@ describe('PostHog Node.js', () => {
|
|
|
1064
1064
|
distinct_id: 'some_id',
|
|
1065
1065
|
groups: { company: 'id:5', instance: 'app.posthog.com' },
|
|
1066
1066
|
person_properties: {
|
|
1067
|
-
|
|
1067
|
+
distinct_id: 'override',
|
|
1068
1068
|
},
|
|
1069
1069
|
group_properties: {
|
|
1070
1070
|
company: { $group_key: 'group_override' },
|
|
@@ -1094,7 +1094,7 @@ describe('PostHog Node.js', () => {
|
|
|
1094
1094
|
distinct_id: 'some_id',
|
|
1095
1095
|
groups: {},
|
|
1096
1096
|
person_properties: {
|
|
1097
|
-
|
|
1097
|
+
distinct_id: 'some_id',
|
|
1098
1098
|
},
|
|
1099
1099
|
group_properties: {},
|
|
1100
1100
|
geoip_disable: true,
|
|
@@ -1118,7 +1118,7 @@ describe('PostHog Node.js', () => {
|
|
|
1118
1118
|
distinct_id: 'some_id',
|
|
1119
1119
|
groups: { company: 'id:5' },
|
|
1120
1120
|
person_properties: {
|
|
1121
|
-
|
|
1121
|
+
distinct_id: 'some_id',
|
|
1122
1122
|
},
|
|
1123
1123
|
group_properties: { company: { $group_key: 'id:5' } },
|
|
1124
1124
|
geoip_disable: true,
|
|
@@ -1138,7 +1138,7 @@ describe('PostHog Node.js', () => {
|
|
|
1138
1138
|
distinct_id: 'some_id',
|
|
1139
1139
|
groups: {},
|
|
1140
1140
|
person_properties: {
|
|
1141
|
-
|
|
1141
|
+
distinct_id: 'some_id',
|
|
1142
1142
|
},
|
|
1143
1143
|
group_properties: {},
|
|
1144
1144
|
geoip_disable: true,
|
|
@@ -1159,7 +1159,7 @@ describe('PostHog Node.js', () => {
|
|
|
1159
1159
|
distinct_id: 'some_id',
|
|
1160
1160
|
groups: {},
|
|
1161
1161
|
person_properties: {
|
|
1162
|
-
|
|
1162
|
+
distinct_id: 'some_id',
|
|
1163
1163
|
},
|
|
1164
1164
|
group_properties: {},
|
|
1165
1165
|
geoip_disable: true,
|