feat: Add feature flags with PostHog integration (ESM)#2160
Open
trillium wants to merge 7 commits into
Open
Conversation
Rebase of PR hackforla#2096 onto current development branch, with all code converted from CommonJS to ESM to match the codebase's module system. Backend changes: - Add FeatureFlagsController with custom header validation - Add FeatureFlagsService wrapping posthog-node client - Add featureFlags router with addCookieIfAvailable middleware - Add addCookieIfAvailable to auth middleware (optional JWT decode) - Register featureFlags route in app.js - Add posthog-node dependency - Bump Dockerfile.api to node:22 - Add controller test using vitest (converted from jest) - Add services/index.js barrel export Client changes: - Add FeatureFlagProvider context with useFeatureFlags hook - Add featureFlagApiService for fetching flags - Wrap App with FeatureFlagProvider - Initialize posthog-js in index.jsx - Add posthog.identify in authContext fetchAuth - Add posthog-js dependency Other: - Add .env.test to .gitignore Original PR: hackforla#2096
- Expand controller tests: header validation, distinctId fallback, response body - Add service tests: PostHog SDK wrapper, error propagation - Add middleware tests: addCookieIfAvailable JWT handling - Add router integration tests: route wiring, middleware chain - Add client tests: API service fetch params, context provider lifecycle Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
Cut tests that verify JavaScript operators, Express internals, or duplicate assertions already covered by other tests. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
Previous lockfile had parse errors causing Docker build failures in CI. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
- Backend unit: replace --testPathIgnorePatterns (Jest) with test:unit script using vitest --exclude - Backend integration: replace 'backend/routers/' path filter (wrong cwd inside Docker) with test:integration script using relative paths - Client: remove USER node from client-development Dockerfile stage to fix EACCES when Vite writes temp config files in mounted volume Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
- Mount ./shared:/srv/shared in docker-compose so backend container can resolve '../../shared/authorizationUtils.js' imports - Use vi.hoisted() to set CUSTOM_REQUEST_HEADER before controller module loads, fixing false 400 when env var is unset in CI Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Feature Flags with PostHog Integration (ESM)
Rebases and resolves merge conflicts from #2096 by @kkchu791 onto current
development, with full ESM conversion and expanded test coverage.Background
@kkchu791 built out a feature flags system using PostHog in #2096. That PR developed merge conflicts after the backend ESM migration (#2134) landed on
development. This PR carries his work forward — all the feature flag logic is his design, we just resolved the conflicts, converted to ESM, and added tests.What's included
Feature flags system (@kkchu791's work from #2096):
featureFlags.service.js— wraps PostHoggetAllFlags(distinctId)featureFlags.controller.js— validates custom header, resolves user identity, returns flagsfeatureFlags.router.js—GET /api/featureFlagswith optional auth middlewareaddCookieIfAvailablemiddleware — optional JWT parsing for personalized flagsFeatureFlagProvidercontext +useFeatureFlagshookposthog.identify()on auth for user-level flag targetingConflict resolution + ESM conversion:
require()/module.exports→import/export.jsextensions on relative importsTest coverage (new):
All 109 backend tests and 15 client tests passing.
Credit
All feature flag implementation is @kkchu791's work from #2096. This PR resolves the merge conflicts from the ESM migration and adds test coverage.
Closes #2096