forked from nestjs/config
-
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.
feat(): add support for custom parsers
This commit introduces a new configuration property named parser in ConfigModule, enabling the injection of custom parser functions. If not specified, the default implementation, backed by dotenv, is utilized. closes nestjs#1444
- Loading branch information
Showing
10 changed files
with
95 additions
and
15 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
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 @@ | ||
export type Parser = (buffer: Buffer) => Record<string, any>; |
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,4 @@ | ||
import * as dotenv from 'dotenv'; | ||
import { Parser } from '../types'; | ||
|
||
export const getDefaultParser = (): Parser => dotenv.parse; |
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,3 @@ | ||
export * from './register-as.util'; | ||
export * from './get-config-token.util'; | ||
export * from './get-default-parser.util'; |
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 @@ | ||
eyAibG9yZW0iOiAiaXBzdW0iIH0= |
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,57 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { ConfigModule } from '../../lib'; | ||
import { AppModule } from '../src/app.module'; | ||
import { join } from 'path'; | ||
|
||
describe('Custom parser', () => { | ||
let app: INestApplication; | ||
|
||
it(`should use dotenv parser by default`, async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [AppModule.withEnvVars()], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
await ConfigModule.envVariablesLoaded; | ||
|
||
const envVars = app.get(AppModule).getEnvVariables(); | ||
expect(envVars.PORT).toEqual('4000'); | ||
}); | ||
|
||
it(`should use custom parser when provided`, async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
{ | ||
module: AppModule, | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: join( | ||
process.cwd(), | ||
'tests', | ||
'e2e', | ||
'.custom-config', | ||
), | ||
parser: buffer => | ||
JSON.parse( | ||
Buffer.from(buffer.toString('utf-8'), 'base64').toString( | ||
'utf-8', | ||
), | ||
), | ||
}), | ||
], | ||
}, | ||
], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
const envVars = app.get(AppModule).getEnvVariables(); | ||
expect(envVars.lorem).toEqual('ipsum'); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
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