Skip to content

Commit

Permalink
feat(config): improve ConfigService
Browse files Browse the repository at this point in the history
  • Loading branch information
isuvorov committed Jan 14, 2024
1 parent 3a15bde commit af63c18
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 100 deletions.
2 changes: 0 additions & 2 deletions libs/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"@mikro-orm/nestjs": "^5.2.3",
"@nestjs-modules/mailer": "^1.10.3",
"@nestjs/common": "^10.3.0",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.3.0",
"@nestlib/config": "workspace:^",
"@types/express": "^4.17.17",
Expand All @@ -43,7 +42,6 @@
"@mikro-orm/nestjs": "^5.2.2",
"@nestjs-modules/mailer": "^1.9.1",
"@nestjs/common": "*",
"@nestjs/config": "*",
"@nestjs/core": "*",
"@nestlib/config": "*"
},
Expand Down
2 changes: 1 addition & 1 deletion libs/auth/src/AuthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MailerService } from '@nestjs-modules/mailer';
import { ConfigService } from '@nestlib/config';
import { ErrorInterceptor, ResponseInterceptor } from '@nestlib/interceptors';

import { AuthRole } from './AuthDecorator';
Expand Down
45 changes: 45 additions & 0 deletions libs/config/src/ConfigModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { DynamicModule, Module } from '@nestjs/common';
import {
ConfigModule as NestConfigModule,
ConfigService as NestConfigService,
} from '@nestjs/config';

// import { ConfigModule } from '@nestlib/config';
import { ConfigService } from './ConfigService';
import { loadConfigEnvs } from './loadConfigEnvs';

@Module({
// import: [],
// controllers: [AuthController],
// providers: [CryptoService, AuthOtpService, AuthService],
// exports: [ConfigService],
})
export class ConfigModule {
static forRoot(): DynamicModule {
const rootDir = process.cwd();
const envFiles = ['.env', '../.env', '../../.env'].map((f) => `${rootDir}/${f}`);
const configEnvs = [
'.env.js',
'.env.json',
'../.env.js',
'../.env.json',
'../../.env.js',
'../../.env.json',
].map((f) => `${rootDir}/${f}`);
// console.log('[CnfModule]', { envFiles, configEnvs });
return {
imports: [
NestConfigModule.forRoot({ envFilePath: envFiles }),
NestConfigModule.forRoot(loadConfigEnvs(['process.env.ENV_JSON', ...configEnvs])),
// ConfigModule.forRoot(loadConfigEnvs(['process.env.ENV_JSON', ...configEnvs])),
],
module: ConfigModule,
// controllers: [AuthController],
providers: [ConfigService, NestConfigService],
exports: [ConfigService, NestConfigService],
};
}
static getExports() {
return [ConfigService, NestConfigService];
}
}
15 changes: 15 additions & 0 deletions libs/config/src/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import { ConfigService as NestConfigService } from '@nestjs/config';

@Injectable()
export class ConfigService {
constructor(private configService: NestConfigService) {}
get(key: string) {
const res = this.configService.get(key);
// console.log('ConfigService.get', { key }, res);
// console.log('configService', this.configService);
return res;
}
}

export default ConfigService;
45 changes: 0 additions & 45 deletions libs/config/src/config.ts

This file was deleted.

21 changes: 21 additions & 0 deletions libs/config/src/getConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { pick } from '@lsk4/algos';
import { Err } from '@lsk4/err';
import { ConfigService as NestConfigService } from '@nestjs/config';

// ConfigService
import { ConfigModule } from './ConfigModule';
import { PropsFn } from './types';

export const getConfig = (path: string, fields?: string[] | PropsFn) => ({
imports: [ConfigModule],
useFactory: (configService: NestConfigService) => {
const res = configService.get(path);
if (!res) throw new Err(`!config: ${path}`, { path });
if (Array.isArray(fields)) return pick(res, fields);
if (typeof fields === 'function') return fields(res);
return res;
},
inject: [NestConfigService],
});

export default getConfig;
10 changes: 9 additions & 1 deletion libs/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
/* eslint-disable no-unreachable-loop */
// https://docs.nestjs.com/techniques/configuration

// export * from './config';
export * from './ConfigService';
export * from './ConfigModule';
export * from './getConfig';
export * from './getEnvConfig';
export * from './config';
export * from './loadConfigEnvs';
export * from './types';
19 changes: 19 additions & 0 deletions libs/config/src/loadConfigEnvs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getEnvConfig } from './getEnvConfig';
import { PropsFn } from './types';

const get = (obj: any, key: string) => (key ? obj[key] : obj);

export const loadConfigEnvs = (path: string | string[], keyOrFn?: string | PropsFn) => ({
load: [
() => {
const config = getEnvConfig(path);
if (typeof keyOrFn === 'function') return keyOrFn(config);
if (!keyOrFn) return config;
return get(config, keyOrFn);
},
],
isGlobal: true,
expandVariables: true,
});

export default loadConfigEnvs;
1 change: 1 addition & 0 deletions libs/config/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PropsFn = (res: Record<string, unknown>) => Record<string, unknown>;
4 changes: 2 additions & 2 deletions libs/upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
"@aws-sdk/client-s3": "^3.490.0",
"@aws-sdk/s3-request-presigner": "^3.490.0",
"@lsk4/err": "^4.5.0",
"@nestlib/config": "workspace:^",
"aws-sdk": "^2.1535.0",
"fishbird": "^1.1.8"
},
"devDependencies": {
"@nestjs/config": "^3.0.0",
"@nestjs/common": "^10.3.0",
"nestjs-s3": "^2.0.1"
},
"peerDependencies": {
"@nestjs/common": "*",
"@nestjs/config": "*",
"nestjs-s3": "*"
},
"//": "///////////========================/////////========================/////////========================//////////",
Expand Down
2 changes: 1 addition & 1 deletion libs/upload/src/UploadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Err } from '@lsk4/err';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ConfigService } from '@nestlib/config';
import { S3 } from 'aws-sdk';
import { map } from 'fishbird';
import { InjectS3 } from 'nestjs-s3';
Expand Down
65 changes: 17 additions & 48 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af63c18

Please sign in to comment.