Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Allow setting feature flags using query params (#1844)

Dhruv Bhanushali5995c2a49a1d
diff --git a/src/middleware/middleware.ts b/src/middleware/middleware.ts
index e928f1c61..5aeb47a39 100644
--- a/src/middleware/middleware.ts
+++ b/src/middleware/middleware.ts
@@ -45,5 +45,6 @@ const middleware: Middleware = async ({ app, query, route, $pinia }) => {
const featureFlagStore = useFeatureFlagStore($pinia)
featureFlagStore.initFromCookies(app.$cookies.get('features') ?? {})
+ featureFlagStore.initFromQuery(query)
}
export default middleware
diff --git a/src/stores/feature-flag.ts b/src/stores/feature-flag.ts
index bbf9b999c..0ccc12153 100644
--- a/src/stores/feature-flag.ts
+++ b/src/stores/feature-flag.ts
@@ -16,6 +16,8 @@ import {
} from '~/constants/feature-flag'
import { LOCAL, DEPLOY_ENVS, DeployEnv } from '~/constants/deploy-env'
+import type { Dictionary } from 'vue-router/types/router'
+
type FlagName = keyof typeof featureData['features']
export interface FeatureFlagState {
flags: Record<FlagName, FeatureFlag>
@@ -103,6 +105,9 @@ export const useFeatureFlagStore = defineStore(FEATURE_FLAG, {
* Given a list of key value pairs of flags and their preferred states,
* populate the store state to match the cookie.
*
+ * Values stored in the cookie are stored across sessions and can be
+ * modified using the '/preferences' page.
+ *
* @param cookies - mapping of feature flags and their preferred states
*/
initFromCookies(cookies: Record<string, FeatureState>) {
@@ -111,6 +116,38 @@ export const useFeatureFlagStore = defineStore(FEATURE_FLAG, {
flag.preferredState = cookies[name]
})
},
+ /**
+ * Set the value of flag entries from the query parameters. Only those
+ * query parameters that contain the 'ff_' prefix are considered.
+ *
+ * Values set using query params are per-session, and will not affect the
+ * value stored in the cookie.
+ *
+ * @param query - values for the feature flags
+ */
+ initFromQuery(query: Dictionary<string | (string | null)[]>) {
+ const isValidName = (name: string): name is `ff_${FlagName}` =>
+ name.startsWith('ff_') && name.replace('ff_', '') in this.flags
+ const isValidValue = (
+ value: string | (string | null)[]
+ ): value is FeatureState =>
+ typeof value === 'string' && ['on', 'off'].includes(value)
+ const isValidEntry = (
+ entry: [string, string | (string | null)[]]
+ ): entry is [`ff_${FlagName}`, FeatureState] =>
+ isValidName(entry[0]) && isValidValue(entry[1])
+
+ Object.entries(query)
+ .filter(isValidEntry)
+ .forEach(([name, state]) => {
+ // TODO: type `FlagName` should be inferred by TS
+ const flagName = name.substring(3) as FlagName
+ const flag = this.flags[flagName]
+ if (getFlagStatus(flag) === SWITCHABLE) {
+ flag.preferredState = state
+ }
+ })
+ },
/**
* Toggle the feature flag of the given name to the given preferred state.
*
diff --git a/test/unit/specs/stores/feature-flag-store.spec.js b/test/unit/specs/stores/feature-flag-store.spec.js
index 83bc7b95b..9507b78fd 100644
--- a/test/unit/specs/stores/feature-flag-store.spec.js
+++ b/test/unit/specs/stores/feature-flag-store.spec.js
@@ -82,6 +82,42 @@ describe('Feature flag store', () => {
}
)
+ it.each`
+ cookieState | queryState | finalState
+ ${'off'} | ${'on'} | ${'on'}
+ ${'on'} | ${'off'} | ${'off'}
+ `(
+ 'cascades flag from cookies and query params',
+ ({ cookieState, queryState, finalState }) => {
+ const flagName = 'feat_switchable_optout'
+ const featureFlagStore = useFeatureFlagStore()
+ featureFlagStore.initFromCookies({
+ [flagName]: cookieState,
+ })
+ featureFlagStore.initFromQuery({
+ [`ff_${flagName}`]: queryState,
+ })
+
+ expect(featureFlagStore.featureState(flagName)).toEqual(finalState)
+ }
+ )
+
+ it.each`
+ flagName | queryState | finalState
+ ${'feat_disabled'} | ${'on'} | ${'off'}
+ ${'feat_enabled'} | ${'off'} | ${'on'}
+ `(
+ 'does not cascade non-switchable flags from query params',
+ ({ flagName, queryState, finalState }) => {
+ const featureFlagStore = useFeatureFlagStore()
+ featureFlagStore.initFromQuery({
+ [`ff_${flagName}`]: queryState,
+ })
+
+ expect(featureFlagStore.featureState(flagName)).toEqual(finalState)
+ }
+ )
+
it.each`
environment | featureState
${'local'} | ${'on'}