@tsed/di
Advanced tools
Comparing version
@@ -22,12 +22,14 @@ import { Store, Type } from "@tsed/core"; | ||
constructor(token: TokenProvider); | ||
readonly token: any; | ||
provide: TokenProvider; | ||
get token(): any; | ||
get provide(): TokenProvider; | ||
set provide(value: TokenProvider); | ||
get useClass(): Type<T>; | ||
/** | ||
* Create a new store if the given value is a class. Otherwise the value is ignored. | ||
* @param value | ||
*/ | ||
useClass: Type<T>; | ||
readonly className: string; | ||
readonly name: string; | ||
readonly store: Store; | ||
* Create a new store if the given value is a class. Otherwise the value is ignored. | ||
* @param value | ||
*/ | ||
set useClass(value: Type<T>); | ||
get className(): string; | ||
get name(): string; | ||
get store(): Store; | ||
/** | ||
@@ -42,8 +44,10 @@ * Get the scope of the provider. | ||
*/ | ||
get scope(): ProviderScope; | ||
/** | ||
* Change the scope value of the provider. | ||
* @param scope | ||
*/ | ||
scope: ProviderScope; | ||
configuration: Partial<IDIConfigurationOptions>; | ||
* Change the scope value of the provider. | ||
* @param scope | ||
*/ | ||
set scope(scope: ProviderScope); | ||
get configuration(): Partial<IDIConfigurationOptions>; | ||
set configuration(configuration: Partial<IDIConfigurationOptions>); | ||
isAsync(): boolean; | ||
@@ -50,0 +54,0 @@ clone(): Provider<any>; |
@@ -26,3 +26,3 @@ "use strict"; | ||
return (provide) => { | ||
ProviderRegistry_1.registerProvider(Object.assign({}, options, { provide })); | ||
ProviderRegistry_1.registerProvider(Object.assign(Object.assign({}, options), { provide })); | ||
}; | ||
@@ -29,0 +29,0 @@ } |
@@ -34,3 +34,3 @@ "use strict"; | ||
if (currentDependency && core_1.isClass(token)) { | ||
error.message = printDependencyInjectionError(token, Object.assign({}, currentDependency, { message: error.message })); | ||
error.message = printDependencyInjectionError(token, Object.assign(Object.assign({}, currentDependency), { message: error.message })); | ||
} | ||
@@ -37,0 +37,0 @@ throw new InjectionError(token, error); |
import { IDIConfigurationOptions } from "../interfaces/IDIConfigurationOptions"; | ||
import { ProviderScope } from "../interfaces/ProviderScope"; | ||
export declare class DIConfiguration { | ||
readonly default: Map<string, any>; | ||
protected map: Map<string, any>; | ||
[key: string]: any; | ||
constructor(initialProps?: {}); | ||
scopes: { | ||
get scopes(): { | ||
[key: string]: ProviderScope; | ||
}; | ||
set scopes(value: { | ||
[key: string]: ProviderScope; | ||
}); | ||
/** | ||
@@ -22,2 +26,3 @@ * | ||
set(propertyKey: string | Partial<IDIConfigurationOptions>, value?: any): this; | ||
setRaw(propertyKey: string, value: any): this; | ||
/** | ||
@@ -29,2 +34,3 @@ * | ||
get<T>(propertyKey: string): T; | ||
getRaw(propertyKey: string): any; | ||
merge(obj: Partial<IDIConfigurationOptions>): void; | ||
@@ -37,3 +43,2 @@ /** | ||
resolve(value: any): any; | ||
toRawObject(): IDIConfigurationOptions; | ||
} |
@@ -6,4 +6,7 @@ "use strict"; | ||
constructor(initialProps = {}) { | ||
this.default = new Map(); | ||
this.map = new Map(); | ||
this.set(Object.assign({ scopes: {} }, initialProps)); | ||
Object.entries(Object.assign({ scopes: {} }, initialProps)).forEach(([key, value]) => { | ||
this.default.set(key, value); | ||
}); | ||
return new Proxy(this, { | ||
@@ -38,3 +41,5 @@ getOwnPropertyDescriptor(target, p) { | ||
ownKeys(target) { | ||
return Reflect.ownKeys(target).concat(Array.from(target.map.keys())); | ||
return Reflect.ownKeys(target) | ||
.concat(Array.from(target.default.keys())) | ||
.concat(Array.from(target.map.keys())); | ||
} | ||
@@ -44,6 +49,6 @@ }); | ||
get scopes() { | ||
return this.map.get("scopes"); | ||
return this.getRaw("scopes"); | ||
} | ||
set scopes(value) { | ||
this.map.set("scopes", value); | ||
this.setRaw("scopes", value); | ||
} | ||
@@ -56,3 +61,5 @@ /** | ||
forEach(callbackfn, thisArg) { | ||
return this.map.forEach(callbackfn, thisArg); | ||
return new Set([...Array.from(this.default.keys()), ...Array.from(this.map.keys())]).forEach(key => { | ||
callbackfn(this.getRaw(key), key, this.map); | ||
}, thisArg); | ||
} | ||
@@ -66,3 +73,3 @@ /** | ||
if (typeof propertyKey === "string") { | ||
core_1.setValue(propertyKey, value, this.map); | ||
this.setRaw(propertyKey, value); | ||
} | ||
@@ -74,2 +81,6 @@ else { | ||
} | ||
setRaw(propertyKey, value) { | ||
core_1.setValue(propertyKey, value, this.map); | ||
return this; | ||
} | ||
/** | ||
@@ -81,4 +92,14 @@ * | ||
get(propertyKey) { | ||
return this.resolve(core_1.getValue(propertyKey, this.map)); | ||
return this.resolve(this.getRaw(propertyKey)); | ||
} | ||
getRaw(propertyKey) { | ||
if (["scopes"].includes(propertyKey)) { | ||
return Object.assign(Object.assign({}, this.default.get(propertyKey)), this.map.get(propertyKey)); | ||
} | ||
const value = core_1.getValue(propertyKey, this.map); | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
return core_1.getValue(propertyKey, this.default); | ||
} | ||
merge(obj) { | ||
@@ -89,8 +110,5 @@ Object.entries(obj).forEach(([key, value]) => { | ||
value = core_1.deepExtends(value, originalValue); | ||
if (descriptor && ["set", "map", "get"].indexOf(key) === -1) { | ||
if (descriptor && !["default", "set", "map", "get"].includes(key)) { | ||
this[key] = value; | ||
} | ||
else { | ||
this.set(key, value); | ||
} | ||
}); | ||
@@ -111,12 +129,9 @@ } | ||
if (typeof value === "string") { | ||
return value.replace(/\${([\w.]+)}/gi, (match, key) => core_1.getValue(key, this.map)); | ||
return value | ||
.replace(/\${([\w.]+)}/gi, (match, key) => core_1.getValue(key, this.map)) | ||
.replace(/<([\w.]+)>/gi, (match, key) => core_1.getValue(key, this.map)) | ||
.replace(/{{([\w.]+)}}/gi, (match, key) => core_1.getValue(key, this.map)); | ||
} | ||
return value; | ||
} | ||
toRawObject() { | ||
return Array.from(this.map.entries()).reduce((obj, [key, value]) => { | ||
obj[key] = core_1.deepClone(value); | ||
return obj; | ||
}, {}); | ||
} | ||
} | ||
@@ -123,0 +138,0 @@ exports.DIConfiguration = DIConfiguration; |
@@ -225,11 +225,10 @@ "use strict"; | ||
} | ||
const rawSettings = this.settings.toRawObject(); | ||
// @ts-ignore | ||
this.settings.map.clear(); | ||
super.forEach(provider => { | ||
if (provider.configuration) { | ||
this.settings.merge(provider.configuration); | ||
Object.entries(provider.configuration).forEach(([key, value]) => { | ||
this.settings.default.set(key, core_1.deepExtends(this.settings.default.get(key) || {}, value)); | ||
}); | ||
} | ||
}); | ||
this.settings.merge(rawSettings); | ||
this.resolvedConfiguration = true; | ||
@@ -362,3 +361,3 @@ } | ||
} | ||
return interceptor.intercept(Object.assign({}, context, { options }), next); | ||
return interceptor.intercept(Object.assign(Object.assign({}, context), { options }), next); | ||
}; | ||
@@ -365,0 +364,0 @@ } |
{ | ||
"name": "@tsed/di", | ||
"version": "5.33.0", | ||
"version": "5.33.2", | ||
"description": "DI module for Ts.ED Framework", | ||
@@ -11,3 +11,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@tsed/core": "5.33.0" | ||
"@tsed/core": "5.33.2" | ||
}, | ||
@@ -28,3 +28,3 @@ "devDependencies": {}, | ||
"license": "MIT", | ||
"gitHead": "815107850c07cb70cf82409f1efc555b56962b53" | ||
"gitHead": "bd9007c87ca3379a730cb6336d644b2e2c9c928a" | ||
} |
@@ -1,2 +0,2 @@ | ||
import {deepClone, deepExtends, getValue, setValue} from "@tsed/core"; | ||
import {deepExtends, getValue, setValue} from "@tsed/core"; | ||
import {IDIConfigurationOptions} from "../interfaces/IDIConfigurationOptions"; | ||
@@ -6,2 +6,3 @@ import {ProviderScope} from "../interfaces/ProviderScope"; | ||
export class DIConfiguration { | ||
readonly default: Map<string, any> = new Map(); | ||
protected map: Map<string, any> = new Map(); | ||
@@ -12,5 +13,7 @@ | ||
constructor(initialProps = {}) { | ||
this.set({ | ||
Object.entries({ | ||
scopes: {}, | ||
...initialProps | ||
}).forEach(([key, value]) => { | ||
this.default.set(key, value); | ||
}); | ||
@@ -56,3 +59,5 @@ | ||
ownKeys(target: DIConfiguration): PropertyKey[] { | ||
return Reflect.ownKeys(target).concat(Array.from(target.map.keys())); | ||
return Reflect.ownKeys(target) | ||
.concat(Array.from(target.default.keys())) | ||
.concat(Array.from(target.map.keys())); | ||
} | ||
@@ -63,7 +68,7 @@ }); | ||
get scopes(): {[key: string]: ProviderScope} { | ||
return this.map.get("scopes"); | ||
return this.getRaw("scopes"); | ||
} | ||
set scopes(value: {[key: string]: ProviderScope}) { | ||
this.map.set("scopes", value); | ||
this.setRaw("scopes", value); | ||
} | ||
@@ -77,3 +82,5 @@ | ||
forEach(callbackfn: (value: any, index: string, map: Map<string, any>) => void, thisArg?: any) { | ||
return this.map.forEach(callbackfn, thisArg); | ||
return new Set([...Array.from(this.default.keys()), ...Array.from(this.map.keys())]).forEach(key => { | ||
callbackfn(this.getRaw(key), key, this.map); | ||
}, thisArg); | ||
} | ||
@@ -88,3 +95,3 @@ | ||
if (typeof propertyKey === "string") { | ||
setValue(propertyKey, value, this.map); | ||
this.setRaw(propertyKey, value); | ||
} else { | ||
@@ -97,2 +104,8 @@ Object.assign(this, propertyKey); | ||
setRaw(propertyKey: string, value: any) { | ||
setValue(propertyKey, value, this.map); | ||
return this; | ||
} | ||
/** | ||
@@ -104,5 +117,22 @@ * | ||
get<T>(propertyKey: string): T { | ||
return this.resolve(getValue(propertyKey, this.map)); | ||
return this.resolve(this.getRaw(propertyKey)); | ||
} | ||
getRaw(propertyKey: string): any { | ||
if (["scopes"].includes(propertyKey)) { | ||
return { | ||
...this.default.get(propertyKey), | ||
...this.map.get(propertyKey) | ||
}; | ||
} | ||
const value = getValue(propertyKey, this.map); | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
return getValue(propertyKey, this.default); | ||
} | ||
merge(obj: Partial<IDIConfigurationOptions>) { | ||
@@ -114,6 +144,4 @@ Object.entries(obj).forEach(([key, value]) => { | ||
if (descriptor && ["set", "map", "get"].indexOf(key) === -1) { | ||
if (descriptor && !["default", "set", "map", "get"].includes(key)) { | ||
this[key] = value; | ||
} else { | ||
this.set(key, value); | ||
} | ||
@@ -138,3 +166,6 @@ }); | ||
if (typeof value === "string") { | ||
return value.replace(/\${([\w.]+)}/gi, (match, key) => getValue(key, this.map)); | ||
return value | ||
.replace(/\${([\w.]+)}/gi, (match, key) => getValue(key, this.map)) | ||
.replace(/<([\w.]+)>/gi, (match, key) => getValue(key, this.map)) | ||
.replace(/{{([\w.]+)}}/gi, (match, key) => getValue(key, this.map)); | ||
} | ||
@@ -144,10 +175,2 @@ | ||
} | ||
toRawObject(): IDIConfigurationOptions { | ||
return Array.from(this.map.entries()).reduce((obj: any, [key, value]) => { | ||
obj[key] = deepClone(value); | ||
return obj; | ||
}, {}); | ||
} | ||
} |
@@ -1,2 +0,13 @@ | ||
import {deepClone, getClass, getClassOrSymbol, isFunction, isInheritedFrom, Metadata, nameOf, prototypeOf, Store} from "@tsed/core"; | ||
import { | ||
deepClone, | ||
deepExtends, | ||
getClass, | ||
getClassOrSymbol, | ||
isFunction, | ||
isInheritedFrom, | ||
Metadata, | ||
nameOf, | ||
prototypeOf, | ||
Store | ||
} from "@tsed/core"; | ||
@@ -278,15 +289,11 @@ import * as util from "util"; | ||
const rawSettings = this.settings.toRawObject(); | ||
// @ts-ignore | ||
this.settings.map.clear(); | ||
super.forEach(provider => { | ||
if (provider.configuration) { | ||
this.settings.merge(provider.configuration); | ||
Object.entries(provider.configuration).forEach(([key, value]) => { | ||
this.settings.default.set(key, deepExtends(this.settings.default.get(key) || {}, value)); | ||
}); | ||
} | ||
}); | ||
this.settings.merge(rawSettings); | ||
this.resolvedConfiguration = true; | ||
@@ -293,0 +300,0 @@ } |
@@ -14,3 +14,5 @@ import {expect} from "chai"; | ||
}, | ||
shouldResolved: "${scopes.value}" | ||
shouldResolved: "${scopes.value}", | ||
shouldResolved2: "<scopes.value>", | ||
shouldResolved3: "{{scopes.value}}" | ||
}); | ||
@@ -38,2 +40,10 @@ // WHEN | ||
"${scopes.value}" | ||
], | ||
[ | ||
"shouldResolved2", | ||
"<scopes.value>" | ||
], | ||
[ | ||
"shouldResolved3", | ||
"{{scopes.value}}" | ||
] | ||
@@ -43,2 +53,4 @@ ]); | ||
configuration.get<any>("shouldResolved")!.should.eq("singleton"); | ||
configuration.get<any>("shouldResolved2")!.should.eq("singleton"); | ||
configuration.get<any>("shouldResolved3")!.should.eq("singleton"); | ||
configuration.shouldResolved.should.eq("singleton"); | ||
@@ -61,3 +73,3 @@ }); | ||
configuration.test = "test"; | ||
expect(Reflect.ownKeys(configuration)).to.deep.eq(["map", "scopes", "test"]); | ||
expect(Reflect.ownKeys(configuration)).to.deep.eq(["default", "map", "scopes", "test"]); | ||
}); | ||
@@ -64,0 +76,0 @@ |
@@ -801,2 +801,6 @@ import {Store} from "@tsed/core"; | ||
expect(injector.settings.get("scopes")).to.deep.eq({ | ||
[ProviderType.VALUE]: ProviderScope.SINGLETON | ||
}); | ||
injector.add(Symbol.for("TOKEN1"), { | ||
@@ -803,0 +807,0 @@ configuration: { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
449443
21.99%259
17.73%6163
1.1%