Skip to content
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Commit 4657555

Browse files
author
roman.vasilev
committed
New: First release
1 parent f6a0999 commit 4657555

File tree

8 files changed

+183
-16
lines changed

8 files changed

+183
-16
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# nest-config
2+
Configuration module for Nest.js (wraps [convict](https://github.com/mozilla/node-convict) module).
3+
4+
INSTALL
5+
---
6+
```
7+
npm i -S nest-config
8+
```
9+
10+
USAGE
11+
---
12+
```ts
13+
import { ConfigModule } from 'nest-config';
14+
import { Module } from '@nestjs/common';
15+
16+
@Module({
17+
imports: [ConfigModule.forRoot(schema: convict.Schema<T> | string)],
18+
})
19+
export class AppModule {
20+
}
21+
22+
```
23+
24+
```ts
25+
export class UserController {
26+
27+
constructor (
28+
private config: ConfigService
29+
) { }
30+
31+
hello() {
32+
const foo = this.config.get<string>('foo');
33+
}
34+
}
35+
```

package.json

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,23 @@
33
"version": "0.0.0-dev",
44
"description": "Nest config module",
55
"main": "dist/index.js",
6-
"scripts": {},
6+
"typings": "src/index.ts",
7+
"engines": {
8+
"node": "^8 || ^9 || ^10"
9+
},
10+
"scripts": {
11+
"_setup": "npm i -g commitizen semantic-release-cli",
12+
"mocha": "node -r ts-node/register/transpile-only node_modules/mocha/bin/_mocha",
13+
"test:w": "npm run mocha -- --watch-extensions ts --watch src/**/*.spec.ts",
14+
"test": "npm run test:r",
15+
"test:r": "npm run mocha -- src/*.spec.ts",
16+
"semantic-release": "node -r ts-node/register/transpile-only node_modules/semantic-release/bin/semantic-release.js",
17+
"prepublishOnly": "npm run build",
18+
"build": "tsc",
19+
"prebuild": "npm run clean",
20+
"clean": "rimraf dist",
21+
"commit": "git-cz"
22+
},
723
"repository": {
824
"type": "git",
925
"url": "git+https://github.com/unlight/nest-config.git"
@@ -19,14 +35,63 @@
1935
},
2036
"homepage": "https://github.com/unlight/nest-config#readme",
2137
"peerDependencies": {
22-
"@nestjs/common": ">=5.0.0 <6"
38+
"@nestjs/common": "^5"
2339
},
2440
"devDependencies": {
25-
"reflect-metadata": "^0.1.12",
41+
"@nestjs/common": "^5",
42+
"@nestjs/core": "^5",
43+
"@nestjs/testing": "^5.0.1",
44+
"@semantic-release/changelog": "^2.0.2",
45+
"@semantic-release/git": "^5.0.0",
46+
"@semantic-release/npm": "^3.3.0",
2647
"@types/convict": "^4.2.0",
27-
"rxjs": "^6.0.0"
48+
"@types/mocha": "^5.2.0",
49+
"@types/node": "^10.1.2",
50+
"cz-adapter-eslint": "^0.1.2",
51+
"mocha": "^5.2.0",
52+
"reflect-metadata": "^0.1.12",
53+
"rimraf": "^2.6.2",
54+
"rxjs": "^6.2.0",
55+
"semantic-release": "^15.5.0",
56+
"ts-node": "^6.0.5",
57+
"typescript": "^2.8.3"
2858
},
2959
"dependencies": {
30-
"convict": "^4.2.0"
60+
"convict": "^4.2.0",
61+
"tslib": "^1.9.1"
62+
},
63+
"release": {
64+
"generateNotes": {
65+
"preset": "eslint"
66+
},
67+
"analyzeCommits": {
68+
"preset": "eslint"
69+
},
70+
"verifyConditions": [
71+
"@semantic-release/changelog",
72+
"@semantic-release/github",
73+
"@semantic-release/npm",
74+
"@semantic-release/git"
75+
],
76+
"prepare": [
77+
"@semantic-release/changelog",
78+
"@semantic-release/npm",
79+
"@semantic-release/git"
80+
],
81+
"publish": [
82+
"@semantic-release/npm",
83+
"@semantic-release/github"
84+
],
85+
"success": [
86+
"@semantic-release/github"
87+
],
88+
"fail": [
89+
"@semantic-release/github"
90+
]
91+
},
92+
"config": {
93+
"commitizen": {
94+
"path": "./node_modules/cz-adapter-eslint"
95+
}
3196
}
3297
}

src/config.module.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Global, Module, DynamicModule, Provider } from '@nestjs/common';
2+
import { ConfigService } from './config.service';
3+
import { Schema } from 'convict';
4+
import convict = require('convict');
5+
6+
@Global()
7+
@Module({})
8+
export class ConfigModule {
9+
static forRoot<T = any>(schema: Schema<T> | string): DynamicModule {
10+
const config = convict(schema).validate();
11+
const providers: Provider[] = [
12+
{ provide: ConfigService, useValue: new ConfigService<T>(config) },
13+
];
14+
return {
15+
module: ConfigModule,
16+
providers: providers,
17+
exports: providers,
18+
};
19+
}
20+
}

src/config.service.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ConfigService } from './config.service';
2+
import { ConfigModule } from './config.module';
3+
import { Test, TestingModule } from '@nestjs/testing';
4+
import assert = require('assert');
5+
6+
let testingModule: TestingModule;
7+
let configService: ConfigService;
8+
9+
beforeEach(async () => {
10+
testingModule = await Test.createTestingModule({
11+
imports: [ConfigModule.forRoot({
12+
a: { default: 'A' }
13+
})],
14+
}).compile();
15+
});
16+
17+
it('smoke', () => {
18+
assert(ConfigService);
19+
configService = testingModule.get(ConfigService);
20+
assert(configService instanceof ConfigService);
21+
});
22+
23+
it('should get value key a', () => {
24+
const result = configService.get<string>('a');
25+
assert(result === 'A');
26+
});
27+
28+
it('should fail if trying to get unknown key', () => {
29+
assert.throws(() => {
30+
configService.get<string>('foo');
31+
}, `cannot find configuration param 'foo'`);
32+
});

src/config.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { Injectable } from '@nestjs/common';
22
import * as convict from 'convict';
3-
import { Config } from './app.config';
43

54
@Injectable()
6-
export class ConfigService {
5+
export class ConfigService<T = any> {
76

87
constructor(
9-
private config: convict.Config<Config>
8+
private readonly config: convict.Config<T>
109
) { }
1110

12-
get<T = any>(key: string): T {
11+
get<R = any>(key: string) {
1312
return this.config.get(key);
1413
}
1514
}

src/example.app.module.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ConfigModule } from './config.module';
2+
import { Module } from '@nestjs/common';
3+
4+
@Module({
5+
imports: [ConfigModule.forRoot({})],
6+
})
7+
export class AppModule {
8+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { ConfigModule } from './config.module';
2+
export { ConfigService } from './config.service';

tsconfig.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
// "declaration": true, /* Generates corresponding '.d.ts' file. */
1111
// "sourceMap": true, /* Generates corresponding '.map' file. */
1212
// "outFile": "./", /* Concatenate and emit output to single file. */
13-
// "outDir": "./", /* Redirect output structure to the directory. */
13+
"outDir": "dist", /* Redirect output structure to the directory. */
1414
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
15-
// "removeComments": true, /* Do not emit comments to output. */
15+
"removeComments": false, /* Do not emit comments to output. */
1616
// "noEmit": true, /* Do not emit outputs. */
17-
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
17+
"importHelpers": true, /* Import emit helpers from 'tslib'. */
1818
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
1919
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
2020

@@ -41,7 +41,7 @@
4141
// "typeRoots": [], /* List of folders to include type definitions from. */
4242
// "types": [], /* Type declaration files to be included in compilation. */
4343
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
44-
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
44+
// "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
4545
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
4646

4747
/* Source Map Options */
@@ -51,7 +51,13 @@
5151
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
5252

5353
/* Experimental Options */
54-
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
55-
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
56-
}
54+
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
55+
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
56+
},
57+
"include": [
58+
"src/**/*.ts"
59+
],
60+
"exclude": [
61+
"src/**/*.spec.ts"
62+
]
5763
}

0 commit comments

Comments
 (0)