From db0e8bfe5de416a083207ffd8897bd7254f86530 Mon Sep 17 00:00:00 2001 From: Noah Spannbauer Date: Thu, 6 Jun 2024 07:14:20 -0500 Subject: [PATCH] updating get feature flags functions --- .../src/azAppConfig/az-app-config.service.ts | 59 +++++++------------ package.json | 2 +- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/libs/modules/src/azAppConfig/az-app-config.service.ts b/libs/modules/src/azAppConfig/az-app-config.service.ts index fe46000..b1689f1 100644 --- a/libs/modules/src/azAppConfig/az-app-config.service.ts +++ b/libs/modules/src/azAppConfig/az-app-config.service.ts @@ -2,7 +2,8 @@ import { Inject, Injectable } from '@nestjs/common'; import { AppConfigOptions } from './az-app-config.interface'; import { APP_CONFIG_OPTIONS } from './az-app-config.constants'; 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() export class AppConfigService { @@ -11,45 +12,27 @@ export class AppConfigService { private appConfigOptions: AppConfigOptions ) { } - async getToken(): Promise { - try { - const { data }: AxiosResponse = await axios.post( - `https://login.microsoftonline.com/${this.appConfigOptions.tenantId}/oauth2/token`, - { - grant_type: 'client_credentials', - client_id: this.appConfigOptions.clientId, - client_secret: this.appConfigOptions.clientSecret, - resource: 'https://azconfig.io' - }, - { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' + async getFeatureFlags(keys: string[], label: string): Promise<{ key: string, enabled: boolean }[]> { + const credential: ClientSecretCredential = new ClientSecretCredential(this.appConfigOptions.tenantId, this.appConfigOptions.clientId, this.appConfigOptions.clientSecret); + const client: AppConfigurationClient = new AppConfigurationClient(this.appConfigOptions.url, credential); + const featureFlags: { key: string, enabled: boolean }[] = await Promise.all( + keys.map(async (key: string) => { + const configSetting: GetConfigurationSettingResponse = await client.getConfigurationSetting({ + key: `${featureFlagPrefix}${key}`, + label: label + }); + + if (isFeatureFlag(configSetting)) { + const parsedFeatureFlag: ConfigurationSetting = parseFeatureFlag(configSetting); + + return { + key: parsedFeatureFlag.value.id, + enabled: parsedFeatureFlag.value.enabled } } - ); + }) + ); - return data.access_token; - } catch (error) { - return error; - } - } - - async getFeatureFlags(token: string, key: string, label: string): Promise { - 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; - } + return featureFlags; } } diff --git a/package.json b/package.json index 7236754..06c204c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@noahspan/noahspan-modules", - "version": "0.2.4", + "version": "0.2.7", "description": "", "author": "", "license": "UNLICENSED",