Skip to content

Commit 6358e5c

Browse files
Anton LillebyAnton Lilleby
authored andcommitted
feat: Add reactive flag
1 parent ddb14b3 commit 6358e5c

File tree

4 files changed

+95
-42
lines changed

4 files changed

+95
-42
lines changed

README.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Vue 3 plugin that provides an easy way to manage feature flags in your Vue appli
44

55
## Installation
66

7-
```console
7+
```
88
npm install vue3-app-configuration
99
```
1010

@@ -15,12 +15,12 @@ To use vue3-app-configuration, you need to initialize it in your main Vue applic
1515
```ts
1616
import { createApp } from "vue";
1717
import App from "./App.vue";
18-
import AppConfigurationPlguin from "vue3-app-configuration";
18+
import { AppConfigurationPlugin } from "vue3-app-configuration";
1919

2020
const app = createApp(App);
2121

2222
app.use(
23-
AppConfigurationPlguin,
23+
AppConfigurationPlugin,
2424
"your-azure-configuration-readonly-connection-string"
2525
);
2626

@@ -31,28 +31,54 @@ Replace 'your-azure-configuration-readonly-connection-string' with your actual c
3131

3232
## Using in Vue Components
3333

34-
Inject the feature flags manager and use it to check feature flags:
34+
The plugin provides two methods: getFeatureFlag for a non-reactive, one-time check, and getFeatureFlagReactive for a reactive, continuously updated check.
35+
36+
### Using getFeatureFlagReactive
3537

36-
```js
38+
For a ref check of the feature flag:
39+
40+
```html
3741
<script setup lang="ts">
38-
import { inject, onMounted, ref } from 'vue';
39-
import { FeatureFlagsManagerKey } from 'vue3-app-configuration';
42+
import { inject } from "vue";
43+
import { FeatureFlagsManagerKey } from "vue3-app-configuration";
4044
41-
const isFeatureEnabled = ref(false);
42-
const featureFlagsManager = inject(FeatureFlagsManagerKey);
45+
const { getFeatureFlag } = inject(FeatureFlagsManagerKey);
4346
44-
onMounted(async () => {
45-
if (featureFlagsManager) {
46-
isFeatureEnabled.value = await featureFlagsManager.getFeatureFlag('your-feature-flag-name');
47-
}
48-
});
47+
const isFeatureEnabled = getFeatureFlagRef(
48+
"your-feature-flag-name",
49+
"your-label"
50+
);
4951
</script>
5052

5153
<template>
5254
<p v-if="isFeatureEnabled">This feature is enabled!</p>
5355
<p v-else>This feature is disabled.</p>
5456
</template>
57+
```
58+
59+
### Using getFeatureFlag
60+
61+
For a one-time check of the feature flag:
62+
63+
```html
64+
<script setup lang="ts">
65+
import { inject, onMounted } from "vue";
66+
import { FeatureFlagsManagerKey } from "vue3-app-configuration";
67+
68+
const { getFeatureFlag } = inject(FeatureFlagsManagerKey);
69+
const isFeatureEnabled = ref(false);
70+
71+
onMounted(async () => {
72+
isFeatureEnabled.value = await getFeatureFlag(
73+
"your-feature-flag-name', 'your-label"
74+
);
75+
});
5576
</script>
77+
78+
<template>
79+
<p v-if="isFeatureEnabled">This feature is enabled!</p>
80+
<p v-else>This feature is disabled.</p>
81+
</template>
5682
```
5783

5884
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

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue3-app-configuration",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "A Vue 3 plugin for integrating Azure App Configuration, including feature flags management.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/index.ts

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
import { App, InjectionKey } from "vue";
21
import {
32
AppConfigurationClient,
43
featureFlagPrefix,
54
isFeatureFlag,
65
parseFeatureFlag,
76
} from "@azure/app-configuration";
7+
import { ref, type App, type InjectionKey, type Ref } from "vue";
88

99
interface FeatureFlagsManager {
1010
getFeatureFlag(name: string, label?: string): Promise<boolean>;
11+
getFeatureFlagRef(name: string, label?: string): Ref<boolean>;
1112
}
1213

14+
type GetFeatureFlagFunction = (
15+
name: string,
16+
label?: string
17+
) => Promise<boolean>;
18+
19+
type GetFeatureFlagRefFunction = (name: string, label?: string) => Ref<boolean>;
20+
21+
const FeatureFlagsManagerKey: InjectionKey<{
22+
getFeatureFlag: GetFeatureFlagFunction;
23+
getFeatureFlagRef: GetFeatureFlagRefFunction;
24+
}> = Symbol("FeatureFlagsManager");
25+
1326
const featureFlagsManager = (
1427
connectionString?: string
1528
): FeatureFlagsManager => {
@@ -19,39 +32,53 @@ const featureFlagsManager = (
1932
client = new AppConfigurationClient(connectionString);
2033
}
2134

22-
return {
23-
async getFeatureFlag(name: string, label?: string) {
24-
if (!client) {
25-
console.warn(
26-
"[App Configuration Plugin] AppConfigurationClient is not initialized."
27-
);
28-
return false;
29-
}
30-
try {
31-
const response = await client.getConfigurationSetting({
32-
key: `${featureFlagPrefix}${name}`,
33-
label,
34-
});
35+
const getFeatureFlag = async (
36+
name: string,
37+
label?: string
38+
): Promise<boolean> => {
39+
if (!client) return false;
40+
try {
41+
const response = await client.getConfigurationSetting({
42+
key: `${featureFlagPrefix}${name}`,
43+
label,
44+
});
45+
if (!isFeatureFlag(response)) return false;
46+
return parseFeatureFlag(response).value.enabled;
47+
} catch (error) {
48+
console.error(
49+
"[App Configuration Plugin] Error retrieving feature flag.",
50+
error
51+
);
52+
return false;
53+
}
54+
};
3555

56+
const getFeatureFlagRef = (name: string, label?: string): Ref<boolean> => {
57+
const isEnabled = ref(false);
58+
59+
if (!client) return isEnabled;
60+
61+
client
62+
.getConfigurationSetting({ key: `${featureFlagPrefix}${name}`, label })
63+
.then((response) => {
3664
if (!isFeatureFlag(response)) return false;
37-
return parseFeatureFlag(response).value.enabled;
38-
} catch (error) {
65+
isEnabled.value = parseFeatureFlag(response).value.enabled;
66+
})
67+
.catch((error) => {
3968
console.error(
40-
"[App Configuration Plugin] Error fetching feature flag.",
69+
"[App Configuration Plugin] Error retrieving feature flag",
4170
error
4271
);
43-
return false;
44-
}
45-
},
72+
});
73+
74+
return isEnabled;
4675
};
47-
};
4876

49-
const FeatureFlagsManagerKey: InjectionKey<FeatureFlagsManager> = Symbol(
50-
"FeatureFlagsManager"
51-
);
77+
return { getFeatureFlag, getFeatureFlagRef };
78+
};
5279

5380
function AppConfigurationPlugin(app: App, connectionString?: string) {
5481
app.provide(FeatureFlagsManagerKey, featureFlagsManager(connectionString));
5582
}
5683

57-
export { FeatureFlagsManagerKey, AppConfigurationPlugin };
84+
export { AppConfigurationPlugin, FeatureFlagsManagerKey };

0 commit comments

Comments
 (0)