mirror of
https://github.com/xtr-dev/payload-feature-flags.git
synced 2025-12-10 02:43:25 +00:00
Bump version to 0.0.6
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-feature-flags",
|
"name": "@xtr-dev/payload-feature-flags",
|
||||||
"version": "0.0.5",
|
"version": "0.0.6",
|
||||||
"description": "Feature flags plugin for Payload CMS - manage feature toggles, A/B tests, and gradual rollouts",
|
"description": "Feature flags plugin for Payload CMS - manage feature toggles, A/B tests, and gradual rollouts",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Payload } from 'payload'
|
import { Payload } from 'payload'
|
||||||
|
import { cache } from "react"
|
||||||
|
|
||||||
export interface FeatureFlag {
|
export interface FeatureFlag {
|
||||||
name: string
|
name: string
|
||||||
@@ -33,7 +34,7 @@ function getCollectionSlug(payload: Payload): string {
|
|||||||
/**
|
/**
|
||||||
* Get a specific feature flag by name (for use in React Server Components)
|
* Get a specific feature flag by name (for use in React Server Components)
|
||||||
*/
|
*/
|
||||||
export async function getFeatureFlag(flagName: string, payload?: Payload): Promise<FeatureFlag | null> {
|
export const getFeatureFlag = cache(async (flagName: string, payload?: Payload): Promise<FeatureFlag | null> => {
|
||||||
try {
|
try {
|
||||||
// If no payload provided, return null as these hooks should be used within Payload context
|
// If no payload provided, return null as these hooks should be used within Payload context
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
@@ -70,20 +71,20 @@ export async function getFeatureFlag(flagName: string, payload?: Payload): Promi
|
|||||||
console.error(`Failed to fetch feature flag ${flagName}:`, error)
|
console.error(`Failed to fetch feature flag ${flagName}:`, error)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a feature flag is enabled (for use in React Server Components)
|
* Check if a feature flag is enabled (for use in React Server Components)
|
||||||
*/
|
*/
|
||||||
export async function isFeatureEnabled(flagName: string, payload?: Payload): Promise<boolean> {
|
export const isFeatureEnabled = cache(async (flagName: string, payload?: Payload): Promise<boolean> => {
|
||||||
const flag = await getFeatureFlag(flagName, payload)
|
const flag = await getFeatureFlag(flagName, payload)
|
||||||
return flag?.enabled ?? false
|
return flag?.enabled ?? false
|
||||||
}
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all active feature flags (for use in React Server Components)
|
* Get all active feature flags (for use in React Server Components)
|
||||||
*/
|
*/
|
||||||
export async function getAllFeatureFlags(payload?: Payload): Promise<Record<string, FeatureFlag>> {
|
export const getAllFeatureFlags = cache(async (payload?: Payload): Promise<Record<string, FeatureFlag>> => {
|
||||||
try {
|
try {
|
||||||
// If no payload provided, return empty object as these hooks should be used within Payload context
|
// If no payload provided, return empty object as these hooks should be used within Payload context
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
@@ -120,16 +121,16 @@ export async function getAllFeatureFlags(payload?: Payload): Promise<Record<stri
|
|||||||
console.error('Failed to fetch feature flags:', error)
|
console.error('Failed to fetch feature flags:', error)
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a user is in a feature rollout (for use in React Server Components)
|
* Check if a user is in a feature rollout (for use in React Server Components)
|
||||||
*/
|
*/
|
||||||
export async function isUserInRollout(
|
export const isUserInRollout = cache(async (
|
||||||
flagName: string,
|
flagName: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
payload?: Payload
|
payload?: Payload
|
||||||
): Promise<boolean> {
|
): Promise<boolean> => {
|
||||||
const flag = await getFeatureFlag(flagName, payload)
|
const flag = await getFeatureFlag(flagName, payload)
|
||||||
|
|
||||||
if (!flag?.enabled) {
|
if (!flag?.enabled) {
|
||||||
@@ -146,16 +147,16 @@ export async function isUserInRollout(
|
|||||||
}, 0)
|
}, 0)
|
||||||
|
|
||||||
return (Math.abs(hash) % 100) < flag.rolloutPercentage
|
return (Math.abs(hash) % 100) < flag.rolloutPercentage
|
||||||
}
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the variant for a user in an A/B test (for use in React Server Components)
|
* Get the variant for a user in an A/B test (for use in React Server Components)
|
||||||
*/
|
*/
|
||||||
export async function getUserVariant(
|
export const getUserVariant = cache(async (
|
||||||
flagName: string,
|
flagName: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
payload?: Payload
|
payload?: Payload
|
||||||
): Promise<string | null> {
|
): Promise<string | null> => {
|
||||||
const flag = await getFeatureFlag(flagName, payload)
|
const flag = await getFeatureFlag(flagName, payload)
|
||||||
|
|
||||||
if (!flag?.enabled || !flag.variants || flag.variants.length === 0) {
|
if (!flag?.enabled || !flag.variants || flag.variants.length === 0) {
|
||||||
@@ -178,12 +179,12 @@ export async function getUserVariant(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return flag.variants[0]?.name || null
|
return flag.variants[0]?.name || null
|
||||||
}
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get feature flags by tags (for use in React Server Components)
|
* Get feature flags by tags (for use in React Server Components)
|
||||||
*/
|
*/
|
||||||
export async function getFeatureFlagsByTag(tag: string, payload?: Payload): Promise<FeatureFlag[]> {
|
export const getFeatureFlagsByTag = cache(async (tag: string, payload?: Payload): Promise<FeatureFlag[]> => {
|
||||||
try {
|
try {
|
||||||
// If no payload provided, return empty array as these hooks should be used within Payload context
|
// If no payload provided, return empty array as these hooks should be used within Payload context
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
@@ -214,4 +215,4 @@ export async function getFeatureFlagsByTag(tag: string, payload?: Payload): Prom
|
|||||||
console.error(`Failed to fetch feature flags with tag ${tag}:`, error)
|
console.error(`Failed to fetch feature flags with tag ${tag}:`, error)
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user