-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(feature-flags): manage via
composables
With a `composable` `feature flags` can be used in nearly every `nuxt function`.
- Loading branch information
1 parent
467aa9f
commit 24fdf45
Showing
4 changed files
with
49 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,14 @@ | ||
<script setup lang="ts"> | ||
import { warn } from 'vue' | ||
type Feature = 'feature-account_dashboards' | ||
type Environment = 'development' | 'production' | 'staging' | ||
const currentEnvironment = useRuntimeConfig().public.deploymentType as Environment | ||
if (!currentEnvironment) { | ||
warn('Environment variable `deploymentType` is not set.') | ||
} | ||
const staging: Feature[] = [] | ||
const activeFeatures: Record<Environment, Feature[]> = { | ||
development: [ | ||
...staging, | ||
'feature-account_dashboards', | ||
], | ||
production: [], | ||
staging, | ||
} | ||
import type { FeatureFlag } from '~/types/feature-flags' | ||
const props = defineProps<{ | ||
feature: Feature, | ||
feature: FeatureFlag, | ||
}>() | ||
const isEnabled = computed( | ||
() => activeFeatures[currentEnvironment]?.includes(props.feature), | ||
) | ||
const { has } = useFeatureFlag() | ||
</script> | ||
|
||
<template> | ||
<slot v-if="isEnabled" /> | ||
<slot v-if="has(props.feature)" /> | ||
</template> | ||
|
||
<style scoped></style> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { warn } from 'vue' | ||
import type { FeatureFlag } from '~/types/feature-flags' | ||
|
||
export const useFeatureFlag = () => { | ||
type Environment = 'development' | 'production' | 'staging' | ||
|
||
const currentEnvironment = useRuntimeConfig().public.deploymentType as Environment | ||
if (!currentEnvironment) { | ||
warn('Environment variable `deploymentType` is not set.') | ||
} | ||
|
||
const staging: FeatureFlag[] = [ 'feature-notifications' ] | ||
const development: FeatureFlag[] | ||
= [ | ||
...staging, | ||
'feature-account_dashboards', | ||
'feature-user_settings', | ||
] | ||
const featureCatalog: Record<Environment, FeatureFlag[]> = { | ||
development, | ||
production: [], | ||
staging, | ||
} | ||
|
||
const activeFeatures = featureCatalog[currentEnvironment] | ||
|
||
const has = (feature: FeatureFlag) => activeFeatures.includes(feature) | ||
|
||
return { | ||
activeFeatures, | ||
has, | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type FeatureFlag = (typeof FEATURE_FLAGS)[number] | ||
const FEATURE_FLAGS = [ | ||
'feature-account_dashboards', | ||
'feature-notifications', | ||
'feature-user_settings', | ||
] as const |