badmfck-api-server 1.3.6 → 1.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -43,7 +43,7 @@ async function Initializer(services) {
43
43
  exports.Initializer = Initializer;
44
44
  class APIService extends BaseService_1.BaseService {
45
45
  static nextLogID = 0;
46
- version = "1.3.6";
46
+ version = "1.3.7";
47
47
  options;
48
48
  netLog = [];
49
49
  constructor(options) {
@@ -204,7 +204,10 @@ class APIService extends BaseService_1.BaseService {
204
204
  for (let i in data.headers)
205
205
  res.setHeader(i, data.headers[i]);
206
206
  }
207
- res.statusCode = data.httpStatus ?? 200;
207
+ if (data.error && data.error.httpStatus && data.error.httpStatus > 99)
208
+ res.statusCode = data.error.httpStatus;
209
+ else
210
+ res.statusCode = data.httpStatus ?? 200;
208
211
  if (data.rawResponse) {
209
212
  res.send(data.data);
210
213
  if (log)
@@ -7,8 +7,9 @@ export interface IBaseEndpoint {
7
7
  }
8
8
  export interface IEndpointHandler {
9
9
  endpoint: string;
10
- handler: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
10
+ handler?: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
11
11
  ignoreInterceptor?: boolean;
12
+ independed?: boolean;
12
13
  }
13
14
  export declare class BaseEndpoint implements IBaseEndpoint {
14
15
  endpoints?: IEndpointHandler[];
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.BaseEndpoint = void 0;
7
+ const LogService_1 = require("./LogService");
7
8
  const DefaultErrors_1 = __importDefault(require("./structures/DefaultErrors"));
8
9
  class BaseEndpoint {
9
10
  endpoints;
@@ -34,23 +35,24 @@ class BaseEndpoint {
34
35
  i.endpoint = i.endpoint.substring(1);
35
36
  if (i.endpoint.endsWith("/"))
36
37
  i.endpoint = i.endpoint.substring(0, i.endpoint.length - 1);
37
- i.endpoint = this.endpoint + i.endpoint;
38
+ if (!i.independed)
39
+ i.endpoint = this.endpoint + i.endpoint;
38
40
  }
39
41
  this.endpoints = endpoints;
40
42
  }
41
43
  async init() {
42
44
  if (!this.endpoints) {
43
- console.error("No endpoints registered for " + this.endpoint);
45
+ (0, LogService_1.logInfo)("No endpoints registered for " + this.endpoint);
44
46
  return;
45
47
  }
46
48
  for (let i of this.endpoints)
47
- console.log("endpoint: " + i.endpoint + " initalized, ignoreInterceptor: " + (i.ignoreInterceptor ?? "false"));
49
+ (0, LogService_1.logInfo)("endpoint: " + i.endpoint + " initalized, ignoreInterceptor: " + (i.ignoreInterceptor ?? "false"));
48
50
  }
49
51
  ;
50
52
  async execute(req) {
51
53
  if (this.endpoints && this.endpoints.length > 0) {
52
54
  for (let i of this.endpoints) {
53
- if (BaseEndpoint.entrypoint + i.endpoint === req.endpoint)
55
+ if (BaseEndpoint.entrypoint + i.endpoint === req.endpoint && i.handler && typeof i.handler === "function")
54
56
  return i.handler(req);
55
57
  }
56
58
  }
@@ -24,7 +24,9 @@ export declare const S_LOG: Signal<{
24
24
  export declare const S_LOG_CREATED: Signal<ILogItem>;
25
25
  export declare const S_LOG_CHANGE_LEVEL: Signal<ILogItemLevel | "all">;
26
26
  export declare const S_LOG_FLUSH: Signal<void>;
27
- export declare const REQ_LOG: Req<void, ILogItem[]>;
27
+ export declare const REQ_LOG: Req<{
28
+ lastID: number;
29
+ } | null | undefined, ILogItem[]>;
28
30
  export declare class LogService extends BaseService {
29
31
  epoch: number;
30
32
  log: ILogItem[];
@@ -58,7 +58,14 @@ class LogService extends BaseService_1.BaseService {
58
58
  }
59
59
  async init() {
60
60
  super.init();
61
- exports.REQ_LOG.listener = async () => [...this.log];
61
+ exports.REQ_LOG.listener = async (req) => {
62
+ if (req && req.lastID) {
63
+ let result = this.log.filter(val => val.id > req.lastID);
64
+ return result;
65
+ }
66
+ else
67
+ return [...this.log];
68
+ };
62
69
  exports.S_LOG_CHANGE_LEVEL.subscribe(l => { this.level = l; });
63
70
  exports.S_LOG_FLUSH.subscribe(() => this.log = []);
64
71
  exports.S_LOG.subscribe((log) => {
@@ -1,7 +1,7 @@
1
- import { ErrorVO } from "./Interfaces";
1
+ import { IError } from "./Interfaces";
2
2
  declare class DefaultErrors {
3
- static NOT_IMPLEMENTED: ErrorVO;
4
- static UNKNOWN_REQUEST: ErrorVO;
5
- static JSON_MALFORMED: ErrorVO;
3
+ static NOT_IMPLEMENTED: IError;
4
+ static UNKNOWN_REQUEST: IError;
5
+ static JSON_MALFORMED: IError;
6
6
  }
7
7
  export default DefaultErrors;
@@ -1,6 +1,6 @@
1
1
  export interface TransferPacketVO<T> {
2
2
  data?: T | null;
3
- error?: ErrorVO | null;
3
+ error?: IError | null;
4
4
  responseTime?: number;
5
5
  version?: string;
6
6
  endpoint?: string;
@@ -22,8 +22,9 @@ export interface HTTPRequestVO {
22
22
  endpoint: string;
23
23
  interceptorResult?: TransferPacketVO<any>;
24
24
  }
25
- export interface ErrorVO {
25
+ export interface IError {
26
26
  code: number;
27
27
  message: string;
28
28
  details?: string;
29
+ httpStatus?: number;
29
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.3.6",
3
+ "version": "1.3.7",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",