@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/.turbo/turbo-build.log
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
|
-
$ bun
|
|
2
|
-
$
|
|
3
|
-
$
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
[34mℹ[39m target: [34mesnext[39m
|
|
8
|
-
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
9
|
-
[34mℹ[39m Build start
|
|
10
|
-
[34mℹ[39m [2mdist/[22m[1mposthog.js[22m [2m 7.59 kB[22m [2m│ gzip: 2.32 kB[22m
|
|
11
|
-
[34mℹ[39m [2mdist/[22m[1mdocs/integration-posthog.docblock.js[22m [2m 1.97 kB[22m [2m│ gzip: 0.94 kB[22m
|
|
12
|
-
[34mℹ[39m [2mdist/[22m[1mexample.js[22m [2m 0.99 kB[22m [2m│ gzip: 0.52 kB[22m
|
|
13
|
-
[34mℹ[39m [2mdist/[22m[1mrun.js[22m [2m 0.29 kB[22m [2m│ gzip: 0.22 kB[22m
|
|
14
|
-
[34mℹ[39m [2mdist/[22m[1mindex.js[22m [2m 0.20 kB[22m [2m│ gzip: 0.13 kB[22m
|
|
15
|
-
[34mℹ[39m [2mdist/[22m[1mdocs/index.js[22m [2m 0.04 kB[22m [2m│ gzip: 0.06 kB[22m
|
|
16
|
-
[34mℹ[39m [2mdist/[22mposthog.js.map [2m14.43 kB[22m [2m│ gzip: 4.14 kB[22m
|
|
17
|
-
[34mℹ[39m [2mdist/[22mdocs/integration-posthog.docblock.js.map [2m 2.50 kB[22m [2m│ gzip: 1.13 kB[22m
|
|
18
|
-
[34mℹ[39m [2mdist/[22mexample.js.map [2m 1.46 kB[22m [2m│ gzip: 0.71 kB[22m
|
|
19
|
-
[34mℹ[39m [2mdist/[22mrun.js.map [2m 0.49 kB[22m [2m│ gzip: 0.33 kB[22m
|
|
20
|
-
[34mℹ[39m [2mdist/[22mposthog.d.ts.map [2m 0.33 kB[22m [2m│ gzip: 0.21 kB[22m
|
|
21
|
-
[34mℹ[39m [2mdist/[22mexample.d.ts.map [2m 0.13 kB[22m [2m│ gzip: 0.13 kB[22m
|
|
22
|
-
[34mℹ[39m [2mdist/[22m[32m[1mposthog.d.ts[22m[39m [2m 0.74 kB[22m [2m│ gzip: 0.36 kB[22m
|
|
23
|
-
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.ts[22m[39m [2m 0.26 kB[22m [2m│ gzip: 0.12 kB[22m
|
|
24
|
-
[34mℹ[39m [2mdist/[22m[32m[1mexample.d.ts[22m[39m [2m 0.25 kB[22m [2m│ gzip: 0.17 kB[22m
|
|
25
|
-
[34mℹ[39m [2mdist/[22m[32m[1mdocs/index.d.ts[22m[39m [2m 0.01 kB[22m [2m│ gzip: 0.03 kB[22m
|
|
26
|
-
[34mℹ[39m [2mdist/[22m[32m[1mdocs/integration-posthog.docblock.d.ts[22m[39m [2m 0.01 kB[22m [2m│ gzip: 0.03 kB[22m
|
|
27
|
-
[34mℹ[39m [2mdist/[22m[32m[1mrun.d.ts[22m[39m [2m 0.01 kB[22m [2m│ gzip: 0.03 kB[22m
|
|
28
|
-
[34mℹ[39m 18 files, total: 31.72 kB
|
|
29
|
-
[33m[PLUGIN_TIMINGS] Warning:[0m 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 12ms
|
|
33
7
|
|
|
34
|
-
|
|
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 26ms
|
|
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 32ms
|
|
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,18 @@
|
|
|
1
1
|
# @contractspec/example.integration-posthog
|
|
2
2
|
|
|
3
|
+
## 1.58.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 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`.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [d1f0fd0]
|
|
12
|
+
- Updated dependencies [4355a9e]
|
|
13
|
+
- @contractspec/integration.providers-impls@1.58.0
|
|
14
|
+
- @contractspec/lib.contracts@1.58.0
|
|
15
|
+
|
|
3
16
|
## 1.57.0
|
|
4
17
|
|
|
5
18
|
### 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
|
+
};
|