Skip to content

Commit 958e103

Browse files
Anton LillebyAnton Lilleby
authored andcommitted
feat: expose appConfigurationClient instance globally
1 parent 0da4183 commit 958e103

File tree

10 files changed

+141
-134
lines changed

10 files changed

+141
-134
lines changed

README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,48 @@ Replace 'your-azure-configuration-readonly-connection-string' with your actual c
3333

3434
You can use the `vue3-app-configuration` plugin in your Vue components in two ways: using an async non-reactive method or a non-blocking reactive method.
3535

36-
### Non-Reactive Usage
36+
### Reactive Usage
3737

3838
```html
3939
<script setup lang="ts">
40-
import { onMounted, ref } from "vue";
4140
import { useFeatureFlags } from "vue3-app-configuration";
4241
4342
const { getFeatureFlag } = useFeatureFlags();
4443
45-
onMounted(async () => {
46-
const isFeatureEnabled = await getFeatureFlag(
47-
"your-feature-flag-name",
48-
"your-label"
49-
);
50-
});
44+
const isFeatureEnabled = getFeatureFlag(
45+
"your-feature-flag-name",
46+
"your-label"
47+
);
5148
</script>
49+
50+
<template>
51+
<p v-if="isFeatureEnabled">This feature is enabled</p>
52+
<p v-else>This feature is disabled</p>
53+
</template>
5254
```
5355

54-
### Reactive Usage
56+
### Non-Reactive Usage
5557

5658
```html
5759
<script setup lang="ts">
60+
import { onMounted } from "vue";
5861
import { useFeatureFlags } from "vue3-app-configuration";
5962
60-
const { getFeatureFlagRef } = useFeatureFlags();
63+
const { getFeatureFlagAsync } = useFeatureFlags();
6164
62-
const isFeatureEnabled = getFeatureFlagRef(
63-
"your-feature-flag-name",
64-
"your-label"
65-
);
65+
onMounted(async () => {
66+
const isFeatureEnabled = await getFeatureFlagAsync(
67+
"your-feature-flag-name",
68+
"your-label"
69+
);
70+
});
6671
</script>
67-
68-
<template>
69-
<p v-if="isFeatureEnabled">This feature is enabled!</p>
70-
<p v-else>This feature is disabled.</p>
71-
</template>
7272
```
7373

74+
## AppConfigurationClient
75+
76+
The AppConfigurationClient instance is exposed at `this.featureFlagsManager.appConfigurationClient`.
77+
7478
Inspired by
7579

7680
- [@azure/app-configuration](https://www.npmjs.com/package/@azure/app-configuration)

dist/index.d.mts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { InjectionKey, App, Ref } from 'vue';
1+
import { AppConfigurationClient } from '@azure/app-configuration';
2+
import { App, Ref } from 'vue';
23

3-
type TypeGetFeatureFlag = (name: string, label?: string) => Promise<boolean>;
4-
type TypeGetFeatureFlagRef = (name: string, label?: string) => Ref<boolean>;
4+
type TypeAppConfigurationClient = AppConfigurationClient | null;
5+
type TypeGetFeatureFlag = (name: string, label?: string) => Ref<boolean>;
6+
type TypeGetFeatureFlagAsync = (name: string, label?: string) => Promise<boolean>;
57
interface IFeatureFlagsManager {
8+
appConfigurationClient: TypeAppConfigurationClient;
69
getFeatureFlag: TypeGetFeatureFlag;
7-
getFeatureFlagRef: TypeGetFeatureFlagRef;
10+
getFeatureFlagAsync: TypeGetFeatureFlagAsync;
811
}
9-
declare const FeatureFlagsManagerKey: InjectionKey<{
10-
getFeatureFlag: TypeGetFeatureFlag;
11-
getFeatureFlagRef: TypeGetFeatureFlagRef;
12-
}>;
1312
declare function AppConfigurationPlugin(app: App, connectionString?: string): void;
1413
declare const useFeatureFlags: () => IFeatureFlagsManager;
1514

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

dist/index.d.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { InjectionKey, App, Ref } from 'vue';
1+
import { AppConfigurationClient } from '@azure/app-configuration';
2+
import { App, Ref } from 'vue';
23

3-
type TypeGetFeatureFlag = (name: string, label?: string) => Promise<boolean>;
4-
type TypeGetFeatureFlagRef = (name: string, label?: string) => Ref<boolean>;
4+
type TypeAppConfigurationClient = AppConfigurationClient | null;
5+
type TypeGetFeatureFlag = (name: string, label?: string) => Ref<boolean>;
6+
type TypeGetFeatureFlagAsync = (name: string, label?: string) => Promise<boolean>;
57
interface IFeatureFlagsManager {
8+
appConfigurationClient: TypeAppConfigurationClient;
69
getFeatureFlag: TypeGetFeatureFlag;
7-
getFeatureFlagRef: TypeGetFeatureFlagRef;
10+
getFeatureFlagAsync: TypeGetFeatureFlagAsync;
811
}
9-
declare const FeatureFlagsManagerKey: InjectionKey<{
10-
getFeatureFlag: TypeGetFeatureFlag;
11-
getFeatureFlagRef: TypeGetFeatureFlagRef;
12-
}>;
1312
declare function AppConfigurationPlugin(app: App, connectionString?: string): void;
1413
declare const useFeatureFlags: () => IFeatureFlagsManager;
1514

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

dist/index.js

Lines changed: 28 additions & 28 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: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,59 +27,61 @@ import {
2727
parseFeatureFlag
2828
} from "@azure/app-configuration";
2929
import { inject, ref } from "vue";
30-
var FeatureFlagsManagerKey = Symbol("FeatureFlagsManager");
30+
var FeatureFlagsManagerKey = Symbol(
31+
"FeatureFlagsManager"
32+
);
3133
var featureFlagsManager = (connectionString) => {
32-
let client = null;
34+
let appConfigurationClient = null;
3335
if (connectionString) {
34-
client = new AppConfigurationClient(connectionString);
36+
appConfigurationClient = new AppConfigurationClient(connectionString);
3537
}
36-
const getFeatureFlag = (name, label) => __async(void 0, null, function* () {
37-
if (!client)
38-
return false;
38+
const getFeatureFlag = (name, label) => {
39+
const isEnabled = ref(false);
40+
if (!appConfigurationClient)
41+
return isEnabled;
3942
try {
40-
const response = yield client.getConfigurationSetting({
43+
appConfigurationClient.getConfigurationSetting({
4144
key: `${featureFlagPrefix}${name}`,
4245
label
46+
}).then((response) => {
47+
if (!isFeatureFlag(response))
48+
return isEnabled;
49+
isEnabled.value = parseFeatureFlag(response).value.enabled;
50+
return isEnabled;
4351
});
44-
if (!isFeatureFlag(response))
45-
return false;
46-
return parseFeatureFlag(response).value.enabled;
4752
} catch (error) {
4853
console.error(
4954
"[App Configuration Plugin] Error retrieving feature flag.",
5055
error
5156
);
52-
return false;
5357
}
54-
});
55-
const getFeatureFlagRef = (name, label) => {
56-
const isEnabled = ref(false);
57-
if (!client)
58-
return isEnabled;
58+
return isEnabled;
59+
};
60+
const getFeatureFlagAsync = (name, label) => __async(void 0, null, function* () {
61+
if (!appConfigurationClient)
62+
return false;
5963
try {
60-
client.getConfigurationSetting({
64+
const response = yield appConfigurationClient.getConfigurationSetting({
6165
key: `${featureFlagPrefix}${name}`,
6266
label
63-
}).then((response) => {
64-
if (!isFeatureFlag(response))
65-
return isEnabled;
66-
isEnabled.value = parseFeatureFlag(response).value.enabled;
67-
return isEnabled;
6867
});
68+
if (!isFeatureFlag(response))
69+
return false;
70+
return parseFeatureFlag(response).value.enabled;
6971
} catch (error) {
7072
console.error(
7173
"[App Configuration Plugin] Error retrieving feature flag.",
7274
error
7375
);
76+
return false;
7477
}
75-
return isEnabled;
76-
};
77-
return { getFeatureFlag, getFeatureFlagRef };
78+
});
79+
return { appConfigurationClient, getFeatureFlag, getFeatureFlagAsync };
7880
};
7981
function AppConfigurationPlugin(app, connectionString) {
8082
const manager = featureFlagsManager(connectionString);
8183
app.provide(FeatureFlagsManagerKey, manager);
82-
app.config.globalProperties.$featureFlags = manager;
84+
app.config.globalProperties.featureFlagsManager = manager;
8385
}
8486
var useFeatureFlags = () => {
8587
const featureFlagsManager2 = inject(
@@ -94,7 +96,6 @@ var useFeatureFlags = () => {
9496
};
9597
export {
9698
AppConfigurationPlugin,
97-
FeatureFlagsManagerKey,
9899
useFeatureFlags
99100
};
100101
//# sourceMappingURL=index.mjs.map

0 commit comments

Comments
 (0)