Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/openfeature-node-provider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@posthog/openfeature-node-provider': minor
---

Initial release of the official PostHog provider for the OpenFeature server SDK, backed by `posthog-node`.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = {
'packages/mcp/**',
'packages/nextjs-config/**',
'packages/nuxt/**',
'packages/openfeature-node-provider/**',
'packages/openfeature-web-provider/**',
'packages/react-native/**',
'packages/node/**',
Expand Down
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [ ] @posthog/next
- [ ] @posthog/nextjs-config
- [ ] @posthog/nuxt
- [ ] @posthog/openfeature-node-provider
- [ ] @posthog/openfeature-web-provider
- [ ] @posthog/rollup-plugin
- [ ] @posthog/webpack-plugin
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ jobs:
- name: '@posthog/next'
- name: '@posthog/nextjs-config'
- name: '@posthog/nuxt'
- name: '@posthog/openfeature-node-provider'
- name: '@posthog/openfeature-web-provider'
- name: '@posthog/plugin-utils'
- name: '@posthog/rollup-plugin'
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The repository contains the following SDK packages in `./packages/`:
| `nuxt/` | `@posthog/nuxt` | Nuxt framework module |
| `next/` | `@posthog/next` | Next.js framework module |
| `nextjs-config/` | `@posthog/nextjs-config` | Next.js configuration helper |
| `openfeature-node-provider/` | `@posthog/openfeature-node-provider` | OpenFeature server provider (posthog-node) |
| `openfeature-web-provider/` | `@posthog/openfeature-web-provider` | OpenFeature web provider (posthog-js) |
| `plugin-utils/` | `@posthog/plugin-utils` | Shared CLI and sourcemap utilities for plugins |
| `types/` | `@posthog/types` | TypeScript type definitions for the SDK |
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ PostHog JS is a mono-repo. The changelogs are in the individual packages
- [@posthog/convex](./packages/convex/CHANGELOG.md)
- [@posthog/next](./packages/next/CHANGELOG.md)
- [@posthog/nextjs-config](./packages/nextjs-config/CHANGELOG.md)
- [@posthog/openfeature-node-provider](./packages/openfeature-node-provider/CHANGELOG.md)
- [@posthog/openfeature-web-provider](./packages/openfeature-web-provider/CHANGELOG.md)
- [@posthog/plugin-utils](./packages/plugin-utils/CHANGELOG.md)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Please see the main [PostHog docs](https://posthog.com/docs).
- [@posthog/convex](./packages/convex/README.md)
- [@posthog/nextjs-config](./packages/nextjs-config/README.md)
- [@posthog/nuxt](./packages/nuxt/README.md)
- [@posthog/openfeature-node-provider](./packages/openfeature-node-provider/README.md)
- [@posthog/openfeature-web-provider](./packages/openfeature-web-provider/README.md)
- [@posthog/plugin-utils](./packages/plugin-utils/README.md)
- [@posthog/rollup-plugin](./packages/rollup-plugin/README.md)
Expand Down
5 changes: 5 additions & 0 deletions examples/example-openfeature-node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
.DS_Store
.env
.env.local
*.log
2 changes: 2 additions & 0 deletions examples/example-openfeature-node/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Minimum age (in days) before a package version can be installed
min-release-age=7
32 changes: 32 additions & 0 deletions examples/example-openfeature-node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# example-openfeature-node

Minimal Node example of the **[`@posthog/openfeature-node-provider`](../../packages/openfeature-node-provider)** — evaluating PostHog feature flags through the standard [OpenFeature](https://openfeature.dev) server SDK, backed by `posthog-node`.

The browser equivalent lives in [`example-openfeature-web`](../example-openfeature-web) (see `@posthog/openfeature-web-provider`).

## What it shows

- Constructing a `posthog-node` client (you own its lifecycle; call `shutdown()` on exit).
- Registering `PostHogServerProvider` with `OpenFeature.setProviderAndWait(...)`.
- Async flag evaluation via the vendor-neutral OpenFeature client
(`getBooleanValue` / `getStringValue` / `getObjectValue`), with the distinct id
supplied per-call through the evaluation context's `targetingKey`.

See [`index.ts`](./index.ts).

## Run it

These examples install workspace packages as tarballs (see [`../README.md`](../README.md)).

1. From the repo root, build the tarballs:
```bash
pnpm package:watch
```
2. In this folder, install and run:
```bash
pnpm install
POSTHOG_PROJECT_API_KEY=<ph_project_api_key> pnpm start
```
Use real flag keys from your project.

> The lockfile is generated on first `pnpm install` per the examples tarball workflow.
44 changes: 44 additions & 0 deletions examples/example-openfeature-node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { OpenFeature } from '@openfeature/server-sdk'
import { PostHogServerProvider } from '@posthog/openfeature-node-provider'
import { PostHog } from 'posthog-node'

// Configure via env vars or edit inline.
const PROJECT_API_KEY = process.env.POSTHOG_PROJECT_API_KEY ?? '<ph_project_api_key>'
const HOST = process.env.POSTHOG_HOST ?? 'https://us.i.posthog.com'

async function main(): Promise<void> {
// 1. You own the posthog-node lifecycle. Pass a personalApiKey to enable
// local evaluation.
const posthog = new PostHog(PROJECT_API_KEY, { host: HOST })

// 2. Register the PostHog provider with OpenFeature.
await OpenFeature.setProviderAndWait(new PostHogServerProvider(posthog))
const client = OpenFeature.getClient()

// 3. Evaluate flags through the vendor-neutral OpenFeature API. The distinct
// id comes from the evaluation context's targetingKey; other attributes
// map to person/group properties. Swap these keys for real flags.
const context = { targetingKey: 'user_distinct_id', plan: 'enterprise' }
// The reads are independent, so evaluate them concurrently.
const [boolean, multivariate, payload] = await Promise.all([
client.getBooleanValue('my-boolean-flag', false, context),
client.getStringValue('my-multivariate-flag', 'control', context),
client.getObjectValue('my-payload-flag', {}, context),
])
const result = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: These three flag reads run sequentially even though they're independent. Promise.all would model the better pattern here since example code tends to get copy-pasted, though at 3 flags in a one-shot demo the actual cost is negligible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c2ad670 — the three independent reads now run via Promise.all.

'my-boolean-flag': boolean,
'my-multivariate-flag': multivariate,
'my-payload-flag': payload,
}

// eslint-disable-next-line no-console
console.log(result)

await posthog.shutdown()
}

main().catch((err) => {
// eslint-disable-next-line no-console
console.error('OpenFeature example failed:', err)
process.exit(1)
})
23 changes: 23 additions & 0 deletions examples/example-openfeature-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "example-openfeature-node",
"version": "1.0.0",
"private": true,
"engines": {
"node": ">=22.22"
},
"packageManager": "pnpm@10.15.0",
"license": "MIT",
"scripts": {
"start": "ts-node index.ts"
},
"dependencies": {
"@openfeature/server-sdk": "^1.22.0",
"@posthog/openfeature-node-provider": "*",
"posthog-node": "*"
},
"devDependencies": {
"@types/node": "20.19.7",
"ts-node": "^10.8.2",
"typescript": "^5.8.2"
}
}
5 changes: 5 additions & 0 deletions examples/example-openfeature-node/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pnpmfile: ../.pnpmfile.cjs
minimumReleaseAge: 10080

blockExoticSubdeps: true
trustPolicy: no-downgrade
14 changes: 14 additions & 0 deletions examples/example-openfeature-node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"moduleResolution": "node",
"lib": ["es2020"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"noEmit": true
},
"include": ["*.ts"]
}
7 changes: 7 additions & 0 deletions packages/openfeature-node-provider/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"printWidth": 120
}
13 changes: 13 additions & 0 deletions packages/openfeature-node-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @posthog/openfeature-node-provider

Official [PostHog](https://posthog.com) provider for the [OpenFeature](https://openfeature.dev) **server**
SDK ([`@openfeature/server-sdk`](https://openfeature.dev/docs/reference/technologies/server/javascript)),
backed by [`posthog-node`](https://posthog.com/docs/libraries/node).

For the browser, use [`@posthog/openfeature-web-provider`](../openfeature-web-provider).

## Documentation

Installation and usage instructions live in the PostHog docs, so they stay in one place and don't drift:

**https://posthog.com/docs/feature-flags/installation/openfeature-js**
12 changes: 12 additions & 0 deletions packages/openfeature-node-provider/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createDefaultPreset } from 'ts-jest'

const tsJestTransformCfg = createDefaultPreset().transform

/** @type {import("jest").Config} **/
export default {
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
transform: {
...tsJestTransformCfg,
},
}
74 changes: 74 additions & 0 deletions packages/openfeature-node-provider/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "@posthog/openfeature-node-provider",
"version": "0.0.0",
"bugs": {
"url": "https://github.com/PostHog/posthog-js/issues"
},
"description": "Official PostHog provider for the OpenFeature server SDK (Node), backed by posthog-node",
"repository": {
"type": "git",
"url": "git+https://github.com/PostHog/posthog-js.git",
"directory": "packages/openfeature-node-provider"
},
"homepage": "https://github.com/PostHog/posthog-js/tree/main/packages/openfeature-node-provider#readme",
"author": {
"name": "PostHog",
"email": "engineering@posthog.com",
"url": "https://posthog.com"
},
"license": "MIT",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"scripts": {
"clean": "rimraf dist",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"build": "rslib build",
"dev": "rslib build --watch",
"test:unit": "jest",
"prepublishOnly": "pnpm lint && pnpm test:unit && pnpm build",
"package": "pnpm pack --out $PACKAGE_DEST/%s.tgz"
},
"engines": {
"node": "^20.20.0 || >=22.22.0"
},
"files": [
"dist/"
],
"keywords": [
"posthog",
"openfeature",
"feature-flags",
"node",
"server"
],
"peerDependencies": {
"@openfeature/core": "^1.11.0",
"@openfeature/server-sdk": "^1.17.0",
"posthog-node": ">=5.21.2"
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: The @openfeature/server-sdk@^1.7.0 lower bound conflicts with the @openfeature/core@^1.11.0 peer declared above. Every server-sdk version from 1.7.0 through 1.16.x requires @openfeature/core below 1.11.0, so no single installed version satisfies both constraints. A consumer on any server-sdk in that range hits an ERESOLVE failure on npm or yarn install; pnpm only warns unless strict-peer-dependencies is set.

server-sdk@1.17.0 is the first version whose own core peer (^1.6.0) overlaps with 1.11.0:

   "peerDependencies": {
     "@openfeature/core": "^1.11.0",
-    "@openfeature/server-sdk": "^1.7.0",
+    "@openfeature/server-sdk": "^1.17.0",
     "posthog-node": ">=5.21.2"
   },

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7fffb7c — bumped the @openfeature/server-sdk peer floor to ^1.17.0, the first version whose own @openfeature/core peer overlaps ^1.11.0.

"devDependencies": {
"@openfeature/core": "^1.11.0",
"@openfeature/server-sdk": "^1.22.0",
"@posthog-tooling/tsconfig-base": "workspace:*",
"@rslib/core": "catalog:",
"@types/jest": "catalog:",
"@types/node": "^22.15.23",
"jest": "catalog:",
"posthog-node": "workspace:*",
"ts-jest": "catalog:"
}
}
18 changes: 18 additions & 0 deletions packages/openfeature-node-provider/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from '@rslib/core'

export default defineConfig({
lib: [
{ format: 'esm', syntax: 'es2023', dts: true, bundle: false },
{ format: 'cjs', syntax: 'es2023', dts: true, bundle: false },
],
output: {
// Server provider: targets the Node.js runtime.
target: 'node',
},
source: {
entry: {
index: ['src/**/*', '!src/__tests__/**/*', '!src/**/*.spec.ts'],
},
tsconfigPath: './tsconfig.build.json',
},
})
Loading
Loading