@ehildt/nestjs-config-factory 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +82 -0
- package/dist/cache-return-value/index.d.ts +5 -0
- package/dist/cache-return-value/index.mjs +66 -0
- package/dist/config-factory/index.d.ts +12 -0
- package/dist/config-factory/index.mjs +27 -0
- package/dist/validate-return-value/index.d.ts +8 -0
- package/dist/validate-return-value/index.mjs +51 -0
- package/package.json +85 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eugen Hildt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# @ehildt/nestjs-config-factory
|
|
4
|
+
|
|
5
|
+
A NestJS configuration library providing decorators and modules for caching return values and validating configurations.
|
|
6
|
+
|
|
7
|
+
`This library is **ESM-only** and does not support CommonJS.`
|
|
8
|
+
`Your project must use ES modules.`
|
|
9
|
+
|
|
10
|
+
</div>
|
|
11
|
+
|
|
12
|
+
<br>
|
|
13
|
+
|
|
14
|
+
<!-- DEPBADGE:START -->
|
|
15
|
+
<div align="center">
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# StatusBadges
|
|
20
|
+
|
|
21
|
+

|
|
22
|
+

|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
<br>
|
|
28
|
+
|
|
29
|
+
<div align="center">
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# DevDependencies
|
|
34
|
+
|
|
35
|
+

|
|
36
|
+

|
|
37
|
+

|
|
38
|
+

|
|
39
|
+

|
|
40
|
+

|
|
41
|
+

|
|
42
|
+

|
|
43
|
+

|
|
44
|
+

|
|
45
|
+

|
|
46
|
+

|
|
47
|
+

|
|
48
|
+

|
|
49
|
+

|
|
50
|
+

|
|
51
|
+

|
|
52
|
+

|
|
53
|
+

|
|
54
|
+

|
|
55
|
+

|
|
56
|
+

|
|
57
|
+
|
|
58
|
+
</div>
|
|
59
|
+
|
|
60
|
+
<br>
|
|
61
|
+
|
|
62
|
+
<div align="center">
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# PeerDependencies
|
|
67
|
+
|
|
68
|
+
[](https://github.com/ehildt/ckir-helpers)
|
|
69
|
+
[](https://github.com/nestjs/nest)
|
|
70
|
+
[](https://github.com/sideway/joi)
|
|
71
|
+
|
|
72
|
+
</div>
|
|
73
|
+
<!-- DEPBADGE:END -->
|
|
74
|
+
|
|
75
|
+
<br>
|
|
76
|
+
|
|
77
|
+
<div align="center">
|
|
78
|
+
|
|
79
|
+
[E-MAIL](mailto:eugen.hildt@gmail.com) — [WIKI](https://github.com/ehildt/nestjs-config-factory/wiki) — [ISSUES](https://github.com/ehildt/nestjs-config-factory/issues) — [DONATE](https://github.com/sponsors/ehildt)
|
|
80
|
+
|
|
81
|
+
</div>
|
|
82
|
+
<br>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { hashPayload } from '@ehildt/ckir-helpers/hash-payload';
|
|
2
|
+
|
|
3
|
+
// src/cache-return-value/cache-return-value.decorator.ts
|
|
4
|
+
|
|
5
|
+
// src/validate-return-value/validate-return-value.decorator.ts
|
|
6
|
+
var ValidateReturnValueError = class extends Error {
|
|
7
|
+
constructor(message, cause) {
|
|
8
|
+
super(message, { cause });
|
|
9
|
+
this.name = new.target.name;
|
|
10
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/cache-return-value/cache-return-value.decorator.ts
|
|
15
|
+
function CacheReturnValue(schema) {
|
|
16
|
+
return function(_target, propertyKey, descriptor) {
|
|
17
|
+
if (descriptor?.get) {
|
|
18
|
+
const originalGet = descriptor.get;
|
|
19
|
+
descriptor.get = function() {
|
|
20
|
+
const cache = getCacheFor(this);
|
|
21
|
+
if (!cache.has(String(propertyKey))) {
|
|
22
|
+
let value = originalGet.call(this);
|
|
23
|
+
if (schema) {
|
|
24
|
+
const { error, value: validated } = schema.validate(value, {
|
|
25
|
+
abortEarly: false
|
|
26
|
+
});
|
|
27
|
+
if (error) throw new ValidateReturnValueError(`Schema violation`, error.details);
|
|
28
|
+
value = validated;
|
|
29
|
+
}
|
|
30
|
+
cache.set(String(propertyKey), value);
|
|
31
|
+
}
|
|
32
|
+
return cache.get(String(propertyKey));
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (descriptor?.value instanceof Function) {
|
|
36
|
+
const originalMethod = descriptor.value;
|
|
37
|
+
descriptor.value = function(...args) {
|
|
38
|
+
const cache = getCacheFor(this);
|
|
39
|
+
const key = hashPayload(`${String(propertyKey)}:${JSON.stringify(args)}`);
|
|
40
|
+
if (!cache.has(key)) {
|
|
41
|
+
let result = originalMethod.apply(this, args);
|
|
42
|
+
if (schema) {
|
|
43
|
+
const { error, value: validated } = schema.validate(result, {
|
|
44
|
+
abortEarly: false
|
|
45
|
+
});
|
|
46
|
+
if (error) throw new ValidateReturnValueError(`Schema violation`, error.details);
|
|
47
|
+
result = validated;
|
|
48
|
+
}
|
|
49
|
+
cache.set(key, result);
|
|
50
|
+
}
|
|
51
|
+
return cache.get(key);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
var INSTANCE_CACHE = /* @__PURE__ */ new WeakMap();
|
|
57
|
+
function getCacheFor(instance) {
|
|
58
|
+
let cache = INSTANCE_CACHE.get(instance);
|
|
59
|
+
if (!cache) {
|
|
60
|
+
cache = /* @__PURE__ */ new Map();
|
|
61
|
+
INSTANCE_CACHE.set(instance, cache);
|
|
62
|
+
}
|
|
63
|
+
return cache;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { CacheReturnValue };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Provider, DynamicModule } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
interface ConfigFactoryOptions {
|
|
4
|
+
global?: boolean;
|
|
5
|
+
providers?: Array<Provider>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare class ConfigFactoryModule {
|
|
9
|
+
static forRoot({ global, providers }?: ConfigFactoryOptions): DynamicModule;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { ConfigFactoryModule, type ConfigFactoryOptions };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
5
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
6
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
7
|
+
if (decorator = decorators[i])
|
|
8
|
+
result = (decorator(result)) || result;
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
var ConfigFactoryModule = class {
|
|
12
|
+
static forRoot({ global = false, providers = [] } = {}) {
|
|
13
|
+
if (!providers.length)
|
|
14
|
+
console.warn("[ConfigFactoryModule] No providers registered.");
|
|
15
|
+
return {
|
|
16
|
+
global,
|
|
17
|
+
module: ConfigFactoryModule,
|
|
18
|
+
providers,
|
|
19
|
+
exports: providers
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
ConfigFactoryModule = __decorateClass([
|
|
24
|
+
Module({})
|
|
25
|
+
], ConfigFactoryModule);
|
|
26
|
+
|
|
27
|
+
export { ConfigFactoryModule };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
|
|
3
|
+
declare function ValidateReturnValue<T>(schema: Joi.Schema<T>): (target: any, propertyKey: string, descriptor?: PropertyDescriptor) => void;
|
|
4
|
+
declare class ValidateReturnValueError extends Error {
|
|
5
|
+
constructor(message?: string, cause?: unknown);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export { ValidateReturnValue, ValidateReturnValueError };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/validate-return-value/validate-return-value.decorator.ts
|
|
2
|
+
function ValidateReturnValue(schema) {
|
|
3
|
+
return function(target, propertyKey, descriptor) {
|
|
4
|
+
if (descriptor?.get) {
|
|
5
|
+
const originalGet = descriptor.get;
|
|
6
|
+
descriptor.get = function() {
|
|
7
|
+
const value = originalGet.call(this);
|
|
8
|
+
const { error, value: validated } = schema.validate(value, {
|
|
9
|
+
abortEarly: false
|
|
10
|
+
});
|
|
11
|
+
if (!error) return validated;
|
|
12
|
+
throw new ValidateReturnValueError(`Schema violation`, error.details);
|
|
13
|
+
};
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (descriptor?.value instanceof Function) {
|
|
17
|
+
const originalMethod = descriptor.value;
|
|
18
|
+
descriptor.value = function(...args) {
|
|
19
|
+
const result = originalMethod.apply(this, args);
|
|
20
|
+
const { error, value: validated } = schema.validate(result, {
|
|
21
|
+
abortEarly: false
|
|
22
|
+
});
|
|
23
|
+
if (!error) return validated;
|
|
24
|
+
throw new ValidateReturnValueError(`Schema violation`, error.details);
|
|
25
|
+
};
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const privateKey = Symbol(propertyKey);
|
|
29
|
+
Object.defineProperty(target, propertyKey, {
|
|
30
|
+
configurable: true,
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function() {
|
|
33
|
+
const value = this[privateKey];
|
|
34
|
+
const { error, value: validated } = schema.validate(value, {
|
|
35
|
+
abortEarly: false
|
|
36
|
+
});
|
|
37
|
+
if (!error) return validated;
|
|
38
|
+
throw new ValidateReturnValueError(`Schema violation`, error.details);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
var ValidateReturnValueError = class extends Error {
|
|
44
|
+
constructor(message, cause) {
|
|
45
|
+
super(message, { cause });
|
|
46
|
+
this.name = new.target.name;
|
|
47
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { ValidateReturnValue, ValidateReturnValueError };
|
package/package.json
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ehildt/nestjs-config-factory",
|
|
3
|
+
"description": "A dynamic NestJS module factory designed to register, manage, and expose custom configuration providers throughout the application.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": null,
|
|
9
|
+
"./cache-return-value": {
|
|
10
|
+
"types": "./dist/cache-return-value/index.d.ts",
|
|
11
|
+
"import": "./dist/cache-return-value/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"./config-factory": {
|
|
14
|
+
"types": "./dist/config-factory/index.d.ts",
|
|
15
|
+
"import": "./dist/config-factory/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"./validate-return-value": {
|
|
18
|
+
"types": "./dist/validate-return-value/index.d.ts",
|
|
19
|
+
"import": "./dist/validate-return-value/index.mjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"main": "dist/index.mjs",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/ehildt/nestjs-config-factory.git"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"author": {
|
|
34
|
+
"email": "eugen.hildt@gmail.com",
|
|
35
|
+
"name": "Eugen Hildt"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"email": "eugen.hildt@gmail.com"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@changesets/cli": "^2.30.0",
|
|
42
|
+
"@eslint/js": "^10.0.1",
|
|
43
|
+
"@types/eslint": "^9.6.1",
|
|
44
|
+
"@types/node": "^25.5.0",
|
|
45
|
+
"@vitest/coverage-v8": "4.1.0",
|
|
46
|
+
"depcheck": "^1.4.7",
|
|
47
|
+
"dependency-cruiser": "^17.3.9",
|
|
48
|
+
"eslint": "^10.1.0",
|
|
49
|
+
"eslint-config-prettier": "^10.1.8",
|
|
50
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
51
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
52
|
+
"eslint-plugin-sonarjs": "^4.0.2",
|
|
53
|
+
"globals": "^17.4.0",
|
|
54
|
+
"husky": "^9.1.7",
|
|
55
|
+
"jiti": "^2.6.1",
|
|
56
|
+
"lint-staged": "^16.4.0",
|
|
57
|
+
"npm-check-updates": "^19.6.5",
|
|
58
|
+
"rimraf": "^6.1.3",
|
|
59
|
+
"ts-unused-exports": "^11.0.1",
|
|
60
|
+
"tsup": "^8.5.1",
|
|
61
|
+
"typescript-eslint": "^8.57.1",
|
|
62
|
+
"vitest": "^4.1.0"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"@ehildt/ckir-helpers": "^1.2.2",
|
|
66
|
+
"@nestjs/common": "^11.1.17",
|
|
67
|
+
"joi": "^18.0.2"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "pnpm tsup",
|
|
71
|
+
"depcheck": "npx depcheck .",
|
|
72
|
+
"depcruise": "npx dependency-cruiser -c .depcruise.mjs --ts-config tsconfig.json src",
|
|
73
|
+
"format": "prettier --write .",
|
|
74
|
+
"lint": "eslint ./src",
|
|
75
|
+
"lint-staged": "npx lint-staged --allow-empty",
|
|
76
|
+
"ncu:update": "npx npm-check-updates -u --format group",
|
|
77
|
+
"ncu:interactive": "npx npm-check-updates -u --interactive --format group",
|
|
78
|
+
"ncu:validate": "npx npm-check-updates -e 2 --packageFile=./package.json",
|
|
79
|
+
"prebuild": "rimraf dist",
|
|
80
|
+
"test": "vitest run",
|
|
81
|
+
"test:watch": "vitest",
|
|
82
|
+
"test:cov": "vitest run --coverage",
|
|
83
|
+
"lint:unused": "npx ts-unused-exports ./tsconfig.exclude.json --maxIssues=6"
|
|
84
|
+
}
|
|
85
|
+
}
|