@contractspec/example.integration-posthog 1.57.0 → 1.59.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.
Files changed (41) hide show
  1. package/.turbo/turbo-build.log +34 -33
  2. package/.turbo/turbo-prebuild.log +1 -0
  3. package/CHANGELOG.md +25 -0
  4. package/dist/browser/docs/index.js +50 -0
  5. package/dist/browser/docs/integration-posthog.docblock.js +50 -0
  6. package/dist/browser/example.js +33 -0
  7. package/dist/browser/index.js +358 -0
  8. package/dist/browser/posthog.js +276 -0
  9. package/dist/browser/run.js +280 -0
  10. package/dist/docs/index.d.ts +2 -1
  11. package/dist/docs/index.d.ts.map +1 -0
  12. package/dist/docs/index.js +51 -1
  13. package/dist/docs/integration-posthog.docblock.d.ts +2 -1
  14. package/dist/docs/integration-posthog.docblock.d.ts.map +1 -0
  15. package/dist/docs/integration-posthog.docblock.js +25 -29
  16. package/dist/example.d.ts +2 -6
  17. package/dist/example.d.ts.map +1 -1
  18. package/dist/example.js +32 -43
  19. package/dist/index.d.ts +4 -3
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +358 -4
  22. package/dist/node/docs/index.js +50 -0
  23. package/dist/node/docs/integration-posthog.docblock.js +50 -0
  24. package/dist/node/example.js +33 -0
  25. package/dist/node/index.js +358 -0
  26. package/dist/node/posthog.js +276 -0
  27. package/dist/node/run.js +280 -0
  28. package/dist/posthog.d.ts +13 -17
  29. package/dist/posthog.d.ts.map +1 -1
  30. package/dist/posthog.js +241 -188
  31. package/dist/run.d.ts +2 -1
  32. package/dist/run.d.ts.map +1 -0
  33. package/dist/run.js +277 -8
  34. package/package.json +69 -26
  35. package/tsdown.config.js +1 -2
  36. package/.turbo/turbo-build$colon$bundle.log +0 -33
  37. package/dist/docs/integration-posthog.docblock.js.map +0 -1
  38. package/dist/example.js.map +0 -1
  39. package/dist/posthog.js.map +0 -1
  40. package/dist/run.js.map +0 -1
  41. package/tsconfig.tsbuildinfo +0 -1
@@ -1,34 +1,35 @@
1
- $ bun build:types && bun build:bundle
2
- $ tsc --noEmit
3
- $ tsdown
4
- ℹ tsdown v0.20.3 powered by rolldown v1.0.0-rc.3
5
- ℹ config file: /home/runner/work/contractspec/contractspec/packages/examples/integration-posthog/tsdown.config.js
6
- ℹ entry: src/example.ts, src/index.ts, src/posthog.ts, src/run.ts, src/docs/index.ts, src/docs/integration-posthog.docblock.ts
7
- ℹ target: esnext
8
- ℹ tsconfig: tsconfig.json
9
- ℹ Build start
10
- ℹ dist/posthog.js  7.59 kB │ gzip: 2.32 kB
11
- ℹ dist/docs/integration-posthog.docblock.js  1.97 kB │ gzip: 0.94 kB
12
- ℹ dist/example.js  0.99 kB │ gzip: 0.52 kB
13
- ℹ dist/run.js  0.29 kB │ gzip: 0.22 kB
14
- ℹ dist/index.js  0.20 kB │ gzip: 0.13 kB
15
- ℹ dist/docs/index.js  0.04 kB │ gzip: 0.06 kB
16
- ℹ dist/posthog.js.map 14.43 kB │ gzip: 4.14 kB
17
- ℹ dist/docs/integration-posthog.docblock.js.map  2.50 kB │ gzip: 1.13 kB
18
- ℹ dist/example.js.map  1.46 kB │ gzip: 0.71 kB
19
- ℹ dist/run.js.map  0.49 kB │ gzip: 0.33 kB
20
- ℹ dist/posthog.d.ts.map  0.33 kB │ gzip: 0.21 kB
21
- ℹ dist/example.d.ts.map  0.13 kB │ gzip: 0.13 kB
22
- ℹ dist/posthog.d.ts  0.74 kB │ gzip: 0.36 kB
23
- ℹ dist/index.d.ts  0.26 kB │ gzip: 0.12 kB
24
- ℹ dist/example.d.ts  0.25 kB │ gzip: 0.17 kB
25
- ℹ dist/docs/index.d.ts  0.01 kB │ gzip: 0.03 kB
26
- ℹ dist/docs/integration-posthog.docblock.d.ts  0.01 kB │ gzip: 0.03 kB
27
- ℹ dist/run.d.ts  0.01 kB │ gzip: 0.03 kB
28
- ℹ 18 files, total: 31.72 kB
29
- [PLUGIN_TIMINGS] Warning: Your build spent significant time in plugins. Here is a breakdown:
30
- - rolldown-plugin-dts:generate (55%)
31
- - tsdown:external (41%)
32
- See https://rolldown.rs/options/checks#plugintimings for more details.
1
+ $ contractspec-bun-build prebuild
2
+ $ bun run prebuild && bun run build:bundle && bun run build:types
3
+ $ contractspec-bun-build prebuild
4
+ $ contractspec-bun-build transpile
5
+ [contractspec-bun-build] transpile target=bun root=src entries=6
6
+ Bundled 6 modules in 30ms
33
7
 
34
- ✔ Build complete in 20615ms
8
+ docs/index.js 1.98 KB (entry point)
9
+ ./index.js 11.20 KB (entry point)
10
+ docs/integration-posthog.docblock.js 1.98 KB (entry point)
11
+ ./example.js 1.00 KB (entry point)
12
+ ./run.js 8.37 KB (entry point)
13
+ ./posthog.js 8.25 KB (entry point)
14
+
15
+ [contractspec-bun-build] transpile target=node root=src entries=6
16
+ Bundled 6 modules in 39ms
17
+
18
+ docs/index.js 1.96 KB (entry point)
19
+ ./index.js 11.18 KB (entry point)
20
+ docs/integration-posthog.docblock.js 1.96 KB (entry point)
21
+ ./example.js 0.99 KB (entry point)
22
+ ./run.js 8.37 KB (entry point)
23
+ ./posthog.js 8.24 KB (entry point)
24
+
25
+ [contractspec-bun-build] transpile target=browser root=src entries=6
26
+ Bundled 6 modules in 28ms
27
+
28
+ docs/index.js 1.96 KB (entry point)
29
+ ./index.js 11.18 KB (entry point)
30
+ docs/integration-posthog.docblock.js 1.96 KB (entry point)
31
+ ./example.js 0.99 KB (entry point)
32
+ ./run.js 8.37 KB (entry point)
33
+ ./posthog.js 8.24 KB (entry point)
34
+
35
+ $ contractspec-bun-build types
@@ -0,0 +1 @@
1
+ $ contractspec-bun-build prebuild
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # @contractspec/example.integration-posthog
2
2
 
3
+ ## 1.59.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 1a0cf44: fix: publishConfig not supported by bun
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [1a0cf44]
12
+ - @contractspec/integration.providers-impls@1.59.0
13
+ - @contractspec/lib.contracts@1.59.0
14
+
15
+ ## 1.58.0
16
+
17
+ ### Minor Changes
18
+
19
+ - d1f0fd0: chore: Migrate non-app package builds from tsdown to shared Bun tooling, add `@contractspec/tool.bun`, and standardize `prebuild`/`build`/`typecheck` with platform-aware exports and `tsc` declaration emission into `dist`.
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies [d1f0fd0]
24
+ - Updated dependencies [4355a9e]
25
+ - @contractspec/integration.providers-impls@1.58.0
26
+ - @contractspec/lib.contracts@1.58.0
27
+
3
28
  ## 1.57.0
4
29
 
5
30
  ### Minor Changes
@@ -0,0 +1,50 @@
1
+ // src/docs/integration-posthog.docblock.ts
2
+ import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
3
+ var blocks = [
4
+ {
5
+ id: "docs.examples.integration-posthog",
6
+ title: "Integration Example — PostHog Analytics",
7
+ summary: "Capture events, run HogQL, and manage feature flags using the PostHog analytics provider.",
8
+ kind: "reference",
9
+ visibility: "public",
10
+ route: "/docs/examples/integration-posthog",
11
+ tags: ["posthog", "analytics", "hogql", "example"],
12
+ body: `## What this example shows
13
+ - Capture product events and identify users.
14
+ - Run HogQL queries through the analytics provider.
15
+ - Perform generic REST API requests (feature flag list/create/delete).
16
+ - Read events, persons, insights, and feature flags using typed reader methods.
17
+ - Optionally call a PostHog MCP tool via JSON-RPC.
18
+
19
+ ## Guardrails
20
+ - Use dry-run while validating credentials.
21
+ - Writes are gated behind CONTRACTSPEC_POSTHOG_ALLOW_WRITES.
22
+ - Keep API keys in secret storage.`
23
+ },
24
+ {
25
+ id: "docs.examples.integration-posthog.usage",
26
+ title: "PostHog Integration Example — Usage",
27
+ summary: "How to run the PostHog example with env-driven settings.",
28
+ kind: "usage",
29
+ visibility: "public",
30
+ route: "/docs/examples/integration-posthog/usage",
31
+ tags: ["posthog", "usage"],
32
+ body: `## Usage
33
+ - Set CONTRACTSPEC_POSTHOG_MODE to capture | query | request | read | all.
34
+ - Set CONTRACTSPEC_POSTHOG_DRY_RUN=true for a safe preview.
35
+ - Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture and create/delete.
36
+
37
+ ## Required env vars
38
+ - POSTHOG_PROJECT_ID for queries and REST requests.
39
+ - POSTHOG_PROJECT_API_KEY for capture/identify.
40
+ - POSTHOG_PERSONAL_API_KEY for HogQL queries and REST requests.
41
+
42
+ ## MCP
43
+ - Set POSTHOG_MCP_URL to call tools/call.
44
+ - Optionally set POSTHOG_MCP_TOOL_NAME and POSTHOG_MCP_TOOL_ARGS (JSON).
45
+
46
+ ## Example
47
+ - Call runPosthogExampleFromEnv() from run.ts to execute the flow.`
48
+ }
49
+ ];
50
+ registerDocBlocks(blocks);
@@ -0,0 +1,50 @@
1
+ // src/docs/integration-posthog.docblock.ts
2
+ import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
3
+ var blocks = [
4
+ {
5
+ id: "docs.examples.integration-posthog",
6
+ title: "Integration Example — PostHog Analytics",
7
+ summary: "Capture events, run HogQL, and manage feature flags using the PostHog analytics provider.",
8
+ kind: "reference",
9
+ visibility: "public",
10
+ route: "/docs/examples/integration-posthog",
11
+ tags: ["posthog", "analytics", "hogql", "example"],
12
+ body: `## What this example shows
13
+ - Capture product events and identify users.
14
+ - Run HogQL queries through the analytics provider.
15
+ - Perform generic REST API requests (feature flag list/create/delete).
16
+ - Read events, persons, insights, and feature flags using typed reader methods.
17
+ - Optionally call a PostHog MCP tool via JSON-RPC.
18
+
19
+ ## Guardrails
20
+ - Use dry-run while validating credentials.
21
+ - Writes are gated behind CONTRACTSPEC_POSTHOG_ALLOW_WRITES.
22
+ - Keep API keys in secret storage.`
23
+ },
24
+ {
25
+ id: "docs.examples.integration-posthog.usage",
26
+ title: "PostHog Integration Example — Usage",
27
+ summary: "How to run the PostHog example with env-driven settings.",
28
+ kind: "usage",
29
+ visibility: "public",
30
+ route: "/docs/examples/integration-posthog/usage",
31
+ tags: ["posthog", "usage"],
32
+ body: `## Usage
33
+ - Set CONTRACTSPEC_POSTHOG_MODE to capture | query | request | read | all.
34
+ - Set CONTRACTSPEC_POSTHOG_DRY_RUN=true for a safe preview.
35
+ - Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture and create/delete.
36
+
37
+ ## Required env vars
38
+ - POSTHOG_PROJECT_ID for queries and REST requests.
39
+ - POSTHOG_PROJECT_API_KEY for capture/identify.
40
+ - POSTHOG_PERSONAL_API_KEY for HogQL queries and REST requests.
41
+
42
+ ## MCP
43
+ - Set POSTHOG_MCP_URL to call tools/call.
44
+ - Optionally set POSTHOG_MCP_TOOL_NAME and POSTHOG_MCP_TOOL_ARGS (JSON).
45
+
46
+ ## Example
47
+ - Call runPosthogExampleFromEnv() from run.ts to execute the flow.`
48
+ }
49
+ ];
50
+ registerDocBlocks(blocks);
@@ -0,0 +1,33 @@
1
+ // src/example.ts
2
+ import { defineExample } from "@contractspec/lib.contracts";
3
+ var example = defineExample({
4
+ meta: {
5
+ key: "integration-posthog",
6
+ version: "1.0.0",
7
+ title: "Integration — PostHog Analytics",
8
+ description: "Capture events, run HogQL, and manage PostHog assets via AnalyticsProvider.",
9
+ kind: "integration",
10
+ visibility: "public",
11
+ stability: "experimental",
12
+ owners: ["@platform.integrations"],
13
+ tags: ["posthog", "analytics", "hogql", "integration"]
14
+ },
15
+ docs: {
16
+ rootDocId: "docs.examples.integration-posthog",
17
+ usageDocId: "docs.examples.integration-posthog.usage"
18
+ },
19
+ entrypoints: {
20
+ packageName: "@contractspec/example.integration-posthog",
21
+ docs: "./docs"
22
+ },
23
+ surfaces: {
24
+ templates: true,
25
+ sandbox: { enabled: true, modes: ["markdown", "specs"] },
26
+ studio: { enabled: true, installable: true },
27
+ mcp: { enabled: true }
28
+ }
29
+ });
30
+ var example_default = example;
31
+ export {
32
+ example_default as default
33
+ };
@@ -0,0 +1,358 @@
1
+ // src/docs/integration-posthog.docblock.ts
2
+ import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
3
+ var blocks = [
4
+ {
5
+ id: "docs.examples.integration-posthog",
6
+ title: "Integration Example — PostHog Analytics",
7
+ summary: "Capture events, run HogQL, and manage feature flags using the PostHog analytics provider.",
8
+ kind: "reference",
9
+ visibility: "public",
10
+ route: "/docs/examples/integration-posthog",
11
+ tags: ["posthog", "analytics", "hogql", "example"],
12
+ body: `## What this example shows
13
+ - Capture product events and identify users.
14
+ - Run HogQL queries through the analytics provider.
15
+ - Perform generic REST API requests (feature flag list/create/delete).
16
+ - Read events, persons, insights, and feature flags using typed reader methods.
17
+ - Optionally call a PostHog MCP tool via JSON-RPC.
18
+
19
+ ## Guardrails
20
+ - Use dry-run while validating credentials.
21
+ - Writes are gated behind CONTRACTSPEC_POSTHOG_ALLOW_WRITES.
22
+ - Keep API keys in secret storage.`
23
+ },
24
+ {
25
+ id: "docs.examples.integration-posthog.usage",
26
+ title: "PostHog Integration Example — Usage",
27
+ summary: "How to run the PostHog example with env-driven settings.",
28
+ kind: "usage",
29
+ visibility: "public",
30
+ route: "/docs/examples/integration-posthog/usage",
31
+ tags: ["posthog", "usage"],
32
+ body: `## Usage
33
+ - Set CONTRACTSPEC_POSTHOG_MODE to capture | query | request | read | all.
34
+ - Set CONTRACTSPEC_POSTHOG_DRY_RUN=true for a safe preview.
35
+ - Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture and create/delete.
36
+
37
+ ## Required env vars
38
+ - POSTHOG_PROJECT_ID for queries and REST requests.
39
+ - POSTHOG_PROJECT_API_KEY for capture/identify.
40
+ - POSTHOG_PERSONAL_API_KEY for HogQL queries and REST requests.
41
+
42
+ ## MCP
43
+ - Set POSTHOG_MCP_URL to call tools/call.
44
+ - Optionally set POSTHOG_MCP_TOOL_NAME and POSTHOG_MCP_TOOL_ARGS (JSON).
45
+
46
+ ## Example
47
+ - Call runPosthogExampleFromEnv() from run.ts to execute the flow.`
48
+ }
49
+ ];
50
+ registerDocBlocks(blocks);
51
+ // src/example.ts
52
+ import { defineExample } from "@contractspec/lib.contracts";
53
+ var example = defineExample({
54
+ meta: {
55
+ key: "integration-posthog",
56
+ version: "1.0.0",
57
+ title: "Integration — PostHog Analytics",
58
+ description: "Capture events, run HogQL, and manage PostHog assets via AnalyticsProvider.",
59
+ kind: "integration",
60
+ visibility: "public",
61
+ stability: "experimental",
62
+ owners: ["@platform.integrations"],
63
+ tags: ["posthog", "analytics", "hogql", "integration"]
64
+ },
65
+ docs: {
66
+ rootDocId: "docs.examples.integration-posthog",
67
+ usageDocId: "docs.examples.integration-posthog.usage"
68
+ },
69
+ entrypoints: {
70
+ packageName: "@contractspec/example.integration-posthog",
71
+ docs: "./docs"
72
+ },
73
+ surfaces: {
74
+ templates: true,
75
+ sandbox: { enabled: true, modes: ["markdown", "specs"] },
76
+ studio: { enabled: true, installable: true },
77
+ mcp: { enabled: true }
78
+ }
79
+ });
80
+ var example_default = example;
81
+
82
+ // src/posthog.ts
83
+ import { PosthogAnalyticsProvider } from "@contractspec/integration.providers-impls/impls/posthog";
84
+ async function runPosthogExampleFromEnv() {
85
+ const mode = resolvePosthogMode();
86
+ const dryRun = resolveBooleanEnv("CONTRACTSPEC_POSTHOG_DRY_RUN", true);
87
+ const allowWrites = resolveBooleanEnv("CONTRACTSPEC_POSTHOG_ALLOW_WRITES", false);
88
+ const output = { mode, dryRun, allowWrites };
89
+ if (dryRun) {
90
+ if (mode === "capture" || mode === "all") {
91
+ output.capture = buildCapturePreview();
92
+ }
93
+ if (mode === "query" || mode === "all") {
94
+ output.query = { query: buildHogQLQuery() };
95
+ }
96
+ if (mode === "request" || mode === "all") {
97
+ output.request = buildRequestPreview();
98
+ }
99
+ if (mode === "read" || mode === "all") {
100
+ output.read = buildReadPreview();
101
+ }
102
+ if (process.env.POSTHOG_MCP_URL) {
103
+ output.mcp = buildMcpPreview();
104
+ }
105
+ return output;
106
+ }
107
+ const provider = createProviderFromEnv();
108
+ if (mode === "capture" || mode === "all") {
109
+ output.capture = await runCapture(provider, allowWrites);
110
+ }
111
+ if (mode === "query" || mode === "all") {
112
+ output.query = await runHogQLQuery(provider);
113
+ }
114
+ if (mode === "request" || mode === "all") {
115
+ output.request = await runApiRequests(provider, allowWrites);
116
+ }
117
+ if (mode === "read" || mode === "all") {
118
+ output.read = await runReadOperations(provider);
119
+ }
120
+ if (process.env.POSTHOG_MCP_URL) {
121
+ output.mcp = await runMcpToolCall(provider);
122
+ }
123
+ return output;
124
+ }
125
+ function resolvePosthogMode() {
126
+ const raw = (process.env.CONTRACTSPEC_POSTHOG_MODE ?? "all").toLowerCase();
127
+ if (raw === "capture" || raw === "query" || raw === "request" || raw === "read" || raw === "all") {
128
+ return raw;
129
+ }
130
+ throw new Error(`Unsupported CONTRACTSPEC_POSTHOG_MODE: ${raw}. Use capture, query, request, read, or all.`);
131
+ }
132
+ function createProviderFromEnv() {
133
+ return new PosthogAnalyticsProvider({
134
+ host: process.env.POSTHOG_HOST,
135
+ projectId: process.env.POSTHOG_PROJECT_ID,
136
+ projectApiKey: process.env.POSTHOG_PROJECT_API_KEY,
137
+ personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY,
138
+ mcpUrl: process.env.POSTHOG_MCP_URL,
139
+ requestTimeoutMs: 1e4
140
+ });
141
+ }
142
+ async function runCapture(provider, allowWrites) {
143
+ if (!allowWrites) {
144
+ return {
145
+ skipped: true,
146
+ reason: "Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture."
147
+ };
148
+ }
149
+ const event = buildCaptureEvent();
150
+ if (provider.identify) {
151
+ await provider.identify({
152
+ distinctId: event.distinctId,
153
+ properties: { plan: "pro", source: "contractspec-example" }
154
+ });
155
+ }
156
+ await provider.capture(event);
157
+ return { captured: true, event };
158
+ }
159
+ async function runHogQLQuery(provider) {
160
+ if (!provider.queryHogQL) {
161
+ throw new Error("Analytics provider does not support HogQL queries.");
162
+ }
163
+ const query = buildHogQLQuery();
164
+ return provider.queryHogQL({ query });
165
+ }
166
+ async function runApiRequests(provider, allowWrites) {
167
+ const projectId = requireEnv("POSTHOG_PROJECT_ID");
168
+ const listRequest = {
169
+ method: "GET",
170
+ path: `/api/projects/${projectId}/feature_flags/`,
171
+ query: { limit: 5 }
172
+ };
173
+ const listResponse = await provider.request(listRequest);
174
+ const output = {
175
+ list: listResponse.data
176
+ };
177
+ if (!allowWrites) {
178
+ output.writeGuard = "Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to create/delete feature flags.";
179
+ return output;
180
+ }
181
+ const flagKey = `contractspec-example-${Date.now()}`;
182
+ const createResponse = await provider.request({
183
+ method: "POST",
184
+ path: `/api/projects/${projectId}/feature_flags/`,
185
+ body: {
186
+ name: "ContractSpec Example Flag",
187
+ key: flagKey,
188
+ active: true,
189
+ filters: {
190
+ groups: [
191
+ {
192
+ properties: [],
193
+ rollout_percentage: 100
194
+ }
195
+ ]
196
+ }
197
+ }
198
+ });
199
+ output.created = createResponse.data;
200
+ const createdId = extractId(createResponse);
201
+ if (createdId) {
202
+ const deleteResponse = await provider.request({
203
+ method: "DELETE",
204
+ path: `/api/projects/${projectId}/feature_flags/${createdId}`
205
+ });
206
+ output.deleted = deleteResponse.status;
207
+ } else {
208
+ output.deleted = "Skipped delete: response did not include an id.";
209
+ }
210
+ return output;
211
+ }
212
+ async function runReadOperations(provider) {
213
+ const now = new Date;
214
+ const from = new Date(now.getTime() - 24 * 60 * 60 * 1000);
215
+ const dateRange = { from, to: now };
216
+ const output = {
217
+ window: { from: from.toISOString(), to: now.toISOString() }
218
+ };
219
+ if (provider.getEvents) {
220
+ output.events = await provider.getEvents({ dateRange, limit: 5 });
221
+ } else {
222
+ output.events = "Provider does not support getEvents.";
223
+ }
224
+ if (provider.getPersons) {
225
+ output.persons = await provider.getPersons({ limit: 5 });
226
+ } else {
227
+ output.persons = "Provider does not support getPersons.";
228
+ }
229
+ if (provider.getInsights) {
230
+ output.insights = await provider.getInsights({ limit: 5 });
231
+ } else {
232
+ output.insights = "Provider does not support getInsights.";
233
+ }
234
+ if (provider.getFeatureFlags) {
235
+ output.featureFlags = await provider.getFeatureFlags({ limit: 5 });
236
+ } else {
237
+ output.featureFlags = "Provider does not support getFeatureFlags.";
238
+ }
239
+ return output;
240
+ }
241
+ async function runMcpToolCall(provider) {
242
+ if (!provider.callMcpTool) {
243
+ throw new Error("Analytics provider does not support MCP tool calls.");
244
+ }
245
+ const name = process.env.POSTHOG_MCP_TOOL_NAME ?? "posthog.query";
246
+ const argumentsValue = parseOptionalJsonEnv("POSTHOG_MCP_TOOL_ARGS", {
247
+ query: buildHogQLQuery()
248
+ });
249
+ return provider.callMcpTool({ name, arguments: argumentsValue });
250
+ }
251
+ function buildCaptureEvent() {
252
+ const distinctId = process.env.POSTHOG_DISTINCT_ID ?? "contractspec-demo";
253
+ const event = process.env.POSTHOG_EVENT_NAME ?? "contractspec.example.event";
254
+ return {
255
+ distinctId,
256
+ event,
257
+ properties: {
258
+ source: "contractspec-example",
259
+ environment: "development"
260
+ },
261
+ timestamp: new Date
262
+ };
263
+ }
264
+ function buildHogQLQuery() {
265
+ return "select event, count() as count " + "from events " + "where timestamp >= now() - interval 1 day " + "group by event " + "order by count desc " + "limit 5";
266
+ }
267
+ function buildCapturePreview() {
268
+ return {
269
+ event: buildCaptureEvent(),
270
+ hint: "Dry run enabled. Set CONTRACTSPEC_POSTHOG_DRY_RUN=false."
271
+ };
272
+ }
273
+ function buildRequestPreview() {
274
+ const projectId = process.env.POSTHOG_PROJECT_ID ?? "PROJECT_ID";
275
+ return {
276
+ listRequest: {
277
+ method: "GET",
278
+ path: `/api/projects/${projectId}/feature_flags/`,
279
+ query: { limit: 5 }
280
+ },
281
+ writeRequests: {
282
+ create: {
283
+ method: "POST",
284
+ path: `/api/projects/${projectId}/feature_flags/`
285
+ },
286
+ delete: {
287
+ method: "DELETE",
288
+ path: `/api/projects/${projectId}/feature_flags/{id}`
289
+ }
290
+ }
291
+ };
292
+ }
293
+ function buildReadPreview() {
294
+ return {
295
+ events: {
296
+ dateRange: "last_24_hours",
297
+ limit: 5
298
+ },
299
+ persons: {
300
+ limit: 5
301
+ },
302
+ insights: {
303
+ limit: 5
304
+ },
305
+ featureFlags: {
306
+ limit: 5
307
+ }
308
+ };
309
+ }
310
+ function buildMcpPreview() {
311
+ return {
312
+ url: process.env.POSTHOG_MCP_URL,
313
+ toolName: process.env.POSTHOG_MCP_TOOL_NAME ?? "posthog.query",
314
+ arguments: parseOptionalJsonEnv("POSTHOG_MCP_TOOL_ARGS", {
315
+ query: buildHogQLQuery()
316
+ })
317
+ };
318
+ }
319
+ function extractId(response) {
320
+ if (!response || typeof response !== "object")
321
+ return null;
322
+ const data = response.data;
323
+ if (data && (typeof data.id === "string" || typeof data.id === "number")) {
324
+ return data.id;
325
+ }
326
+ return null;
327
+ }
328
+ function resolveBooleanEnv(key, defaultValue) {
329
+ const value = process.env[key];
330
+ if (value === undefined)
331
+ return defaultValue;
332
+ return value.toLowerCase() === "true";
333
+ }
334
+ function requireEnv(key) {
335
+ const value = process.env[key];
336
+ if (!value) {
337
+ throw new Error(`Missing required env var: ${key}`);
338
+ }
339
+ return value;
340
+ }
341
+ function parseOptionalJsonEnv(key, fallback) {
342
+ const raw = process.env[key];
343
+ if (!raw)
344
+ return fallback;
345
+ try {
346
+ const parsed = JSON.parse(raw);
347
+ if (parsed && typeof parsed === "object")
348
+ return parsed;
349
+ } catch {
350
+ throw new Error(`Invalid JSON in ${key}`);
351
+ }
352
+ return fallback;
353
+ }
354
+ export {
355
+ runPosthogExampleFromEnv,
356
+ resolvePosthogMode,
357
+ example_default as example
358
+ };