badmfck-api-server 1.8.9 → 1.9.2

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,7 +25,8 @@ export interface APIServiceOptions {
25
25
  onError?: ((...rest: any[]) => void) | null;
26
26
  isProductionEnvironment: boolean;
27
27
  interceptor?: IBaseEndpoint;
28
- postproducer?: (packet: TransferPacketVO) => Promise<TransferPacketVO>;
28
+ postproducer?: (req: HTTPRequestVO | undefined | null, res: Response, data: TransferPacketVO<any>, requestTime: number, endpoint?: string, log?: APIServiceNetworkLogItem | null) => Promise<TransferPacketVO>;
29
+ preproducer?: (req: HTTPRequestVO | undefined | null) => any;
29
30
  monitor?: {
30
31
  login: string;
31
32
  password: string;
@@ -50,5 +51,5 @@ export declare class APIService extends BaseService {
50
51
  netLog: APIServiceNetworkLogItem[];
51
52
  constructor(options?: APIServiceOptions | null);
52
53
  init(): Promise<void>;
53
- sendResponse(res: Response, data: TransferPacketVO<any>, requestTime: number, endpoint?: string, log?: APIServiceNetworkLogItem | null): Promise<void>;
54
+ sendResponse(res: Response, data: TransferPacketVO<any>, requestTime: number, endpoint?: string, log?: APIServiceNetworkLogItem | null, req?: HTTPRequestVO): Promise<void>;
54
55
  }
@@ -186,6 +186,17 @@ class APIService extends BaseService_1.BaseService {
186
186
  endpoint: ep,
187
187
  files: req.files ?? null
188
188
  };
189
+ if (this.options.preproducer) {
190
+ try {
191
+ const preproducerResult = await this.options.preproducer(httpRequest);
192
+ if (preproducerResult)
193
+ httpRequest.preproducerResult = preproducerResult;
194
+ }
195
+ catch (e) {
196
+ this.sendResponse(res, { error: { code: 10002, message: "Internal server error", details: `${e}` }, data: null, httpStatus: 500 }, tme, ep, log, httpRequest);
197
+ return;
198
+ }
199
+ }
189
200
  let result;
190
201
  try {
191
202
  if (!j.ignoreInterceptor) {
@@ -193,7 +204,7 @@ class APIService extends BaseService_1.BaseService {
193
204
  if (this.options.interceptor) {
194
205
  interceptorResult = await this.options.interceptor.execute(httpRequest);
195
206
  if (interceptorResult.error && !j.allowInterceptorError) {
196
- this.sendResponse(res, interceptorResult, tme, ep, log);
207
+ this.sendResponse(res, interceptorResult, tme, ep, log, httpRequest);
197
208
  return;
198
209
  }
199
210
  httpRequest.interceptorResult = interceptorResult;
@@ -203,7 +214,7 @@ class APIService extends BaseService_1.BaseService {
203
214
  httpRequest.precheck = { data: this.options.monitor };
204
215
  const precheck = await i.precheck(httpRequest);
205
216
  if (precheck && precheck.error) {
206
- this.sendResponse(res, precheck, tme, ep, log);
217
+ this.sendResponse(res, precheck, tme, ep, log, httpRequest);
207
218
  return;
208
219
  }
209
220
  httpRequest.precheck = precheck;
@@ -222,10 +233,10 @@ class APIService extends BaseService_1.BaseService {
222
233
  message: "Internal server error",
223
234
  details: `${e}`
224
235
  }
225
- }, tme, ep, log);
236
+ }, tme, ep, log, httpRequest);
226
237
  return;
227
238
  }
228
- this.sendResponse(res, result, tme, ep, log);
239
+ this.sendResponse(res, result, tme, ep, log, httpRequest);
229
240
  };
230
241
  execute();
231
242
  });
@@ -277,10 +288,10 @@ class APIService extends BaseService_1.BaseService {
277
288
  (0, LogService_1.logCrit)('${APIService.js}', 'API Service started at: ' + this.options.port + ", with base endpoint:" + this.options.baseEndPoint + ", ver.: " + this.version);
278
289
  });
279
290
  }
280
- async sendResponse(res, data, requestTime, endpoint, log) {
291
+ async sendResponse(res, data, requestTime, endpoint, log, req) {
281
292
  if (this.options.postproducer) {
282
293
  try {
283
- data = await this.options.postproducer({ ...data });
294
+ data = await this.options.postproducer(req, res, data, requestTime, endpoint, log);
284
295
  }
285
296
  catch (e) {
286
297
  (0, LogService_1.logError)("Postproducer error", e);
@@ -25,6 +25,7 @@ export interface HTTPRequestVO<T = any> {
25
25
  };
26
26
  endpoint: string;
27
27
  interceptorResult?: TransferPacketVO<any>;
28
+ preproducerResult?: any;
28
29
  precheck?: TransferPacketVO<any> | null;
29
30
  files: FileArray | null | undefined;
30
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.8.9",
3
+ "version": "1.9.2",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",