@bool-ts/core 1.0.0 → 1.0.1
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/dist/decorators/module.d.ts +2 -0
- package/dist/hooks/factory.js +26 -0
- package/package.json +1 -1
- package/src/decorators/module.ts +2 -0
- package/src/hooks/factory.ts +33 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export type TModuleOptions = Partial<{
|
|
2
2
|
controllers: Array<new (...args: any[]) => unknown>;
|
|
3
3
|
dependencies: Array<new (...args: any[]) => unknown>;
|
|
4
|
+
allowOrigins: string | Array<string>;
|
|
5
|
+
allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
4
6
|
}> | undefined;
|
|
5
7
|
export declare const moduleKey = "__bool:module__";
|
|
6
8
|
export declare const Module: (args?: TModuleOptions) => <T extends new (...args: any[]) => {}>(target: T, context?: ClassDecoratorContext) => T;
|
package/dist/hooks/factory.js
CHANGED
|
@@ -113,6 +113,32 @@ export const BoolFactory = (target) => {
|
|
|
113
113
|
const convertedTime = `${Math.round((time + Number.EPSILON) * 10 ** 2) / 10 ** 2}ms`.yellow;
|
|
114
114
|
console.info(`PID: ${convertedPID} - Method: ${convertedMethod} - IP: ${convertedReqIp} - ${req.originalUrl.blue} - Time: ${convertedTime}`);
|
|
115
115
|
}));
|
|
116
|
+
const allowOrigins = !metadata?.allowOrigins ?
|
|
117
|
+
["*"] : typeof metadata.allowOrigins !== "string" ?
|
|
118
|
+
metadata.allowOrigins : [metadata.allowOrigins];
|
|
119
|
+
const allowMethods = !metadata?.allowMethods ?
|
|
120
|
+
["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] : metadata.allowMethods;
|
|
121
|
+
app.use((req, res, next) => {
|
|
122
|
+
if (!req.headers.origin) {
|
|
123
|
+
return res.status(403).json({
|
|
124
|
+
["httpCode"]: 403,
|
|
125
|
+
["data"]: "CORS Origin - Not found."
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (!allowOrigins.includes("*")) {
|
|
129
|
+
if (!allowOrigins.includes(req.headers.origin)) {
|
|
130
|
+
return res.status(403).json({
|
|
131
|
+
["httpCode"]: 403,
|
|
132
|
+
["data"]: "Invalid origin."
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
res.header("Access-Control-Allow-Origin", req.headers.origin);
|
|
137
|
+
res.header("Access-Control-Allow-Headers", "*");
|
|
138
|
+
res.header("Access-Control-Allow-Credentials", "true");
|
|
139
|
+
res.header("Access-Control-Allow-Methods", allowMethods.join(", "));
|
|
140
|
+
next();
|
|
141
|
+
});
|
|
116
142
|
app.use(routers);
|
|
117
143
|
return app;
|
|
118
144
|
};
|
package/package.json
CHANGED
package/src/decorators/module.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export type TModuleOptions = Partial<{
|
|
2
2
|
controllers: Array<new (...args: any[]) => unknown>;
|
|
3
3
|
dependencies: Array<new (...args: any[]) => unknown>;
|
|
4
|
+
allowOrigins: string | Array<string>;
|
|
5
|
+
allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
4
6
|
}> | undefined;
|
|
5
7
|
|
|
6
8
|
export const moduleKey = "__bool:module__";
|
package/src/hooks/factory.ts
CHANGED
|
@@ -2,7 +2,7 @@ import "reflect-metadata";
|
|
|
2
2
|
import "colors";
|
|
3
3
|
|
|
4
4
|
import * as Qs from "qs";
|
|
5
|
-
import * as
|
|
5
|
+
import * as ResponseTime from "response-time";
|
|
6
6
|
|
|
7
7
|
import { type IControllerRoute, type TModuleOptions, controllerKey, controllerRoutesKey, moduleKey } from "../decorators";
|
|
8
8
|
import { default as ExpressApp, Router, json, urlencoded, Request, Response, NextFunction, Errback } from "express";
|
|
@@ -143,6 +143,38 @@ export const BoolFactory = (
|
|
|
143
143
|
})
|
|
144
144
|
);
|
|
145
145
|
|
|
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
|
+
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
154
|
+
if (!req.headers.origin) {
|
|
155
|
+
return res.status(403).json({
|
|
156
|
+
["httpCode"]: 403,
|
|
157
|
+
["data"]: "CORS Origin - Not found."
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!allowOrigins.includes("*")) {
|
|
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);
|
|
171
|
+
res.header("Access-Control-Allow-Headers", "*");
|
|
172
|
+
res.header("Access-Control-Allow-Credentials", "true");
|
|
173
|
+
res.header("Access-Control-Allow-Methods", allowMethods.join(", "));
|
|
174
|
+
|
|
175
|
+
next();
|
|
176
|
+
});
|
|
177
|
+
|
|
146
178
|
app.use(routers);
|
|
147
179
|
|
|
148
180
|
return app;
|