badmfck-api-server 1.3.6 → 1.3.8

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.
@@ -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.8";
47
47
  options;
48
48
  netLog = [];
49
49
  constructor(options) {
@@ -177,7 +177,7 @@ class APIService extends BaseService_1.BaseService {
177
177
  }, tme, req.path);
178
178
  });
179
179
  app.listen(this.options.port, () => {
180
- console.log('API Service started at: ' + this.options.port + ", with base endpoint:" + this.options.baseEndPoint + ", ver.: " + this.version);
180
+ (0, LogService_1.logCrit)('API Service started at: ' + this.options.port + ", with base endpoint:" + this.options.baseEndPoint + ", ver.: " + this.version);
181
181
  });
182
182
  }
183
183
  sendResponse(res, data, requestTime, endpoint, log) {
@@ -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
  }
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseService = void 0;
4
+ const LogService_1 = require("./LogService");
4
5
  class BaseService {
5
6
  name;
6
7
  constructor(name) {
7
8
  this.name = name;
8
9
  }
9
10
  async init() {
10
- console.log("Service: " + this.name + " initialized");
11
+ (0, LogService_1.logInfo)("Service: " + this.name + " initialized");
11
12
  }
12
13
  applicationReady() { }
13
14
  }
@@ -1,9 +1,15 @@
1
1
  import Signal, { Req } from "badmfck-signal";
2
2
  import { BaseService } from "./BaseService";
3
- export type ILogItemLevel = "info" | "error" | "warn" | "crit";
3
+ export declare enum LOG_LEVEL {
4
+ ALL = 0,
5
+ INFO = 1,
6
+ WARN = 2,
7
+ ERROR = 3,
8
+ CRIT = 4
9
+ }
4
10
  export interface ILogItem {
5
11
  id: number;
6
- level: ILogItemLevel;
12
+ level: LOG_LEVEL;
7
13
  time: number;
8
14
  text: string;
9
15
  date: string;
@@ -18,18 +24,20 @@ export declare const logWarn: (message: any, ...optional: any[]) => void;
18
24
  export declare const logCrit: (message: any, ...optional: any[]) => void;
19
25
  export declare const logError: (message: any, ...optional: any[]) => void;
20
26
  export declare const S_LOG: Signal<{
21
- level: ILogItemLevel;
27
+ level: LOG_LEVEL;
22
28
  data: any[];
23
29
  }>;
24
30
  export declare const S_LOG_CREATED: Signal<ILogItem>;
25
- export declare const S_LOG_CHANGE_LEVEL: Signal<ILogItemLevel | "all">;
31
+ export declare const S_LOG_CHANGE_LEVEL: Signal<LOG_LEVEL>;
26
32
  export declare const S_LOG_FLUSH: Signal<void>;
27
- export declare const REQ_LOG: Req<void, ILogItem[]>;
33
+ export declare const REQ_LOG: Req<{
34
+ lastID: number;
35
+ } | null | undefined, ILogItem[]>;
28
36
  export declare class LogService extends BaseService {
29
37
  epoch: number;
30
38
  log: ILogItem[];
31
39
  options: ILogServiceOptions;
32
- level: "all" | ILogItemLevel;
40
+ level: LOG_LEVEL;
33
41
  constructor(opt?: ILogServiceOptions);
34
42
  init(): Promise<void>;
35
43
  createDate(d?: Date): string;
@@ -23,22 +23,30 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.LogService = exports.REQ_LOG = exports.S_LOG_FLUSH = exports.S_LOG_CHANGE_LEVEL = exports.S_LOG_CREATED = exports.S_LOG = exports.logError = exports.logCrit = exports.logWarn = exports.logInfo = exports.getDefaultLogOptions = void 0;
26
+ exports.LogService = exports.REQ_LOG = exports.S_LOG_FLUSH = exports.S_LOG_CHANGE_LEVEL = exports.S_LOG_CREATED = exports.S_LOG = exports.logError = exports.logCrit = exports.logWarn = exports.logInfo = exports.getDefaultLogOptions = exports.LOG_LEVEL = void 0;
27
27
  const badmfck_signal_1 = __importStar(require("badmfck-signal"));
28
28
  const BaseService_1 = require("./BaseService");
29
+ var LOG_LEVEL;
30
+ (function (LOG_LEVEL) {
31
+ LOG_LEVEL[LOG_LEVEL["ALL"] = 0] = "ALL";
32
+ LOG_LEVEL[LOG_LEVEL["INFO"] = 1] = "INFO";
33
+ LOG_LEVEL[LOG_LEVEL["WARN"] = 2] = "WARN";
34
+ LOG_LEVEL[LOG_LEVEL["ERROR"] = 3] = "ERROR";
35
+ LOG_LEVEL[LOG_LEVEL["CRIT"] = 4] = "CRIT";
36
+ })(LOG_LEVEL || (exports.LOG_LEVEL = LOG_LEVEL = {}));
29
37
  const getDefaultLogOptions = () => {
30
38
  return {
31
39
  stackSize: 1000
32
40
  };
33
41
  };
34
42
  exports.getDefaultLogOptions = getDefaultLogOptions;
35
- const logInfo = (message, ...optional) => { exports.S_LOG.invoke({ level: "info", data: [message, optional] }); };
43
+ const logInfo = (message, ...optional) => { exports.S_LOG.invoke({ level: LOG_LEVEL.INFO, data: [message, optional] }); };
36
44
  exports.logInfo = logInfo;
37
- const logWarn = (message, ...optional) => { exports.S_LOG.invoke({ level: "warn", data: [message, optional] }); };
45
+ const logWarn = (message, ...optional) => { exports.S_LOG.invoke({ level: LOG_LEVEL.WARN, data: [message, optional] }); };
38
46
  exports.logWarn = logWarn;
39
- const logCrit = (message, ...optional) => { exports.S_LOG.invoke({ level: "crit", data: [message, optional] }); };
47
+ const logCrit = (message, ...optional) => { exports.S_LOG.invoke({ level: LOG_LEVEL.CRIT, data: [message, optional] }); };
40
48
  exports.logCrit = logCrit;
41
- const logError = (message, ...optional) => { exports.S_LOG.invoke({ level: "error", data: [message, optional] }); };
49
+ const logError = (message, ...optional) => { exports.S_LOG.invoke({ level: LOG_LEVEL.ERROR, data: [message, optional] }); };
42
50
  exports.logError = logError;
43
51
  exports.S_LOG = new badmfck_signal_1.default();
44
52
  exports.S_LOG_CREATED = new badmfck_signal_1.default();
@@ -49,7 +57,7 @@ class LogService extends BaseService_1.BaseService {
49
57
  epoch = 1699476234961;
50
58
  log = [];
51
59
  options;
52
- level = "all";
60
+ level = LOG_LEVEL.ALL;
53
61
  constructor(opt) {
54
62
  super("LogService");
55
63
  if (!opt)
@@ -58,14 +66,19 @@ class LogService extends BaseService_1.BaseService {
58
66
  }
59
67
  async init() {
60
68
  super.init();
61
- exports.REQ_LOG.listener = async () => [...this.log];
69
+ exports.REQ_LOG.listener = async (req) => {
70
+ if (req && req.lastID) {
71
+ let result = this.log.filter(val => val.id > req.lastID);
72
+ return result;
73
+ }
74
+ else
75
+ return [...this.log];
76
+ };
62
77
  exports.S_LOG_CHANGE_LEVEL.subscribe(l => { this.level = l; });
63
78
  exports.S_LOG_FLUSH.subscribe(() => this.log = []);
64
79
  exports.S_LOG.subscribe((log) => {
65
- if (log.level !== this.level) {
66
- if (this.level !== "all")
67
- return;
68
- }
80
+ if (log.level < this.level)
81
+ return;
69
82
  const msg = log.data[0];
70
83
  const optional = log.data[1];
71
84
  let text = this.createText(msg);
@@ -121,6 +134,8 @@ class LogService extends BaseService_1.BaseService {
121
134
  res = `${data}`;
122
135
  }
123
136
  }
137
+ if (Array.isArray(data))
138
+ res + ", total items: " + data.length;
124
139
  }
125
140
  else {
126
141
  res = data + "";
@@ -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.8",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",