badmfck-api-server 2.1.0 → 2.1.2

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.
@@ -2,7 +2,7 @@
2
2
  import { Application, Response } from 'express';
3
3
  import { BaseService, IBaseService } from './BaseService';
4
4
  import { IBaseEndpoint } from './BaseEndpoint';
5
- import { HTTPRequestVO, TransferPacketVO } from './structures/Interfaces';
5
+ import { HTTPRequestVO, IError, TransferPacketVO } from './structures/Interfaces';
6
6
  import { Req } from "badmfck-signal";
7
7
  import http from 'http';
8
8
  export interface APIServiceNetworkLogItem {
@@ -45,7 +45,7 @@ export declare const REQ_HTTP_SERVER: Req<void, {
45
45
  express: Application;
46
46
  http: http.Server;
47
47
  }>;
48
- export declare const REQ_INTERNAL_CALL: Req<HTTPRequestVO, TransferPacketVO>;
48
+ export declare const REQ_INTERNAL_CALL: Req<HTTPRequestVO, IError | any>;
49
49
  export declare function Initializer(services: IBaseService[]): Promise<void>;
50
50
  export declare class APIService extends BaseService {
51
51
  private static nextLogID;
@@ -83,7 +83,7 @@ async function Initializer(services) {
83
83
  exports.Initializer = Initializer;
84
84
  class APIService extends BaseService_1.BaseService {
85
85
  static nextLogID = 0;
86
- version = "2.1.0";
86
+ version = "2.1.2";
87
87
  options;
88
88
  monitor;
89
89
  monitorIndexFile;
@@ -136,7 +136,10 @@ class APIService extends BaseService_1.BaseService {
136
136
  let ep = BaseEndpoint_1.BaseEndpoint.getEntryPoint() + j.endpoint;
137
137
  ep = ep.replaceAll("//", "/");
138
138
  if (ep === req.endpoint) {
139
- return await i.execute(req);
139
+ const response = await i.execute(req);
140
+ if (response.error)
141
+ return response.error;
142
+ return response.data;
140
143
  }
141
144
  }
142
145
  }
@@ -362,6 +365,24 @@ class APIService extends BaseService_1.BaseService {
362
365
  }
363
366
  else {
364
367
  try {
368
+ if (data.file) {
369
+ res.sendFile(data.file, err => {
370
+ if (err) {
371
+ if (log)
372
+ log.error = "Can't send file: " + data.file;
373
+ if (this.options.onError)
374
+ this.options.onError("Can't send file: " + data.file, err);
375
+ if (this.monitor && data.endpoint)
376
+ this.monitor.registrateAPIError(data.endpoint);
377
+ this.sendResponse(res, {
378
+ error: DefaultErrors_1.default.CANT_SEND_FILE,
379
+ data: null,
380
+ httpStatus: 500
381
+ }, requestTime, data.endpoint);
382
+ }
383
+ });
384
+ return;
385
+ }
365
386
  if (data.redirect) {
366
387
  res.redirect(data.redirect);
367
388
  if (log)
@@ -4,6 +4,8 @@ declare class DefaultErrors {
4
4
  static UNKNOWN_REQUEST: IError;
5
5
  static JSON_MALFORMED: IError;
6
6
  static FILE_TOO_LARGE: IError;
7
+ static CANT_SEND_FILE: IError;
8
+ static FILE_NOT_EXISTS: IError;
7
9
  }
8
10
  export declare class ErrorUtils {
9
11
  static isError(obj: any): boolean;
@@ -7,6 +7,8 @@ class DefaultErrors {
7
7
  static UNKNOWN_REQUEST = { code: 2, message: "Unknown request" };
8
8
  static JSON_MALFORMED = { code: 3, message: "JSON malformed" };
9
9
  static FILE_TOO_LARGE = { code: 4, message: "File is too large" };
10
+ static CANT_SEND_FILE = { code: 5, message: "Can't send file" };
11
+ static FILE_NOT_EXISTS = { code: 5, message: "File not exists" };
10
12
  }
11
13
  class ErrorUtils {
12
14
  static isError(obj) {
@@ -12,6 +12,7 @@ export interface TransferPacketVO<T = any> {
12
12
  headers?: {
13
13
  [key: string]: string;
14
14
  };
15
+ file?: string;
15
16
  }
16
17
  export interface HTTPRequestVO<T = any> {
17
18
  raw: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",