badmfck-api-server 1.8.8 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
|
|
1
1
|
import { Response } from 'express';
|
2
2
|
import { BaseService, IBaseService } from './BaseService';
|
3
3
|
import { IBaseEndpoint } from './BaseEndpoint';
|
4
|
-
import { TransferPacketVO } from './structures/Interfaces';
|
4
|
+
import { HTTPRequestVO, TransferPacketVO } from './structures/Interfaces';
|
5
5
|
import { Req } from "badmfck-signal";
|
6
6
|
export interface APIServiceNetworkLogItem {
|
7
7
|
id: number;
|
@@ -25,6 +25,7 @@ export interface APIServiceOptions {
|
|
25
25
|
onError?: ((...rest: any[]) => void) | null;
|
26
26
|
isProductionEnvironment: boolean;
|
27
27
|
interceptor?: IBaseEndpoint;
|
28
|
+
postproducer?: (req: HTTPRequestVO | undefined | null, res: Response, data: TransferPacketVO<any>, requestTime: number, endpoint?: string, log?: APIServiceNetworkLogItem | null) => Promise<TransferPacketVO>;
|
28
29
|
monitor?: {
|
29
30
|
login: string;
|
30
31
|
password: string;
|
@@ -49,5 +50,5 @@ export declare class APIService extends BaseService {
|
|
49
50
|
netLog: APIServiceNetworkLogItem[];
|
50
51
|
constructor(options?: APIServiceOptions | null);
|
51
52
|
init(): Promise<void>;
|
52
|
-
sendResponse(res: Response, data: TransferPacketVO<any>, requestTime: number, endpoint?: string, log?: APIServiceNetworkLogItem | null): void
|
53
|
+
sendResponse(res: Response, data: TransferPacketVO<any>, requestTime: number, endpoint?: string, log?: APIServiceNetworkLogItem | null, req?: HTTPRequestVO): Promise<void>;
|
53
54
|
}
|
@@ -57,7 +57,7 @@ async function Initializer(services) {
|
|
57
57
|
exports.Initializer = Initializer;
|
58
58
|
class APIService extends BaseService_1.BaseService {
|
59
59
|
static nextLogID = 0;
|
60
|
-
version = "1.8.
|
60
|
+
version = "1.8.9";
|
61
61
|
options;
|
62
62
|
monitor;
|
63
63
|
monitorIndexFile;
|
@@ -193,7 +193,7 @@ class APIService extends BaseService_1.BaseService {
|
|
193
193
|
if (this.options.interceptor) {
|
194
194
|
interceptorResult = await this.options.interceptor.execute(httpRequest);
|
195
195
|
if (interceptorResult.error && !j.allowInterceptorError) {
|
196
|
-
this.sendResponse(res, interceptorResult, tme, ep, log);
|
196
|
+
this.sendResponse(res, interceptorResult, tme, ep, log, httpRequest);
|
197
197
|
return;
|
198
198
|
}
|
199
199
|
httpRequest.interceptorResult = interceptorResult;
|
@@ -203,7 +203,7 @@ class APIService extends BaseService_1.BaseService {
|
|
203
203
|
httpRequest.precheck = { data: this.options.monitor };
|
204
204
|
const precheck = await i.precheck(httpRequest);
|
205
205
|
if (precheck && precheck.error) {
|
206
|
-
this.sendResponse(res, precheck, tme, ep, log);
|
206
|
+
this.sendResponse(res, precheck, tme, ep, log, httpRequest);
|
207
207
|
return;
|
208
208
|
}
|
209
209
|
httpRequest.precheck = precheck;
|
@@ -222,10 +222,10 @@ class APIService extends BaseService_1.BaseService {
|
|
222
222
|
message: "Internal server error",
|
223
223
|
details: `${e}`
|
224
224
|
}
|
225
|
-
}, tme, ep, log);
|
225
|
+
}, tme, ep, log, httpRequest);
|
226
226
|
return;
|
227
227
|
}
|
228
|
-
this.sendResponse(res, result, tme, ep, log);
|
228
|
+
this.sendResponse(res, result, tme, ep, log, httpRequest);
|
229
229
|
};
|
230
230
|
execute();
|
231
231
|
});
|
@@ -277,16 +277,24 @@ class APIService extends BaseService_1.BaseService {
|
|
277
277
|
(0, LogService_1.logCrit)('${APIService.js}', 'API Service started at: ' + this.options.port + ", with base endpoint:" + this.options.baseEndPoint + ", ver.: " + this.version);
|
278
278
|
});
|
279
279
|
}
|
280
|
-
sendResponse(res, data, requestTime, endpoint, log) {
|
280
|
+
async sendResponse(res, data, requestTime, endpoint, log, req) {
|
281
|
+
if (this.options.postproducer) {
|
282
|
+
try {
|
283
|
+
data = await this.options.postproducer(req, res, data, requestTime, endpoint, log);
|
284
|
+
}
|
285
|
+
catch (e) {
|
286
|
+
(0, LogService_1.logError)("Postproducer error", e);
|
287
|
+
}
|
288
|
+
}
|
281
289
|
data.responseTime = (+new Date()) - requestTime;
|
282
290
|
data.core = this.version;
|
283
291
|
data.endpoint = endpoint ?? "no_endpoint";
|
284
292
|
if (this.options.appVersion)
|
285
293
|
data.version = this.options.appVersion;
|
286
|
-
if (this.monitor)
|
287
|
-
this.monitor.registrateResponse(data.endpoint, data.responseTime);
|
288
294
|
if (log)
|
289
295
|
log.time = data.responseTime;
|
296
|
+
if (this.monitor)
|
297
|
+
this.monitor.registrateResponse(data.endpoint, data.responseTime);
|
290
298
|
if (res.destroyed || res.closed) {
|
291
299
|
if (log)
|
292
300
|
log.error = "Connection already closed, can't send response for: " + data.endpoint;
|