Skip to content

Commit b3eb8fb

Browse files
Anton LillebyAnton Lilleby
authored andcommitted
feat: add reactive flag
1 parent f6816f2 commit b3eb8fb

File tree

10 files changed

+694
-256
lines changed

10 files changed

+694
-256
lines changed

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ Replace 'your-azure-configuration-readonly-connection-string' with your actual c
3131

3232
## Using in Vue Components
3333

34-
This plugin provides a non-reactive, one-time check called getFeatureFlag
34+
You can use the `vue3-app-configuration` plugin in your Vue components in two ways: using a non-reactive method for a one-time check or a reactive method for ongoing checks and shorter code.
35+
36+
### Non-Reactive Usage
37+
38+
For a one-time check of the feature flag, you can use the `getFeatureFlag` method. This is useful when you need to check the feature flag status once, such as on component mount.
3539

3640
```html
3741
<script setup lang="ts">
@@ -44,7 +48,8 @@ This plugin provides a non-reactive, one-time check called getFeatureFlag
4448
onMounted(async () => {
4549
if (featureFlagsManager) {
4650
isFeatureEnabled.value = await getFeatureFlag(
47-
"your-feature-flag-name', 'your-label"
51+
"your-feature-flag-name",
52+
"your-label"
4853
);
4954
}
5055
});
@@ -56,5 +61,33 @@ This plugin provides a non-reactive, one-time check called getFeatureFlag
5661
</template>
5762
```
5863

59-
Inspired by [@azure/app-configuration](https://www.npmjs.com/package/@azure/app-configuration), [vue3-application-insights](https://www.npmjs.com/package/vue3-application-insights) and
60-
[Feature Flags in Vue with Azure App Configuration](https://www.tvaidyan.com/2022/07/14/feature-flags-in-vue-with-azure-app-configuration)
64+
### Reactive Usage
65+
66+
For ongoing checks where you want the component to reactively update based on the feature flag status, use the getFeatureFlagReactive method. This returns a Vue ref and features a shorter code snippet.
67+
68+
```html
69+
<script setup lang="ts">
70+
import { inject } from "vue";
71+
import { FeatureFlagsManagerKey } from "vue3-app-configuration";
72+
73+
const featureFlagsManager = inject(FeatureFlagsManagerKey);
74+
75+
const isFeatureEnabled = featureFlagsManager
76+
? featureFlagsManager.getFeatureFlagReactive(
77+
"your-feature-flag-name",
78+
"your-label"
79+
)
80+
: ref(false);
81+
</script>
82+
83+
<template>
84+
<p v-if="isFeatureEnabled">This feature is enabled!</p>
85+
<p v-else>This feature is disabled.</p>
86+
</template>
87+
```
88+
89+
Inspired by
90+
[@azure/app-configuration](https://www.npmjs.com/package/@azure/app-configuration),
91+
[vue3-application-insights](https://www.npmjs.com/package/vue3-application-insights)
92+
and [Feature Flags in Vue with Azure App
93+
Configuration](https://www.tvaidyan.com/2022/07/14/feature-flags-in-vue-with-azure-app-configuration)

dist/index.d.mts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { InjectionKey, App } from 'vue';
1+
import { InjectionKey, App, Ref } from 'vue';
22

3-
type GetFeatureFlagFunction = (name: string, label?: string) => Promise<boolean>;
3+
type TypeGetFeatureFlag = (name: string, label?: string) => Promise<boolean>;
4+
type TypeGetFeatureFlagRef = (name: string, label?: string) => Ref<boolean>;
45
declare const FeatureFlagsManagerKey: InjectionKey<{
5-
getFeatureFlag: GetFeatureFlagFunction;
6+
getFeatureFlag: TypeGetFeatureFlag;
7+
getFeatureFlagRef: TypeGetFeatureFlagRef;
68
}>;
79
declare function AppConfigurationPlugin(app: App, connectionString?: string): void;
810

dist/index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { InjectionKey, App } from 'vue';
1+
import { InjectionKey, App, Ref } from 'vue';
22

3-
type GetFeatureFlagFunction = (name: string, label?: string) => Promise<boolean>;
3+
type TypeGetFeatureFlag = (name: string, label?: string) => Promise<boolean>;
4+
type TypeGetFeatureFlagRef = (name: string, label?: string) => Ref<boolean>;
45
declare const FeatureFlagsManagerKey: InjectionKey<{
5-
getFeatureFlag: GetFeatureFlagFunction;
6+
getFeatureFlag: TypeGetFeatureFlag;
7+
getFeatureFlagRef: TypeGetFeatureFlagRef;
68
}>;
79
declare function AppConfigurationPlugin(app: App, connectionString?: string): void;
810

dist/index.js

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
isFeatureFlag,
2727
parseFeatureFlag
2828
} from "@azure/app-configuration";
29+
import { ref } from "vue";
2930
var FeatureFlagsManagerKey = Symbol("FeatureFlagsManager");
3031
var featureFlagsManager = (connectionString) => {
3132
let client = null;
@@ -51,7 +52,29 @@ var featureFlagsManager = (connectionString) => {
5152
return false;
5253
}
5354
});
54-
return { getFeatureFlag };
55+
const getFeatureFlagRef = (name, label) => {
56+
const isEnabled = ref(false);
57+
if (!client)
58+
return isEnabled;
59+
try {
60+
client.getConfigurationSetting({
61+
key: `${featureFlagPrefix}${name}`,
62+
label
63+
}).then((response) => {
64+
if (!isFeatureFlag(response))
65+
return isEnabled;
66+
isEnabled.value = parseFeatureFlag(response).value.enabled;
67+
return isEnabled;
68+
});
69+
} catch (error) {
70+
console.error(
71+
"[App Configuration Plugin] Error retrieving feature flag.",
72+
error
73+
);
74+
}
75+
return isEnabled;
76+
};
77+
return { getFeatureFlag, getFeatureFlagRef };
5578
};
5679
function AppConfigurationPlugin(app, connectionString) {
5780
app.provide(FeatureFlagsManagerKey, featureFlagsManager(connectionString));

dist/index.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)