updating get feature flags functions
This commit is contained in:
@@ -2,7 +2,8 @@ import { Inject, Injectable } from '@nestjs/common';
|
|||||||
import { AppConfigOptions } from './az-app-config.interface';
|
import { AppConfigOptions } from './az-app-config.interface';
|
||||||
import { APP_CONFIG_OPTIONS } from './az-app-config.constants';
|
import { APP_CONFIG_OPTIONS } from './az-app-config.constants';
|
||||||
import axios, { AxiosResponse } from 'axios';
|
import axios, { AxiosResponse } from 'axios';
|
||||||
import { FeatureFlagValue } from '@azure/app-configuration';
|
import { AppConfigurationClient, ConfigurationSetting, FeatureFlagValue, GetConfigurationSettingResponse, featureFlagPrefix, isFeatureFlag, parseFeatureFlag } from '@azure/app-configuration';
|
||||||
|
import { ClientSecretCredential } from '@azure/identity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AppConfigService {
|
export class AppConfigService {
|
||||||
@@ -11,45 +12,27 @@ export class AppConfigService {
|
|||||||
private appConfigOptions: AppConfigOptions
|
private appConfigOptions: AppConfigOptions
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async getToken(): Promise<string> {
|
async getFeatureFlags(keys: string[], label: string): Promise<{ key: string, enabled: boolean }[]> {
|
||||||
try {
|
const credential: ClientSecretCredential = new ClientSecretCredential(this.appConfigOptions.tenantId, this.appConfigOptions.clientId, this.appConfigOptions.clientSecret);
|
||||||
const { data }: AxiosResponse = await axios.post(
|
const client: AppConfigurationClient = new AppConfigurationClient(this.appConfigOptions.url, credential);
|
||||||
`https://login.microsoftonline.com/${this.appConfigOptions.tenantId}/oauth2/token`,
|
const featureFlags: { key: string, enabled: boolean }[] = await Promise.all(
|
||||||
{
|
keys.map(async (key: string) => {
|
||||||
grant_type: 'client_credentials',
|
const configSetting: GetConfigurationSettingResponse = await client.getConfigurationSetting({
|
||||||
client_id: this.appConfigOptions.clientId,
|
key: `${featureFlagPrefix}${key}`,
|
||||||
client_secret: this.appConfigOptions.clientSecret,
|
label: label
|
||||||
resource: 'https://azconfig.io'
|
});
|
||||||
},
|
|
||||||
{
|
if (isFeatureFlag(configSetting)) {
|
||||||
headers: {
|
const parsedFeatureFlag: ConfigurationSetting<FeatureFlagValue> = parseFeatureFlag(configSetting);
|
||||||
'Content-Type': 'application/x-www-form-urlencoded'
|
|
||||||
|
return {
|
||||||
|
key: parsedFeatureFlag.value.id,
|
||||||
|
enabled: parsedFeatureFlag.value.enabled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
})
|
||||||
|
);
|
||||||
|
|
||||||
return data.access_token;
|
return featureFlags;
|
||||||
} catch (error) {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getFeatureFlags(token: string, key: string, label: string): Promise<FeatureFlagValue[]> {
|
|
||||||
try {
|
|
||||||
const token: string = await this.getToken();
|
|
||||||
const response: AxiosResponse = await axios.get(
|
|
||||||
`${this.appConfigOptions.url}/kv?key=.appconfig.featureflag/${key}&label=${label}`,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${token}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
const featureFlags: FeatureFlagValue[] = response.data.items.map((item) => JSON.parse(item.value));
|
|
||||||
|
|
||||||
return featureFlags;
|
|
||||||
} catch (error) {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@noahspan/noahspan-modules",
|
"name": "@noahspan/noahspan-modules",
|
||||||
"version": "0.2.4",
|
"version": "0.2.7",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
|
|||||||
Reference in New Issue
Block a user