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