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 +1 -0
- package/cli.js +0 -0
- package/feature-flags.js +31 -9
- package/index.d.ts +1 -6
- package/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
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
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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:
|
|
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?:
|
|
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
|
-
|
|
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 =
|
|
167
|
+
apiMessage.distinct_id = message.distinctId || message.distinct_id
|
|
168
168
|
|
|
169
169
|
this.enqueue('alias', apiMessage, callback)
|
|
170
170
|
return this
|