adding app config and auth modules

This commit is contained in:
Noah Spannbauer
2024-05-27 18:53:16 -05:00
parent 32c2da22f5
commit 373337d8d6
17 changed files with 168 additions and 155 deletions

View File

@@ -0,0 +1 @@
export const APP_CONFIG_OPTIONS = 'APP_CONFIG_OPTIONS';

View File

@@ -0,0 +1,3 @@
export interface AppConfigOptions {
url: string;
}

View File

@@ -0,0 +1,21 @@
import { Module, DynamicModule } from '@nestjs/common';
import { AppConfigService } from './app-config.service';
import { AppConfigOptions } from './app-config.interface';
import { APP_CONFIG_OPTIONS } from './app-config.constants';
@Module({})
export class AppConfigModule {
static register(options: AppConfigOptions): DynamicModule {
return {
module: AppConfigModule,
providers: [
{
provide: APP_CONFIG_OPTIONS,
useValue: options
},
AppConfigService
],
exports: [AppConfigService]
};
}
}

View File

@@ -0,0 +1,31 @@
import { Inject, Injectable } from '@nestjs/common';
import { AppConfigOptions } from './app-config.interface';
import { APP_CONFIG_OPTIONS } from './app-config.constants';
import axios, { AxiosResponse } from 'axios';
import { FeatureFlagValue } from '@azure/app-configuration';
@Injectable()
export class AppConfigService {
constructor(
@Inject(APP_CONFIG_OPTIONS)
private appConfigOptions: AppConfigOptions
) { }
async getFeatureFlags(token: string, key: string, label: string): Promise<FeatureFlagValue[]> {
try {
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) => item.value)
return featureFlags;
} catch (error) {
return error;
}
}
}

View File

@@ -0,0 +1 @@
export const AUTH_OPTIONS = 'AUTH_OPTIONS';

View File

@@ -0,0 +1,3 @@
export interface AuthOptions {
tenantId: string;
}

View File

@@ -0,0 +1,21 @@
import { Module, DynamicModule } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthOptions } from './auth.interface';
import { AUTH_OPTIONS } from './auth.constants';
@Module({})
export class AuthModule {
static register(options: AuthOptions): DynamicModule {
return {
module: AuthModule,
providers: [
{
provide: AUTH_OPTIONS,
useValue: options
},
AuthService
],
exports: [AuthService]
};
}
}

View File

@@ -0,0 +1,41 @@
import { Inject, Injectable } from '@nestjs/common';
import { AuthOptions } from './auth.interface';
import { AUTH_OPTIONS } from './auth.constants';
import axios, { AxiosResponse } from 'axios';
@Injectable()
export class AuthService {
constructor(
@Inject(AUTH_OPTIONS)
private authOptions: AuthOptions
) { }
async getToken(
grantType: string,
clientId: string,
clientSecret: string,
resource: string
): Promise<string> {
try {
const { data }: AxiosResponse = await axios.post(
`https://login.microsoftonline.com/${this.authOptions.tenantId}/oauth2/token`,
{
grant_type: grantType,
client_id: clientId,
client_secret: clientSecret,
resource: resource
},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
return data.access_token;
} catch (error) {
return error;
}
}
}

View File

@@ -1 +0,0 @@
export const AZ_APP_CONFIG_OPTIONS = 'AZ_APP_CONFIG_OPTIONS';

View File

@@ -1,3 +0,0 @@
export interface AzAppConfigOptions {
connectionString: string;
}

View File

@@ -1,21 +0,0 @@
import { Module, DynamicModule } from '@nestjs/common';
import { AzAppConfigService } from './az-app-config.service';
import { AzAppConfigOptions } from './az-app-config.interface';
import { AZ_APP_CONFIG_OPTIONS } from './az-app-config.constants';
@Module({})
export class AzAppConfigModule {
static register(options: AzAppConfigOptions): DynamicModule {
return {
module: AzAppConfigModule,
providers: [
{
provide: AZ_APP_CONFIG_OPTIONS,
useValue: options,
},
AzAppConfigService,
],
exports: [AzAppConfigService],
};
}
}

View File

@@ -1,34 +0,0 @@
import { Inject, Injectable } from '@nestjs/common';
import { AzAppConfigOptions } from './az-app-config.interface';
import { FeatureFlag } from './feature-flag.interface';
import { AppConfigurationClient } from '@azure/app-configuration';
import { AZ_APP_CONFIG_OPTIONS } from './az-app-config.constants';
@Injectable()
export class AzAppConfigService {
constructor(
@Inject(AZ_APP_CONFIG_OPTIONS)
private azAppConfigOptions: AzAppConfigOptions,
) { }
async getFeatureFlag(key: string, label: string): Promise<FeatureFlag> {
try {
const client = new AppConfigurationClient(
this.azAppConfigOptions.connectionString,
);
const configurationSetting = await client.getConfigurationSetting({
key: key,
label: label,
});
const featureFlag: FeatureFlag = {
name: configurationSetting.key,
environment: configurationSetting.label,
active: configurationSetting.value,
};
return featureFlag;
} catch (error) {
return error;
}
}
}

View File

@@ -1,5 +0,0 @@
export interface FeatureFlag {
name: string;
environment: string;
active: string;
}

View File

@@ -1,2 +1,4 @@
export * from './azAppConfig/az-app-config.module';
export * from './azAppConfig/az-app-config.service';
export * from './appConfig/app-config.module';
export * from './appConfig/app-config.service';
export * from './auth/auth.module';
export * from './auth/auth.service'