badmfck-api-server 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -1,8 +1,8 @@
1
1
  import { Response } from 'express';
2
2
  import { BaseService, IBaseService } from './BaseService';
3
3
  import { IBaseEndpoint } from './BaseEndpoint';
4
- import { SyncSignal } from '../signal/Signal';
5
4
  import { TransferPacketVO } from './structures/Interfaces';
5
+ import { Req } from "badmfck-signal";
6
6
  export interface APIServiceNetworkLogItem {
7
7
  id: number;
8
8
  created: number;
@@ -23,10 +23,11 @@ export interface APIServiceOptions {
23
23
  jsonLimit: string;
24
24
  onNetworkLog?: ((log: APIServiceNetworkLogItem) => void) | null;
25
25
  onError?: ((...rest: any[]) => void) | null;
26
+ isProductionEnvironment: boolean;
26
27
  }
27
28
  export declare function getDefaultOptions(): APIServiceOptions;
28
- export declare const REQ_CREATE_NET_LOG: SyncSignal<void, APIServiceNetworkLogItem>;
29
- export declare const REQ_HTTP_LOG: SyncSignal<void, APIServiceNetworkLogItem[]>;
29
+ export declare const REQ_CREATE_NET_LOG: Req<void, APIServiceNetworkLogItem>;
30
+ export declare const REQ_HTTP_LOG: Req<void, APIServiceNetworkLogItem[]>;
30
31
  export declare function Initializer(services: IBaseService[]): Promise<void>;
31
32
  export declare class APIService extends BaseService {
32
33
  private static nextLogID;
@@ -9,7 +9,7 @@ const BaseService_1 = require("./BaseService");
9
9
  const cors_1 = __importDefault(require("cors"));
10
10
  const BaseEndpoint_1 = require("./BaseEndpoint");
11
11
  const DefaultErrors_1 = __importDefault(require("./structures/DefaultErrors"));
12
- const Signal_1 = require("../signal/Signal");
12
+ const badmfck_signal_1 = require("badmfck-signal");
13
13
  function getDefaultOptions() {
14
14
  return {
15
15
  port: 8091,
@@ -25,12 +25,13 @@ function getDefaultOptions() {
25
25
  },
26
26
  onError: function (err) {
27
27
  console.error.call(this, err);
28
- }
28
+ },
29
+ isProductionEnvironment: false
29
30
  };
30
31
  }
31
32
  exports.getDefaultOptions = getDefaultOptions;
32
- exports.REQ_CREATE_NET_LOG = new Signal_1.SyncSignal();
33
- exports.REQ_HTTP_LOG = new Signal_1.SyncSignal();
33
+ exports.REQ_CREATE_NET_LOG = new badmfck_signal_1.Req();
34
+ exports.REQ_HTTP_LOG = new badmfck_signal_1.Req();
34
35
  async function Initializer(services) {
35
36
  for (let i of services) {
36
37
  await i.init();
@@ -66,8 +67,32 @@ class APIService extends BaseService_1.BaseService {
66
67
  cb(log);
67
68
  };
68
69
  const app = (0, express_1.default)();
70
+ if (this.options.isProductionEnvironment)
71
+ app.set("env", 'production');
69
72
  app.use(express_1.default.json({ limit: '10mb' }));
70
73
  app.use(express_1.default.urlencoded({ limit: '10mb', extended: true }));
74
+ app.use(async (err, req, resp, next) => {
75
+ if (!err) {
76
+ next();
77
+ return;
78
+ }
79
+ const tme = +new Date();
80
+ const log = await exports.REQ_CREATE_NET_LOG.request();
81
+ log.request = {
82
+ method: req.method,
83
+ data: "",
84
+ params: req.params
85
+ };
86
+ let responseError = DefaultErrors_1.default.UNKNOWN_REQUEST;
87
+ if (typeof err === "object" && err.status === 400 && 'body' in err && err.type === 'entity.parse.failed') {
88
+ responseError = DefaultErrors_1.default.JSON_MALFORMED;
89
+ }
90
+ this.sendResponse(resp, {
91
+ error: responseError,
92
+ data: null,
93
+ httpStatus: 400
94
+ }, tme, "", log);
95
+ });
71
96
  const corsOptions = {
72
97
  origin: (origin, callback) => {
73
98
  const originIsWhitelisted = this.options.corsHostWhiteList.includes(origin);
@@ -2,5 +2,6 @@ import { ErrorVO } from "./Interfaces";
2
2
  declare class DefaultErrors {
3
3
  static NOT_IMPLEMENTED: ErrorVO;
4
4
  static UNKNOWN_REQUEST: ErrorVO;
5
+ static JSON_MALFORMED: ErrorVO;
5
6
  }
6
7
  export default DefaultErrors;
@@ -3,5 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class DefaultErrors {
4
4
  static NOT_IMPLEMENTED = { code: 1, message: "Not implemented" };
5
5
  static UNKNOWN_REQUEST = { code: 2, message: "Unknown request" };
6
+ static JSON_MALFORMED = { code: 3, message: "JSON malformed" };
6
7
  }
7
8
  exports.default = DefaultErrors;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",