Commit f4a17cac authored by Jean-Philippe Steinmetz's avatar Jean-Philippe Steinmetz
Browse files

@Config can now take a path to a specific variable to inject
Fixing issue with ObjectFactory test
parent 6d8841e4
......@@ -105,7 +105,18 @@ export default class ObjectFactory {
// Inject @Config
const injectConfig: any = Reflect.getMetadata("axr:injectConfig", proto, member);
if (injectConfig) {
obj[member] = this.config;
// If the value is a string, then it must be a path to a specific variable desired
if (typeof injectConfig === "string") {
const value: any = this.config.get(injectConfig);
if (value) {
obj[member] = value;
} else {
throw new Error("No configuration variable is defined at path: " + injectConfig);
}
} else {
// No specific variable is desired, inject the whole config object
obj[member] = this.config;
}
}
// Inject @Logger
......
......@@ -42,16 +42,21 @@ export function Init(target: any, propertyKey: string, descriptor: PropertyDescr
}
/**
* Apply this to a property to have the global configuration object injected at instantiation.
* Apply this to a property to have a configuration variable be injected at instantiation. If no path is given, the
* global configuration object is injected.
*
* @param path The path to the configuration variable to inject.
*/
export function Config(target: any, propertyKey: string | symbol) {
Reflect.defineMetadata("axr:injectConfig", true, target, propertyKey);
const key = `__${String(propertyKey)}`;
Object.defineProperty(target, propertyKey, {
enumerable: true,
writable: true,
value: undefined,
});
export function Config(path?: string) {
return function(target: any, propertyKey: string | symbol) {
Reflect.defineMetadata("axr:injectConfig", path ? path : true, target, propertyKey);
const key = `__${String(propertyKey)}`;
Object.defineProperty(target, propertyKey, {
enumerable: true,
writable: true,
value: undefined,
});
}
}
/**
......
......@@ -13,7 +13,7 @@ import { JWTUser, UserUtils } from "@composer-js/core";
@Model(AccessControlListMongo)
@Route("/acls")
export default class ACLRouteMongo extends ModelRoute<AccessControlListMongo> {
@Config
@Config()
private config?: any;
@MongoRepository(AccessControlListMongo)
......
......@@ -13,7 +13,7 @@ import { ACLUtils } from "../service_core";
@Model(AccessControlListSQL)
@Route("/acls")
export default class ACLRouteSQL extends ModelRoute<AccessControlListSQL> {
@Config
@Config()
private config?: any;
@Repository(AccessControlListSQL)
......
......@@ -64,9 +64,9 @@ describe("ObjectFactory Tests", () => {
expect(instance).toBeInstanceOf(TestClassA);
});
it("Can initialize existing objects.", () => {
it("Can initialize existing objects.", async () => {
const instance2: TestClassC = new TestClassC();
factory.initialize(instance2);
await factory.initialize(instance2);
expect(instance2.dep).toBeDefined();
expect(instance2.dep).toBeInstanceOf(TestClassA);
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment