@sharangyawali/sg-app 1.0.10
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 +24 -0
- 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 +25 -0
- package/common/decorators/parameter-validator.decorator.d.ts +7 -0
- package/common/decorators/parameter-validator.decorator.js +25 -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 +9 -0
- package/common/enum/request-parameters.enum.js +13 -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 +6 -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 +28 -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 +19 -0
- package/di-container.js +113 -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 +24 -0
- package/routes-config.js +224 -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 +7 -0
- package/sg-factory.js +36 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppLogger = void 0;
|
|
4
|
+
const winston_1 = require("winston");
|
|
5
|
+
class AppLogger {
|
|
6
|
+
logger;
|
|
7
|
+
constructor() {
|
|
8
|
+
const colorizer = winston_1.format.colorize();
|
|
9
|
+
(0, winston_1.addColors)({
|
|
10
|
+
info: "bold green",
|
|
11
|
+
log: "bold white",
|
|
12
|
+
warn: "bold yellow"
|
|
13
|
+
});
|
|
14
|
+
const myFormat = winston_1.format.printf(({ message }) => {
|
|
15
|
+
const coloredTime = colorizer.colorize("log", new Date().toLocaleString());
|
|
16
|
+
const coloredServer = colorizer.colorize("info", "[NODE] SGAPP");
|
|
17
|
+
return `${coloredServer}- ${coloredTime} ${message}`;
|
|
18
|
+
});
|
|
19
|
+
this.logger = (0, winston_1.createLogger)({
|
|
20
|
+
level: "info",
|
|
21
|
+
format: winston_1.format.combine(winston_1.format.colorize({ all: true }), myFormat),
|
|
22
|
+
transports: [
|
|
23
|
+
new winston_1.transports.Console(),
|
|
24
|
+
],
|
|
25
|
+
exitOnError: false
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
log(message) {
|
|
29
|
+
this.logger.log("info", message);
|
|
30
|
+
}
|
|
31
|
+
debug(message) {
|
|
32
|
+
this.logger.debug(message);
|
|
33
|
+
}
|
|
34
|
+
warn(message) {
|
|
35
|
+
this.logger.warn(message);
|
|
36
|
+
}
|
|
37
|
+
error(message) {
|
|
38
|
+
this.logger.error(message);
|
|
39
|
+
}
|
|
40
|
+
logWithContext(context, message) {
|
|
41
|
+
this.logger.info(`[${context}] ${message}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.AppLogger = AppLogger;
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sharangyawali/sg-app",
|
|
3
|
+
"version": "1.0.10",
|
|
4
|
+
"keywords": [],
|
|
5
|
+
"author": "Sharan Gyawali",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"description": "SG-APP - Express App In CLEAN ARCHITECTURE",
|
|
8
|
+
"devDependencies": {
|
|
9
|
+
"@types/express": "^5.0.6",
|
|
10
|
+
"@types/node": "^25.0.3",
|
|
11
|
+
"ts-node": "^10.9.2",
|
|
12
|
+
"tsx": "^4.21.0",
|
|
13
|
+
"typescript": "^5.9.3"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"class-transformer": "^0.5.1",
|
|
17
|
+
"class-validator": "^0.14.3",
|
|
18
|
+
"cors": "^2.8.5",
|
|
19
|
+
"dotenv": "^17.2.3",
|
|
20
|
+
"express": "^5.2.1",
|
|
21
|
+
"reflect-metadata": "^0.2.2",
|
|
22
|
+
"winston": "^3.19.0"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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 registerCLS;
|
|
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
|
+
private reduceErrors;
|
|
23
|
+
private capitalizeFirstLetter;
|
|
24
|
+
}
|
package/routes-config.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
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 di_container_1 = require("./di-container");
|
|
10
|
+
class RoutRegister {
|
|
11
|
+
logger;
|
|
12
|
+
router;
|
|
13
|
+
prefix;
|
|
14
|
+
prefixOptions;
|
|
15
|
+
app;
|
|
16
|
+
constructor() {
|
|
17
|
+
this.logger = new logger_1.AppLogger();
|
|
18
|
+
}
|
|
19
|
+
async register(app, globalPrefix, prefixOptions) {
|
|
20
|
+
this.app = app;
|
|
21
|
+
this.router = app.router;
|
|
22
|
+
this.prefix = globalPrefix;
|
|
23
|
+
this.prefixOptions = prefixOptions;
|
|
24
|
+
this.registerCLS();
|
|
25
|
+
this.registerMiddleWares();
|
|
26
|
+
await this.registerControllerCLasses();
|
|
27
|
+
}
|
|
28
|
+
registerCLS() {
|
|
29
|
+
di_container_1.container.register({
|
|
30
|
+
scope: common_1.Scope.SINGLETON,
|
|
31
|
+
token: cls_1.CLSService,
|
|
32
|
+
useClass: cls_1.CLSService
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
registerMiddleWares() {
|
|
36
|
+
for (const middleware of common_1.middlewares) {
|
|
37
|
+
this.app.use(async (req, res, next) => {
|
|
38
|
+
let path = req.path;
|
|
39
|
+
if (this.prefix) {
|
|
40
|
+
const splits = path.split(`${this.prefix}`);
|
|
41
|
+
if (splits.length > 1)
|
|
42
|
+
path = splits[1];
|
|
43
|
+
else
|
|
44
|
+
path = splits[0];
|
|
45
|
+
}
|
|
46
|
+
const options = middleware.options;
|
|
47
|
+
if (options?.exclude &&
|
|
48
|
+
options?.exclude.length > 0 &&
|
|
49
|
+
this.doContains(options?.exclude, path, req.method)) {
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
52
|
+
if (options?.forRoutes &&
|
|
53
|
+
options?.forRoutes.length > 0 &&
|
|
54
|
+
!this.doContains(options?.forRoutes, path, req.method)) {
|
|
55
|
+
return next();
|
|
56
|
+
}
|
|
57
|
+
const middlewareInstance = await di_container_1.container.resolve(middleware.target);
|
|
58
|
+
await middlewareInstance.use(req, res, next);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
doContains(excludes, path, method) {
|
|
63
|
+
return excludes.some((e) => path.startsWith(e.routePrefix) &&
|
|
64
|
+
(e.method === common_1.RequestMethodEnum.ALL ||
|
|
65
|
+
e.method === method.toLowerCase()));
|
|
66
|
+
}
|
|
67
|
+
async registerControllerCLasses() {
|
|
68
|
+
for (let ControllerClass of common_1.controllers) {
|
|
69
|
+
const prefix = ControllerClass.prototype.prefix;
|
|
70
|
+
for (const method of (ControllerClass.prototype
|
|
71
|
+
.methods || [])) {
|
|
72
|
+
const guards = [
|
|
73
|
+
...(ControllerClass.prototype[method.handler].guards || []),
|
|
74
|
+
...common_1.globalGuards
|
|
75
|
+
];
|
|
76
|
+
const interceptors = [
|
|
77
|
+
...(ControllerClass.prototype[method.handler].interceptors || []),
|
|
78
|
+
...common_1.globalInterceptors
|
|
79
|
+
];
|
|
80
|
+
const parameters = ControllerClass.prototype[method.handler].parameters || [];
|
|
81
|
+
const scope = di_container_1.container.computeEffectiveScope(ControllerClass);
|
|
82
|
+
await this.registerRoute(prefix, method, guards, interceptors, parameters, ControllerClass, scope);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async registerRoute(prefix, method, guards, interceptors, parameters, controllerInstance, scope) {
|
|
87
|
+
let path = method.path ? `${prefix}${method.path}` : prefix;
|
|
88
|
+
if (this.prefix &&
|
|
89
|
+
(!this.prefixOptions ||
|
|
90
|
+
!this.prefixOptions.exclude ||
|
|
91
|
+
this.prefixOptions.exclude.length <= 0 ||
|
|
92
|
+
!this.doContains(this.prefixOptions.exclude, path, method.method))) {
|
|
93
|
+
path = this.prefix + path;
|
|
94
|
+
}
|
|
95
|
+
const logString = `[ROUTE] MAPPED{${path}, ${method.method.toUpperCase()}}`;
|
|
96
|
+
if (scope == common_1.Scope.SINGLETON) {
|
|
97
|
+
controllerInstance = await di_container_1.container.resolve(controllerInstance);
|
|
98
|
+
}
|
|
99
|
+
this.router[method.method](path, async (req, res) => {
|
|
100
|
+
const cls = await di_container_1.container.resolve(cls_1.CLSService);
|
|
101
|
+
cls.run(req, async () => {
|
|
102
|
+
const httpContext = new common_1.HttpContext(req, res, method);
|
|
103
|
+
try {
|
|
104
|
+
await this.routeHandler(req, res, guards, interceptors, httpContext, parameters, scope !== common_1.Scope.SINGLETON ? await di_container_1.container.resolve(controllerInstance, cls.get('requestId')) : controllerInstance, method);
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
console.log({ error });
|
|
108
|
+
if (error instanceof common_1.HttpException) {
|
|
109
|
+
await this.catchExceptions(httpContext, error);
|
|
110
|
+
if (!res.headersSent)
|
|
111
|
+
res.status(error.statusCode).json({ ...error.message });
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
if (!res.headersSent)
|
|
115
|
+
res.status(500).json({ message: "Internal Server Error!!" });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
di_container_1.container.removeRequest(cls.get('instanceId'));
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
this.logger.log(logString);
|
|
124
|
+
}
|
|
125
|
+
async routeHandler(req, res, guards, interceptors, httpContext, parameters, controller, method) {
|
|
126
|
+
await this.registerGuards(guards, httpContext);
|
|
127
|
+
const afterResponseInterceptor = await this.handleRequestInterceptors(interceptors, httpContext);
|
|
128
|
+
const validatedParameters = await this.validateParameters(parameters, req, res);
|
|
129
|
+
let data = await controller[method.handler](...validatedParameters);
|
|
130
|
+
data = await this.handleResponseInterceptors(afterResponseInterceptor, data);
|
|
131
|
+
if (!res.headersSent)
|
|
132
|
+
res.send(data);
|
|
133
|
+
}
|
|
134
|
+
async registerGuards(guards, httpContext) {
|
|
135
|
+
for (const guard of guards) {
|
|
136
|
+
const guardInstance = await di_container_1.container.resolve(guard);
|
|
137
|
+
const canActive = await guardInstance.canAccess(httpContext);
|
|
138
|
+
if (!canActive)
|
|
139
|
+
throw new common_1.HttpException("Forbidden", 403);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async handleRequestInterceptors(interceptors, httpContext) {
|
|
143
|
+
const afterInterceptors = [];
|
|
144
|
+
for (const interceptor of interceptors) {
|
|
145
|
+
let interceptorInstance;
|
|
146
|
+
if (typeof interceptor == "object")
|
|
147
|
+
interceptorInstance = interceptor;
|
|
148
|
+
else
|
|
149
|
+
interceptorInstance = await di_container_1.container.resolve(interceptor);
|
|
150
|
+
const afterIntercept = await interceptorInstance.intercept(httpContext);
|
|
151
|
+
if (afterIntercept)
|
|
152
|
+
afterInterceptors.push(afterIntercept);
|
|
153
|
+
}
|
|
154
|
+
return afterInterceptors;
|
|
155
|
+
}
|
|
156
|
+
async handleResponseInterceptors(interceptors, data) {
|
|
157
|
+
for (const afterInterceptor of interceptors) {
|
|
158
|
+
data = await afterInterceptor(data);
|
|
159
|
+
}
|
|
160
|
+
return data;
|
|
161
|
+
}
|
|
162
|
+
async validateParameters(parameters, request, response) {
|
|
163
|
+
const transformedParameters = [];
|
|
164
|
+
parameters.sort((a, b) => a.index - b.index);
|
|
165
|
+
for (const { paramType, dtoClass } of parameters) {
|
|
166
|
+
let paramData;
|
|
167
|
+
if (paramType == common_1.RequestParameters.QUERY) {
|
|
168
|
+
paramData = structuredClone(request.query);
|
|
169
|
+
}
|
|
170
|
+
else if (paramType == common_1.RequestParameters.BODY)
|
|
171
|
+
paramData = structuredClone(request.body);
|
|
172
|
+
else if (paramType == common_1.RequestParameters.PARAMS)
|
|
173
|
+
paramData = structuredClone(request.params);
|
|
174
|
+
else if (paramType == common_1.RequestParameters.FILE)
|
|
175
|
+
paramData = structuredClone(request["file"]);
|
|
176
|
+
else if (paramType == common_1.RequestParameters.FILES)
|
|
177
|
+
paramData = structuredClone(request["files"]);
|
|
178
|
+
else if (paramType == common_1.RequestParameters.REQUEST)
|
|
179
|
+
paramData = request;
|
|
180
|
+
else if (paramType == common_1.RequestParameters.RESPONSE)
|
|
181
|
+
paramData = response;
|
|
182
|
+
if (dtoClass) {
|
|
183
|
+
const transformedClass = (0, class_transformer_1.plainToInstance)(dtoClass, { ...paramData }, { enableImplicitConversion: true });
|
|
184
|
+
let errors = [];
|
|
185
|
+
if ((0, class_validator_1.getMetadataStorage)().getTargetValidationMetadatas(dtoClass, '', false, false).length > 0)
|
|
186
|
+
errors = await (0, class_validator_1.validate)(transformedClass, {
|
|
187
|
+
whitelist: true
|
|
188
|
+
});
|
|
189
|
+
if (errors.length > 0) {
|
|
190
|
+
throw new common_1.HttpException(this.reduceErrors(errors) || 'Validation error!!', 400);
|
|
191
|
+
}
|
|
192
|
+
transformedParameters.push(transformedClass);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
transformedParameters.push(paramData);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return transformedParameters;
|
|
199
|
+
}
|
|
200
|
+
async catchExceptions(httpContext, error) {
|
|
201
|
+
for (const filter of common_1.filters) {
|
|
202
|
+
const filterInstance = await di_container_1.container.resolve(filter);
|
|
203
|
+
await filterInstance.catch(error, httpContext);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
reduceErrors(errors) {
|
|
207
|
+
return errors?.reduce((obj, item) => {
|
|
208
|
+
if (item.children?.length > 0) {
|
|
209
|
+
obj[item.property] = this.reduceErrors(item.children);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
obj[item.property] = this.capitalizeFirstLetter(Array.isArray(Object.values(item.constraints))
|
|
213
|
+
? Object.values(item.constraints)[0]
|
|
214
|
+
: Object.values(item.constraints).toString());
|
|
215
|
+
}
|
|
216
|
+
return obj;
|
|
217
|
+
}, {});
|
|
218
|
+
}
|
|
219
|
+
capitalizeFirstLetter(string) {
|
|
220
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
221
|
+
}
|
|
222
|
+
;
|
|
223
|
+
}
|
|
224
|
+
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
|
+
await 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,36 @@
|
|
|
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
|
+
this.registerClasses();
|
|
14
|
+
}
|
|
15
|
+
create(_, options) {
|
|
16
|
+
this.app = (0, express_1.default)();
|
|
17
|
+
this.app.use(express_1.default.json());
|
|
18
|
+
this.app.use(express_1.default.urlencoded({ extended: true }));
|
|
19
|
+
if (options && options.cors)
|
|
20
|
+
typeof options.cors === 'boolean' ? this.app.use((0, cors_1.default)()) : this.app.use((0, cors_1.default)({ ...options.cors }));
|
|
21
|
+
return new _1.SGApplication(this.app);
|
|
22
|
+
}
|
|
23
|
+
registerClasses() {
|
|
24
|
+
_1.container.register({
|
|
25
|
+
scope: _1.Scope.SINGLETON,
|
|
26
|
+
token: _1.ConfigService,
|
|
27
|
+
useClass: _1.ConfigService
|
|
28
|
+
});
|
|
29
|
+
_1.container.register({
|
|
30
|
+
scope: _1.Scope.SINGLETON,
|
|
31
|
+
token: _1.AppLogger,
|
|
32
|
+
useClass: _1.AppLogger
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.SGFactory = SGFactory;
|