@bool-ts/core 1.0.4 → 1.0.6
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/__test/module.ts +2 -2
- package/dist/hooks/injector.js +5 -6
- package/package.json +1 -1
- package/src/hooks/factory.ts +8 -19
- package/src/hooks/injector.ts +7 -8
package/__test/module.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Module } from "../src";
|
|
2
2
|
import { TestController } from "./controller";
|
|
3
|
-
import { TestRepository } from "./repository";
|
|
3
|
+
// import { TestRepository } from "./repository";
|
|
4
4
|
import { TestService } from "./service";
|
|
5
5
|
|
|
6
6
|
@Module({
|
|
@@ -8,7 +8,7 @@ import { TestService } from "./service";
|
|
|
8
8
|
TestController
|
|
9
9
|
],
|
|
10
10
|
dependencies: [
|
|
11
|
-
TestService,
|
|
11
|
+
// TestService,
|
|
12
12
|
// TestRepository
|
|
13
13
|
]
|
|
14
14
|
})
|
package/dist/hooks/injector.js
CHANGED
|
@@ -7,19 +7,18 @@ export const Injector = new class {
|
|
|
7
7
|
* @param constructor
|
|
8
8
|
*/
|
|
9
9
|
get(target) {
|
|
10
|
-
if (this._mapper.has(target)) {
|
|
11
|
-
return this._mapper.get(target);
|
|
10
|
+
if (this._mapper.has(target.constructor)) {
|
|
11
|
+
return this._mapper.get(target.constructor);
|
|
12
12
|
}
|
|
13
|
-
const ownMetadataKeys = Reflect.getOwnMetadataKeys(target);
|
|
13
|
+
const ownMetadataKeys = Reflect.getOwnMetadataKeys(target.constructor);
|
|
14
14
|
if (!ownMetadataKeys.includes(injectableKey)) {
|
|
15
|
-
console.error("Current metadata keys:", ownMetadataKeys, Reflect.getOwnMetadataKeys(target.constructor));
|
|
16
15
|
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
17
16
|
}
|
|
18
17
|
// Initialize dependencies injection
|
|
19
18
|
const dependencies = Reflect.getOwnMetadata("design:paramtypes", target) || [];
|
|
20
19
|
const injections = dependencies.map(dependency => Injector.get(dependency));
|
|
21
|
-
const instance =
|
|
22
|
-
this._mapper.set(target, instance);
|
|
20
|
+
const instance = target.constructor(...injections);
|
|
21
|
+
this._mapper.set(target.constructor, instance);
|
|
23
22
|
return instance;
|
|
24
23
|
}
|
|
25
24
|
};
|
package/package.json
CHANGED
package/src/hooks/factory.ts
CHANGED
|
@@ -70,6 +70,11 @@ export const BoolFactory = (
|
|
|
70
70
|
|
|
71
71
|
const metadata = Reflect.getOwnMetadata(moduleKey, target) as TModuleOptions;
|
|
72
72
|
const routers = !metadata?.controllers ? [] : metadata.controllers.map(controllerConstructor => controllerCreator(controllerConstructor));
|
|
73
|
+
const allowOrigins = !metadata?.allowOrigins ?
|
|
74
|
+
["*"] : typeof metadata.allowOrigins !== "string" ?
|
|
75
|
+
metadata.allowOrigins : [metadata.allowOrigins];
|
|
76
|
+
const allowMethods = !metadata?.allowMethods ?
|
|
77
|
+
["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] : metadata.allowMethods;
|
|
73
78
|
const app = ExpressApp();
|
|
74
79
|
const configs = Object.freeze({
|
|
75
80
|
allowLogsMethods: [
|
|
@@ -143,31 +148,15 @@ export const BoolFactory = (
|
|
|
143
148
|
})
|
|
144
149
|
);
|
|
145
150
|
|
|
146
|
-
const allowOrigins = !metadata?.allowOrigins ?
|
|
147
|
-
["*"] : typeof metadata.allowOrigins !== "string" ?
|
|
148
|
-
metadata.allowOrigins : [metadata.allowOrigins];
|
|
149
|
-
|
|
150
|
-
const allowMethods = !metadata?.allowMethods ?
|
|
151
|
-
["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] : metadata.allowMethods;
|
|
152
|
-
|
|
153
151
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
154
|
-
if (!req.headers.origin) {
|
|
152
|
+
if (!allowOrigins.includes(req.headers.origin || "*")) {
|
|
155
153
|
return res.status(403).json({
|
|
156
154
|
["httpCode"]: 403,
|
|
157
|
-
["data"]: "
|
|
155
|
+
["data"]: "Invalid origin."
|
|
158
156
|
});
|
|
159
157
|
}
|
|
160
158
|
|
|
161
|
-
|
|
162
|
-
if (!allowOrigins.includes(req.headers.origin)) {
|
|
163
|
-
return res.status(403).json({
|
|
164
|
-
["httpCode"]: 403,
|
|
165
|
-
["data"]: "Invalid origin."
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
res.header("Access-Control-Allow-Origin", req.headers.origin);
|
|
159
|
+
res.header("Access-Control-Allow-Origin", req.headers.origin || "*");
|
|
171
160
|
res.header("Access-Control-Allow-Headers", "*");
|
|
172
161
|
res.header("Access-Control-Allow-Credentials", "true");
|
|
173
162
|
res.header("Access-Control-Allow-Methods", allowMethods.join(", "));
|
package/src/hooks/injector.ts
CHANGED
|
@@ -15,26 +15,25 @@ export const Injector: IInjector = new class {
|
|
|
15
15
|
*
|
|
16
16
|
* @param constructor
|
|
17
17
|
*/
|
|
18
|
-
get<T>(
|
|
19
|
-
target:
|
|
18
|
+
get<T extends Object>(
|
|
19
|
+
target: T
|
|
20
20
|
) {
|
|
21
|
-
if (this._mapper.has(target)) {
|
|
22
|
-
return this._mapper.get(target) as T;
|
|
21
|
+
if (this._mapper.has(target.constructor)) {
|
|
22
|
+
return this._mapper.get(target.constructor) as T;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
const ownMetadataKeys = Reflect.getOwnMetadataKeys(target);
|
|
25
|
+
const ownMetadataKeys = Reflect.getOwnMetadataKeys(target.constructor);
|
|
26
26
|
|
|
27
27
|
if (!ownMetadataKeys.includes(injectableKey)) {
|
|
28
|
-
console.error("Current metadata keys:", ownMetadataKeys, Reflect.getOwnMetadataKeys((target as Function).constructor));
|
|
29
28
|
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
// Initialize dependencies injection
|
|
33
32
|
const dependencies: any[] = Reflect.getOwnMetadata("design:paramtypes", target) || [];
|
|
34
33
|
const injections: any[] = dependencies.map(dependency => Injector.get(dependency));
|
|
35
|
-
const instance =
|
|
34
|
+
const instance = target.constructor(...injections);
|
|
36
35
|
|
|
37
|
-
this._mapper.set(target, instance);
|
|
36
|
+
this._mapper.set(target.constructor, instance);
|
|
38
37
|
|
|
39
38
|
return instance;
|
|
40
39
|
}
|