mirror of
https://github.com/xtr-dev/payload-feature-flags.git
synced 2025-12-10 10:53:24 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be24beeaa5 | ||
| 05952e3e72 | |||
|
|
e6f56535ca | ||
| eefe9bdaf3 | |||
| 49c10c7091 | |||
|
|
fe3da4fe52 | ||
| e82bf9c6d4 | |||
| 460d627d92 | |||
|
|
14a5acd222 | ||
| d3b8a8446e | |||
| 7dc17bc80a | |||
| b642b653d0 | |||
|
|
1db434e701 | ||
| 7fd6194712 | |||
| 259599ddcc |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "payload-feature-flags",
|
||||
"version": "1.0.0",
|
||||
"version": "0.0.19",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "payload-feature-flags",
|
||||
"version": "1.0.0",
|
||||
"version": "0.0.19",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3.2.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-feature-flags",
|
||||
"version": "0.0.14",
|
||||
"version": "0.0.20",
|
||||
"description": "Feature flags plugin for Payload CMS - manage feature toggles, A/B tests, and gradual rollouts",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
|
||||
@@ -21,23 +21,31 @@ export interface FeatureFlagOptions {
|
||||
|
||||
// Helper to get config from options or defaults
|
||||
function getConfig(options?: FeatureFlagOptions) {
|
||||
// In server-side environments, serverURL must be explicitly provided
|
||||
const serverURL = options?.serverURL ||
|
||||
(typeof window !== 'undefined' ? window.location.origin : undefined)
|
||||
|
||||
if (!serverURL) {
|
||||
console.warn(
|
||||
'FeatureFlags: serverURL must be provided when using hooks in server-side environment. ' +
|
||||
'Falling back to relative URL which may not work correctly.'
|
||||
)
|
||||
// Use relative URL as fallback - will work if API is on same domain
|
||||
return { serverURL: '', apiPath: options?.apiPath || '/api', collectionSlug: options?.collectionSlug || 'feature-flags' }
|
||||
// Check if serverURL is explicitly provided
|
||||
if (options?.serverURL) {
|
||||
return {
|
||||
serverURL: options.serverURL,
|
||||
apiPath: options.apiPath || '/api',
|
||||
collectionSlug: options.collectionSlug || 'feature-flags'
|
||||
}
|
||||
}
|
||||
|
||||
const apiPath = options?.apiPath || '/api'
|
||||
const collectionSlug = options?.collectionSlug || 'feature-flags'
|
||||
// In browser environment, use window.location.origin
|
||||
if (typeof window !== 'undefined') {
|
||||
return {
|
||||
serverURL: window.location.origin,
|
||||
apiPath: options?.apiPath || '/api',
|
||||
collectionSlug: options?.collectionSlug || 'feature-flags'
|
||||
}
|
||||
}
|
||||
|
||||
return { serverURL, apiPath, collectionSlug }
|
||||
// During SSR or in non-browser environments, use relative URL
|
||||
// This will work for same-origin requests
|
||||
return {
|
||||
serverURL: '',
|
||||
apiPath: options?.apiPath || '/api',
|
||||
collectionSlug: options?.collectionSlug || 'feature-flags'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
15
src/index.ts
15
src/index.ts
@@ -34,6 +34,11 @@ export type PayloadFeatureFlagsConfig = {
|
||||
* Override collection configuration
|
||||
*/
|
||||
collectionOverrides?: CollectionOverrides
|
||||
/**
|
||||
* Enable custom list view for feature flags
|
||||
* @default false
|
||||
*/
|
||||
enableCustomListView?: boolean
|
||||
}
|
||||
|
||||
export const payloadFeatureFlags =
|
||||
@@ -44,6 +49,7 @@ export const payloadFeatureFlags =
|
||||
defaultValue = false,
|
||||
enableRollouts = true,
|
||||
enableVariants = true,
|
||||
enableCustomListView = false,
|
||||
collectionOverrides,
|
||||
} = pluginOptions
|
||||
|
||||
@@ -163,6 +169,15 @@ export const payloadFeatureFlags =
|
||||
useAsTitle: 'name',
|
||||
group: 'Configuration',
|
||||
description: 'Manage feature flags for your application',
|
||||
components: enableCustomListView ? {
|
||||
...collectionOverrides?.admin?.components,
|
||||
views: {
|
||||
...collectionOverrides?.admin?.components?.views,
|
||||
list: {
|
||||
Component: '@xtr-dev/payload-feature-flags/views#FeatureFlagsView'
|
||||
}
|
||||
}
|
||||
} : collectionOverrides?.admin?.components || {},
|
||||
...(collectionOverrides?.admin || {}),
|
||||
},
|
||||
fields,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
'use client'
|
||||
import React from 'react'
|
||||
import type { ListViewClientProps } from 'payload'
|
||||
import { useState, useEffect, useCallback, useMemo, memo } from 'react'
|
||||
import {
|
||||
useConfig,
|
||||
@@ -236,7 +238,7 @@ const FeatureFlagsClientComponent = ({
|
||||
color: styles.text,
|
||||
margin: '0 0 0.5rem 0'
|
||||
}}>
|
||||
Feature Flags Dashboard
|
||||
Feature Flags
|
||||
</h1>
|
||||
<p style={{
|
||||
color: styles.textMuted,
|
||||
|
||||
@@ -1,19 +1,13 @@
|
||||
import type { AdminViewServerProps } from 'payload'
|
||||
import { DefaultTemplate } from '@payloadcms/next/templates'
|
||||
import { Gutter } from '@payloadcms/ui'
|
||||
import React from 'react'
|
||||
import type { ListViewServerProps } from 'payload'
|
||||
import FeatureFlagsClient from './FeatureFlagsClient.js'
|
||||
import type { FeatureFlag } from '../types/index.js'
|
||||
|
||||
async function fetchInitialFlags(payload: any, searchParams?: Record<string, any>): Promise<FeatureFlag[]> {
|
||||
async function fetchInitialFlags(payload: any, collectionSlug: string): Promise<FeatureFlag[]> {
|
||||
try {
|
||||
const limit = Math.min(1000, parseInt(searchParams?.limit as string) || 100)
|
||||
const page = Math.max(1, parseInt(searchParams?.page as string) || 1)
|
||||
const collectionSlug = searchParams?.collectionSlug as string || 'feature-flags'
|
||||
|
||||
const result = await payload.find({
|
||||
collection: collectionSlug,
|
||||
limit,
|
||||
page,
|
||||
limit: 1000,
|
||||
sort: 'name',
|
||||
})
|
||||
|
||||
@@ -24,30 +18,12 @@ async function fetchInitialFlags(payload: any, searchParams?: Record<string, any
|
||||
}
|
||||
}
|
||||
|
||||
export default async function FeatureFlagsView({
|
||||
initPageResult,
|
||||
params,
|
||||
searchParams,
|
||||
}: AdminViewServerProps) {
|
||||
const {
|
||||
req: { user },
|
||||
permissions,
|
||||
} = initPageResult
|
||||
export default async function FeatureFlagsView(props: ListViewServerProps) {
|
||||
const { collectionConfig, user, permissions, payload } = props
|
||||
|
||||
// Security check: User must be logged in
|
||||
if (!user) {
|
||||
return (
|
||||
<DefaultTemplate
|
||||
i18n={initPageResult.req.i18n}
|
||||
locale={initPageResult.locale}
|
||||
params={params}
|
||||
payload={initPageResult.req.payload}
|
||||
permissions={initPageResult.permissions}
|
||||
searchParams={searchParams}
|
||||
user={undefined}
|
||||
visibleEntities={initPageResult.visibleEntities}
|
||||
>
|
||||
<Gutter>
|
||||
<div style={{
|
||||
padding: '2rem',
|
||||
textAlign: 'center',
|
||||
@@ -78,27 +54,13 @@ export default async function FeatureFlagsView({
|
||||
Go to Login
|
||||
</a>
|
||||
</div>
|
||||
</Gutter>
|
||||
</DefaultTemplate>
|
||||
)
|
||||
}
|
||||
|
||||
// Security check: User must have permissions to access feature-flags collection
|
||||
const collectionSlug = searchParams?.collectionSlug as string || 'feature-flags'
|
||||
const canReadFeatureFlags = permissions?.collections?.[collectionSlug]?.read
|
||||
// Security check: User must have permissions to access the collection
|
||||
const canReadFeatureFlags = permissions?.collections?.[collectionConfig.slug]?.read
|
||||
if (!canReadFeatureFlags) {
|
||||
return (
|
||||
<DefaultTemplate
|
||||
i18n={initPageResult.req.i18n}
|
||||
locale={initPageResult.locale}
|
||||
params={params}
|
||||
payload={initPageResult.req.payload}
|
||||
permissions={initPageResult.permissions}
|
||||
searchParams={searchParams}
|
||||
user={initPageResult.req.user || undefined}
|
||||
visibleEntities={initPageResult.visibleEntities}
|
||||
>
|
||||
<Gutter>
|
||||
<div style={{
|
||||
padding: '2rem',
|
||||
textAlign: 'center',
|
||||
@@ -115,40 +77,24 @@ export default async function FeatureFlagsView({
|
||||
You don't have permission to access the Feature Flags Dashboard.
|
||||
</p>
|
||||
<p style={{ fontSize: '0.875rem', color: 'var(--theme-warning-600)' }}>
|
||||
Contact your administrator to request access to the feature-flags collection.
|
||||
Contact your administrator to request access to the {collectionConfig.slug} collection.
|
||||
</p>
|
||||
</div>
|
||||
</Gutter>
|
||||
</DefaultTemplate>
|
||||
)
|
||||
}
|
||||
|
||||
// Fetch initial data server-side (only if user has access)
|
||||
const initialFlags = await fetchInitialFlags(initPageResult.req.payload, searchParams)
|
||||
const initialFlags = await fetchInitialFlags(payload, collectionConfig.slug)
|
||||
|
||||
// Check if user can update feature flags (use already defined collection slug)
|
||||
const canUpdateFeatureFlags = permissions?.collections?.[collectionSlug]?.update || false
|
||||
// Check if user can update feature flags
|
||||
const canUpdateFeatureFlags = permissions?.collections?.[collectionConfig.slug]?.update || false
|
||||
|
||||
// Use DefaultTemplate with proper props structure from initPageResult
|
||||
return (
|
||||
<DefaultTemplate
|
||||
i18n={initPageResult.req.i18n}
|
||||
locale={initPageResult.locale}
|
||||
params={params}
|
||||
payload={initPageResult.req.payload}
|
||||
permissions={initPageResult.permissions}
|
||||
searchParams={searchParams}
|
||||
user={initPageResult.req.user || undefined}
|
||||
visibleEntities={initPageResult.visibleEntities}
|
||||
>
|
||||
<Gutter>
|
||||
<FeatureFlagsClient
|
||||
initialFlags={initialFlags}
|
||||
canUpdate={canUpdateFeatureFlags}
|
||||
maxFlags={parseInt(searchParams?.maxFlags as string) || 100}
|
||||
collectionSlug={collectionSlug}
|
||||
maxFlags={100}
|
||||
collectionSlug={collectionConfig.slug}
|
||||
/>
|
||||
</Gutter>
|
||||
</DefaultTemplate>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user