feat(flags): Add getFeatureFlagResult method for efficient flag + payload retrieval#2920
feat(flags): Add getFeatureFlagResult method for efficient flag + payload retrieval#2920
getFeatureFlagResult method for efficient flag + payload retrieval#2920Conversation
Add a new method that returns flag evaluation results including value and parsed payload in a single call. This is more efficient than calling `getFeatureFlag` and `getFeatureFlagPayload` separately when both are needed, as it avoids duplicate evaluation and eliminates double JSON serialization. The method: - Returns a new `FeatureFlagResult` type with `key`, `enabled`, `variant`, and `payload` (already parsed as JsonType) - Checks for flag overrides first - Tries local evaluation when personal API key is configured - Falls back to remote evaluation - Captures $feature_flag_called event (respects sendFeatureFlagEvents option) Key improvements over initial implementation: - Renamed from `getFeatureFlagDetails` to `getFeatureFlagResult` - `FeatureFlagResult` has parsed payload directly (not stringified) - Eliminates double serialization: local eval no longer stringifies payloads only to have them parsed back by `getFeatureFlagPayload` - Simpler public API: no `reason` or `metadata` fields exposed (internal metadata still tracked for event capture)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR adds a new getFeatureFlagResult method to the PostHog Node.js SDK that efficiently retrieves both a feature flag's value and its payload in a single call, addressing performance issues with the previous approach of making separate calls.
Changes:
- Added
FeatureFlagResulttype andgetFeatureFlagResultmethod to retrieve flag value and payload in one operation - Refactored
getFeatureFlagandgetFeatureFlagPayloadto use a new internal_getFeatureFlagResultmethod, consolidating evaluation logic - Added comprehensive tests covering remote evaluation, local evaluation, overrides, and event tracking scenarios
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/node/src/types.ts | Adds FeatureFlagResult type definition and interface declaration for the new method |
| packages/node/src/client.ts | Implements _getFeatureFlagResult private method and refactors existing methods to use it, adds public getFeatureFlagResult method |
| packages/node/src/tests/feature-flags.flags.spec.ts | Adds comprehensive test suite for getFeatureFlagResult covering various scenarios |
| packages/node/src/tests/posthog-node.spec.ts | Updates test assertion from toEqual to toMatchObject for better compatibility |
| packages/node/example.mjs | Adds demonstration script showing usage of the new method |
| packages/node/.env.example | Adds environment configuration example for the demo script |
| .changeset/wise-candies-clap.md | Documents the change for release notes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Size Change: +3.65 kB (+0.06%) Total Size: 5.79 MB
ℹ️ View Unchanged
|
When a flag is overridden to undefined, return undefined from getFeatureFlagResult (and getFeatureFlag) to properly simulate "flag doesn't exist" behavior, rather than returning enabled: true. Add tests for both getFeatureFlagResult and getFeatureFlag to verify this behavior.
7e89004 to
a1a4998
Compare
dustinbyrne
left a comment
There was a problem hiding this comment.
Looks good! The greptile comment regarding payload-only overrides may be worth considering. I left one other comment below, I'll leave it up to you.
When a user sets only a payload override (no flag override), getFeatureFlagResult was skipping the override and returning the evaluated payload instead. This was inconsistent with getFeatureFlagPayload which correctly handles payload-only overrides. The fix applies payload overrides after evaluation, ensuring both methods behave consistently.
…tions Move sendFeatureFlagEvents from a separate parameter into the options object. This simplifies callers which no longer need to destructure the option out just to pass it separately.
When the API returns a payload that cannot be parsed as JSON, return the raw string instead of undefined. This matches the existing parsePayload behavior used elsewhere in the codebase.
…ation Verify that getFeatureFlag includes $feature_flag_id and $feature_flag_reason properties in the $feature_flag_called event when flags are evaluated locally. This documents the improved behavior where local evaluations now include metadata that was previously only available for remote evaluations.
Problem
The
posthog-nodeSDK doesn't have a method that returns both the flag value AND payload in a single call. We have one inposthog-python. We should do the same here.Why this matters
Currently, if you need both a flag's value and its payload, you must make two separate calls:
This has several problems:
/flagsrequestsflagValuetogetFeatureFlagPayloadto avoid yet another evaluation, which is unintuitiveWith the new
getFeatureFlagResultmethod, users can get everything in one call:Changes
FeatureFlagResulttype withkey,enabled,variant, andpayloadfieldsgetFeatureFlagResultmethod to the PostHog client that:/decide$feature_flag_calledevent (respectssendFeatureFlagEventsoption)Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file