-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
131 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type PropsFn = (res: Record<string, unknown>) => Record<string, unknown>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.