Skip to content

Commit db3b187

Browse files
Anton LillebyAnton Lilleby
authored andcommitted
feat: inject key globally for simpler usage
1 parent b3eb8fb commit db3b187

File tree

10 files changed

+83
-51
lines changed

10 files changed

+83
-51
lines changed

README.md

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vue3-app-configuration
22

3-
Vue 3 plugin that provides an easy way to manage feature flags in your Vue application using Azure App Configuration. This plugin allows you to use feature flags and toggling in different environments.
3+
Vue 3 plugin that provides an easy way to manage feature flags in your Vue 3 application using Azure App Configuration. This plugin allows you to use feature flags and toggle different environments.
44

55
## Installation
66

@@ -10,7 +10,7 @@ npm install vue3-app-configuration
1010

1111
## Get started
1212

13-
To use vue3-app-configuration, you need to initialize it in your main Vue application file, typically main.ts or main.js.
13+
To use vue3-app-configuration, you need to initialize it in your main application file, typically main.ts or main.js.
1414

1515
```ts
1616
import { createApp } from "vue";
@@ -27,57 +27,42 @@ app.use(
2727
app.mount("#app");
2828
```
2929

30-
Replace 'your-azure-configuration-readonly-connection-string' with your actual connection string.
30+
Replace 'your-azure-configuration-readonly-connection-string' with your actual connection string. You can use this guide for refence [Feature Flags in Vue with Azure App Configuration](https://www.tvaidyan.com/2022/07/14/feature-flags-in-vue-with-azure-app-configuration).
3131

3232
## Using in Vue Components
3333

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.
34+
You can use the `vue3-app-configuration` plugin in your Vue components in two ways: using a async non-reactive method or a reactive method.
3535

3636
### Non-Reactive Usage
3737

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.
39-
4038
```html
4139
<script setup lang="ts">
42-
import { inject, onMounted } from "vue";
43-
import { FeatureFlagsManagerKey } from "vue3-app-configuration";
40+
import { onMounted, ref } from "vue";
41+
import { useFeatureFlags } from "vue3-app-configuration";
4442
45-
const isFeatureEnabled = ref(false);
46-
const featureFlagsManager = inject(FeatureFlagsManagerKey);
43+
const { getFeatureFlag } = useFeatureFlags();
4744
4845
onMounted(async () => {
49-
if (featureFlagsManager) {
50-
isFeatureEnabled.value = await getFeatureFlag(
51-
"your-feature-flag-name",
52-
"your-label"
53-
);
54-
}
46+
const isFeatureEnabled = await getFeatureFlag(
47+
"your-feature-flag-name",
48+
"your-label"
49+
);
5550
});
5651
</script>
57-
58-
<template>
59-
<p v-if="isFeatureEnabled">This feature is enabled!</p>
60-
<p v-else>This feature is disabled.</p>
61-
</template>
6252
```
6353

6454
### Reactive Usage
6555

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-
6856
```html
6957
<script setup lang="ts">
70-
import { inject } from "vue";
71-
import { FeatureFlagsManagerKey } from "vue3-app-configuration";
58+
import { useFeatureFlags } from "vue3-app-configuration";
7259
73-
const featureFlagsManager = inject(FeatureFlagsManagerKey);
60+
const { getFeatureFlagRef } = useFeatureFlags();
7461
75-
const isFeatureEnabled = featureFlagsManager
76-
? featureFlagsManager.getFeatureFlagReactive(
77-
"your-feature-flag-name",
78-
"your-label"
79-
)
80-
: ref(false);
62+
const isFeatureEnabled = getFeatureFlagRef(
63+
"your-feature-flag-name",
64+
"your-label"
65+
);
8166
</script>
8267

8368
<template>
@@ -87,7 +72,7 @@ For ongoing checks where you want the component to reactively update based on th
8772
```
8873

8974
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)
75+
76+
- [@azure/app-configuration](https://www.npmjs.com/package/@azure/app-configuration)
77+
- [vue3-application-insights](https://www.npmjs.com/package/vue3-application-insights)
78+
- [Feature Flags in Vue with Azure App Configuration](https://www.tvaidyan.com/2022/07/14/feature-flags-in-vue-with-azure-app-configuration)

dist/index.d.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { InjectionKey, App, Ref } from 'vue';
22

33
type TypeGetFeatureFlag = (name: string, label?: string) => Promise<boolean>;
44
type TypeGetFeatureFlagRef = (name: string, label?: string) => Ref<boolean>;
5+
interface IFeatureFlagsManager {
6+
getFeatureFlag: TypeGetFeatureFlag;
7+
getFeatureFlagRef: TypeGetFeatureFlagRef;
8+
}
59
declare const FeatureFlagsManagerKey: InjectionKey<{
610
getFeatureFlag: TypeGetFeatureFlag;
711
getFeatureFlagRef: TypeGetFeatureFlagRef;
812
}>;
913
declare function AppConfigurationPlugin(app: App, connectionString?: string): void;
14+
declare const useFeatureFlags: () => IFeatureFlagsManager;
1015

11-
export { AppConfigurationPlugin, FeatureFlagsManagerKey };
16+
export { AppConfigurationPlugin, FeatureFlagsManagerKey, useFeatureFlags };

dist/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { InjectionKey, App, Ref } from 'vue';
22

33
type TypeGetFeatureFlag = (name: string, label?: string) => Promise<boolean>;
44
type TypeGetFeatureFlagRef = (name: string, label?: string) => Ref<boolean>;
5+
interface IFeatureFlagsManager {
6+
getFeatureFlag: TypeGetFeatureFlag;
7+
getFeatureFlagRef: TypeGetFeatureFlagRef;
8+
}
59
declare const FeatureFlagsManagerKey: InjectionKey<{
610
getFeatureFlag: TypeGetFeatureFlag;
711
getFeatureFlagRef: TypeGetFeatureFlagRef;
812
}>;
913
declare function AppConfigurationPlugin(app: App, connectionString?: string): void;
14+
declare const useFeatureFlags: () => IFeatureFlagsManager;
1015

11-
export { AppConfigurationPlugin, FeatureFlagsManagerKey };
16+
export { AppConfigurationPlugin, FeatureFlagsManagerKey, useFeatureFlags };

dist/index.js

Lines changed: 16 additions & 3 deletions
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: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
isFeatureFlag,
2727
parseFeatureFlag
2828
} from "@azure/app-configuration";
29-
import { ref } from "vue";
29+
import { ref, inject } from "vue";
3030
var FeatureFlagsManagerKey = Symbol("FeatureFlagsManager");
3131
var featureFlagsManager = (connectionString) => {
3232
let client = null;
@@ -77,10 +77,22 @@ var featureFlagsManager = (connectionString) => {
7777
return { getFeatureFlag, getFeatureFlagRef };
7878
};
7979
function AppConfigurationPlugin(app, connectionString) {
80-
app.provide(FeatureFlagsManagerKey, featureFlagsManager(connectionString));
80+
const manager = featureFlagsManager(connectionString);
81+
app.provide(FeatureFlagsManagerKey, manager);
82+
app.config.globalProperties.$featureFlags = manager;
8183
}
84+
var useFeatureFlags = () => {
85+
const featureFlagsManager2 = inject(
86+
FeatureFlagsManagerKey
87+
);
88+
if (!featureFlagsManager2) {
89+
throw new Error("FeatureFlagsManager is not provided");
90+
}
91+
return featureFlagsManager2;
92+
};
8293
export {
8394
AppConfigurationPlugin,
84-
FeatureFlagsManagerKey
95+
FeatureFlagsManagerKey,
96+
useFeatureFlags
8597
};
8698
//# sourceMappingURL=index.mjs.map

0 commit comments

Comments
 (0)