mirror of
https://github.com/xtr-dev/payload-feature-flags.git
synced 2025-12-08 00:13:23 +00:00
- Removed custom endpoint handler and endpoints directory - Removed enableApi configuration option from plugin - Updated client hooks to use Payload's native collection API - Updated documentation to reflect API changes - Updated view component to handle Payload API response format The plugin now uses Payload CMS's built-in REST API for the feature-flags collection, which provides standard query syntax, pagination, and automatic access control enforcement. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
4.1 KiB
4.1 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
This is a Payload CMS v3 plugin for feature flags (@xtr-dev/payload-feature-flags). The plugin enables feature toggles, A/B testing, and gradual rollouts through a dedicated collection and server-side hooks.
Key Commands
Development
pnpm dev- Start Next.js dev server with Payload CMS admin panelpnpm dev:payload- Run Payload CLI commands for developmentpnpm dev:generate-types- Generate TypeScript types from Payload configpnpm dev:generate-importmap- Generate import map for admin panel
Building
pnpm build- Full production build (copies files, builds types, compiles with SWC)pnpm build:types- Generate TypeScript declarations onlypnpm build:swc- Compile TypeScript to JavaScript using SWCpnpm clean- Remove dist directory and build artifactspnpm copyfiles- Copy non-TS assets to dist
Testing & Quality
pnpm test- Run all tests (integration + e2e)pnpm test:int- Run integration tests with Vitestpnpm test:e2e- Run end-to-end tests with Playwrightpnpm lint- Run ESLintpnpm lint:fix- Run ESLint with auto-fix
Publishing
pnpm prepublishOnly- Clean and build before publishing (runs automatically)
Architecture
Plugin Structure
The plugin follows Payload's plugin architecture with multiple exports:
- Main export (
src/index.ts): Core plugin configuration function - RSC export (
src/exports/rsc.ts): Server-side hooks for React Server Components - Client export (
src/exports/client.ts): Client-side components (currently placeholder)
Core Components
Plugin Configuration (src/index.ts)
PayloadFeatureFlagsConfig: Main configuration typepayloadFeatureFlags(): Plugin factory function that creates a Payload collection and optional API endpoints- Collection overrides support for full customization
- Feature toggles for rollouts, variants, and API endpoints
Server Hooks (src/hooks/server.ts)
getFeatureFlag(): Fetch individual flag dataisFeatureEnabled(): Simple boolean checkgetAllFeatureFlags(): Get all active flagsisUserInRollout(): Check percentage-based rollouts with consistent hashinggetUserVariant(): A/B testing variant selectiongetFeatureFlagsByTag(): Query flags by tags
API Access
- The plugin uses Payload's native REST API for the feature-flags collection
- Standard Payload query syntax is supported
- Collection access controls are enforced
Collection Schema
The plugin creates a feature flags collection with these key fields:
name(unique): Flag identifierenabled: On/off togglerolloutPercentage: 0-100% rollout controlvariants: Array for A/B testing with weightstags: Organization and filteringmetadata: Additional JSON data
Development Environment
- Uses MongoDB Memory Server for testing
- Next.js for admin panel development
- SWC for fast compilation
- Vitest for integration testing
- Playwright for E2E testing
Export Strategy
The plugin publishes with different entry points:
- Development: Points to TypeScript sources (
src/) - Production: Points to compiled JavaScript (
dist/) - Supports both CommonJS and ESM through package.json exports
Important Notes
Plugin Integration
The plugin integrates with Payload by:
- Creating a feature flags collection with configurable slug and fields
- Adding optional REST API endpoints
- Providing server-side hooks that work with any collection slug
- Supporting full collection customization through
collectionOverrides
Security Considerations
- Server-side hooks are the preferred method for accessing feature flags
- Collection access can be restricted through
collectionOverrides.access - API access follows standard Payload authentication and authorization
Testing Setup
The development configuration (dev/payload.config.ts) includes:
- MongoDB Memory Server for isolated testing
- Test collections (posts, media)
- Example plugin configuration with collection overrides
- Seeding functionality for development data