posthog-node 1.1.2 → 1.1.6

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/README.md CHANGED
@@ -22,3 +22,4 @@ This library is largely based on the `analytics-node` package.
22
22
  ## Questions?
23
23
 
24
24
  ### [Join our Slack community.](https://posthog.com/slack)
25
+
package/cli.js CHANGED
File without changes
package/feature-flags.js CHANGED
@@ -5,6 +5,17 @@ const version = require('./package.json').version
5
5
 
6
6
  const LONG_SCALE = 0xfffffffffffffff
7
7
 
8
+ class ClientError extends Error {
9
+ constructor(message, extra) {
10
+ super()
11
+ Error.captureStackTrace(this, this.constructor)
12
+ this.name = 'ClientError'
13
+ this.message = message
14
+ if (extra) {
15
+ this.extra = extra
16
+ }
17
+ }
18
+ }
8
19
 
9
20
  class FeatureFlagsPoller {
10
21
  constructor({ pollingInterval, personalApiKey, projectApiKey, timeout, host, featureFlagCalledCallback }) {
@@ -72,16 +83,24 @@ class FeatureFlagsPoller {
72
83
  }
73
84
  this.poller = setTimeout(() => this._loadFeatureFlags(), this.pollingInterval)
74
85
 
75
- const res = await this._request({ path: 'api/feature_flag', usePersonalApiKey: true })
86
+ try {
87
+ const res = await this._request({ path: 'api/feature_flag', usePersonalApiKey: true })
88
+ if (res && res.status === 401) {
89
+ throw new ClientError(
90
+ `Your personalApiKey is invalid. Are you sure you're not using your Project API key? More information: https://posthog.com/docs/api/overview`
91
+ )
92
+ }
93
+
94
+ this.featureFlags = res.data.results.filter(flag => flag.active)
76
95
 
77
- if (res && res.status === 401) {
78
- throw new Error(
79
- `Your personalApiKey is invalid. Are you sure you're not using your Project API key? More information: https://posthog.com/docs/api/overview`
80
- )
96
+ this.loadedSuccessfullyOnce = true
97
+ } catch (err) {
98
+ // if an error that is not an instance of ClientError is thrown
99
+ // we silently ignore the error when reloading feature flags
100
+ if (err instanceof ClientError) {
101
+ throw err
102
+ }
81
103
  }
82
-
83
- this.featureFlags = res.data.results
84
- this.loadedSuccessfullyOnce = true
85
104
  }
86
105
 
87
106
  // sha1('a.b') should equal '69f6642c9d71b463485b4faf4e989dc3fe77a8c6'
@@ -98,12 +117,14 @@ class FeatureFlagsPoller {
98
117
 
99
118
  /* istanbul ignore next */
100
119
  async _request({ path, method = 'GET', usePersonalApiKey = false, data = {} }) {
120
+ let url = `${this.host}/${path}/`
101
121
  let headers = {
102
122
  'Content-Type': 'application/json',
103
123
  }
104
124
 
105
125
  if (usePersonalApiKey) {
106
126
  headers = { ...headers, Authorization: `Bearer ${this.personalApiKey}` }
127
+ url = url + `?token=${this.projectApiKey}`
107
128
  } else {
108
129
  data = { ...data, token: this.projectApiKey }
109
130
  }
@@ -114,7 +135,7 @@ class FeatureFlagsPoller {
114
135
 
115
136
  const req = {
116
137
  method: method,
117
- url: `${this.host}/${path}/`,
138
+ url: url,
118
139
  headers: headers,
119
140
  data: JSON.stringify(data),
120
141
  }
@@ -123,6 +144,7 @@ class FeatureFlagsPoller {
123
144
  req.timeout = typeof this.timeout === 'string' ? ms(this.timeout) : this.timeout
124
145
  }
125
146
 
147
+
126
148
  let res
127
149
  try {
128
150
  res = await axios(req)
package/index.d.ts CHANGED
@@ -10,14 +10,9 @@ declare module 'posthog-node' {
10
10
  personalApiKey?: string
11
11
  featureFlagsPollingInterval?: number
12
12
  }
13
-
14
- interface CommonParamsInterfacePropertiesProp {
15
- [key: string]: string | number | Array<any | { [key: string]: string | number }>
16
- }
17
-
18
13
  interface IdentifyMessage {
19
14
  distinctId: string
20
- properties?: CommonParamsInterfacePropertiesProp
15
+ properties?: Record<string | number, any>
21
16
  }
22
17
 
23
18
  interface EventMessage extends IdentifyMessage {
package/index.js CHANGED
@@ -68,7 +68,7 @@ class PostHog {
68
68
  }
69
69
 
70
70
  this.featureFlagsPoller = new FeatureFlagsPoller({
71
- featureFlagsPollingInterval:
71
+ pollingInterval:
72
72
  typeof options.featureFlagsPollingInterval === 'number'
73
73
  ? options.featureFlagsPollingInterval
74
74
  : FIVE_MINUTES,
@@ -164,7 +164,7 @@ class PostHog {
164
164
  })
165
165
  delete apiMessage.alias
166
166
  delete apiMessage.distinctId
167
- apiMessage.distinct_id = null
167
+ apiMessage.distinct_id = message.distinctId || message.distinct_id
168
168
 
169
169
  this.enqueue('alias', apiMessage, callback)
170
170
  return this
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "1.1.2",
3
+ "version": "1.1.6",
4
4
  "description": "PostHog Node.js integration",
5
5
  "license": "MIT",
6
6
  "repository": "PostHog/posthog-node",