posthog-node 3.1.2 → 3.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.
@@ -117,9 +117,19 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
117
117
  /***
118
118
  * PROPERTIES
119
119
  ***/
120
+ setPersonPropertiesForFlags(properties: {
121
+ [type: string]: string;
122
+ }): this;
123
+ resetPersonPropertiesForFlags(): void;
124
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
120
125
  personProperties(properties: {
121
126
  [type: string]: string;
122
127
  }): this;
128
+ setGroupPropertiesForFlags(properties: {
129
+ [type: string]: Record<string, string>;
130
+ }): this;
131
+ resetGroupPropertiesForFlags(): void;
132
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
123
133
  groupProperties(properties: {
124
134
  [type: string]: Record<string, string>;
125
135
  }): this;
@@ -31,7 +31,9 @@ export declare enum PostHogPersistedProperty {
31
31
  SessionId = "session_id",
32
32
  SessionLastTimestamp = "session_timestamp",
33
33
  PersonProperties = "person_properties",
34
- GroupProperties = "group_properties"
34
+ GroupProperties = "group_properties",
35
+ InstalledAppBuild = "installed_app_build",
36
+ InstalledAppVersion = "installed_app_version"
35
37
  }
36
38
  export declare type PostHogFetchOptions = {
37
39
  method: 'GET' | 'POST' | 'PUT' | 'PATCH';
@@ -1,2 +1,12 @@
1
+ /**
2
+ * Fetch wrapper
3
+ *
4
+ * We want to polyfill fetch when not available with axios but use it when it is.
5
+ * NOTE: The current version of Axios has an issue when in non-node environments like Clouflare Workers.
6
+ * This is currently solved by using the global fetch if available instead.
7
+ * See https://github.com/PostHog/posthog-js-lite/issues/127 for more info
8
+ */
1
9
  import { PostHogFetchOptions, PostHogFetchResponse } from 'posthog-core/src';
2
- export declare const fetch: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
10
+ declare type FetchLike = (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
11
+ declare const _default: FetchLike;
12
+ export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "3.1.2",
3
+ "version": "3.2.0",
4
4
  "description": "PostHog Node.js integration",
5
5
  "repository": "PostHog/posthog-node",
6
6
  "scripts": {
@@ -19,7 +19,7 @@
19
19
  "module": "lib/index.esm.js",
20
20
  "types": "lib/index.d.ts",
21
21
  "dependencies": {
22
- "axios": "^0.27.0",
22
+ "axios": "^1.6.2",
23
23
  "rusha": "^0.8.14"
24
24
  },
25
25
  "devDependencies": {
@@ -3,7 +3,7 @@ import { FeatureFlagCondition, FlagProperty, PostHogFeatureFlag, PropertyGroup }
3
3
  import { version } from '../package.json'
4
4
  import { JsonType, PostHogFetchOptions, PostHogFetchResponse } from 'posthog-core/src'
5
5
  import { safeSetTimeout } from 'posthog-core/src/utils'
6
- import { fetch } from './fetch'
6
+ import fetch from './fetch'
7
7
 
8
8
  // eslint-disable-next-line
9
9
  const LONG_SCALE = 0xfffffffffffffff
@@ -424,8 +424,6 @@ class FeatureFlagsPoller {
424
424
 
425
425
  try {
426
426
  return await this.fetch(url, options)
427
- } catch (err) {
428
- throw err
429
427
  } finally {
430
428
  clearTimeout(abortTimeout)
431
429
  }
@@ -487,13 +485,14 @@ function matchProperty(
487
485
  case 'lte':
488
486
  return typeof overrideValue == typeof value && overrideValue <= value
489
487
  case 'is_date_after':
490
- case 'is_date_before':
488
+ case 'is_date_before': {
491
489
  const parsedDate = convertToDateTime(value)
492
490
  const overrideDate = convertToDateTime(overrideValue)
493
491
  if (operator === 'is_date_before') {
494
492
  return overrideDate < parsedDate
495
493
  }
496
494
  return overrideDate > parsedDate
495
+ }
497
496
  default:
498
497
  console.error(`Unknown operator: ${operator}`)
499
498
  return false
package/src/fetch.ts CHANGED
@@ -1,22 +1,43 @@
1
- import axios from 'axios'
1
+ /**
2
+ * Fetch wrapper
3
+ *
4
+ * We want to polyfill fetch when not available with axios but use it when it is.
5
+ * NOTE: The current version of Axios has an issue when in non-node environments like Clouflare Workers.
6
+ * This is currently solved by using the global fetch if available instead.
7
+ * See https://github.com/PostHog/posthog-js-lite/issues/127 for more info
8
+ */
9
+
2
10
  import { PostHogFetchOptions, PostHogFetchResponse } from 'posthog-core/src'
3
11
 
4
- // NOTE: We use axios as a reliable, well supported request library but follow the Fetch API (roughly)
5
- // So that alternative implementations can be used if desired
6
- export const fetch = async (url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> => {
7
- const res = await axios.request({
8
- url,
9
- headers: options.headers,
10
- method: options.method.toLowerCase(),
11
- data: options.body,
12
- signal: options.signal,
13
- // fetch only throws on network errors, not on HTTP errors
14
- validateStatus: () => true,
15
- })
12
+ type FetchLike = (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>
13
+
14
+ let _fetch: FetchLike | undefined =
15
+ // eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
16
+ // @ts-ignore
17
+ typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined
18
+
19
+ if (!_fetch) {
20
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
21
+ const axios = require('axios')
16
22
 
17
- return {
18
- status: res.status,
19
- text: async () => res.data,
20
- json: async () => res.data,
23
+ _fetch = async (url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> => {
24
+ const res = await axios.request({
25
+ url,
26
+ headers: options.headers,
27
+ method: options.method.toLowerCase(),
28
+ data: options.body,
29
+ signal: options.signal,
30
+ // fetch only throws on network errors, not on HTTP errors
31
+ validateStatus: () => true,
32
+ })
33
+
34
+ return {
35
+ status: res.status,
36
+ text: async () => res.data,
37
+ json: async () => res.data,
38
+ }
21
39
  }
22
40
  }
41
+
42
+ // NOTE: We have to export this as default, even though we prefer named exports as we are relying on detecting "fetch" in the global scope
43
+ export default _fetch as FetchLike
@@ -12,7 +12,7 @@ import {
12
12
  import { PostHogMemoryStorage } from '../../posthog-core/src/storage-memory'
13
13
  import { EventMessageV1, GroupIdentifyMessage, IdentifyMessageV1, PostHogNodeV1 } from './types'
14
14
  import { FeatureFlagsPoller } from './feature-flags'
15
- import { fetch } from './fetch'
15
+ import fetch from './fetch'
16
16
 
17
17
  export type PostHogOptions = PosthogCoreOptions & {
18
18
  persistence?: 'memory'
@@ -3,9 +3,9 @@
3
3
  import { PostHog as PostHog, PostHogOptions } from '../src/posthog-node'
4
4
  import { matchProperty, InconclusiveMatchError } from '../src/feature-flags'
5
5
  jest.mock('../src/fetch')
6
- import { fetch } from '../src/fetch'
6
+ import fetch from '../src/fetch'
7
7
 
8
- jest.spyOn(global.console, 'debug').mockImplementation()
8
+ jest.spyOn(console, 'debug').mockImplementation()
9
9
 
10
10
  const mockedFetch = jest.mocked(fetch, true)
11
11
 
@@ -1,7 +1,7 @@
1
1
  // import { PostHog } from '../'
2
2
  import { PostHog as PostHog } from '../src/posthog-node'
3
3
  jest.mock('../src/fetch')
4
- import { fetch } from '../src/fetch'
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
7
 
@@ -295,7 +295,7 @@ describe('PostHog Node.js', () => {
295
295
  flushAt: 1,
296
296
  })
297
297
 
298
- const logSpy = jest.spyOn(global.console, 'log').mockImplementation(() => {})
298
+ const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
299
299
  jest.useRealTimers()
300
300
  // using debug mode to check console.log output
301
301
  // which tells us when the flush is complete