adding app config and auth modules
This commit is contained in:
1
libs/modules/src/appConfig/app-config.constants.ts
Normal file
1
libs/modules/src/appConfig/app-config.constants.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const APP_CONFIG_OPTIONS = 'APP_CONFIG_OPTIONS';
|
||||
3
libs/modules/src/appConfig/app-config.interface.ts
Normal file
3
libs/modules/src/appConfig/app-config.interface.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface AppConfigOptions {
|
||||
url: string;
|
||||
}
|
||||
21
libs/modules/src/appConfig/app-config.module.ts
Normal file
21
libs/modules/src/appConfig/app-config.module.ts
Normal 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]
|
||||
};
|
||||
}
|
||||
}
|
||||
31
libs/modules/src/appConfig/app-config.service.ts
Normal file
31
libs/modules/src/appConfig/app-config.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
1
libs/modules/src/auth/auth.constants.ts
Normal file
1
libs/modules/src/auth/auth.constants.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const AUTH_OPTIONS = 'AUTH_OPTIONS';
|
||||
3
libs/modules/src/auth/auth.interface.ts
Normal file
3
libs/modules/src/auth/auth.interface.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface AuthOptions {
|
||||
tenantId: string;
|
||||
}
|
||||
21
libs/modules/src/auth/auth.module.ts
Normal file
21
libs/modules/src/auth/auth.module.ts
Normal 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]
|
||||
};
|
||||
}
|
||||
}
|
||||
41
libs/modules/src/auth/auth.service.ts
Normal file
41
libs/modules/src/auth/auth.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export const AZ_APP_CONFIG_OPTIONS = 'AZ_APP_CONFIG_OPTIONS';
|
||||
@@ -1,3 +0,0 @@
|
||||
export interface AzAppConfigOptions {
|
||||
connectionString: string;
|
||||
}
|
||||
@@ -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],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
export interface FeatureFlag {
|
||||
name: string;
|
||||
environment: string;
|
||||
active: string;
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user