@sharangyawali/sg-app 0.0.5
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.
Potentially problematic release.
This version of @sharangyawali/sg-app might be problematic. Click here for more details.
- package/cls/cls.service.d.ts +8 -0
- package/cls/cls.service.js +33 -0
- package/cls/index.d.ts +1 -0
- package/cls/index.js +17 -0
- package/common/constants/index.d.ts +1 -0
- package/common/constants/index.js +17 -0
- package/common/constants/providers.constants.d.ts +1 -0
- package/common/constants/providers.constants.js +4 -0
- package/common/decorators/component.decorators.d.ts +2 -0
- package/common/decorators/component.decorators.js +14 -0
- package/common/decorators/controller.decorator.d.ts +2 -0
- package/common/decorators/controller.decorator.js +18 -0
- package/common/decorators/filters.decorator.d.ts +2 -0
- package/common/decorators/filters.decorator.js +19 -0
- package/common/decorators/guards.decorator.d.ts +4 -0
- package/common/decorators/guards.decorator.js +25 -0
- package/common/decorators/index.d.ts +10 -0
- package/common/decorators/index.js +26 -0
- package/common/decorators/inject.decorators.d.ts +3 -0
- package/common/decorators/inject.decorators.js +13 -0
- package/common/decorators/interceptors.decorator.d.ts +4 -0
- package/common/decorators/interceptors.decorator.js +25 -0
- package/common/decorators/middlewares.decorator.d.ts +3 -0
- package/common/decorators/middlewares.decorator.js +19 -0
- package/common/decorators/module.decorator.d.ts +2 -0
- package/common/decorators/module.decorator.js +31 -0
- package/common/decorators/parameter-validator.decorator.d.ts +5 -0
- package/common/decorators/parameter-validator.decorator.js +21 -0
- package/common/decorators/route-mapping.decorator.d.ts +5 -0
- package/common/decorators/route-mapping.decorator.js +32 -0
- package/common/enum/index.d.ts +3 -0
- package/common/enum/index.js +19 -0
- package/common/enum/request-method.enum.d.ts +11 -0
- package/common/enum/request-method.enum.js +15 -0
- package/common/enum/request-parameters.enum.d.ts +7 -0
- package/common/enum/request-parameters.enum.js +11 -0
- package/common/enum/scope.enum.d.ts +5 -0
- package/common/enum/scope.enum.js +9 -0
- package/common/http/http-context.d.ts +13 -0
- package/common/http/http-context.js +27 -0
- package/common/http/http-exception.d.ts +7 -0
- package/common/http/http-exception.js +17 -0
- package/common/http/index.d.ts +2 -0
- package/common/http/index.js +18 -0
- package/common/index.d.ts +5 -0
- package/common/index.js +21 -0
- package/common/interface/controller-method-paramenters.interface.d.ts +6 -0
- package/common/interface/controller-method-paramenters.interface.js +2 -0
- package/common/interface/controller-methods.interface.d.ts +6 -0
- package/common/interface/controller-methods.interface.js +2 -0
- package/common/interface/cors.interface.d.ts +14 -0
- package/common/interface/cors.interface.js +2 -0
- package/common/interface/features/index.d.ts +4 -0
- package/common/interface/features/index.js +20 -0
- package/common/interface/features/sg-filter.interface.d.ts +4 -0
- package/common/interface/features/sg-filter.interface.js +2 -0
- package/common/interface/features/sg-guard.interface.d.ts +4 -0
- package/common/interface/features/sg-guard.interface.js +2 -0
- package/common/interface/features/sg-interceptor.interface.d.ts +4 -0
- package/common/interface/features/sg-interceptor.interface.js +2 -0
- package/common/interface/features/sg-middleware.interface.d.ts +4 -0
- package/common/interface/features/sg-middleware.interface.js +2 -0
- package/common/interface/global-middleware-options.interface.d.ts +5 -0
- package/common/interface/global-middleware-options.interface.js +2 -0
- package/common/interface/global-prefix-options.interface.d.ts +4 -0
- package/common/interface/global-prefix-options.interface.js +2 -0
- package/common/interface/index.d.ts +15 -0
- package/common/interface/index.js +31 -0
- package/common/interface/injectable-options.interface.d.ts +5 -0
- package/common/interface/injectable-options.interface.js +2 -0
- package/common/interface/injection-token.interface.d.ts +2 -0
- package/common/interface/injection-token.interface.js +2 -0
- package/common/interface/module-options.interface.d.ts +6 -0
- package/common/interface/module-options.interface.js +2 -0
- package/common/interface/provider.interface.d.ts +30 -0
- package/common/interface/provider.interface.js +2 -0
- package/common/interface/route-info.interface.d.ts +5 -0
- package/common/interface/route-info.interface.js +2 -0
- package/common/interface/sg-application.interface.d.ts +8 -0
- package/common/interface/sg-application.interface.js +2 -0
- package/common/interface/sg-factory.interface.d.ts +8 -0
- package/common/interface/sg-factory.interface.js +2 -0
- package/common/interface/sg-interceptor.interface.d.ts +3 -0
- package/common/interface/sg-interceptor.interface.js +2 -0
- package/common/interface/static-route.interface.d.ts +20 -0
- package/common/interface/static-route.interface.js +2 -0
- package/common/interface/type.interface.d.ts +3 -0
- package/common/interface/type.interface.js +2 -0
- package/config/config-service.d.ts +3 -0
- package/config/config-service.js +11 -0
- package/config/index.d.ts +1 -0
- package/config/index.js +17 -0
- package/di-container.d.ts +15 -0
- package/di-container.js +103 -0
- package/index.d.ts +9 -0
- package/index.js +25 -0
- package/logger/index.d.ts +1 -0
- package/logger/index.js +17 -0
- package/logger/logger-service.d.ts +9 -0
- package/logger/logger-service.js +44 -0
- package/package.json +24 -0
- package/routes-config.d.ts +22 -0
- package/routes-config.js +204 -0
- package/set-meta-data.d.ts +1 -0
- package/set-meta-data.js +35 -0
- package/sg-application.d.ts +12 -0
- package/sg-application.js +37 -0
- package/sg-factory.d.ts +6 -0
- package/sg-factory.js +22 -0
- package/src/cls/cls.service.ts +31 -0
- package/src/cls/index.ts +1 -0
- package/src/common/constants/index.ts +1 -0
- package/src/common/constants/providers.constants.ts +1 -0
- package/src/common/decorators/component.decorators.ts +13 -0
- package/src/common/decorators/controller.decorator.ts +16 -0
- package/src/common/decorators/filters.decorator.ts +17 -0
- package/src/common/decorators/guards.decorator.ts +26 -0
- package/src/common/decorators/index.ts +10 -0
- package/src/common/decorators/inject.decorators.ts +12 -0
- package/src/common/decorators/interceptors.decorator.ts +26 -0
- package/src/common/decorators/middlewares.decorator.ts +18 -0
- package/src/common/decorators/module.decorator.ts +29 -0
- package/src/common/decorators/parameter-validator.decorator.ts +17 -0
- package/src/common/decorators/route-mapping.decorator.ts +27 -0
- package/src/common/enum/index.ts +3 -0
- package/src/common/enum/request-method.enum.ts +11 -0
- package/src/common/enum/request-parameters.enum.ts +7 -0
- package/src/common/enum/scope.enum.ts +5 -0
- package/src/common/http/http-context.ts +25 -0
- package/src/common/http/http-exception.ts +12 -0
- package/src/common/http/index.ts +2 -0
- package/src/common/index.ts +5 -0
- package/src/common/interface/controller-method-paramenters.interface.ts +7 -0
- package/src/common/interface/controller-methods.interface.ts +7 -0
- package/src/common/interface/cors.interface.ts +15 -0
- package/src/common/interface/features/index.ts +4 -0
- package/src/common/interface/features/sg-filter.interface.ts +5 -0
- package/src/common/interface/features/sg-guard.interface.ts +5 -0
- package/src/common/interface/features/sg-interceptor.interface.ts +5 -0
- package/src/common/interface/features/sg-middleware.interface.ts +5 -0
- package/src/common/interface/global-middleware-options.interface.ts +7 -0
- package/src/common/interface/global-prefix-options.interface.ts +5 -0
- package/src/common/interface/index.ts +15 -0
- package/src/common/interface/injectable-options.interface.ts +6 -0
- package/src/common/interface/injection-token.interface.ts +2 -0
- package/src/common/interface/module-options.interface.ts +7 -0
- package/src/common/interface/provider.interface.ts +36 -0
- package/src/common/interface/route-info.interface.ts +6 -0
- package/src/common/interface/sg-application.interface.ts +9 -0
- package/src/common/interface/sg-factory.interface.ts +10 -0
- package/src/common/interface/sg-interceptor.interface.ts +3 -0
- package/src/common/interface/static-route.interface.ts +22 -0
- package/src/common/interface/type.interface.ts +3 -0
- package/src/config/config-service.ts +7 -0
- package/src/config/index.ts +1 -0
- package/src/di-container.ts +135 -0
- package/src/index.ts +9 -0
- package/src/logger/index.ts +1 -0
- package/src/logger/logger-service.ts +48 -0
- package/src/routes-config.ts +305 -0
- package/src/set-meta-data.ts +30 -0
- package/src/sg-application.ts +29 -0
- package/src/sg-factory.ts +15 -0
- package/tsconfig.json +31 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Express } from "express";
|
|
2
|
+
import { GlobalPrefixOptions } from "./common";
|
|
3
|
+
export declare class RoutRegister {
|
|
4
|
+
private readonly logger;
|
|
5
|
+
private router;
|
|
6
|
+
private prefix;
|
|
7
|
+
private prefixOptions;
|
|
8
|
+
private app;
|
|
9
|
+
constructor();
|
|
10
|
+
register(app: Express, globalPrefix?: string, prefixOptions?: GlobalPrefixOptions): Promise<void>;
|
|
11
|
+
private registerClasses;
|
|
12
|
+
private registerMiddleWares;
|
|
13
|
+
private doContains;
|
|
14
|
+
private registerControllerCLasses;
|
|
15
|
+
private registerRoute;
|
|
16
|
+
private routeHandler;
|
|
17
|
+
private registerGuards;
|
|
18
|
+
private handleRequestInterceptors;
|
|
19
|
+
private handleResponseInterceptors;
|
|
20
|
+
private validateParameters;
|
|
21
|
+
private catchExceptions;
|
|
22
|
+
}
|
package/routes-config.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RoutRegister = void 0;
|
|
4
|
+
const logger_1 = require("./logger");
|
|
5
|
+
const common_1 = require("./common");
|
|
6
|
+
const class_transformer_1 = require("class-transformer");
|
|
7
|
+
const class_validator_1 = require("class-validator");
|
|
8
|
+
const cls_1 = require("./cls");
|
|
9
|
+
const config_1 = require("./config");
|
|
10
|
+
const di_container_1 = require("./di-container");
|
|
11
|
+
class RoutRegister {
|
|
12
|
+
logger;
|
|
13
|
+
router;
|
|
14
|
+
prefix;
|
|
15
|
+
prefixOptions;
|
|
16
|
+
app;
|
|
17
|
+
constructor() {
|
|
18
|
+
this.logger = new logger_1.AppLogger();
|
|
19
|
+
}
|
|
20
|
+
async register(app, globalPrefix, prefixOptions) {
|
|
21
|
+
this.app = app;
|
|
22
|
+
this.router = app.router;
|
|
23
|
+
this.prefix = globalPrefix;
|
|
24
|
+
this.prefixOptions = prefixOptions;
|
|
25
|
+
this.registerClasses();
|
|
26
|
+
this.registerMiddleWares();
|
|
27
|
+
this.registerControllerCLasses();
|
|
28
|
+
}
|
|
29
|
+
registerClasses() {
|
|
30
|
+
di_container_1.container.register({
|
|
31
|
+
scope: common_1.Scope.SINGLETON,
|
|
32
|
+
token: config_1.ConfigService,
|
|
33
|
+
useClass: config_1.ConfigService
|
|
34
|
+
});
|
|
35
|
+
di_container_1.container.register({
|
|
36
|
+
scope: common_1.Scope.SINGLETON,
|
|
37
|
+
token: logger_1.AppLogger,
|
|
38
|
+
useClass: logger_1.AppLogger
|
|
39
|
+
});
|
|
40
|
+
di_container_1.container.register({
|
|
41
|
+
scope: common_1.Scope.SINGLETON,
|
|
42
|
+
token: cls_1.CLSService,
|
|
43
|
+
useClass: cls_1.CLSService
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
registerMiddleWares() {
|
|
47
|
+
for (const middleware of common_1.middlewares) {
|
|
48
|
+
this.app.use(async (req, res, next) => {
|
|
49
|
+
let path = req.path;
|
|
50
|
+
if (this.prefix) {
|
|
51
|
+
const splits = path.split(`${this.prefix}`);
|
|
52
|
+
if (splits.length > 1)
|
|
53
|
+
path = splits[1];
|
|
54
|
+
else
|
|
55
|
+
path = splits[0];
|
|
56
|
+
}
|
|
57
|
+
const options = middleware.options;
|
|
58
|
+
if (options?.exclude &&
|
|
59
|
+
options?.exclude.length > 0 &&
|
|
60
|
+
this.doContains(options?.exclude, path, req.method)) {
|
|
61
|
+
return next();
|
|
62
|
+
}
|
|
63
|
+
if (options?.forRoutes &&
|
|
64
|
+
options?.forRoutes.length > 0 &&
|
|
65
|
+
!this.doContains(options?.forRoutes, path, req.method)) {
|
|
66
|
+
return next();
|
|
67
|
+
}
|
|
68
|
+
const middlewareInstance = di_container_1.container.resolve(middleware.target);
|
|
69
|
+
await middlewareInstance.use(req, res, next);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
doContains(excludes, path, method) {
|
|
74
|
+
return excludes.some((e) => path.startsWith(e.routePrefix) &&
|
|
75
|
+
(e.method === common_1.RequestMethodEnum.ALL ||
|
|
76
|
+
e.method === method.toLowerCase()));
|
|
77
|
+
}
|
|
78
|
+
registerControllerCLasses() {
|
|
79
|
+
for (const ControllerClass of common_1.controllers) {
|
|
80
|
+
const prefix = ControllerClass.prototype.prefix;
|
|
81
|
+
for (const method of ControllerClass.prototype
|
|
82
|
+
.methods) {
|
|
83
|
+
const guards = [
|
|
84
|
+
...(ControllerClass.prototype[method.handler].guards || []),
|
|
85
|
+
...common_1.globalGuards
|
|
86
|
+
];
|
|
87
|
+
const interceptors = [
|
|
88
|
+
...(ControllerClass.prototype[method.handler].interceptors || []),
|
|
89
|
+
...common_1.globalInterceptors
|
|
90
|
+
];
|
|
91
|
+
const parameters = ControllerClass.prototype[method.handler].parameters || [];
|
|
92
|
+
this.registerRoute(prefix, method, guards, interceptors, parameters, ControllerClass);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
registerRoute(prefix, method, guards, interceptors, parameters, controller) {
|
|
97
|
+
let path = method.path ? `${prefix}${method.path}` : prefix;
|
|
98
|
+
if (this.prefix &&
|
|
99
|
+
(!this.prefixOptions ||
|
|
100
|
+
!this.prefixOptions.exclude ||
|
|
101
|
+
this.prefixOptions.exclude.length <= 0 ||
|
|
102
|
+
!this.doContains(this.prefixOptions.exclude, path, method.method))) {
|
|
103
|
+
path = this.prefix + path;
|
|
104
|
+
}
|
|
105
|
+
const logString = `[ROUTE] MAPPED{${path}, ${method.method.toUpperCase()}}`;
|
|
106
|
+
this.router[method.method](path, async (req, res) => {
|
|
107
|
+
const cls = di_container_1.container.resolve(cls_1.CLSService);
|
|
108
|
+
cls.run(req, async () => {
|
|
109
|
+
const httpContext = new common_1.HttpContext(req, res, method);
|
|
110
|
+
const controllerInstance = di_container_1.container.resolve(controller);
|
|
111
|
+
try {
|
|
112
|
+
await this.routeHandler(req, res, guards, interceptors, httpContext, parameters, controllerInstance, method);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.log({ error });
|
|
116
|
+
if (error instanceof common_1.HttpException) {
|
|
117
|
+
await this.catchExceptions(httpContext, error);
|
|
118
|
+
}
|
|
119
|
+
else
|
|
120
|
+
res.status(500).json({ message: "Internal Server Error!!" });
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
di_container_1.container.removeRequestInstances(cls.get('instanceId'));
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
this.logger.log(logString);
|
|
128
|
+
}
|
|
129
|
+
async routeHandler(req, res, guards, interceptors, httpContext, parameters, controller, method) {
|
|
130
|
+
await this.registerGuards(guards, httpContext);
|
|
131
|
+
const afterResponseInterceptor = await this.handleRequestInterceptors(interceptors, httpContext);
|
|
132
|
+
const validatedParameters = await this.validateParameters(parameters, req);
|
|
133
|
+
let data = await controller[method.handler](...validatedParameters);
|
|
134
|
+
data = await this.handleResponseInterceptors(afterResponseInterceptor, data);
|
|
135
|
+
res.send(data);
|
|
136
|
+
}
|
|
137
|
+
async registerGuards(guards, httpContext) {
|
|
138
|
+
for (const guard of guards) {
|
|
139
|
+
const guardInstance = di_container_1.container.resolve(guard);
|
|
140
|
+
const canActive = await guardInstance.canAccess(httpContext);
|
|
141
|
+
if (!canActive)
|
|
142
|
+
throw new common_1.HttpException("Forbidden", 403);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async handleRequestInterceptors(interceptors, httpContext) {
|
|
146
|
+
const afterInterceptors = [];
|
|
147
|
+
for (const interceptor of interceptors) {
|
|
148
|
+
let interceptorInstance;
|
|
149
|
+
if (typeof interceptor == "object")
|
|
150
|
+
interceptorInstance = interceptor;
|
|
151
|
+
else
|
|
152
|
+
interceptorInstance = di_container_1.container.resolve(interceptor);
|
|
153
|
+
const afterIntercept = await interceptorInstance.intercept(httpContext);
|
|
154
|
+
if (afterIntercept)
|
|
155
|
+
afterInterceptors.push(afterIntercept);
|
|
156
|
+
}
|
|
157
|
+
return afterInterceptors;
|
|
158
|
+
}
|
|
159
|
+
async handleResponseInterceptors(interceptors, data) {
|
|
160
|
+
for (const afterInterceptor of interceptors) {
|
|
161
|
+
data = await afterInterceptor(data);
|
|
162
|
+
}
|
|
163
|
+
return data;
|
|
164
|
+
}
|
|
165
|
+
async validateParameters(parameters, request) {
|
|
166
|
+
const transformedParameters = [];
|
|
167
|
+
parameters.sort((a, b) => a.index - b.index);
|
|
168
|
+
for (const { paramType, dtoClass } of parameters) {
|
|
169
|
+
let paramData;
|
|
170
|
+
if (paramType == common_1.RequestParameters.QUERY) {
|
|
171
|
+
paramData = structuredClone(request.query);
|
|
172
|
+
}
|
|
173
|
+
else if (paramType == common_1.RequestParameters.BODY)
|
|
174
|
+
paramData = structuredClone(request.body);
|
|
175
|
+
else if (paramType == common_1.RequestParameters.PARAMS)
|
|
176
|
+
paramData = structuredClone(request.params);
|
|
177
|
+
else if (paramType == common_1.RequestParameters.FILE)
|
|
178
|
+
paramData = structuredClone(request["file"]);
|
|
179
|
+
else if (paramType == common_1.RequestParameters.FILES)
|
|
180
|
+
paramData = structuredClone(request["files"]);
|
|
181
|
+
if (dtoClass) {
|
|
182
|
+
const transformedClass = (0, class_transformer_1.plainToInstance)(dtoClass, { ...paramData }, { enableImplicitConversion: true });
|
|
183
|
+
const errors = await (0, class_validator_1.validate)(transformedClass, {
|
|
184
|
+
whitelist: true
|
|
185
|
+
});
|
|
186
|
+
if (errors.length > 0) {
|
|
187
|
+
throw new common_1.HttpException(errors, 400);
|
|
188
|
+
}
|
|
189
|
+
transformedParameters.push(transformedClass);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
transformedParameters.push(paramData);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return transformedParameters;
|
|
196
|
+
}
|
|
197
|
+
async catchExceptions(httpContext, error) {
|
|
198
|
+
for (const filter of common_1.filters) {
|
|
199
|
+
const filterInstance = di_container_1.container.resolve(filter);
|
|
200
|
+
await filterInstance.catch(error, httpContext);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.RoutRegister = RoutRegister;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const SetMetaData: (key: string, value: any) => MethodDecorator;
|
package/set-meta-data.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SetMetaData = void 0;
|
|
4
|
+
const SetMetaData = (key, value) => {
|
|
5
|
+
return function (target, propertyKey) {
|
|
6
|
+
if (!target.methods) {
|
|
7
|
+
target.methods = [];
|
|
8
|
+
const data = {
|
|
9
|
+
handler: propertyKey
|
|
10
|
+
};
|
|
11
|
+
data[key] = value;
|
|
12
|
+
target.methods.push(data);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
const newMethods = [];
|
|
16
|
+
let include = false;
|
|
17
|
+
for (const r of target.methods) {
|
|
18
|
+
if (r.handler == propertyKey) {
|
|
19
|
+
r[key] = value;
|
|
20
|
+
include = true;
|
|
21
|
+
}
|
|
22
|
+
newMethods.push(r);
|
|
23
|
+
}
|
|
24
|
+
if (!include) {
|
|
25
|
+
const data = {
|
|
26
|
+
handler: propertyKey
|
|
27
|
+
};
|
|
28
|
+
data[key] = value;
|
|
29
|
+
newMethods.push(data);
|
|
30
|
+
}
|
|
31
|
+
target.methods = newMethods;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
exports.SetMetaData = SetMetaData;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Express } from "express";
|
|
2
|
+
import { GlobalPrefixOptions, ISGApplication, StaticRoutes } from ".";
|
|
3
|
+
export declare class SGApplication implements ISGApplication {
|
|
4
|
+
private app;
|
|
5
|
+
private prefixOptions;
|
|
6
|
+
private prefix;
|
|
7
|
+
constructor(app: Express);
|
|
8
|
+
use(...args: any[]): this;
|
|
9
|
+
setStaticRoutes(options: StaticRoutes[]): this;
|
|
10
|
+
setGlobalPrefix(prefix: string, options?: GlobalPrefixOptions): this;
|
|
11
|
+
listen(port: number, callback?: (error?: Error) => void): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SGApplication = void 0;
|
|
7
|
+
const _1 = require(".");
|
|
8
|
+
const express_1 = __importDefault(require("express"));
|
|
9
|
+
class SGApplication {
|
|
10
|
+
app;
|
|
11
|
+
prefixOptions;
|
|
12
|
+
prefix;
|
|
13
|
+
constructor(app) {
|
|
14
|
+
this.app = app;
|
|
15
|
+
}
|
|
16
|
+
use(...args) {
|
|
17
|
+
this.app.use(...args);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
setStaticRoutes(options) {
|
|
21
|
+
options.forEach((o) => {
|
|
22
|
+
o.prefix ? this.app.use(o.prefix, express_1.default.static(o.path, { ...(o.options) })) : this.app.use(express_1.default.static(o.path, { ...(o.options) }));
|
|
23
|
+
});
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
setGlobalPrefix(prefix, options) {
|
|
27
|
+
this.prefix = prefix;
|
|
28
|
+
if (options)
|
|
29
|
+
this.prefixOptions = options;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
async listen(port, callback) {
|
|
33
|
+
new _1.RoutRegister().register(this.app, this.prefix, this.prefixOptions);
|
|
34
|
+
return this.app.listen(port, callback);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.SGApplication = SGApplication;
|
package/sg-factory.d.ts
ADDED
package/sg-factory.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SGFactory = void 0;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const _1 = require(".");
|
|
9
|
+
const cors_1 = __importDefault(require("cors"));
|
|
10
|
+
class SGFactory {
|
|
11
|
+
app;
|
|
12
|
+
constructor() { }
|
|
13
|
+
create(_, options) {
|
|
14
|
+
this.app = (0, express_1.default)();
|
|
15
|
+
this.app.use(express_1.default.json());
|
|
16
|
+
this.app.use(express_1.default.urlencoded({ extended: true }));
|
|
17
|
+
if (options && options.cors)
|
|
18
|
+
typeof options.cors === 'boolean' ? this.app.use((0, cors_1.default)()) : this.app.use((0, cors_1.default)({ ...options.cors }));
|
|
19
|
+
return new _1.SGApplication(this.app);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.SGFactory = SGFactory;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
import { Request, Response } from "express";
|
|
4
|
+
|
|
5
|
+
export class CLSService {
|
|
6
|
+
private readonly asyncLocalStorage: AsyncLocalStorage<any>;
|
|
7
|
+
constructor() {
|
|
8
|
+
this.asyncLocalStorage = new AsyncLocalStorage();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
run(req: Request, fn: () => Promise<any> | any) {
|
|
12
|
+
const requestId = crypto.randomUUID();
|
|
13
|
+
return this.asyncLocalStorage.run({ requestId, request: req }, fn);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
set(key: string, value: any): void {
|
|
17
|
+
const store = this.asyncLocalStorage.getStore();
|
|
18
|
+
if (!store) {
|
|
19
|
+
throw new Error("CLS context not initialized");
|
|
20
|
+
}
|
|
21
|
+
store[key] = value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get<T = any>(key: string): T {
|
|
25
|
+
const store = this.asyncLocalStorage.getStore();
|
|
26
|
+
if (!store) {
|
|
27
|
+
throw new Error("CLS context not initialized");
|
|
28
|
+
}
|
|
29
|
+
return store[key];
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/cls/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./cls.service";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./providers.constants";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const REQUEST = "request";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { container } from "../../di-container";
|
|
2
|
+
import { Scope } from "../enum";
|
|
3
|
+
import { InjectableOptions } from "../interface";
|
|
4
|
+
|
|
5
|
+
export function Component(options?: InjectableOptions): ClassDecorator {
|
|
6
|
+
return function (target: any) {
|
|
7
|
+
container.register({
|
|
8
|
+
token: options && options.token ? options.token : target,
|
|
9
|
+
useClass: target as any,
|
|
10
|
+
scope: options && options.scope ? options.scope : Scope.SINGLETON
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { container } from "../../di-container";
|
|
2
|
+
import { Scope } from "../enum";
|
|
3
|
+
|
|
4
|
+
export const controllers: any[] = [];
|
|
5
|
+
|
|
6
|
+
export function RestController(prefix: string = "/"): ClassDecorator {
|
|
7
|
+
return function (target: any) {
|
|
8
|
+
target.prototype.prefix = prefix[0] !== "/" ? "/" + prefix : prefix;
|
|
9
|
+
controllers.push(target);
|
|
10
|
+
container.register({
|
|
11
|
+
token: target,
|
|
12
|
+
useClass: target as any,
|
|
13
|
+
scope: Scope.SINGLETON
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { container } from "../../di-container";
|
|
2
|
+
import { Scope } from "../enum";
|
|
3
|
+
|
|
4
|
+
export const filters: any[] = [];
|
|
5
|
+
|
|
6
|
+
export function Catch(): ClassDecorator {
|
|
7
|
+
return function (target: any) {
|
|
8
|
+
if (typeof target.prototype.catch !== "function")
|
|
9
|
+
throw new Error(`${target.name} does not implements catch method`);
|
|
10
|
+
filters.push(target);
|
|
11
|
+
container.register({
|
|
12
|
+
token: target,
|
|
13
|
+
useClass: target as any,
|
|
14
|
+
scope: Scope.SINGLETON
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SGGuard } from "../interface";
|
|
2
|
+
import { container } from "../../di-container";
|
|
3
|
+
import { Scope } from "../enum";
|
|
4
|
+
|
|
5
|
+
export const globalGuards: any[] = [];
|
|
6
|
+
|
|
7
|
+
export function UseGuard(
|
|
8
|
+
...guardClasses: (SGGuard | Function)[]
|
|
9
|
+
): MethodDecorator {
|
|
10
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
11
|
+
target[propertyKey].guards = guardClasses;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function GlobalGuard(): ClassDecorator {
|
|
16
|
+
return function (target: any) {
|
|
17
|
+
if (typeof target.prototype.canAccess !== "function")
|
|
18
|
+
throw new Error(`${target.name} does not implements canActivate method`);
|
|
19
|
+
globalGuards.push(target);
|
|
20
|
+
container.register({
|
|
21
|
+
token: target,
|
|
22
|
+
useClass: target as any,
|
|
23
|
+
scope: Scope.SINGLETON
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./controller.decorator";
|
|
2
|
+
export * from "./filters.decorator";
|
|
3
|
+
export * from "./guards.decorator";
|
|
4
|
+
export * from "./interceptors.decorator";
|
|
5
|
+
export * from "./middlewares.decorator";
|
|
6
|
+
export * from "./parameter-validator.decorator";
|
|
7
|
+
export * from "./route-mapping.decorator";
|
|
8
|
+
export * from "./module.decorator";
|
|
9
|
+
export * from "./component.decorators";
|
|
10
|
+
export * from "./inject.decorators";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Token } from "../interface";
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
export function Inject(token: Token): ParameterDecorator & PropertyDecorator {
|
|
4
|
+
return (target: any, _: string | symbol | undefined, index?: number) => {
|
|
5
|
+
if (typeof index === "number") {
|
|
6
|
+
const existing =
|
|
7
|
+
Reflect.getMetadata("custom:inject_tokens", target) || {};
|
|
8
|
+
existing[index] = token;
|
|
9
|
+
Reflect.defineMetadata("custom:inject_tokens", existing, target);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SGInterceptor } from "../interface";
|
|
2
|
+
import { container } from "../../di-container";
|
|
3
|
+
import { Scope } from "../enum";
|
|
4
|
+
|
|
5
|
+
export const globalInterceptors: any[] = [];
|
|
6
|
+
|
|
7
|
+
export function UseInterceptor(
|
|
8
|
+
...interceptorClasses: (SGInterceptor | Function)[]
|
|
9
|
+
): MethodDecorator {
|
|
10
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
11
|
+
target[propertyKey].interceptors = interceptorClasses;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function GlobalInterceptors(): ClassDecorator {
|
|
16
|
+
return function (target: any) {
|
|
17
|
+
if (typeof target.prototype.intercept !== "function")
|
|
18
|
+
throw new Error(`${target.name} does not implements intercept method`);
|
|
19
|
+
globalInterceptors.push(target);
|
|
20
|
+
container.register({
|
|
21
|
+
token: target,
|
|
22
|
+
useClass: target as any,
|
|
23
|
+
scope: Scope.SINGLETON
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { container } from "../../di-container";
|
|
2
|
+
import { Scope } from "../enum/scope.enum";
|
|
3
|
+
import { MiddlewareOptions } from "../interface/global-middleware-options.interface";
|
|
4
|
+
|
|
5
|
+
export const middlewares: any[] = [];
|
|
6
|
+
|
|
7
|
+
export function Middleware(options?: MiddlewareOptions): ClassDecorator {
|
|
8
|
+
return function (target: any) {
|
|
9
|
+
if (typeof target.prototype.use !== "function")
|
|
10
|
+
throw new Error(`${target.name} does not implements use method`);
|
|
11
|
+
middlewares.push({ target, options });
|
|
12
|
+
container.register({
|
|
13
|
+
token: target,
|
|
14
|
+
useClass: target as any,
|
|
15
|
+
scope: Scope.SINGLETON
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { container } from "../../di-container";
|
|
2
|
+
import { Scope } from "../enum";
|
|
3
|
+
import { ModuleOptions, Provider } from "../interface";
|
|
4
|
+
|
|
5
|
+
export function Module(options?: ModuleOptions): ClassDecorator {
|
|
6
|
+
return function (target: any) {
|
|
7
|
+
if (options.providers) {
|
|
8
|
+
options.providers.forEach((p) => {
|
|
9
|
+
if (Object.keys(p).length > 0) {
|
|
10
|
+
const provider = p as Provider;
|
|
11
|
+
container.register({
|
|
12
|
+
scope: provider.scope || Scope.SINGLETON,
|
|
13
|
+
token: provider.token,
|
|
14
|
+
useValue: provider.useValue,
|
|
15
|
+
useClass: provider.useClass,
|
|
16
|
+
useFactory: provider.useFactory,
|
|
17
|
+
inject: provider.inject
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
container.register({
|
|
21
|
+
scope: Scope.SINGLETON,
|
|
22
|
+
token: p as any,
|
|
23
|
+
useClass: p as any
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RequestParameters } from "../enum";
|
|
2
|
+
import { ControllerMethodParams } from "../interface";
|
|
3
|
+
|
|
4
|
+
function Validator(data: string, dtoClass: any): ParameterDecorator {
|
|
5
|
+
return function (target: any, propertyKey: string | symbol, index: number) {
|
|
6
|
+
const parameters: ControllerMethodParams[] =
|
|
7
|
+
target[propertyKey].parameters || [];
|
|
8
|
+
parameters.push({ index, dtoClass, paramType: data });
|
|
9
|
+
target[propertyKey].parameters = parameters;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const Query = (dtoClass: any) => Validator(RequestParameters.QUERY, dtoClass);
|
|
14
|
+
export const Body = (dtoClass: any) => Validator(RequestParameters.BODY, dtoClass);
|
|
15
|
+
export const Params = (dtoClass: any) => Validator(RequestParameters.PARAMS, dtoClass);
|
|
16
|
+
export const UploadedFile = () => Validator(RequestParameters.FILE, null)
|
|
17
|
+
export const UploadedFiles = () => Validator(RequestParameters.FILES, null)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ControllerMethods } from "../interface";
|
|
2
|
+
|
|
3
|
+
function RequestMapping(method: string, path?: string): MethodDecorator {
|
|
4
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
5
|
+
const methods: ControllerMethods[] = target.methods || [];
|
|
6
|
+
const index = methods.findIndex((m) => m.handler === propertyKey);
|
|
7
|
+
if (index > -1)
|
|
8
|
+
methods[index] = {
|
|
9
|
+
...methods[index],
|
|
10
|
+
method: method,
|
|
11
|
+
path: path && path.length > 0 && path[0] !== "/" ? "/" + path : path
|
|
12
|
+
};
|
|
13
|
+
else
|
|
14
|
+
methods.push({
|
|
15
|
+
handler: propertyKey,
|
|
16
|
+
method,
|
|
17
|
+
path: path && path.length > 0 && path[0] !== "/" ? "/" + path : path
|
|
18
|
+
});
|
|
19
|
+
target.methods = methods;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const Get = (path?: string) => RequestMapping("get", path);
|
|
24
|
+
export const Post = (path?: string) => RequestMapping("post", path);
|
|
25
|
+
export const Put = (path?: string) => RequestMapping("put", path);
|
|
26
|
+
export const Patch = (path?: string) => RequestMapping("patch", path);
|
|
27
|
+
export const Delete = (path?: string) => RequestMapping("delete", path);
|