posthog-node 2.0.0-alpha5 → 2.0.0-alpha8

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.
@@ -62,10 +62,14 @@ export class PostHogGlobal implements PostHogNodeV1 {
62
62
  }
63
63
 
64
64
  private reInit(distinctId: string): void {
65
- // Remove all state except the queue
66
- const queue = this._sharedClient.getPersistedProperty(PostHogPersistedProperty.Queue)
67
- this._sharedClient.reset()
68
- this._sharedClient.setPersistedProperty(PostHogPersistedProperty.Queue, queue)
65
+ // Certain properties we want to persist
66
+ const propertiesToKeep = [PostHogPersistedProperty.Queue, PostHogPersistedProperty.OptedOut]
67
+
68
+ for (const key in PostHogPersistedProperty) {
69
+ if (!propertiesToKeep.includes(key as any)) {
70
+ this._sharedClient.setPersistedProperty((PostHogPersistedProperty as any)[key], null)
71
+ }
72
+ }
69
73
  this._sharedClient.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId)
70
74
  }
71
75
 
@@ -114,7 +118,7 @@ export class PostHogGlobal implements PostHogNodeV1 {
114
118
  defaultResult?: boolean | undefined,
115
119
  groups?: Record<string, string> | undefined
116
120
  ): Promise<boolean> {
117
- const feat = this.getFeatureFlag(key, distinctId, groups)
121
+ const feat = await this.getFeatureFlag(key, distinctId, groups)
118
122
  return !!feat || defaultResult || false
119
123
  }
120
124
 
@@ -133,4 +137,8 @@ export class PostHogGlobal implements PostHogNodeV1 {
133
137
  shutdownAsync(): Promise<void> {
134
138
  return this._sharedClient.shutdownAsync()
135
139
  }
140
+
141
+ debug(enabled?: boolean): void {
142
+ return this._sharedClient.debug(enabled)
143
+ }
136
144
  }
@@ -17,7 +17,7 @@ const getLastBatchEvents = (): any[] | undefined => {
17
17
  return JSON.parse((call[1] as any).body as any).batch
18
18
  }
19
19
 
20
- describe('PostHog Core', () => {
20
+ describe('PostHog Node.js', () => {
21
21
  let posthog: PostHog
22
22
 
23
23
  jest.useFakeTimers()
@@ -37,7 +37,7 @@ describe('PostHog Core', () => {
37
37
  } as any)
38
38
  })
39
39
 
40
- describe('legacy methods', () => {
40
+ describe('core methods', () => {
41
41
  it('should capture an event to shared queue', async () => {
42
42
  expect(mockedUndici.fetch).toHaveBeenCalledTimes(0)
43
43
  posthog.capture({ distinctId: '123', event: 'test-event', properties: { foo: 'bar' }, groups: { org: 123 } })
@@ -89,4 +89,49 @@ describe('PostHog Core', () => {
89
89
  ])
90
90
  })
91
91
  })
92
+
93
+ describe('feature flags', () => {
94
+ beforeEach(() => {
95
+ const mockFeatureFlags = {
96
+ 'feature-1': true,
97
+ 'feature-2': true,
98
+ 'feature-variant': 'variant',
99
+ }
100
+
101
+ mockedUndici.fetch.mockImplementation((url) => {
102
+ if ((url as any).includes('/decide/')) {
103
+ return Promise.resolve({
104
+ status: 200,
105
+ text: () => Promise.resolve('ok'),
106
+ json: () =>
107
+ Promise.resolve({
108
+ featureFlags: mockFeatureFlags,
109
+ }),
110
+ }) as any
111
+ }
112
+
113
+ return Promise.resolve({
114
+ status: 200,
115
+ text: () => Promise.resolve('ok'),
116
+ json: () =>
117
+ Promise.resolve({
118
+ status: 'ok',
119
+ }),
120
+ }) as any
121
+ })
122
+ })
123
+
124
+ it('should do getFeatureFlag', async () => {
125
+ expect(mockedUndici.fetch).toHaveBeenCalledTimes(0)
126
+ await expect(posthog.getFeatureFlag('feature-variant', '123', { org: '123' })).resolves.toEqual('variant')
127
+ expect(mockedUndici.fetch).toHaveBeenCalledTimes(1)
128
+ })
129
+
130
+ it('should do isFeatureEnabled', async () => {
131
+ expect(mockedUndici.fetch).toHaveBeenCalledTimes(0)
132
+ await expect(posthog.isFeatureEnabled('feature-1', '123', false, { org: '123' })).resolves.toEqual(true)
133
+ await expect(posthog.isFeatureEnabled('feature-4', '123', false, { org: '123' })).resolves.toEqual(false)
134
+ expect(mockedUndici.fetch).toHaveBeenCalledTimes(2)
135
+ })
136
+ })
92
137
  })