damex 2.0.19 → 3.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/README.md +27 -0
- package/container.d.ts +7 -0
- package/container.js +23 -0
- package/container.js.map +1 -0
- package/decorators.d.ts +1 -0
- package/decorators.js +8 -1
- package/decorators.js.map +1 -1
- package/di.d.ts +2 -0
- package/di.js +22 -2
- package/di.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,33 @@ Receives an array with all the middleware methods that will be applied to all th
|
|
|
20
20
|
|
|
21
21
|
Used on classes that required DI. If the class Already has a decorator (as @Controller), the @Inject is not required.
|
|
22
22
|
|
|
23
|
+
```typescript
|
|
24
|
+
@Implementations
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Receives an array with the concrete classes that will be injected into the constructor parameters.
|
|
28
|
+
The implementations must follow the same order as the constructor arguments.
|
|
29
|
+
If used, the @Inject decorator is not required.
|
|
30
|
+
|
|
31
|
+
See a @Implementations usage example:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
@Controller("/users")
|
|
35
|
+
@Implementations([ConsoleLogger, FileLogger])
|
|
36
|
+
class UserController {
|
|
37
|
+
constructor(
|
|
38
|
+
private readonly logger1: Logger,
|
|
39
|
+
private readonly logger2: Logger
|
|
40
|
+
) {}
|
|
41
|
+
|
|
42
|
+
@Get("/")
|
|
43
|
+
getUsers(req: Request, res: Response) {
|
|
44
|
+
this.logger1.log("fetching...");
|
|
45
|
+
this.logger2.log("done!");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
23
50
|
### Used in Methods
|
|
24
51
|
|
|
25
52
|
```typescript
|
package/container.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class Container {
|
|
2
|
+
private static implementationsMap;
|
|
3
|
+
static register(target: any, implementation: any): void;
|
|
4
|
+
static get(target: any): any;
|
|
5
|
+
static getGenerator(target: any): Generator<any, void, unknown>;
|
|
6
|
+
static hasTargetRegister(target: any): boolean;
|
|
7
|
+
}
|
package/container.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Container = void 0;
|
|
4
|
+
class Container {
|
|
5
|
+
static register(target, implementation) {
|
|
6
|
+
this.implementationsMap.set(target, implementation);
|
|
7
|
+
}
|
|
8
|
+
static get(target) {
|
|
9
|
+
return this.implementationsMap.get(target);
|
|
10
|
+
}
|
|
11
|
+
static *getGenerator(target) {
|
|
12
|
+
const implementations = this.get(target);
|
|
13
|
+
for (const implementation of implementations) {
|
|
14
|
+
yield implementation;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
static hasTargetRegister(target) {
|
|
18
|
+
return this.implementationsMap.has(target);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.Container = Container;
|
|
22
|
+
Container.implementationsMap = new Map();
|
|
23
|
+
//# sourceMappingURL=container.js.map
|
package/container.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container.js","sourceRoot":"","sources":["../src/container.ts"],"names":[],"mappings":";;;AAAA,MAAa,SAAS;IAGlB,MAAM,CAAC,QAAQ,CAAC,MAAW,EAAE,cAAmB;QAC5C,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED,MAAM,CAAC,GAAG,CAAC,MAAW;QAClB,OAAO,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,CAAC,CAAC,YAAY,CAAC,MAAW;QAC5B,MAAM,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAEzC,KAAK,MAAM,cAAc,IAAI,eAAe,EAAE,CAAC;YAC3C,MAAM,cAAc,CAAC;QACzB,CAAC;IACL,CAAC;IAED,MAAM,CAAC,iBAAiB,CAAC,MAAW;QAChC,OAAO,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC/C,CAAC;;AArBL,8BAsBC;AArBkB,4BAAkB,GAAG,IAAI,GAAG,EAAE,CAAC"}
|
package/decorators.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RequestHandler } from 'express';
|
|
2
2
|
export declare function Controller(controllerPath?: string): (target: any) => any;
|
|
3
|
+
export declare function Implementations(implementations: any[]): (target: any) => void;
|
|
3
4
|
export declare function Injectable(): (_: any) => any;
|
|
4
5
|
export declare function Middleware(handlers: RequestHandler[]): (target: any, propertyKey: any) => any;
|
|
5
6
|
export declare function ClassMiddleware(handlers: RequestHandler[]): (target: any) => void;
|
package/decorators.js
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = void 0;
|
|
4
4
|
exports.Controller = Controller;
|
|
5
|
+
exports.Implementations = Implementations;
|
|
5
6
|
exports.Injectable = Injectable;
|
|
6
7
|
exports.Middleware = Middleware;
|
|
7
8
|
exports.ClassMiddleware = ClassMiddleware;
|
|
8
9
|
const consts_1 = require("./consts");
|
|
9
10
|
const http_core_1 = require("./http-core");
|
|
10
11
|
const di_1 = require("./di");
|
|
12
|
+
const container_1 = require("./container");
|
|
11
13
|
function Controller(controllerPath) {
|
|
12
14
|
const router = http_core_1.AppRouter.get();
|
|
13
15
|
return (target) => {
|
|
@@ -28,12 +30,17 @@ function Controller(controllerPath) {
|
|
|
28
30
|
});
|
|
29
31
|
};
|
|
30
32
|
}
|
|
33
|
+
function Implementations(implementations) {
|
|
34
|
+
return (target) => {
|
|
35
|
+
container_1.Container.register(target, implementations);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
31
38
|
function Injectable() {
|
|
32
39
|
return (_) => { };
|
|
33
40
|
}
|
|
34
41
|
function Middleware(handlers) {
|
|
35
42
|
return function (target, propertyKey) {
|
|
36
|
-
Reflect.defineMetadata(
|
|
43
|
+
Reflect.defineMetadata(consts_1.CONTROLLER_MIDDLEWARE, handlers, target, propertyKey);
|
|
37
44
|
};
|
|
38
45
|
}
|
|
39
46
|
function ClassMiddleware(handlers) {
|
package/decorators.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":";;;AAWA,gCAmCC;AAED,0CAIC;AAED,gCAEC;AAED,gCASC;AAED,0CAIC;AAxED,qCAKkB;AAClB,2CAAwE;AACxE,6BAA0B;AAC1B,2CAAwC;AAExC,SAAgB,UAAU,CAAC,cAAuB;IAC9C,MAAM,MAAM,GAAG,qBAAS,CAAC,GAAG,EAAE,CAAC;IAE/B,OAAO,CAAC,MAAW,EAAO,EAAE;QACxB,MAAM,QAAQ,GAAG,OAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAChC,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAClD,MAAM,CAAC,EAAE,GAAG,YAAY,CAAC,GAAG,MAAM,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;QAElE,YAAY,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YAC5B,MAAM,sBAAsB,GAAG,IAAI,kCAAsB,CACrD,MAAM,EACN,SAAS,EACT,MAAM,CACT,CAAC;YAEF,MAAM,UAAU,GAAG,sBAAsB,CAAC,OAAO,EAAE,CAAC;YACpD,MAAM,MAAM,GAAG,sBAAsB,CAAC,SAAS,EAAE,CAAC;YAClD,MAAM,WAAW,GAAG,sBAAsB,CAAC,cAAc,EAAE,CAAC;YAC5D,MAAM,gBAAgB,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AW,CACd,CAAC;YACF,OAAO,CAAC,cAAc,CAAC,wBAAe,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACvE,CAAC,CAAC;IACN,CAAC,CAAC;AACN,CAAC;AAEY,QAAA,GAAG,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAC9B,QAAA,IAAI,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;AAChC,QAAA,GAAG,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAC9B,QAAA,KAAK,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;AAClC,QAAA,MAAM,GAAG,gBAAgB,CAAC,QAAQ,CAAC,CAAC"}
|
package/di.d.ts
CHANGED
package/di.js
CHANGED
|
@@ -1,24 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DI = void 0;
|
|
4
|
+
const container_1 = require("./container");
|
|
4
5
|
class DI {
|
|
5
6
|
static new(target) {
|
|
6
7
|
const params = DI.getParams(target);
|
|
7
8
|
if (!params) {
|
|
8
9
|
return new target();
|
|
9
10
|
}
|
|
10
|
-
const
|
|
11
|
+
const generator = container_1.Container.hasTargetRegister(target)
|
|
12
|
+
? container_1.Container.getGenerator(target)
|
|
13
|
+
: null;
|
|
14
|
+
const instantiatedParams = params.map((param) => DI.instantiateAndDIParam(param, generator, target));
|
|
11
15
|
return new target(...instantiatedParams);
|
|
12
16
|
}
|
|
13
|
-
static instantiateAndDIParam(param) {
|
|
17
|
+
static instantiateAndDIParam(param, generator, target) {
|
|
14
18
|
if (!param)
|
|
15
19
|
return;
|
|
16
20
|
const hasParams = DI.getParams(param);
|
|
21
|
+
if (DI.isInterface(param) && generator) {
|
|
22
|
+
const value = generator.next().value;
|
|
23
|
+
return DI.new(value);
|
|
24
|
+
}
|
|
25
|
+
if (DI.isInterface(param) && !generator) {
|
|
26
|
+
console.warn(`Some dependencies may have failed to inject into ${target.constructor.name}.
|
|
27
|
+
Make sure you are:
|
|
28
|
+
- Using the @Implementations decorator, if using interfaces; or
|
|
29
|
+
- Avoiding primitive types as dependency types.`);
|
|
30
|
+
}
|
|
17
31
|
if (hasParams) {
|
|
18
32
|
return DI.new(param);
|
|
19
33
|
}
|
|
20
34
|
return new param();
|
|
21
35
|
}
|
|
36
|
+
static loadTargetInterfaces(target) {
|
|
37
|
+
const interfaces = container_1.Container.getGenerator(target);
|
|
38
|
+
}
|
|
39
|
+
static isInterface(param) {
|
|
40
|
+
return (param === null || param === void 0 ? void 0 : param.constructor.name) === 'Function';
|
|
41
|
+
}
|
|
22
42
|
static getParams(target) {
|
|
23
43
|
return Reflect.getMetadata('design:paramtypes', target);
|
|
24
44
|
}
|
package/di.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"di.js","sourceRoot":"","sources":["../src/di.ts"],"names":[],"mappings":";;;AAAA,MAAa,EAAE;IACX,MAAM,CAAC,GAAG,CAAC,MAAW;QAClB,MAAM,MAAM,GAAG,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAEpC,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,OAAO,IAAI,MAAM,EAAE,CAAC;QACxB,CAAC;QAED,MAAM,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,qBAAqB,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"di.js","sourceRoot":"","sources":["../src/di.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAExC,MAAa,EAAE;IACX,MAAM,CAAC,GAAG,CAAC,MAAW;QAClB,MAAM,MAAM,GAAG,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAEpC,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,OAAO,IAAI,MAAM,EAAE,CAAC;QACxB,CAAC;QAED,MAAM,SAAS,GAAG,qBAAS,CAAC,iBAAiB,CAAC,MAAM,CAAC;YACjD,CAAC,CAAC,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