@contractspec/example.integration-posthog 1.57.0 → 1.58.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.
- package/.turbo/turbo-build.log +34 -33
- package/.turbo/turbo-prebuild.log +1 -0
- package/CHANGELOG.md +13 -0
- package/dist/browser/docs/index.js +50 -0
- package/dist/browser/docs/integration-posthog.docblock.js +50 -0
- package/dist/browser/example.js +33 -0
- package/dist/browser/index.js +358 -0
- package/dist/browser/posthog.js +276 -0
- package/dist/browser/run.js +280 -0
- package/dist/docs/index.d.ts +2 -1
- package/dist/docs/index.d.ts.map +1 -0
- package/dist/docs/index.js +51 -1
- package/dist/docs/integration-posthog.docblock.d.ts +2 -1
- package/dist/docs/integration-posthog.docblock.d.ts.map +1 -0
- package/dist/docs/integration-posthog.docblock.js +25 -29
- package/dist/example.d.ts +2 -6
- package/dist/example.d.ts.map +1 -1
- package/dist/example.js +32 -43
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +358 -4
- package/dist/node/docs/index.js +50 -0
- package/dist/node/docs/integration-posthog.docblock.js +50 -0
- package/dist/node/example.js +33 -0
- package/dist/node/index.js +358 -0
- package/dist/node/posthog.js +276 -0
- package/dist/node/run.js +280 -0
- package/dist/posthog.d.ts +13 -17
- package/dist/posthog.d.ts.map +1 -1
- package/dist/posthog.js +241 -188
- package/dist/run.d.ts +2 -1
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +277 -8
- package/package.json +69 -26
- package/tsdown.config.js +1 -2
- package/.turbo/turbo-build$colon$bundle.log +0 -33
- package/dist/docs/integration-posthog.docblock.js.map +0 -1
- package/dist/example.js.map +0 -1
- package/dist/posthog.js.map +0 -1
- package/dist/run.js.map +0 -1
- package/tsconfig.tsbuildinfo +0 -1
package/dist/posthog.js
CHANGED
|
@@ -1,224 +1,277 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/posthog.ts
|
|
1
3
|
import { PosthogAnalyticsProvider } from "@contractspec/integration.providers-impls/impls/posthog";
|
|
2
|
-
|
|
3
|
-
//#region src/posthog.ts
|
|
4
4
|
async function runPosthogExampleFromEnv() {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
5
|
+
const mode = resolvePosthogMode();
|
|
6
|
+
const dryRun = resolveBooleanEnv("CONTRACTSPEC_POSTHOG_DRY_RUN", true);
|
|
7
|
+
const allowWrites = resolveBooleanEnv("CONTRACTSPEC_POSTHOG_ALLOW_WRITES", false);
|
|
8
|
+
const output = { mode, dryRun, allowWrites };
|
|
9
|
+
if (dryRun) {
|
|
10
|
+
if (mode === "capture" || mode === "all") {
|
|
11
|
+
output.capture = buildCapturePreview();
|
|
12
|
+
}
|
|
13
|
+
if (mode === "query" || mode === "all") {
|
|
14
|
+
output.query = { query: buildHogQLQuery() };
|
|
15
|
+
}
|
|
16
|
+
if (mode === "request" || mode === "all") {
|
|
17
|
+
output.request = buildRequestPreview();
|
|
18
|
+
}
|
|
19
|
+
if (mode === "read" || mode === "all") {
|
|
20
|
+
output.read = buildReadPreview();
|
|
21
|
+
}
|
|
22
|
+
if (process.env.POSTHOG_MCP_URL) {
|
|
23
|
+
output.mcp = buildMcpPreview();
|
|
24
|
+
}
|
|
25
|
+
return output;
|
|
26
|
+
}
|
|
27
|
+
const provider = createProviderFromEnv();
|
|
28
|
+
if (mode === "capture" || mode === "all") {
|
|
29
|
+
output.capture = await runCapture(provider, allowWrites);
|
|
30
|
+
}
|
|
31
|
+
if (mode === "query" || mode === "all") {
|
|
32
|
+
output.query = await runHogQLQuery(provider);
|
|
33
|
+
}
|
|
34
|
+
if (mode === "request" || mode === "all") {
|
|
35
|
+
output.request = await runApiRequests(provider, allowWrites);
|
|
36
|
+
}
|
|
37
|
+
if (mode === "read" || mode === "all") {
|
|
38
|
+
output.read = await runReadOperations(provider);
|
|
39
|
+
}
|
|
40
|
+
if (process.env.POSTHOG_MCP_URL) {
|
|
41
|
+
output.mcp = await runMcpToolCall(provider);
|
|
42
|
+
}
|
|
43
|
+
return output;
|
|
28
44
|
}
|
|
29
45
|
function resolvePosthogMode() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
const raw = (process.env.CONTRACTSPEC_POSTHOG_MODE ?? "all").toLowerCase();
|
|
47
|
+
if (raw === "capture" || raw === "query" || raw === "request" || raw === "read" || raw === "all") {
|
|
48
|
+
return raw;
|
|
49
|
+
}
|
|
50
|
+
throw new Error(`Unsupported CONTRACTSPEC_POSTHOG_MODE: ${raw}. Use capture, query, request, read, or all.`);
|
|
33
51
|
}
|
|
34
52
|
function createProviderFromEnv() {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
53
|
+
return new PosthogAnalyticsProvider({
|
|
54
|
+
host: process.env.POSTHOG_HOST,
|
|
55
|
+
projectId: process.env.POSTHOG_PROJECT_ID,
|
|
56
|
+
projectApiKey: process.env.POSTHOG_PROJECT_API_KEY,
|
|
57
|
+
personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY,
|
|
58
|
+
mcpUrl: process.env.POSTHOG_MCP_URL,
|
|
59
|
+
requestTimeoutMs: 1e4
|
|
60
|
+
});
|
|
43
61
|
}
|
|
44
62
|
async function runCapture(provider, allowWrites) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
event
|
|
61
|
-
};
|
|
63
|
+
if (!allowWrites) {
|
|
64
|
+
return {
|
|
65
|
+
skipped: true,
|
|
66
|
+
reason: "Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture."
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const event = buildCaptureEvent();
|
|
70
|
+
if (provider.identify) {
|
|
71
|
+
await provider.identify({
|
|
72
|
+
distinctId: event.distinctId,
|
|
73
|
+
properties: { plan: "pro", source: "contractspec-example" }
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
await provider.capture(event);
|
|
77
|
+
return { captured: true, event };
|
|
62
78
|
}
|
|
63
79
|
async function runHogQLQuery(provider) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
80
|
+
if (!provider.queryHogQL) {
|
|
81
|
+
throw new Error("Analytics provider does not support HogQL queries.");
|
|
82
|
+
}
|
|
83
|
+
const query = buildHogQLQuery();
|
|
84
|
+
return provider.queryHogQL({ query });
|
|
67
85
|
}
|
|
68
86
|
async function runApiRequests(provider, allowWrites) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
87
|
+
const projectId = requireEnv("POSTHOG_PROJECT_ID");
|
|
88
|
+
const listRequest = {
|
|
89
|
+
method: "GET",
|
|
90
|
+
path: `/api/projects/${projectId}/feature_flags/`,
|
|
91
|
+
query: { limit: 5 }
|
|
92
|
+
};
|
|
93
|
+
const listResponse = await provider.request(listRequest);
|
|
94
|
+
const output = {
|
|
95
|
+
list: listResponse.data
|
|
96
|
+
};
|
|
97
|
+
if (!allowWrites) {
|
|
98
|
+
output.writeGuard = "Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to create/delete feature flags.";
|
|
99
|
+
return output;
|
|
100
|
+
}
|
|
101
|
+
const flagKey = `contractspec-example-${Date.now()}`;
|
|
102
|
+
const createResponse = await provider.request({
|
|
103
|
+
method: "POST",
|
|
104
|
+
path: `/api/projects/${projectId}/feature_flags/`,
|
|
105
|
+
body: {
|
|
106
|
+
name: "ContractSpec Example Flag",
|
|
107
|
+
key: flagKey,
|
|
108
|
+
active: true,
|
|
109
|
+
filters: {
|
|
110
|
+
groups: [
|
|
111
|
+
{
|
|
112
|
+
properties: [],
|
|
113
|
+
rollout_percentage: 100
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
output.created = createResponse.data;
|
|
120
|
+
const createdId = extractId(createResponse);
|
|
121
|
+
if (createdId) {
|
|
122
|
+
const deleteResponse = await provider.request({
|
|
123
|
+
method: "DELETE",
|
|
124
|
+
path: `/api/projects/${projectId}/feature_flags/${createdId}`
|
|
125
|
+
});
|
|
126
|
+
output.deleted = deleteResponse.status;
|
|
127
|
+
} else {
|
|
128
|
+
output.deleted = "Skipped delete: response did not include an id.";
|
|
129
|
+
}
|
|
130
|
+
return output;
|
|
102
131
|
}
|
|
103
132
|
async function runReadOperations(provider) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
133
|
+
const now = new Date;
|
|
134
|
+
const from = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
135
|
+
const dateRange = { from, to: now };
|
|
136
|
+
const output = {
|
|
137
|
+
window: { from: from.toISOString(), to: now.toISOString() }
|
|
138
|
+
};
|
|
139
|
+
if (provider.getEvents) {
|
|
140
|
+
output.events = await provider.getEvents({ dateRange, limit: 5 });
|
|
141
|
+
} else {
|
|
142
|
+
output.events = "Provider does not support getEvents.";
|
|
143
|
+
}
|
|
144
|
+
if (provider.getPersons) {
|
|
145
|
+
output.persons = await provider.getPersons({ limit: 5 });
|
|
146
|
+
} else {
|
|
147
|
+
output.persons = "Provider does not support getPersons.";
|
|
148
|
+
}
|
|
149
|
+
if (provider.getInsights) {
|
|
150
|
+
output.insights = await provider.getInsights({ limit: 5 });
|
|
151
|
+
} else {
|
|
152
|
+
output.insights = "Provider does not support getInsights.";
|
|
153
|
+
}
|
|
154
|
+
if (provider.getFeatureFlags) {
|
|
155
|
+
output.featureFlags = await provider.getFeatureFlags({ limit: 5 });
|
|
156
|
+
} else {
|
|
157
|
+
output.featureFlags = "Provider does not support getFeatureFlags.";
|
|
158
|
+
}
|
|
159
|
+
return output;
|
|
126
160
|
}
|
|
127
161
|
async function runMcpToolCall(provider) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
162
|
+
if (!provider.callMcpTool) {
|
|
163
|
+
throw new Error("Analytics provider does not support MCP tool calls.");
|
|
164
|
+
}
|
|
165
|
+
const name = process.env.POSTHOG_MCP_TOOL_NAME ?? "posthog.query";
|
|
166
|
+
const argumentsValue = parseOptionalJsonEnv("POSTHOG_MCP_TOOL_ARGS", {
|
|
167
|
+
query: buildHogQLQuery()
|
|
168
|
+
});
|
|
169
|
+
return provider.callMcpTool({ name, arguments: argumentsValue });
|
|
135
170
|
}
|
|
136
171
|
function buildCaptureEvent() {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
172
|
+
const distinctId = process.env.POSTHOG_DISTINCT_ID ?? "contractspec-demo";
|
|
173
|
+
const event = process.env.POSTHOG_EVENT_NAME ?? "contractspec.example.event";
|
|
174
|
+
return {
|
|
175
|
+
distinctId,
|
|
176
|
+
event,
|
|
177
|
+
properties: {
|
|
178
|
+
source: "contractspec-example",
|
|
179
|
+
environment: "development"
|
|
180
|
+
},
|
|
181
|
+
timestamp: new Date
|
|
182
|
+
};
|
|
146
183
|
}
|
|
147
184
|
function buildHogQLQuery() {
|
|
148
|
-
|
|
185
|
+
return "select event, count() as count " + "from events " + "where timestamp >= now() - interval 1 day " + "group by event " + "order by count desc " + "limit 5";
|
|
149
186
|
}
|
|
150
187
|
function buildCapturePreview() {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
188
|
+
return {
|
|
189
|
+
event: buildCaptureEvent(),
|
|
190
|
+
hint: "Dry run enabled. Set CONTRACTSPEC_POSTHOG_DRY_RUN=false."
|
|
191
|
+
};
|
|
155
192
|
}
|
|
156
193
|
function buildRequestPreview() {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
194
|
+
const projectId = process.env.POSTHOG_PROJECT_ID ?? "PROJECT_ID";
|
|
195
|
+
return {
|
|
196
|
+
listRequest: {
|
|
197
|
+
method: "GET",
|
|
198
|
+
path: `/api/projects/${projectId}/feature_flags/`,
|
|
199
|
+
query: { limit: 5 }
|
|
200
|
+
},
|
|
201
|
+
writeRequests: {
|
|
202
|
+
create: {
|
|
203
|
+
method: "POST",
|
|
204
|
+
path: `/api/projects/${projectId}/feature_flags/`
|
|
205
|
+
},
|
|
206
|
+
delete: {
|
|
207
|
+
method: "DELETE",
|
|
208
|
+
path: `/api/projects/${projectId}/feature_flags/{id}`
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
};
|
|
175
212
|
}
|
|
176
213
|
function buildReadPreview() {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
214
|
+
return {
|
|
215
|
+
events: {
|
|
216
|
+
dateRange: "last_24_hours",
|
|
217
|
+
limit: 5
|
|
218
|
+
},
|
|
219
|
+
persons: {
|
|
220
|
+
limit: 5
|
|
221
|
+
},
|
|
222
|
+
insights: {
|
|
223
|
+
limit: 5
|
|
224
|
+
},
|
|
225
|
+
featureFlags: {
|
|
226
|
+
limit: 5
|
|
227
|
+
}
|
|
228
|
+
};
|
|
186
229
|
}
|
|
187
230
|
function buildMcpPreview() {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
231
|
+
return {
|
|
232
|
+
url: process.env.POSTHOG_MCP_URL,
|
|
233
|
+
toolName: process.env.POSTHOG_MCP_TOOL_NAME ?? "posthog.query",
|
|
234
|
+
arguments: parseOptionalJsonEnv("POSTHOG_MCP_TOOL_ARGS", {
|
|
235
|
+
query: buildHogQLQuery()
|
|
236
|
+
})
|
|
237
|
+
};
|
|
193
238
|
}
|
|
194
239
|
function extractId(response) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
240
|
+
if (!response || typeof response !== "object")
|
|
241
|
+
return null;
|
|
242
|
+
const data = response.data;
|
|
243
|
+
if (data && (typeof data.id === "string" || typeof data.id === "number")) {
|
|
244
|
+
return data.id;
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
199
247
|
}
|
|
200
248
|
function resolveBooleanEnv(key, defaultValue) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
249
|
+
const value = process.env[key];
|
|
250
|
+
if (value === undefined)
|
|
251
|
+
return defaultValue;
|
|
252
|
+
return value.toLowerCase() === "true";
|
|
204
253
|
}
|
|
205
254
|
function requireEnv(key) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
255
|
+
const value = process.env[key];
|
|
256
|
+
if (!value) {
|
|
257
|
+
throw new Error(`Missing required env var: ${key}`);
|
|
258
|
+
}
|
|
259
|
+
return value;
|
|
209
260
|
}
|
|
210
261
|
function parseOptionalJsonEnv(key, fallback) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
export {
|
|
224
|
-
|
|
262
|
+
const raw = process.env[key];
|
|
263
|
+
if (!raw)
|
|
264
|
+
return fallback;
|
|
265
|
+
try {
|
|
266
|
+
const parsed = JSON.parse(raw);
|
|
267
|
+
if (parsed && typeof parsed === "object")
|
|
268
|
+
return parsed;
|
|
269
|
+
} catch {
|
|
270
|
+
throw new Error(`Invalid JSON in ${key}`);
|
|
271
|
+
}
|
|
272
|
+
return fallback;
|
|
273
|
+
}
|
|
274
|
+
export {
|
|
275
|
+
runPosthogExampleFromEnv,
|
|
276
|
+
resolvePosthogMode
|
|
277
|
+
};
|
package/dist/run.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":""}
|