namirasoft-node 1.0.13 → 1.0.15
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/BaseApplication.d.ts +1 -1
- package/dist/BaseApplication.js +8 -5
- package/dist/BaseApplication.js.map +1 -1
- package/dist/BaseController.d.ts +17 -9
- package/dist/BaseController.js +31 -30
- package/dist/BaseController.js.map +1 -1
- package/package.json +9 -9
- package/src/BaseApplication.ts +7 -5
- package/src/BaseController.ts +93 -88
|
@@ -17,7 +17,7 @@ export declare abstract class BaseApplication<D extends BaseDatabase> {
|
|
|
17
17
|
protected abstract getLogger(): ILogger;
|
|
18
18
|
protected abstract getRouter(): Router;
|
|
19
19
|
protected abstract getPort(): number;
|
|
20
|
-
constructor(
|
|
20
|
+
constructor(serverPath: string, swaggerPath?: string | null);
|
|
21
21
|
addLink(link: BaseApplicationLink): void;
|
|
22
22
|
addSwaggerLink(): void;
|
|
23
23
|
start(): void;
|
package/dist/BaseApplication.js
CHANGED
|
@@ -19,13 +19,16 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
19
19
|
const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
|
|
20
20
|
const swagger_jsdoc_1 = __importDefault(require("swagger-jsdoc"));
|
|
21
21
|
const path_1 = __importDefault(require("path"));
|
|
22
|
+
const namirasoft_core_1 = require("namirasoft-core");
|
|
22
23
|
class BaseApplication {
|
|
23
|
-
constructor(
|
|
24
|
+
constructor(serverPath, swaggerPath = null) {
|
|
25
|
+
var _a, _b, _c, _d;
|
|
24
26
|
this.links = [];
|
|
25
|
-
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
27
|
+
let json = namirasoft_core_1.PackageService.getMain();
|
|
28
|
+
this.title = (_a = json === null || json === void 0 ? void 0 : json.getTitle()) !== null && _a !== void 0 ? _a : "";
|
|
29
|
+
this.description = (_b = json === null || json === void 0 ? void 0 : json.getDescription()) !== null && _b !== void 0 ? _b : "";
|
|
30
|
+
this.logo = (_c = json === null || json === void 0 ? void 0 : json.getLogo()) !== null && _c !== void 0 ? _c : "";
|
|
31
|
+
this.version = (_d = json === null || json === void 0 ? void 0 : json.getVersion()) !== null && _d !== void 0 ? _d : "";
|
|
29
32
|
this.serverPath = serverPath;
|
|
30
33
|
this.swaggerPath = swaggerPath !== null && swaggerPath !== void 0 ? swaggerPath : (serverPath + "/swagger");
|
|
31
34
|
this.logger = this.getLogger();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseApplication.js","sourceRoot":"","sources":["../src/BaseApplication.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sDAA0C;AAC1C,gDAAwB;AACxB,4CAAoB;AACpB,4EAA2C;AAC3C,kEAAyC;AAIzC,gDAAwB;
|
|
1
|
+
{"version":3,"file":"BaseApplication.js","sourceRoot":"","sources":["../src/BaseApplication.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sDAA0C;AAC1C,gDAAwB;AACxB,4CAAoB;AACpB,4EAA2C;AAC3C,kEAAyC;AAIzC,gDAAwB;AACxB,qDAAiD;AAEjD,MAAsB,eAAe;IAgBjC,YAAY,UAAkB,EAAE,cAA6B,IAAI;;QARzD,UAAK,GAA0B,EAAE,CAAC;QAUtC,IAAI,IAAI,GAAG,gCAAc,CAAC,OAAO,EAAE,CAAC;QACpC,IAAI,CAAC,KAAK,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,QAAQ,EAAE,mCAAI,EAAE,CAAC;QACpC,IAAI,CAAC,WAAW,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,cAAc,EAAE,mCAAI,EAAE,CAAC;QAChD,IAAI,CAAC,IAAI,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,OAAO,EAAE,mCAAI,EAAE,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAA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} from './BaseDatabase';
|
|
|
3
3
|
import { SchemaLike } from 'joi';
|
|
4
4
|
import { AnomalyDetector } from './AnomalyDetector';
|
|
5
5
|
import { ILogger } from 'namirasoft-log';
|
|
6
|
+
import { Meta } from './Meta';
|
|
6
7
|
export declare abstract class BaseController<D extends BaseDatabase, State, Props> {
|
|
7
|
-
showLogAtTheBeginning: boolean;
|
|
8
|
-
showLogAtTheEnd: boolean;
|
|
8
|
+
protected showLogAtTheBeginning: boolean;
|
|
9
|
+
protected showLogAtTheEnd: boolean;
|
|
10
|
+
protected req: express.Request;
|
|
11
|
+
protected res: express.Response;
|
|
9
12
|
protected logger: ILogger;
|
|
10
|
-
|
|
13
|
+
protected meta: Meta;
|
|
14
|
+
protected result: {};
|
|
15
|
+
protected database: D;
|
|
16
|
+
protected state: State;
|
|
17
|
+
protected props: Props;
|
|
18
|
+
constructor(req: express.Request, res: express.Response);
|
|
11
19
|
abstract getLogger(): ILogger;
|
|
12
20
|
abstract getDatabase(): Promise<D>;
|
|
13
21
|
abstract getAnomaly(): AnomalyDetector | null;
|
|
14
22
|
abstract getBodySchema(): SchemaLike | null;
|
|
15
23
|
abstract getQuerySchema(): SchemaLike | null;
|
|
16
|
-
abstract getState(): State
|
|
17
|
-
abstract getProps(
|
|
18
|
-
abstract preHandle(
|
|
19
|
-
abstract handle(
|
|
20
|
-
abstract postHandle(
|
|
21
|
-
|
|
24
|
+
abstract getState(): Promise<State>;
|
|
25
|
+
abstract getProps(): Promise<Props>;
|
|
26
|
+
abstract preHandle(): Promise<void>;
|
|
27
|
+
abstract handle(): Promise<any>;
|
|
28
|
+
abstract postHandle(): Promise<void>;
|
|
29
|
+
run(): Promise<express.Response<any, Record<string, any>>>;
|
|
22
30
|
}
|
package/dist/BaseController.js
CHANGED
|
@@ -17,39 +17,40 @@ const Meta_1 = require("./Meta");
|
|
|
17
17
|
const joi_1 = __importDefault(require("joi"));
|
|
18
18
|
const namirasoft_core_1 = require("namirasoft-core");
|
|
19
19
|
class BaseController {
|
|
20
|
-
constructor() {
|
|
20
|
+
constructor(req, res) {
|
|
21
21
|
this.showLogAtTheBeginning = false;
|
|
22
22
|
this.showLogAtTheEnd = true;
|
|
23
|
+
this.req = req;
|
|
24
|
+
this.res = res;
|
|
23
25
|
this.logger = this.getLogger();
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
return
|
|
27
|
-
|
|
28
|
-
let meta = new Meta_1.Meta(req);
|
|
27
|
+
run() {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
this.meta = new Meta_1.Meta(this.req);
|
|
29
30
|
// result
|
|
30
|
-
|
|
31
|
+
this.result = {};
|
|
31
32
|
try {
|
|
32
33
|
// meta
|
|
33
|
-
meta.onStart();
|
|
34
|
+
this.meta.onStart();
|
|
34
35
|
if (this.showLogAtTheBeginning)
|
|
35
|
-
this.logger.info(JSON.stringify(meta));
|
|
36
|
+
this.logger.info(JSON.stringify(this.meta));
|
|
36
37
|
// init controller
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
this.database = yield this.getDatabase();
|
|
39
|
+
this.state = yield this.getState();
|
|
40
|
+
this.props = yield this.getProps();
|
|
40
41
|
// preHandle
|
|
41
|
-
yield this.preHandle(
|
|
42
|
+
yield this.preHandle();
|
|
42
43
|
// check for anomaly
|
|
43
44
|
let anomaly = this.getAnomaly();
|
|
44
45
|
if (anomaly != null)
|
|
45
|
-
if (anomaly.isAnomaly(meta.ip, meta.url))
|
|
46
|
+
if (anomaly.isAnomaly(this.meta.ip, this.meta.url))
|
|
46
47
|
namirasoft_core_1.ErrorOperation.throwHTTP(403, 'Suspicious activity detected.');
|
|
47
48
|
// check body validation
|
|
48
49
|
let bodySchema = this.getBodySchema();
|
|
49
50
|
if (bodySchema != null) {
|
|
50
51
|
const validation = yield joi_1.default.compile(bodySchema)
|
|
51
52
|
.prefs({ errors: { label: 'key' } })
|
|
52
|
-
.validate(req.body);
|
|
53
|
+
.validate(this.req.body);
|
|
53
54
|
if (validation.error) {
|
|
54
55
|
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
55
56
|
namirasoft_core_1.ErrorOperation.throwHTTP(400, message);
|
|
@@ -60,46 +61,46 @@ class BaseController {
|
|
|
60
61
|
if (querySchema != null) {
|
|
61
62
|
const validation = yield joi_1.default.compile(querySchema)
|
|
62
63
|
.prefs({ errors: { label: 'key' } })
|
|
63
|
-
.validate(req.query);
|
|
64
|
+
.validate(this.req.query);
|
|
64
65
|
if (validation.error) {
|
|
65
66
|
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
66
67
|
namirasoft_core_1.ErrorOperation.throwHTTP(400, message);
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
// call controller
|
|
70
|
-
result = yield this.handle(
|
|
71
|
-
if (result == null)
|
|
72
|
-
result = "Success";
|
|
71
|
+
this.result = yield this.handle();
|
|
72
|
+
if (this.result == null)
|
|
73
|
+
this.result = "Success";
|
|
73
74
|
// postHandle
|
|
74
|
-
yield this.postHandle(
|
|
75
|
+
yield this.postHandle();
|
|
75
76
|
}
|
|
76
77
|
catch (error) {
|
|
77
78
|
let message;
|
|
78
79
|
if (error instanceof Error) {
|
|
79
|
-
meta.error = error;
|
|
80
|
+
this.meta.error = error;
|
|
80
81
|
message = error.message;
|
|
81
82
|
}
|
|
82
83
|
else
|
|
83
84
|
message = error + "";
|
|
84
85
|
if (error instanceof namirasoft_core_1.HTTPError) {
|
|
85
|
-
meta.code = error.code;
|
|
86
|
-
this.logger.error(error.message + "\n" + JSON.stringify(meta), undefined, error.stack);
|
|
86
|
+
this.meta.code = error.code;
|
|
87
|
+
this.logger.error(error.message + "\n" + JSON.stringify(this.meta), undefined, error.stack);
|
|
87
88
|
}
|
|
88
89
|
else {
|
|
89
|
-
meta.code = 500;
|
|
90
|
+
this.meta.code = 500;
|
|
90
91
|
if (error instanceof Error)
|
|
91
|
-
this.logger.critical(error.message + "\n" + JSON.stringify(meta), undefined, error.stack);
|
|
92
|
+
this.logger.critical(error.message + "\n" + JSON.stringify(this.meta), undefined, error.stack);
|
|
92
93
|
}
|
|
93
|
-
meta.message = message;
|
|
94
|
+
this.meta.message = message;
|
|
94
95
|
if (error instanceof namirasoft_core_1.HTTPError)
|
|
95
|
-
result = meta.message;
|
|
96
|
+
this.result = this.meta.message;
|
|
96
97
|
else
|
|
97
|
-
result = "Sorry, internl server error.";
|
|
98
|
+
this.result = "Sorry, internl server error.";
|
|
98
99
|
}
|
|
99
|
-
meta.onFinish();
|
|
100
|
+
this.meta.onFinish();
|
|
100
101
|
if (this.showLogAtTheEnd)
|
|
101
|
-
this.logger.info(JSON.stringify(meta));
|
|
102
|
-
return res.status(meta.code).send(result);
|
|
102
|
+
this.logger.info(JSON.stringify(this.meta));
|
|
103
|
+
return this.res.status(this.meta.code).send(this.result);
|
|
103
104
|
});
|
|
104
105
|
}
|
|
105
106
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseController.js","sourceRoot":"","sources":["../src/BaseController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,iCAA8B;AAC9B,8CAAsB;AACtB,qDAA4D;AAE5D,MAAsB,cAAc;
|
|
1
|
+
{"version":3,"file":"BaseController.js","sourceRoot":"","sources":["../src/BaseController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,iCAA8B;AAC9B,8CAAsB;AACtB,qDAA4D;AAE5D,MAAsB,cAAc;IAYhC,YAAY,GAAoB,EAAE,GAAqB;QAV7C,0BAAqB,GAAY,KAAK,CAAC;QACvC,oBAAe,GAAY,IAAI,CAAC;QAWtC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;IACnC,CAAC;IAYK,GAAG;;YAEL,IAAI,CAAC,IAAI,GAAG,IAAI,WAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC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oft-node",
|
|
3
3
|
"description": "Namira Software Corporation Node NPM Package",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.15",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"scripts": {},
|
|
@@ -9,25 +9,25 @@
|
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@supercharge/request-ip": "^1.2.0",
|
|
12
|
-
"@types/cors": "^2.8.
|
|
13
|
-
"@types/express": "^4.17.
|
|
12
|
+
"@types/cors": "^2.8.16",
|
|
13
|
+
"@types/express": "^4.17.21",
|
|
14
14
|
"@types/nodemailer": "^6.4.13",
|
|
15
|
-
"@types/nodemailer-smtp-transport": "^2.7.
|
|
15
|
+
"@types/nodemailer-smtp-transport": "^2.7.8",
|
|
16
16
|
"@types/swagger-jsdoc": "^6.0.2",
|
|
17
17
|
"@types/swagger-ui-express": "^4.1.5",
|
|
18
18
|
"cors": "^2.8.5",
|
|
19
19
|
"express": "^4.18.2",
|
|
20
20
|
"joi": "^17.11.0",
|
|
21
|
-
"namirasoft-core": "^1.0.
|
|
22
|
-
"namirasoft-log": "^1.0.
|
|
21
|
+
"namirasoft-core": "^1.0.11",
|
|
22
|
+
"namirasoft-log": "^1.0.1",
|
|
23
23
|
"nodemailer": "^6.9.7",
|
|
24
|
-
"nodemailer-smtp-transport": "^2.4
|
|
24
|
+
"nodemailer-smtp-transport": "^2.7.4",
|
|
25
25
|
"request-ip": "^3.3.0",
|
|
26
|
-
"sequelize": "^6.
|
|
26
|
+
"sequelize": "^6.34.0",
|
|
27
27
|
"swagger-jsdoc": "^6.2.8",
|
|
28
28
|
"swagger-ui-express": "^5.0.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@types/node": "^20.
|
|
31
|
+
"@types/node": "^20.8.10"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/BaseApplication.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { BaseDatabase } from './BaseDatabase';
|
|
|
7
7
|
import { ILogger } from "namirasoft-log";
|
|
8
8
|
import { BaseApplicationLink } from './BaseApplicationLink';
|
|
9
9
|
import path from 'path';
|
|
10
|
+
import { PackageService } from 'namirasoft-core';
|
|
10
11
|
|
|
11
12
|
export abstract class BaseApplication<D extends BaseDatabase>
|
|
12
13
|
{
|
|
@@ -24,12 +25,13 @@ export abstract class BaseApplication<D extends BaseDatabase>
|
|
|
24
25
|
protected abstract getLogger(): ILogger;
|
|
25
26
|
protected abstract getRouter(): Router;
|
|
26
27
|
protected abstract getPort(): number;
|
|
27
|
-
constructor(
|
|
28
|
+
constructor(serverPath: string, swaggerPath: string | null = null)
|
|
28
29
|
{
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
30
|
+
let json = PackageService.getMain();
|
|
31
|
+
this.title = json?.getTitle() ?? "";
|
|
32
|
+
this.description = json?.getDescription() ?? "";
|
|
33
|
+
this.logo = json?.getLogo() ?? "";
|
|
34
|
+
this.version = json?.getVersion() ?? "";
|
|
33
35
|
this.serverPath = serverPath;
|
|
34
36
|
this.swaggerPath = swaggerPath ?? (serverPath + "/swagger");
|
|
35
37
|
this.logger = this.getLogger();
|
package/src/BaseController.ts
CHANGED
|
@@ -9,11 +9,20 @@ import { ErrorOperation, HTTPError } from 'namirasoft-core';
|
|
|
9
9
|
|
|
10
10
|
export abstract class BaseController<D extends BaseDatabase, State, Props>
|
|
11
11
|
{
|
|
12
|
-
showLogAtTheBeginning: boolean = false;
|
|
13
|
-
showLogAtTheEnd: boolean = true;
|
|
12
|
+
protected showLogAtTheBeginning: boolean = false;
|
|
13
|
+
protected showLogAtTheEnd: boolean = true;
|
|
14
|
+
protected req: express.Request;
|
|
15
|
+
protected res: express.Response;
|
|
14
16
|
protected logger: ILogger;
|
|
15
|
-
|
|
17
|
+
protected meta!: Meta;
|
|
18
|
+
protected result!: {};
|
|
19
|
+
protected database!: D;
|
|
20
|
+
protected state!: State;
|
|
21
|
+
protected props!: Props;
|
|
22
|
+
constructor(req: express.Request, res: express.Response)
|
|
16
23
|
{
|
|
24
|
+
this.req = req;
|
|
25
|
+
this.res = res;
|
|
17
26
|
this.logger = this.getLogger();
|
|
18
27
|
}
|
|
19
28
|
abstract getLogger(): ILogger;
|
|
@@ -21,106 +30,102 @@ export abstract class BaseController<D extends BaseDatabase, State, Props>
|
|
|
21
30
|
abstract getAnomaly(): AnomalyDetector | null;
|
|
22
31
|
abstract getBodySchema(): SchemaLike | null;
|
|
23
32
|
abstract getQuerySchema(): SchemaLike | null;
|
|
24
|
-
abstract getState(): State
|
|
25
|
-
abstract getProps(
|
|
26
|
-
abstract preHandle(
|
|
27
|
-
abstract handle(
|
|
28
|
-
abstract postHandle(
|
|
33
|
+
abstract getState(): Promise<State>;
|
|
34
|
+
abstract getProps(): Promise<Props>;
|
|
35
|
+
abstract preHandle(): Promise<void>;
|
|
36
|
+
abstract handle(): Promise<any>;
|
|
37
|
+
abstract postHandle(): Promise<void>;
|
|
29
38
|
|
|
30
|
-
|
|
39
|
+
async run()
|
|
31
40
|
{
|
|
32
|
-
|
|
41
|
+
this.meta = new Meta(this.req);
|
|
42
|
+
// result
|
|
43
|
+
this.result = {};
|
|
44
|
+
try
|
|
33
45
|
{
|
|
34
46
|
// meta
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (this.showLogAtTheBeginning)
|
|
43
|
-
this.logger.info(JSON.stringify(meta));
|
|
44
|
-
// init controller
|
|
45
|
-
let database = await this.getDatabase();
|
|
46
|
-
let state = this.getState();
|
|
47
|
-
let props = await this.getProps(req, res, database, state);
|
|
47
|
+
this.meta.onStart();
|
|
48
|
+
if (this.showLogAtTheBeginning)
|
|
49
|
+
this.logger.info(JSON.stringify(this.meta));
|
|
50
|
+
// init controller
|
|
51
|
+
this.database = await this.getDatabase();
|
|
52
|
+
this.state = await this.getState();
|
|
53
|
+
this.props = await this.getProps();
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
55
|
+
// preHandle
|
|
56
|
+
await this.preHandle();
|
|
51
57
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
// check for anomaly
|
|
59
|
+
let anomaly: AnomalyDetector | null = this.getAnomaly();
|
|
60
|
+
if (anomaly != null)
|
|
61
|
+
if (anomaly.isAnomaly(this.meta.ip, this.meta.url))
|
|
62
|
+
ErrorOperation.throwHTTP(403, 'Suspicious activity detected.');
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
// check body validation
|
|
65
|
+
let bodySchema = this.getBodySchema();
|
|
66
|
+
if (bodySchema != null)
|
|
67
|
+
{
|
|
68
|
+
const validation = await Joi.compile(bodySchema)
|
|
69
|
+
.prefs({ errors: { label: 'key' } })
|
|
70
|
+
.validate(this.req.body);
|
|
71
|
+
if (validation.error)
|
|
61
72
|
{
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.validate(req.body);
|
|
65
|
-
if (validation.error)
|
|
66
|
-
{
|
|
67
|
-
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
68
|
-
ErrorOperation.throwHTTP(400, message);
|
|
69
|
-
}
|
|
73
|
+
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
74
|
+
ErrorOperation.throwHTTP(400, message);
|
|
70
75
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
}
|
|
77
|
+
// check query validation
|
|
78
|
+
let querySchema = this.getQuerySchema();
|
|
79
|
+
if (querySchema != null)
|
|
80
|
+
{
|
|
81
|
+
const validation = await Joi.compile(querySchema)
|
|
82
|
+
.prefs({ errors: { label: 'key' } })
|
|
83
|
+
.validate(this.req.query);
|
|
84
|
+
if (validation.error)
|
|
74
85
|
{
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
.validate(req.query);
|
|
78
|
-
if (validation.error)
|
|
79
|
-
{
|
|
80
|
-
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
81
|
-
ErrorOperation.throwHTTP(400, message);
|
|
82
|
-
}
|
|
86
|
+
let message = validation.error.details.map((details) => details.message).join(', ');
|
|
87
|
+
ErrorOperation.throwHTTP(400, message);
|
|
83
88
|
}
|
|
89
|
+
}
|
|
84
90
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
91
|
+
// call controller
|
|
92
|
+
this.result = await this.handle();
|
|
93
|
+
if (this.result == null)
|
|
94
|
+
this.result = "Success";
|
|
89
95
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
96
|
+
// postHandle
|
|
97
|
+
await this.postHandle();
|
|
98
|
+
} catch (error)
|
|
99
|
+
{
|
|
100
|
+
let message: string;
|
|
101
|
+
if (error instanceof Error)
|
|
93
102
|
{
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
else
|
|
101
|
-
message = error + "";
|
|
103
|
+
this.meta.error = error;
|
|
104
|
+
message = error.message;
|
|
105
|
+
}
|
|
106
|
+
else
|
|
107
|
+
message = error + "";
|
|
102
108
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
meta.message = message;
|
|
115
|
-
if (error instanceof HTTPError)
|
|
116
|
-
result = meta.message;
|
|
117
|
-
else
|
|
118
|
-
result = "Sorry, internl server error.";
|
|
109
|
+
if (error instanceof HTTPError)
|
|
110
|
+
{
|
|
111
|
+
this.meta.code = error.code;
|
|
112
|
+
this.logger.error(error.message + "\n" + JSON.stringify(this.meta), undefined, error.stack);
|
|
113
|
+
}
|
|
114
|
+
else
|
|
115
|
+
{
|
|
116
|
+
this.meta.code = 500;
|
|
117
|
+
if (error instanceof Error)
|
|
118
|
+
this.logger.critical(error.message + "\n" + JSON.stringify(this.meta), undefined, error.stack);
|
|
119
119
|
}
|
|
120
|
-
meta.
|
|
121
|
-
if (
|
|
122
|
-
this.
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
this.meta.message = message;
|
|
121
|
+
if (error instanceof HTTPError)
|
|
122
|
+
this.result = this.meta.message;
|
|
123
|
+
else
|
|
124
|
+
this.result = "Sorry, internl server error.";
|
|
125
|
+
}
|
|
126
|
+
this.meta.onFinish();
|
|
127
|
+
if (this.showLogAtTheEnd)
|
|
128
|
+
this.logger.info(JSON.stringify(this.meta));
|
|
129
|
+
return this.res.status(this.meta.code).send(this.result);
|
|
125
130
|
}
|
|
126
131
|
}
|